用于调试的printf函数和自定义log函数
2016-12-06 19:15
627 查看
1. 用宏定义调试用的DPRINT
2. 自定义的log函数模型:
#define DEBUG_ENABLE #ifdef DEBUG_ENABLE #define DPRINT(fmt, args...) fprintf(stderr, "[DPRINT...][%s %d] "fmt"\n", __FILE__, __LINE__, ##args); #else #define DPRINT(fmt, ...) #endif发布时,将#define DEBUG_ENABLE去掉即可
#define DPRINT(fmt, args...) if( d_print ) printf(fmt,##args);也可以这样,就能在传入参数时设置是否打印调试信息了。这里,如果可变参数被忽略或为空,‘##'操作将使预处理器(preprocessor)去除掉它前面的那个逗号。
2. 自定义的log函数模型:
char LogLastMsg[128]; // all info of the last log, all the info to log last time int Log2Stderr = LOG_ERR; //control Loging to stderr /** * @Synopsis a log func demo * demo for how user defined module log info * * @Param priority: level of log, LOG_ERR, LOG_DEBUG etc. * @Param errno: errno * @Param fmt: format of message to log * @Param ...: args follow by fmt */ void mylog(int priority, int errno, char* fmt, ...) { DPRINT("mylog Begin..."); char priVc[][8] = {"Emerg", "Alert", "Crit", "Error", "Warning", "Notice", "Info", "Debug"}; char* priPt = priority < 0 || priority >= sizeof(priVc)/sizeof(priVc[0]) ? "Unknow priority!" : priVc[priority]; char *errMsg = errno <= 0 ? NULL : (const char*)strerror(errno); { va_list argPt; unsigned Ln; va_start(argPt, fmt); //now argPt is point to mylog's param:... Ln = snprintf(LogLastMsg, sizeof(LogLastMsg), "[mylog...][%s]: ", priPt); Ln += vsnprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, fmt, argPt); if (NULL != errMsg) { Ln += snprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, "%d:%s", errno, errMsg); } va_end(argPt); } //choose the log which should be show on stderr if (priority < LOG_ERR || priority <= Log2Stderr) { fprintf(stderr, "%s\n", LogLastMsg); } DPRINT("log to stderr"); //always to syslog syslog(priority, "%s", LogLastMsg); if (priority <= LOG_ERR) { exit(-1); } return ; }
相关文章推荐
- 用于调试的printf函数和自定义log函数
- 我常用的自定义函数之p 用于打印数据,调试代码很方便
- 2440汇编控制LED(IO端口)和串口输出,用于调试wince的startup.s函数
- 自定义的调试函数clr/c++
- Python帮助函数调试函数 用于获取对象的属性及属性值
- javaScript中自定义sort中的比较函数,用于比较字符串长度,数值大小
- Python帮助函数调试函数 用于获取对象的属性及属性值
- SQL自定义函数实现字符串截取转化为整型(主要用于对批量数据的操作)
- VBA 连接文本的自定义函数(可用于数组公式)
- IAR瑞萨单片机开发加入printf调试函数
- VC++ 用于调试的printf显示控件
- 自定义类printf变参数的输出函数_常用于调试
- cocos2d-x 给函数接口生命周期添加检测函数,用于调试程序时检测错误。
- Keil MDK 中利用串口及c标准库函数printf为cortex-m3做调试输出(lpc1788)
- 写了一个将VxWorks的shell打印输出到指定内存中的接口,可广泛用于CLI下的调试函数显示以及故障自动捕获等功能,稍作修改可以适合其它嵌入式OS
- [STM32F10x] 使用printf函数进行串口调试问题
- scanf和printf函数家族用于格式化输入和输出(转)
- Keil MDK 中利用串口及c标准库函数printf为cortex-m3做调试输出(lpc1788)
- Keil MDK 中利用串口及c标准库函数printf为cortex-m3做调试输出(lpc1788)
- CLR 自定义函数调试错误 DataAccessKind.Read or SystemDataAccessKind.Read