您的位置:首页 > 其它

打印错误信息的宏

2010-12-23 15:26 253 查看
两个版本:普通信息输出和错误信息输出

note:宏中应增加fflush()函数以及时输出信息。

版本一:

#define info(format, ...)		printf("@@@info:"format"@@@location:Line:%d, Function:%s,File:%s/n", ##__VA_ARGS__, __LINE__ , __FUNCTION__, __FILE__)


版本二:

#define debug_err(format, ...)	do {		/
if(DEBUG)	{			/
printf("@@@error:"format"@@@location:Line:%d, Function:%s,File:%s/n", ##__VA_ARGS__, __LINE__ , __FUNCTION__, __FILE__);	/
}	/
} while(0)


版本三:

#define debug(format, ...)  	do {\
if(strcmp(strerror(errno), "Success"))	{\
system("echo -n `date +%Y.%m.%d-%H:%M:%S`");\
fprintf(stdout, "\tERROR: "format"\tLine:%d, File: %s\n\t\t\tReasonNo:%d\tReason:%s\n", ##__VA_ARGS__, __LINE__ ,__FILE__, errno, strerror(errno) );\
}else	{\
fprintf(stdout, format"\n", ##__VA_ARGS__);\
}\
fflush(stdout);\
errno = 0;\
}while(0)


测试代码:

#include <iostream>
#include<stdio.h>
#include<stdarg.h>
#define DEBUG 1
#define info(format, ...) printf("@@@info:"format"@@@location:Line:%d, Function:%s,File:%s/n", ##__VA_ARGS__, __LINE__ , __FUNCTION__, __FILE__)
#define debug_err(format, ...) do { / if(DEBUG) { / printf("@@@error:"format"@@@location:Line:%d, Function:%s,File:%s/n", ##__VA_ARGS__, __LINE__ , __FUNCTION__, __FILE__); / } / } while(0)
int main()
{
char* str = "test";
int i = 123456;

printf("printf 版本:/n");
printf("str: %s, i: %02d/n", str, i);
printf("/ndebug 版本:/n");
debug_err("str: %s, i: %02d/n", str, i);

printf("/nrelease 版本:/n");
info("str: %s, i: %02d/n", str, i);
system("pause");
return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: