Solved

weird macros

Posted on 2003-11-13
13
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 107
trigs fail! I thought I knew how to do trignometry 3 83
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 48
C++ Code Issue 4 38
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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