I wrote the following trace macro in a file named "debug.h".
我在名为“debug.h”的文件中编写了以下跟踪宏。
#define TRACE(x) \
printf( \
"%s(%d): ", \
__FILE__, \
__LINE__ \
); \
\
printf(x);
In debug I'd like to enable the macro only for certain files since resources are limited on the platform that I'm using. I don't want to completely remove the TRACE calls from the files. Just disable them.
在调试中我想只为某些文件启用宏,因为资源在我正在使用的平台上是有限的。我不想从文件中完全删除TRACE调用。只是禁用它们。
Is there a clean way to do this in C using the preprocessor?
有没有一种干净的方法在使用预处理器的C中执行此操作?
4 个解决方案
#1
2
In debug.h
:
#if TRACE_ENABLE
#define TRACE(x) \
printf( \
"%s(%d): ", \
__FILE__, \
__LINE__ \
); \
\
printf(x);
#else
#define TRACE(x)
#endif
Then, in your source files where you don't want trace:
然后,在您不想要跟踪的源文件中:
#define TRACE_ENABLE 0
#include "debug.h"
or just:
#include "debug.h"
In source files to enable trace:
在源文件中启用跟踪:
#define TRACE_ENABLE 1
#include "debug.h"
#2
1
While both answers seems good to me, I think Giuseppe's answer is more useful most of the time since if you use this macro many times in a file, and you want to switch debug on/off for complete files, pmg's method is exhausting. The important thing is to not forget adding the else statement: #else TRACE(X);
if you want to edit it in the specific file and not in header, use:
虽然这两个答案对我来说似乎都很好,但我认为Giuseppe的答案在大多数情况下更有用,因为如果你在文件中多次使用这个宏,并且想要为完整文件打开/关闭调试,那么pmg的方法就会让人筋疲力尽。重要的是不要忘记添加else语句:#else TRACE(X);如果要在特定文件中而不是在标题中编辑它,请使用:
#ifdef TRACE
#undef TRACE
#endif
#define TRACE(X)
#3
1
A trick I've used somtimes is the use of a bit mask to enable a subset of the files whete the TRACE is used: File1.c:
我曾经使用过的一个技巧是使用位掩码来启用TRACE使用的文件子集:File1.c:
#if TRACE_MASK & 0x01
#define TRACE(x) ...
#endif
File2.c:
#if TRACE_MASK & 0x02
#define TRACE(x) ...
#endif
... Then you can define your TRACE_MASK macro in the preprocessing options: /DTRACE_MASK=0x03 to enable the trace on both File1.c and File2.c The only problem is that there is a limited numner of bits... (but you can use more than one macro: TRACE_MASK1, TRACE_MASK2...) Bye
...然后你可以在预处理选项中定义你的TRACE_MASK宏:/ DTRACE_MASK = 0x03来启用File1.c和File2.c上的跟踪。唯一的问题是有一个有限的位数......(但是你可以使用多个宏:TRACE_MASK1,TRACE_MASK2 ...)再见
EDIT: of course you can write tdefinition once in a file "trace.h", and just redefine the mask in each source:
编辑:当然你可以在文件“trace.h”中写一次tdefinition,然后重新定义每个源中的掩码:
File trace.h:
#if TRACE_MASK & TRACE_CURRENT
#define TRACE(x) ...
#else
#define TRACE(x) do {} while(0)
#endif
File1.c:
#define TRACE_CURRENT 0x01
#include "trace.h"
File2.c:
#define TRACE_CURRENT 0x02
#include "trace.h"
#4
0
What about
#define TRACE(x, y) do if (y) {/*your prints*/} while (0)
and also
#define TRACE_ENABLE 1
or
#define TRACE_ENABLE 0
at the top of your sources.
在你的消息来源的顶部。
Then replace the TRACE invocations with
然后用。替换TRACE调用
TRACE(foo, TRACE_ENABLE);
#1
2
In debug.h
:
#if TRACE_ENABLE
#define TRACE(x) \
printf( \
"%s(%d): ", \
__FILE__, \
__LINE__ \
); \
\
printf(x);
#else
#define TRACE(x)
#endif
Then, in your source files where you don't want trace:
然后,在您不想要跟踪的源文件中:
#define TRACE_ENABLE 0
#include "debug.h"
or just:
#include "debug.h"
In source files to enable trace:
在源文件中启用跟踪:
#define TRACE_ENABLE 1
#include "debug.h"
#2
1
While both answers seems good to me, I think Giuseppe's answer is more useful most of the time since if you use this macro many times in a file, and you want to switch debug on/off for complete files, pmg's method is exhausting. The important thing is to not forget adding the else statement: #else TRACE(X);
if you want to edit it in the specific file and not in header, use:
虽然这两个答案对我来说似乎都很好,但我认为Giuseppe的答案在大多数情况下更有用,因为如果你在文件中多次使用这个宏,并且想要为完整文件打开/关闭调试,那么pmg的方法就会让人筋疲力尽。重要的是不要忘记添加else语句:#else TRACE(X);如果要在特定文件中而不是在标题中编辑它,请使用:
#ifdef TRACE
#undef TRACE
#endif
#define TRACE(X)
#3
1
A trick I've used somtimes is the use of a bit mask to enable a subset of the files whete the TRACE is used: File1.c:
我曾经使用过的一个技巧是使用位掩码来启用TRACE使用的文件子集:File1.c:
#if TRACE_MASK & 0x01
#define TRACE(x) ...
#endif
File2.c:
#if TRACE_MASK & 0x02
#define TRACE(x) ...
#endif
... Then you can define your TRACE_MASK macro in the preprocessing options: /DTRACE_MASK=0x03 to enable the trace on both File1.c and File2.c The only problem is that there is a limited numner of bits... (but you can use more than one macro: TRACE_MASK1, TRACE_MASK2...) Bye
...然后你可以在预处理选项中定义你的TRACE_MASK宏:/ DTRACE_MASK = 0x03来启用File1.c和File2.c上的跟踪。唯一的问题是有一个有限的位数......(但是你可以使用多个宏:TRACE_MASK1,TRACE_MASK2 ...)再见
EDIT: of course you can write tdefinition once in a file "trace.h", and just redefine the mask in each source:
编辑:当然你可以在文件“trace.h”中写一次tdefinition,然后重新定义每个源中的掩码:
File trace.h:
#if TRACE_MASK & TRACE_CURRENT
#define TRACE(x) ...
#else
#define TRACE(x) do {} while(0)
#endif
File1.c:
#define TRACE_CURRENT 0x01
#include "trace.h"
File2.c:
#define TRACE_CURRENT 0x02
#include "trace.h"
#4
0
What about
#define TRACE(x, y) do if (y) {/*your prints*/} while (0)
and also
#define TRACE_ENABLE 1
or
#define TRACE_ENABLE 0
at the top of your sources.
在你的消息来源的顶部。
Then replace the TRACE invocations with
然后用。替换TRACE调用
TRACE(foo, TRACE_ENABLE);