Debug Macro

2013-01-10 Klaus Ma 更多博文 » 博客 » GitHub »

原文链接 http://www.k8s.tips/tech/2013/01/10/debug_macro/
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


Code slice

#define debug(fmt,...) do {                                             \
        fprintf(stderr,"[ DEBUG ] : [ %s, %d ] ",__FILE__,__LINE__);    \
        fprintf(stderr,fmt,##__VA_ARGS__);                              \
        fprintf(stderr,"\n");                                           \
} while(0)

How to use

#include <stdio.h>

#define debug(fmt,...) do {                                             \
        fprintf(stderr,"[ DEBUG ] : [ %s, %d ] ",__FILE__,__LINE__);    \
        fprintf(stderr,fmt,##__VA_ARGS__);                              \
        fprintf(stderr,"\n");                                           \
} while(0)

int main(int argc, char** argv)
{
        debug("this is a debug sample with <%d> args", argc);
            debug("debug sample exit.");
                return 0;
}

Output of the sample

[ DEBUG ] : [ main.cpp, 12 ] this is a debug sample with <1> args
[ DEBUG ] : [ main.cpp, 13 ] debug sample exit.