Solved

weird macros

Posted on 2003-11-13
13
226 Views
Last Modified: 2010-04-01
hello I’m trying to make a better logging class
http://oldlook.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20791532.html

but i have come across a problem, and i think i need a macro to finish it off.
When ever I use the log class i want the writeflags function to be called but I want it to be invisible so I don’t have to write that function line out every time.
i hope that makes sense

Normally
log.writeflags(__FILE__,__LINE__) << "something" ;

what i want
log << "something" ;

what i'm using right now
#define __FLAGS    writeflags(__FILE__,__LINE__)
log.__FLAGS << "something" ;  
0
Comment
Question by:funvill
  • 6
  • 5
  • 2
13 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9742090
Hi funvill

I've read your post, but I quite don't get what the problem is............. I guess that what you posted is correct

#define __FLAGS    writeflags(__FILE__,__LINE__)

log.__FLAGS << "something" ;  

with this piece of code,

before compiling, the preprocessor would replace all the ocurrences of __FLAGS with writeflags(__FILE__,__LINE__)

so, in

log.__FLAGS << "something" ;  

it would replace it with

log.writeflags(__FILE__,__LINE__)  << "something" ;  

and I guess that this is what you're looking for

and that's why I think that your piece of code is OK

Not sure if this helps.... hope it does

Tincho
0
 
LVL 2

Author Comment

by:funvill
ID: 9742168
i want it too look like this,

log << "something" ;

when the user is calling the log function i also whant it to call writeflags(__FILE__,__LINE__)  function before the opertator <<
and i do not want to have to type anything extra
so

i want this;
log.writeflags(__FILE__,__LINE__) << "something" ;

to turn in to this
log << "something" ;
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9742240
Well, actually there you have 2 different possibilities............

1) I'm not quite sure what the class of the log object is........ but if you reimplement operator<< for that class so that you include those writeflags there is one possibility...........

2) you can also, and similar to what you posted...........

#define LOG log.writeflags(__FILE__,__LINE__)

LOG << "something"

The problem with the first one is that I'm not sure if you can change the implementation of the operator<< as I'm not sure of which class it belongs to

The problem with the last one is that the preprocessor will replace all LOG with log.writeflags( __FILE__, __LINE__ )
so, your variable can only be called log and not with any other name.

Hope this helps

Tincho
0
 
LVL 1

Expert Comment

by:osp70
ID: 9742250
what about overriding the operator << in your log class so that is takes in the string but then does the rest.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9742311
Well, I guess that osp chooses option 1 then. :)

Tincho
0
 
LVL 1

Expert Comment

by:osp70
ID: 9742354
yes, I read your answer and copied part of it in the hopes to grab some points

OR

I was reading the post before you submitted your answer and submitted mine.

:)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:funvill
ID: 9742361
re: tinchos
[1]
my log is a class that opens a file when it is created and puts time stamps and stores the log file in a perditulare place. and yes i have  reimplement <<
writeflags(__FILE__,__LINE__) is a function that returns a refercen to a log obj after it writes the file and line to a file.

[2]
#define LOG log.writeflags(__FILE__,__LINE__)

this will not work because, what if they make a log object called errorlog and other called userlog
log errorlog;
log userlog;
i should have stated that my class is called log

re: osp70

i cant implment it in the operator << because i need to call writeflags( __FILE__, __LINE__ ) from the file that is sending the log message

__FILE__
__LINE__
are macros http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/prepr_18.asp that have to be define in the file that they came from.
err if i where to put writeflags( __FILE__, __LINE__ ) in the implment for operator << it would just keep displaying the
__FILE__
__LINE__
with the line and file of the source for the implment of operator <<
0
 
LVL 2

Author Comment

by:funvill
ID: 9742398
sorry i should have done this first off

heres my source code
http://downloads.funvill.com/stuff/logy_v0.01aA.zip

in the main function i use
a.__flags << "something";

i don't want it have to write __flags every time. and i want it to look like this
a. << "something";


0
 
LVL 9

Expert Comment

by:tinchos
ID: 9742412
osp:

please, don't take it wrong.......
just a joking......
I know it happens all the time

funvill:
I guess that if those macros must appear in that line, then your only choice is


#define LOG log.writeflags(__FILE__,__LINE__)

this will not work because, what if they make a log object called errorlog and other called userlog
log errorlog;
log userlog;
i should have stated that my class is called log

for which you can always say

#define ERRORLOG errorlog.writeflags(__FILE__,__LINE__)
#define USERLOG userlog.writeflags(__FILE__,__LINE__)

Hope this helps

Tincho
0
 
LVL 2

Author Comment

by:funvill
ID: 9742467
I’m am not just writing this for my self, when I deem it complete I will give it to my boss and it will become the coding slandered for my company.
When people look at this class they do not want to have to do any extra steps like

#define USERLOG userlog.writeflags(__FILE__,__LINE__)

So, sorry I do not want to have to ask them to make a #define for each different file
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9742537
Ok funvill

Sincerely I don't quite like this solution, I would be very happy to be able to program without any macro.

I'm not saying neither that there aren't any other possibilites, nor that this is a good one, just that it's the only that came to mi mind.

Finally one last suggestion in order to improve it a little more

#define LOG( varName ) varname.writeflags(__FILE__,__LINE__)

in this case you would just have to add the define in the log.h header class and you would just have to include it and call it with the variable name

calling it with

LOG( userLog );
LOG( errorLog );

Hope this helps

Tincho
0
 
LVL 2

Author Comment

by:funvill
ID: 9742601
re: tinchos
this might be a decent solution...
but I’m going to look around for the rest of the day for my perfect solution, if I don't find it then i will accept this one.
thank you very much for your help
0
 
LVL 9

Accepted Solution

by:
tinchos earned 50 total points
ID: 9742633
Ok funvill

I agree with you that this is a decent solution, not an optimal one.

Please, if you find a better one let me know....... ;)

Cheers

Tincho
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now