您的位置:首页 > 运维架构 > Linux

Linux 程序开发打印 Debug 信息的使用技巧

2016-06-02 00:14 786 查看
coding 最重要的是如何debug,debug 当然就少不了把程序信息输出,如何清晰明了地打印出程序信息,可以快速判断程序运行情况,定位程序出问题的地方。先来一段代码实战下再说:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#ifndef __USE_DEBUG

#define __USE_DEBUG

#define USE_DEBUG

#ifdef USE_DEBUG

#define DEBUG_LINE() printf("[%s:%s] line=%d\r\n",__FILE__, __func__, __LINE__)

#define DEBUG_ERR(fmt, args...)
printf("\033[46;31m[%s:%d]\033[0m "#fmt" errno=%d, %m\r\n", __func__, __LINE__, ##args,errno, errno)

#define DEBUG_INFO(fmt, args...)
printf("\033[33m[%s:%d]\033[0m "#fmt"\r\n", __func__, __LINE__, ##args)

#else

#define DEBUG_LINE()

#define DEBUG_ERR(fmt, ...)

#define DEBUG_INFO(fmt, ...)

#endif

#endif

void func()

{

DEBUG_LINE();

DEBUG_INFO("Garfield test DEBUG_INFO() d: %d ; s: %s", 1 , __FUNCTION__);

DEBUG_ERR("Garfield test DEBUG_ERR() d: %d ; s: %s", 2 , __FUNCTION__);

}

int main(int argc,char **argv)

{

func();

return 0;

}

分析:
1, 使用颜色打印调试信息:

printf("\033[46;31m[%s:%d]\033[0m "#fmt" errno=%d, %m\r\n", __func__, __LINE__, ##args, errno, errno);

上面printf时在Linux 命令行下打印出带颜色的字体,方便一眼区分不同种类的调试信息,只需要加上一些颜色代码,例如:这里的46代表底色,
31代表字体的颜色。
使用ascii code 是对颜色调用的始末格式如下:

\033[ ; m …… \033[0m

后面哪个 ”\033[0m” 是对前面哪个颜色载入的结束,恢复到终端原来的背景色和字体色,可以把后面哪个修改成如下试试:

#define DEBUG_ERR(fmt, args...) printf("\033[46;31m[%s:%d]\033[40;37m
"#fmt" errno=%d, %m\r\n", __func__, __LINE__, ##args, errno, errno);

下面列出 ascii code 的颜色值:

字背景颜色范围:40----49 字颜色:30-----------39

40:黑 30:黑

41:深红 31:红

42:绿 32:绿

43:黄色 33:黄

44:蓝色 34:蓝色

45:紫色 35:紫色

46:深绿 36:深绿

47:白色 37:白色

2, 打印调试信息的跟踪位置:

printf("[%s:%s] line=%d\r\n",__FILE__, __func__, __LINE__);

printf("\033[33m[%s:%d]\033[0m "#fmt"\r\n", __func__, __LINE__, ##args);

如上代码:

1)__FILE__ 打印出调试信息所在的文件名;

2)__func__ 将会打印出调试信息所在的函数名;

3)__LINE__ 将会打印出调试信息所在文件的行号;

3, 使用不定参数向打印信息里面加入自己想看到的调试信息:

#define DEBUG_INFO(fmt, args...) printf("\033[33m[%s:%d]\033[0m
"#fmt"\r\n", __func__, __LINE__, ##args);

调用方式如下:

int i = 110;

char * s = “hello world!”;

DEBUG_INFO("Garfield test DEBUG_INFO() d: %d ; s: %s", i , s);

至于不定数量参数宏与不定参数函数的使用就没神马好说的啦,自己去google吧!

下面引用一位大侠的blog,列出一些常用的debug 语句:
出自:http://blog.mcuol.com/User/luoming/Article/16499_1.htm

#ifdef DEBUG

#define F_OUT printf("%s:", __FUNCTION__);fflush(stdout);

#define L_OUT printf("%s:%d:", __FILE__, __LINE__);fflush(stdout);

#define A_OUT printf("%s:%d:%s:", __FILE__, __LINE__, __FUNCTION__);fflush(stdout);

#define D_OUT printf("DEBUG:");fflush(stdout);

#define F_PRINTF(fmt, arg...) F_OUT
printf(fmt, ##arg)

#define L_PRINTF(fmt, arg...) L_OUT
printf(fmt, ##arg)

#define A_PRINTF(fmt, arg...) A_OUT
printf(fmt, ##arg)

#define PRINTF(fmt, arg...) D_OUT
printf(fmt, ##arg)

#define DBUG(a) {a;}

#else

#define F_OUT

#define L_OUT

#define A_OUT

#define D_OUT

#define F_PRINTF(fmt, arg...)

#define L_PRINTF(fmt, arg...)

#define A_PRINTF(fmt, arg...)

#define PRINTF(fmt, arg...)

#define DBUG(a)

#endif

#define F_PERROR(fmt) F_OUT perror(fmt)

#define L_PERROR(fmt) L_OUT perror(fmt)

#define A_PERROR(fmt) A_OUT perror(fmt)

#define PERROR(fmt) D_OUT perror(fmt)

转载于: http://blog.chinaunix.net/uid-20671208-id-3013780.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息