在编写C++/C 的项目,因为调试的需要,经常会输出debug信息,那如何输出debug信息呢?
在C里面可以这样定义一个debug的宏
1
2
3
4
5 |
#ifdef
DEBUG_BUILD
# define DEBUG(x) fprintf(stderr, x)
#else
# define DEBUG(x) do {} while (0)
#endif |
到了CPP可以这样定义:
1
2
3 |
#define
DEBUG(x) do { \
if(debugging_enabled){std::cerr<<x<<std::endl;}\
}while(0) |
当然你也可以将__FILE__,__LINE__,__func__这些变量放上
如果你想支持变长的变量输出的话
1
2
3
4 |
#define
DEBUG(fmt,...) do {\
fprintf(stderr,fmt,##__VA_ARGS__);
\
}\
while(0) |
这样以后使用的话 DEBUG("i is : %d",i);
C++ debug 的宏定义(macros)支持变长变量方法小结,布布扣,bubuko.com
C++ debug 的宏定义(macros)支持变长变量方法小结
原文:http://www.cnblogs.com/harveyaot/p/3600421.html