-
function using "define"
So I am trying to make a program work and this is in one of the h files.
Code:
#define LogDebug( fmt, args... ) do { if ( gDebug ) { Log( fmt, ## args ); }} while (0)
and it is giving me errors. can u declare a function like this or should i write an actual function in the cpp file?
-
The example you described is pointless to use macros with, use a function instead. If you're running the program in no-debug mode you'll get decreased efficiency since the if-statement is always checked. The way you coould define a more efficient debug macro is:
Code:
#ifdef DEBUG_MODE_ON
#define LogDebug(fmt, args) Log(fmt, args);
#else
#define LogDebug(fmt, args)
#endif
This way if debug mode is off it will be expanded to nothing, no unneccessary code is run.
Of course you cannot enable/disable debug mode at runtime, it has to be done at compile time...
-
I take it your trying to forward a arbitrairy amount of arguements from LogDebug to Log. In that case:
I believe there is a way to pass to have a variadic macro, but I do not know it.
Regardless, you can rewrite Log() to take a va_list and pass that from LogDebug, writen as a regular function.
-
Why don't you try
Code:
#if DEBUG
int debug_fn(debug_info a, b, c)
{
//...
}
#define LOG_ERROR(a, b, c) debug_fn(a, b, c);
#else
#define LOG_ERROR(a, b, c)
#endif
It's a Very Bad Idea to use macros for complicated stuff like this. Make it a function.
-
Using #if will make debug mode only controlled at compile time. This is generally perfectly fine for personal projects that will only be used on you computer. Runtime debugging is usefull for traking errors observed on another machine, such as if a user of your program reports a bug.