转自http://longriver.me/?p=325

在编写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);

相关文章:

  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-12-12
  • 2021-12-09
  • 2022-12-23
相关资源
相似解决方案