Link to home
Start Free TrialLog in
Avatar of funvill
funvillFlag for Canada

asked on

weird macros

hello I’m trying to make a better logging class
http://oldlook.experts-exchange.com/questions/20791532/A-better-logging-class.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" ;  
Avatar of tinchos
tinchos

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
Avatar of funvill

ASKER

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" ;
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
what about overriding the operator << in your log class so that is takes in the string but then does the rest.
Well, I guess that osp chooses option 1 then. :)

Tincho
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.

:)
Avatar of funvill

ASKER

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 << 
Avatar of funvill

ASKER

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";


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
Avatar of funvill

ASKER

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
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
Avatar of funvill

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of tinchos
tinchos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial