I am attempting to make some debugging macros because I hate having to type #ifdef DEBUG it takes so long to type and it is not very pretty so I tried making some macros like this:
The basic problem is that afaik you can't use pre-processor directives within pre-processor directives because it only does one pass, so the ones you're swapping the text to are passed to the compiler, which says "wtf? " and falls over.
Use [tt]#define DEBUG[/tt] or [tt]gcc -DDEBUG[/tt] instead. Kemp is right, you cannot "nest" preprocessor directives like that. Use [tt]gcc -E[/tt] to find out what your preprocessor does with your code here.
While you're at it, use NDEBUG instead of DEBUG - that way you can toggle assert() and your DBPRINT() with the same flag.
Every good solution is obvious once you've found it.
I like Candy's [tt]debug { ... }[/tt] syntax though, because it allows arbitary statements, and creates a new local scope for temporaries. Often figuring out the data-to-be-printed is more work than actually printing it.
The [tt]DEBUG_ON[/tt] / [tt]DEBUG_OFF[/tt] is IMHO worse because it too creates a scope without the [tt]{}[/tt] that would normally tell you there's one.
mystran wrote:
I like Candy's [tt]debug { ... }[/tt] syntax though, because it allows arbitary statements, and creates a new local scope for temporaries. Often figuring out the data-to-be-printed is more work than actually printing it.
I'm liking it more & more too, it's in a way very clear about what it's doing (most people would claim you're modifying the language, it fits in that well) and it does exactly what you'd want it to do (so even if you don't know it, you can use it). This one's a keeper for me.
Jordan3 wrote:
though it might cause some confusion
i'd mostly fear that it become tempting to have real code in the "else { ... }" part, and that the "debugging" version differs not only by the verbosity level, but also by the nitty-gritty details of its implementation.
and I define NLOGWRITE in gcc if I need it.. gcc -DNLOGWRITE etc...
I also have another routine for the same thing which you pass a severity flag with it and it will only print logging messages over a certain level.. (user controlled. good for testing....)
One hint on that: Make sure your compiler is smart enough to optimize the call to _LogWrite() away to nothing when NLOGWRITE is defined, or be aware that even with logging disabled you're paying for a function call whenever _LogWrite() comes up.
This can get really nasty when you're applying this to C++ code, where converting a complex object to string (to satisfy operator<<() ) can get really expensive.
Every good solution is obvious once you've found it.
Solar wrote:
This can get really nasty when you're applying this to C++ code, where converting a complex object to string (to satisfy operator<<() ) can get really expensive.
A bit OT, just out of interest: Do you know whether there is a good or common solution against this problem?