[英]C++ on Linux - How do I #define a #ifdef / #endif debug conditional
I want to trace my code if DEBUG
is defined, as in the following #ifdef
code block: 如果要定义DEBUG
,我想跟踪我的代码,如下面的#ifdef
代码块所示:
#ifdef DEBUG
OP_LOG(debug) << "SEQUENCE: " __FILE__ << "::" << __FUNCTION__;
#endif
Someone said I could use a #define
so that the above three lines would only be one line instead. 有人说我可以使用#define
以便上述三行只能是一行。 This is the closest I could get to figuring it out, but it gives tracing output unconditionally, whether or not DEBUG
is defined: 这是我能找出的最接近的结果,但是无论是否定义了DEBUG
,它都会无条件地提供跟踪输出:
#define DEBUG_TRACE(note) OP_LOG(debug) << "SEQUENCE : " << __FILE__ <<"::" <<__FUNCTION__ << note
I don't know how to make a conditional #define
. 我不知道如何进行条件#define
。 Is this possible, and if so, how to do it? 这可能吗?如果可以,怎么做?
You can't put #ifdef
in the replacement part of a #define
, but you can use #define
inside an #ifdef
/ #endif
block, like this: 您不能将#ifdef
放在#define
的替换部分中,但是可以 在 #ifdef
/ #endif
块内使用#define
,如下所示:
#ifdef DEBUG
#define DEBUG_TRACE(note) ... // put your debug code here
#else
#define DEBUG_TRACE(note)
#endif
In the #else
case, this defines DEBUG_TRACE
to expand to nothing if DEBUG
isn't defined. 在#else
情况下,如果未定义DEBUG
,则将DEBUG_TRACE
定义为扩展为DEBUG_TRACE
。
Someone said I could create a #define, so that the above three lines would only be one line instead. 有人说我可以创建一个#define,这样上面的三行就只能是一行。
Not that I have heard of. 不是我听说过的。 My favorite way to do this would be 我最喜欢的方法是
#ifdef DEBUG
#define DEBUG_LOG OP_LOG(debug) << "SEQUENCE: " __FILE__ << "::" << __FUNCTION__;
#else
#define DEBUG_LOG
#endif
This way you have to only write DEBUG_LOG where ever you want to log only if -DDEBUG
is set. 这样,你必须只写DEBUG_LOG在任何你想,如果只记录-DDEBUG
设置。
Look at the #ifdef
, #else
, #endif
statement. 查看#ifdef
, #else
, #endif
语句。 We define DEBUG_LOG
to be expanded to nothing when -DDEBUG
isn't set and as OP_LOG(debug) << "SEQUENCE: " __FILE__ << "::" << __FUNCTION__;
我们定义DEBUG_LOG
扩大到什么时候-DDEBUG
并没有被设置为OP_LOG(debug) << "SEQUENCE: " __FILE__ << "::" << __FUNCTION__;
if it is. 如果是。 That way you don't have to worry about -DDEBUG
later on, you just have to use DEBUG_LOG
when you want to place a trace call. 这样,您以后就不必担心-DDEBUG
了,只需在要进行跟踪调用时使用DEBUG_LOG
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.