Reputation: 3008
I usually debug my programs using print statements ( redirected to a log file).
I place a #define _DEBUG
at the top and place all the print statement between a #ifdef _DEBUG
and #endif
so that once I am convinced that program is running correctly I can change the #define and none of the print statements appear anymore.
The only disadvantage is that with all #ifdefs
in the program, it is not at all readable because #ifdefs
start from the beginning of the line.
Is there a way I can remove all the #ifdef
,#endif
and also all the lines between them ?
I can do this using recording if there are same number of print statements between define condition.
Upvotes: 1
Views: 171
Reputation: 882078
I'm sure you can remove them somehow, but why? You don't need to start #ifdef
s at the start of the line, this is perfectly valid:
int getVal (void) {
int x = 10
#ifdef MY_DEBUG_FLAG
printf ("Returning %d\n", x);
#endif
return x;
}
The other thing you should consider is not defining the variable directly in the source code with something like:
#define MY_DEBUG_FLAG
Most compilers have a switch which will do the same sort of thing before processing the source code:
gcc -DMY_DEBUG_FLAG ...
That way, you'll only need to change the global compiler flags in your makefile (or whatever build tool you use), almost certainly in one spot rather than every single source file.
And you can still selectively turn on debug for individual files if need be, by temporarily changing just the one compiler command. How easy this is depends on your build system but all that I've used make it rather painless.
Upvotes: 4
Reputation: 10497
A fairly common approach is to use a preprocessor macro to insert print statements that only get compiled during a debug build. This should help.
Upvotes: 1