Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160291
  • 博文数量: 56
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 17:36
文章分类

全部博文(56)

文章存档

2016年(43)

2015年(9)

2014年(2)

2013年(2)

我的朋友

分类: LINUX

2015-07-30 13:45:00

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

 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>

  4. #ifndef __USE_DEBUG
  5. #define __USE_DEBUG

  6. #define USE_DEBUG
  7. #ifdef USE_DEBUG
  8. #define DEBUG_LINE() printf("[%s:%s] line=%d\r\n",__FILE__, __func__, __LINE__)
  9. #define DEBUG_ERR(fmt, args...) printf("\033[46;31m[%s:%d]\033[0m "#fmt" errno=%d, %m\r\n", __func__, __LINE__, ##args, errno, errno)
  10. #define DEBUG_INFO(fmt, args...) printf("\033[33m[%s:%d]\033[0m "#fmt"\r\n", __func__, __LINE__, ##args)
  11. #else
  12. #define DEBUG_LINE()
  13. #define DEBUG_ERR(fmt, ...)
  14. #define DEBUG_INFO(fmt, ...)
  15. #endif

  16. #endif

  17. void func()
  18. {
  19.         DEBUG_LINE();
  20.         DEBUG_INFO("Garfield test DEBUG_INFO() d: %d ; s: %s", 1 , __FUNCTION__);
  21.         DEBUG_ERR("Garfield test DEBUG_ERR() d: %d ; s: %s", 2 , __FUNCTION__);
  22. }

  23. int main(int argc,char **argv)
  24. {
  25.         func();

  26.         return 0;
  27. }

分析:

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

 

  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 是对颜色调用的始末格式如下:

 

  1. \033[ ; m …… \033[0m

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

 

  1. #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,   打印调试信息的跟踪位置:

 

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

         如上代码:

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

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

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

 

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

 

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

    调用方式如下:

 

  1. int i = 110;
  2. char * s = “hello world!”;
  3. 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

 

  1. #ifdef DEBUG
  2. #define F_OUT printf("%s:", __FUNCTION__);fflush(stdout);
  3. #define L_OUT printf("%s:%d:", __FILE__, __LINE__);fflush(stdout);
  4. #define A_OUT printf("%s:%d:%s:", __FILE__, __LINE__, __FUNCTION__);fflush(stdout);
  5. #define D_OUT printf("DEBUG:");fflush(stdout);

  6. #define F_PRINTF(fmt, arg...) F_OUT printf(fmt, ##arg)
  7. #define L_PRINTF(fmt, arg...) L_OUT printf(fmt, ##arg)
  8. #define A_PRINTF(fmt, arg...) A_OUT printf(fmt, ##arg)
  9. #define PRINTF(fmt, arg...) D_OUT printf(fmt, ##arg)
  10. #define DBUG(a) {a;}
  11. #else
  12. #define F_OUT
  13. #define L_OUT
  14. #define A_OUT
  15. #define D_OUT

  16. #define F_PRINTF(fmt, arg...)
  17. #define L_PRINTF(fmt, arg...)
  18. #define A_PRINTF(fmt, arg...)
  19. #define PRINTF(fmt, arg...)
  20. #define DBUG(a)
  21. #endif

  22. #define F_PERROR(fmt) F_OUT perror(fmt)
  23. #define L_PERROR(fmt) L_OUT perror(fmt)
  24. #define A_PERROR(fmt) A_OUT perror(fmt)
  25. #define PERROR(fmt) D_OUT perror(fmt)

 

 

 

阅读(1347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~