Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144822
  • 博文数量: 48
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-18 14:46
个人简介

多多学习,多多交流

文章分类
文章存档

2016年(1)

2015年(23)

2014年(24)

我的朋友

分类: 项目管理

2015-10-04 16:41:10

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

点击(此处)折叠或打开

  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 */

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