You can't use preprocessor directives while defining a #define
. This means that your #ifdef USE_WRITE_DEBUG_INFO
won't work.
For this case, use a function rather than a macro:
#include <cstdarg>
void my_printf(const char* format, ...) {
#ifdef USE_WRITE_DEBUG_INFO
char buff[200];
va_list args;
va_start(args, format);
// Note: uses the snprintf variant rather than sprintf variant,
// avoiding buffer overlows.
vsnprintf(buff, 200, format, args);
va_end(args);
WriteDebugInfo(buff);
#else
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
#endif
}
In general, you'd have to bring the preprocessor directives outside of the #define
:
#ifndef MY_PRINTF
#ifdef USE_WRITE_DEBUG_INFO
// do { ... } while (false) makes the macro behave mostly like a regular function call.
#define MY_PRINTF(f_, ...) do { \
char buff[200]; \
sprintf(buff, (f_), __VA_ARGS__); \
WriteDebugInfo(buff); \
} while (false)
#else
#define MY_PRINTF(f_, ...) printf((f_), __VA_ARGS__)
#endif
#endif