Programming with verbose option in C
c
Solution
Just use a (variadic) macro / vararg function that tests a flag before calling something like vfprintf.
/* Crude example. */
void my_log(char *format, ...)
{
va_list args;
if (!ok_to_log)
return;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
EDIT As requested
How about a slightly modified gnu example:
#define eprintf(format, ...) do { \
if (ok_to_print) \
fprintf(stderr, format, ##__VA_ARGS__); \
} while(0)
Problem
Does someone know how to write a program in C with the verbose option (the option to choose if messages are printed or not) in a nice way. I mean, not writing an if(verbose) for each printf in the code. Is there a more elegant solution?