printf 调试相关知识

我们在写程序的时候,一般会加入printf之类的语句来输出调试信息,然而当程序写完之后如何关闭这些调试信息就比较麻烦,这段时间看LoRa相关知识时,发现了官方源码中一个比较好的处理方法,代码如下:

#include <stdio.h>

#define DEBUG_LOG       1

#define MSG(args...) printf(args) /* message that is destined to the user */
#define MSG_DEBUG(FLAG, fmt, ...)                                                                         \
            do  {                                                                                         \
                if (FLAG)                                                                                 \
                    fprintf(stdout, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
            } while (0)

int main() {  
    char str[]="Hello World";  
    MSG_DEBUG(DEBUG_LOG, "~~~: %s\n",str);  
    return 0;  
}  

当不需要输出调试信息时,将DEBUG_LOG置零即可,也可以根据需要多定义几个调试的类型。

 

编译执行程序:

 

posted @ 2017-10-11 11:18  colmoon  阅读(145)  评论(0编辑  收藏  举报