2 years ago

#61689

test-img

Axel Persinger

Remove debug strings from release build

I am trying to find the best way to exclude my debug strings from making it to my release build. Currently, I'm outputting debug strings via syslog, and I have the current setup in my debug.c file:

#ifdef DEBUG
void init_debug(void)
{
    openlog("MyAppName", LOG_PERROR, LOG_SYSLOG);
}
#else
void init_debug(void){}
#endif

#ifdef DEBUG
void debug(const char* format, ...)
{
    va_list args;
    va_start(args, format);
    vsyslog(LOG_DEBUG, format, args);
    va_end(args);
}
#else
void debug(const char* format, ...){}
#endif

So, you can see that the functions will be empty when I build the release version. However, when I call the function, I would do something like:

#include "debug.h"

int main(int argc, const char* argv[])
{
    init_debug();
    debug("Number of arguments: %d", argc);
}

However, this of course makes my strings appear in the release build as they're still htere. The easy solution is to do something like:

#include "debug.h"

int main(int argc, const char* argv[])
{
    init_debug();
#ifdef DEBUG
   debug("Number of arguments: %d", argc);
#endif
}

But that's incredibly verbose, and hard to read (especially when it's grouped up with real code).

Are there other, more elegant solutions?

c

string

release

0 Answers

Your Answer

Accepted video resources