Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7539109
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 项目管理

2012-11-08 16:33:13

以下代码可以在项目开发过程中统一控制调试信息是否打印

点击(此处)折叠或打开

  1. /**
  2.  * 简单打印调试信息
  3.  */
  4. #define DEBUG_SWITCH        1
  5. #ifdef    DEBUG_SWITCH
  6. #define pr_debug(fmt,args...) printf(fmt, ##args)
  7. #else
  8. #define pr_debug(fmt,args...) /*do nothing */
  9. #endif

  10. /**
  11.  * 错误信息打印
  12.  * 自动打印发生错误时代码所在的位置
  13.  */
  14. #define     ERR_DEBUG_SWITCH        1
  15. #ifdef    ERR_DEBUG_SWITCH
  16. #define pr_err(fmt,args...) printf("\nFile:<%s> Fun:[%s] Line:%d\n "fmt, __FILE__, __FUNCTION__, __LINE__, ##args)
  17. #else
  18. #define pr_err(fmt,args...) /*do nothing */
  19. #endif

  20. /**
  21.  * 断言
  22.  * 对某种假设条件进行检查(若条件成立则无动作,否则报告错误信息)
  23.  */
  24. #define _EXAM_ASSERT_TEST_        1
  25. #define _EXAM_ASSERT_EXIT_        1
  26. #ifdef _EXAM_ASSERT_TEST_ // 若使用断言测试
  27. void exam_assert(int condition, const char * file_name, const char *fun, unsigned int line_no, const char *fmt, ...)
  28. {
  29.     char sBuf[1024];
  30.     va_list va;

  31.     if (!condition)
  32.     {
  33.         bzero(sBuf, sizeof(sBuf));
  34.         va_start(va, fmt);
  35.         vsprintf(sBuf, fmt, va);

  36.         printf("\n[EXAM]Assert failed: File:<%s> Fun:[%s] Line:%d\n %s", file_name, fun, line_no, sBuf);
  37. #ifdef _EXAM_ASSERT_EXIT_
  38.         abort();
  39. #endif
  40.     }
  41. }

  42. #define EXAM_ASSERT(condition, fmt, args...)    exam_assert(condition, __FILE__, __FUNCTION__, __LINE__,fmt, ##args)

  43. #else // 若不使用断言测试
  44. #define EXAM_ASSERT(condition, fmt, args...) NULL
  45. #endif /* end of ASSERT */

阅读(2543) | 评论(0) | 转发(7) |
0

上一篇:随机数

下一篇:C语言编程规范概要

给主人留下些什么吧!~~