log levels

Jun 26, 2011 at 11:24pm
Hi,

I would like to have different log levels in my code. For example: something like

cout.INFO << "x " << x << endl;
cout.INFO << "y " << y << endl;
cout.MOREINFO << "x2 " << x**2 << endl;

I compile the code and when I run my executable, I would like to be able to run it in different modes like INFO, MOREINFO...

./executable --INFO

or

./executable --MOREINFO

What is the easiest way to do it ?

Thanks in advance.
Jun 27, 2011 at 12:05am
1
2
3
4
5
6
7
8
9
10
#if DEBUG_LEVEL < 1
#define INFO tellp();//
#define MOREINFO tellp();//
#elif DEBUG_LEVEL < 2
#define INFO
#define MOREINFO tellp();//
#elif DEBUG_LEVEL >= 2
#define INFO
#define MOREINFO
#endif 

With your current code, this will work depending on whether debug level is 0 (no debugging), 1 (low debugging) or 2 (high debugging). Though, this must be defined at compile time, to use command line arguments you'd have to have the appropriate main() definition.
Last edited on Jun 27, 2011 at 12:06am
Jun 27, 2011 at 12:42am
That actually wouldn't work because of the pesky dot operator:

cout.INFO <<
Will evaluate to the below if INFO is an empty define:

cout. << which of course is nonsense because of the dot

also, comments are not part of macros, so you can't remove the rest of the line like that.

The question is, do you want this log level to be determined at compile time or run time? From your original post I thought you were looking for runtime, but L B thought compile time.
Jun 27, 2011 at 1:05am
Disch, haha, I took care of the . one way but forgot the other way; here is the fixed compile time debugging:
1
2
3
4
5
6
7
8
9
10
#if DEBUG_LEVEL < 1
#define INFO tellp(); /##/
#define MOREINFO tellp(); /##/
#elif DEBUG_LEVEL < 2
#define INFO write("Debug: ", 7)
#define MOREINFO tellp(); /##/
#elif DEBUG_LEVEL >= 2
#define INFO write("Debug: ", 7)
#define MOREINFO write("Debug: ", 7)
#endif 


EDIT: I had to replace the // with /##/, otherwise they would not be considered part of the macro (just a comment after the macro)
Last edited on Jun 27, 2011 at 1:23am
Jun 27, 2011 at 9:50am
thanks for the replies. I prefer to do it at runtime which I guess easier for me.

What I want to do is to print "sometimes" only SOME of the cout statements in the code and "sometimes" ALL the cout statements in the code.

I say "cout" but any of the way to do it is welcome.
cheers
Jun 27, 2011 at 11:25am
You can define main like this:
int main(unsigned int ArgCount, char* const Args[])
(note you can rename ArgCount and Args to whatever you want)

Then, when someone starts your program with the command line -debug or /debug, you can loop through Args as an array of char* and check with strcmp if it matches "debug". If none of the parameters do, then you're not in debug mode.
Last edited on Jun 27, 2011 at 11:30am
Topic archived. No new replies allowed.