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

 转载地址:http://blog.chinaunix.net/uid-20671208-id-3013780.html
coding  最重要的是如何 debug debug  当然就少不了把程序信息输出,如何清晰明了地打印出程序信息,可以快速判断程序运行情况,定位程序出问题的地方。先来一段代码实战下再说:

 

  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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值