I need to print msg1 and msg2 for the same error when compiled with and without DEBUG flag. E.g.
fprintf( stderr,
#ifdef DEBUG
"error msg1 %s",__FILE__
#else
"error msg2"
#endif
);
or other way might be to pass these msg1
and msg2
to a function and print it using vfprintf()
.
Probably, The second method would have run time overhead. So, I am just wondering what could be better way of doing this?
E.g. A use case might be that the code need to be compiled with info
and debug
flags. info
might be user related messages and debug
for debugging purpose.
Any suggestions?
Usualy the traces are used in the code to help debug it, so for example in your NULL pointer tests you can add smth like if (ptr==NULL) DEBUG("Entering Null pointer");
. I'am just telling you that because i don't udrestand why you want use both msg1
and msg2
.
For me i usually use a macro DEBUG
and a global variable verbose
:
#define DEBUG(...)\
if(verbose && SHOW_ERROR) {\
printf("Error : %s, %d",__FUNCTION__, __LINE__);\
printf(__VA_ARGS__);\
}\
else if (verbose && SHOW_WARNING) {\
printf("Warning : %s, %d",__FUNCTION__, __LINE__);\
printf(__VA_ARGS__);\
}
Example :
#include <stdio.h>
#define SHOW_ERROR 1
#define SHOW_WARNING 2
int verbose = 1;
int main()
{
DEBUG("THIS WILL SHOW ERROR MSG");
verbose = 2;
DEBUG("THIS WILL SHOW WARNING MSG");
}
Hope i help.