Thread: Best way to write a DEBUG() output printing macro? My way isn't, apparently...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    Best way to write a DEBUG() output printing macro? My way isn't, apparently...

    This is code for the C99 standard (gcc on linux).

    What I'm trying to do: Write a macro like DEBUG ("some message") that I can drop into code and will only be compiled in if DEBUG_WANTED (or whatever) is defined. Sounds simple, but...

    I have some time-sensitive code that is very tricky. As I debug it, I've put in commands like this:

    Code:
    debug ("At step three, branching to step %i\n",next_step);
    The debug() function is a straightforward vfprintf() routine (that works fine) and debug_wanted is a global variable set at run-time as a command-line option.

    However, having all these debug() calls all over the place slows down the code when I no longer need debugging. So I could either remove all the debug() calls when I no longer need them (and then hope I never need to go put them back in), or change them to conditionally compiled code. Then I can chose to compile with debug support on or off.

    I like the latter idea, but am struggling to write the macro. I don't want to have to do something like this for each call (there are perhaps 100 debug() lines ;-)

    Code:
    #ifdef DEBUG
    debug ("At step three, branching to step %i\n",next_step);
    #endif
    I would prefer something like this:
    Code:
    DEBUG("At step three, branching to step %i\n",next_step);
    The DEBUG() macro would be like this:

    Code:
    #define DEBUG(msg...) \
    #ifdef DEBUG_WANTED \
    debug (msg) \
    #endif
    ...except that doesn't work ;-)

    I just can't figure out what I'm doing wrong in nesting the preprocessor directives...anyone have a spare clue?

    I'm getting a compiler error that says "'#' is not followed by a macro parameter". I suspect it's not possible to nest preprocessor directives in this way...

    Or is there another way to accomplish this without #ifdef DEBUG_WANTED before every call to a debug printer?

    (BTW, the "msg..." part is right, or I could use __VAR_ARGS__ instead: Variadic Macros - The C Preprocessor ... something I learned today ;-)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The DEBUG macro should then be something like
    Code:
    #ifdef DEBUG_WANTED
    #define DEBUG(msg) {whatever you've got now}
    #else
    #define DEBUG(msg)
    #endif
    The idea being DEBUG has to be defined no matter what (else it won't compile) -- but if you're not debugging it needs to just go away.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    12
    Ah, of course. tabstop, you make it so simple. Call the function if DEBUG_WANTED, otherwise it's just define'd to blank.

    Thanks much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 12:11 PM
  2. output will not write to a second file
    By kdtardy in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 03:31 AM
  3. Visual C++ printing console output
    By clarinetster in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2001, 01:42 AM
  4. printing output from prog
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 08:50 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM