转自http://longriver.me/?p=325spa
在编写C++/C 的项目,由于调试的须要,常常会输出debug信息,那如何输出debug信息呢?
在C里面能够这样定义一个debug的宏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__这些变量放上code
若是你想支持变长的变量输出的话seo
1
2
3
4
|
#define DEBUG(fmt,...) do {\
fprintf
(
stderr
,
fmt
,
##__VA_ARGS__); \
}
\
while
(
0
)
|
这样之后使用的话 DEBUG("i is : %d",i);ip