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

后端 未结 14 2167
心在旅途
心在旅途 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: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);
    

提交回复
热议问题