博客地址:http://home.cnblogs.com/u/zengjianrong/

代码打印调试的模版

  博客地址:http://www.cnblogs.com/zengjianrong/p/4185072.html 

  在代码调试过程中,经常需要打印调试,下面是一个使用模版,可以通过“DEBUGGING”在代码里面(或者编译时添加“-D”选项)关闭(或打开)打印选项,亦可以通过改变“DEBUGGING_LVL”改变打印等级。

  另外还可以通过添加参数,用于区分打印信息所属的代码模块。也可以通过将打印信息分类存于相关log文件,用于运行监控等,这个就自由发挥了。

#include <stdio.h> 

#define SYSTEM_ERROR    1
#define PROCESS_PRINT   0
#define DEBUGGING_LVL   1
#define DEBUGGING

#ifdef DEBUGGING
#define DEBUG(lvl, ...)\
    do\
    {\
        if (lvl >= DEBUGGING_LVL)\
        {\
            printf("[DEBUG] in %s:%d %s(),",\
                __FILE__, __LINE__, __FUNCTION__);\
            printf(__VA_ARGS__);\
            printf("\n");\
        }\
    } while(0)
#else
#define DEBUG(lvl, ...) do {} while(0)
#endif 

int main(void)
{    
    int i = 0;    
    DEBUG(PROCESS_PRINT, "Hello world!");
    if (0 == i)
    {
     /* 假设这里为系统错误 */ DEBUG(SYSTEM_ERROR,
"panic when i=%d.", i); } DEBUG(PROCESS_PRINT, "Goodbye world!"); return 0; }

参考:http://blog.csdn.net/liuhaoyutz/article/details/40370755

posted @ 2014-12-25 17:02  black_man  阅读(193)  评论(0编辑  收藏  举报