C调试宏DEBUG的定义与使用

C调试宏的定义与使用

#ifdef DEBUG
#define debug(...)	fprintf(stderr, "message(%s, %s(), %d): ", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)	
#endif

上面的宏的功能是:将debug定义为调试工具,会格式化的从标准错误流输出此时debug所处的文件名、函数名、行数、以及debug内部自定义的内容
此时,当代码中定义了DEBUG时,使用debug便能打印调试信息,当代码中没有定义DEBUG时,debug没有任何意义,什么都不执行,自动跳过。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEBUG
#ifdef DEBUG
#define debug(...)	fprintf(stderr, "message(%s, %s(), %d): ", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)	do {} while (0)
#endif

int main(void)
{
	//do which you want to do
	//...
	//..
	debug("now-----------------\n");
	return 0;
}

这个时候的debug便有打印信息的能力,执行结果为debug结果
再当取消掉DEBUG的定义时,程序执行便没有调试信息输出。
取消DEBUG定义

发布了7 篇原创文章 · 获赞 2 · 访问量 252

猜你喜欢

转载自blog.csdn.net/weixin_44532706/article/details/105725821