Chinaunix首页 | 论坛 | 博客
  • 博客访问: 73498
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 57
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-30 09:39
个人简介

程序改变世界!

文章分类

全部博文(15)

文章存档

2018年(1)

2017年(4)

2016年(7)

2015年(3)

我的朋友

分类: C/C++

2017-06-21 22:15:40

需求:
    
使用#if...#else...实现打印控制,开发人员能便捷的修改宏的判断条件,而不用关心宏的实现。

实现:
    开关实现处,此处的头文件可放在公共头文件目录include中,供项目使用(不能被C文件直接包含)。
debug.h:

点击(此处)折叠或打开

  1. #ifndef _SYSLOG_DEBUG_H
  2. #define _SYSLOG_DEBUG_H

  3. #ifndef _DEBUG_CONFIG_INCLUDE
  4.     #error "this file cannot be included by C file"
  5. #endif

  6. #if (_PRINT_ENABLE == 1)
  7.     #define LOG_PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
  8. #else
  9.      #define LOG_PRINT
  10. #endif //_PRINT_ENABLE

  11. #endif //_SYSLOG_DEBUG_H
    开关控制处,此处的头文件可放在需要使用LOG_PRINT宏处。
debug_config.h:

点击(此处)折叠或打开

  1. #ifndef _DEBUG_CFG_H
  2. #define _DEBUG_CFG_H

  3. #define _DEBUG_CONFIG_INCLUDE

  4. int _PRINT_ENABLE = 1:

  5. /*following statement must at last*/

  6. #include "debug.h"

  7. #endif //_DEBUG_CFG_H

测试:
main.c:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include "debug_config.h"

  3. int main()
  4. {
  5.     printf("start : \n");
  6.     
  7.     LOG_PRINT("debug enable\n");

  8.     return 0;
  9. }

总结:
    将main.c和debug.c放在一起,开发人员通过修改debug.c的控制开关,实现宏定义的控制。


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