If the condition is a compile-time constant, then your code (after preprocessing) works something like this:
if (0) do the logging
Then the compiler will usually be smart enough to cut dead code, including the lines that you passed to it (unless you also used the lines in other code that was not deleted, of course).
Code that acts like printf is pretty simple:
#include <stdarg.h> void log(char const &fmt, ...) { if (do_logging) { va_list args; va_start(args, fmt); vfprintf(output_file, fmt, args); } }
In a macro (itβs important that it is in the macro, not the function being called), you can use __FILE__ and __LINE__ for the current line number and the name of the source file for logging. With the above code, you probably want to pass them before the format string.
Jerry Coffin
source share