How do you create a debug only function that takes a variable argument list? Like printf()

后端 未结 14 2093
心在旅途
心在旅途 2020-12-23 09:44

I\'d like to make a debug logging function with the same parameters as printf. But one that can be removed by the pre-processor during optimized builds.

<
相关标签:
14条回答
  • 2020-12-23 10:17

    @CodingTheWheel:

    There is one slight problem with your approach. Consider a call such as

    XTRACE("x=%d", x);
    

    This works fine in the debug build, but in the release build it will expand to:

    ("x=%d", x);
    

    Which is perfectly legitimate C and will compile and usually run without side-effects but generates unnecessary code. The approach I usually use to eliminate that problem is:

    1. Make the XTrace function return an int (just return 0, the return value doesn't matter)

    2. Change the #define in the #else clause to:

      0 && XTrace
      

    Now the release version will expand to:

    0 && XTrace("x=%d", x);
    

    and any decent optimizer will throw away the whole thing since short-circuit evaluation would have prevented anything after the && from ever being executed.

    Of course, just as I wrote that last sentence, I realized that perhaps the original form might be optimized away too and in the case of side effects, such as function calls passed as parameters to XTrace, it might be a better solution since it will make sure that debug and release versions will behave the same.

    0 讨论(0)
  • 2020-12-23 10:17

    What platforms are they not available on? stdarg is part of the standard library:

    http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html

    Any platform not providing it is not a standard C implementation (or very, very old). For those, you will have to use varargs:

    http://opengroup.org/onlinepubs/007908775/xsh/varargs.h.html

    0 讨论(0)
  • 2020-12-23 10:21

    Another fun way to stub out variadic functions is:

    #define function sizeof
    
    0 讨论(0)
  • 2020-12-23 10:21

    Having come across the problem today, my solution is the following macro:

        static TCHAR __DEBUG_BUF[1024];
        #define DLog(fmt, ...)  swprintf(__DEBUG_BUF, fmt, ##__VA_ARGS__); OutputDebugString(__DEBUG_BUF) 
      
    

    You can then call the function like this:

        int value = 42;
        DLog(L"The answer is: %d\n", value);
    
    0 讨论(0)
  • 2020-12-23 10:25

    This is how I do debug print outs in C++. Define 'dout' (debug out) like this:

    #ifdef DEBUG
    #define dout cout
    #else
    #define dout 0 && cout
    #endif
    

    In the code I use 'dout' just like 'cout'.

    dout << "in foobar with x= " << x << " and y= " << y << '\n';
    

    If the preprocessor replaces 'dout' with '0 && cout' note that << has higher precedence than && and short-circuit evaluation of && makes the whole line evaluate to 0. Since the 0 is not used the compiler generates no code at all for that line.

    0 讨论(0)
  • 2020-12-23 10:27

    This is what I use:

    inline void DPRINTF(int level, char *format, ...)
    {
    #    ifdef _DEBUG_LOG
            va_list args;
            va_start(args, format);
            if(debugPrint & level) {
                    vfprintf(stdout, format, args);
            }
            va_end(args);
    #    endif /* _DEBUG_LOG */
    }
    

    which costs absolutely nothing at run-time when the _DEBUG_LOG flag is turned off.

    0 讨论(0)
提交回复
热议问题