This question already has an answer here:
这个问题在这里已有答案:
- C #define macro for debug printing 12 answers
C #define宏用于调试打印12个答案
From what I understand assert is a macro in C and supposedly if you use it at compile time but leave it disabled then there won't be overhead (which might not be correct I don't know). The problem for me is that what I'd like to do is get all the variables passed to my function and print out that output, but only if I want debugging enabled. Here is what I have so far:
根据我的理解,断言是C中的一个宏,并且如果你在编译时使用它但是禁用它,那么就不会有开销(这可能不正确我不知道)。对我来说问题是,我想要做的是将所有变量传递给我的函数并打印输出,但仅限于我想要启用调试。这是我到目前为止:
int exampleFunction (int a, int b)
{
#ifdef debugmode
printf("a = %i, b = %i", a, b);
#endif
}
I'm wondering if there is any easier (and less ugly) method for doing something like this. xdebug for php has this feature and I've found is saves me an enormous amount of time when debugging so i want to do it for each function.
我想知道是否有更容易(并且不那么难看)的方法来做这样的事情。 xdebug for php有这个功能,我发现在调试时节省了大量的时间,所以我想为每个功能做这件事。
Thanks
7 个解决方案
#1
try this:
#ifdef debugmode
#define DEBUG(cmd) cmd
#else
#define DEBUG(cmd)
#endif
DEBUG(printf("a = %i, b = %i", a, b));
now, if you have debugmode
defined, you get your print statement. otherwise, it never shows up in the binary.
现在,如果你定义了debugmode,你会得到你的print语句。否则,它永远不会出现在二进制文件中。
#2
Using GCC, I really enjoy to add, per file:
使用GCC,我真的很喜欢添加,每个文件:
#if 0
#define TRACE(pattern,args...) fprintf(stderr,"%s:%s/%u" pattern "\n",__FILE__,__FUNCTION__,__LINE__,##args)
#else
#define TRACE(dummy,args...)
#endif
and then in the code:
然后在代码中:
i++;
TRACE("i=%d",i);
i
will be printed only when I activate the TRACE() macro in the top of the file. Works really great, plus it prints the source file, line and function it occurred.
只有当我激活文件顶部的TRACE()宏时才会打印。工作真的很棒,加上它打印出来的源文件,行和功能。
#3
if (MY_DEBUG_DEFINE) {
do_debug_stuff();
}
Any half decent compiler would optimize the block away. Note you need to define MY_DEBUG_DEFINE
as a boolean (ie 0 or not 0).
任何一半体面的编译器都会优化块。注意,您需要将MY_DEBUG_DEFINE定义为布尔值(即0或不为0)。
#define MY_DEBUG_DEFINE defined(NDEBUG)
If you happen to compile with maximum warning level, this trick avoids unreferenced argument.
如果您碰巧使用最大警告级别进行编译,则此技巧可避免未引用的参数。
#4
Workaround to get vararg with preprocessors that don't support it
使用不支持它的预处理器获取vararg的解决方法
#define DEBUG
#ifdef DEBUG
#define trace(args) printf args
#else
#define trace(args)
#endif
int dostuff(int value)
{
trace(("%d", value));
}
#5
You can define PRINTF_IF_DEBUGGING as
您可以将PRINTF_IF_DEBUGGING定义为
#ifndef NDEBUG
#define PRINTF_IF_DEBUGGING(X) printf(X)
#else
#define PRINTF_IF_DEBUGGING(X)
#endif
This will centralize the #ifdefs in only one place.
这将只在一个地方集中#ifdefs。
#6
Well, the problem with the PRINT_DEBUG type macros as given here, is that they only allow one parameter. For a proper printf() we'll need several, but C macros don't (presently) allow variable arguments.
那么,这里给出的PRINT_DEBUG类型宏的问题是它们只允许一个参数。对于正确的printf(),我们需要几个,但C宏(目前)不允许变量参数。
So, to pull this off, we've got to get creative.
所以,要实现这一目标,我们必须发挥创意。
#ifdef debugmode
#define PRINTF printf
#else
#define PRINTF 1 ? NULL : printf
#endif
Then when you write PRINTF("a = %i, b = %i", a, b);
, in non-debug mode, it will be renders as (effectively):
然后当你在非调试模式下写PRINTF(“a =%i,b =%i”,a,b);时,它将呈现为(有效):
if (true) NULL;
else printf("a = %i, b = %i", a, b);
The compiler is happy, but the printf is never execute, and if the compiler if bright (i.e, any modern C compiler), the code for the printf() will never be generated, as the compiler will recognize that path can never be taken.
编译器很高兴,但printf永远不会执行,如果编译器是明亮的(即任何现代C编译器),printf()的代码将永远不会生成,因为编译器将识别该路径永远不会被执行。
Note, however, that the parameters will still be evaluated, so if they have any side effects (i.e, ++x or a function call), they code may be generated (but not executed)
但请注意,参数仍将被评估,因此如果它们有任何副作用(即++ x或函数调用),它们的代码可能会生成(但不会被执行)
#7
I would also print some other C preprocessor flags that can help you track problems down
我还打印一些其他C预处理器标志,可以帮助您跟踪问题
printf("%s:%d {a=%i, b=%i}\n", __FILE__, __LINE__, a, b);
#1
try this:
#ifdef debugmode
#define DEBUG(cmd) cmd
#else
#define DEBUG(cmd)
#endif
DEBUG(printf("a = %i, b = %i", a, b));
now, if you have debugmode
defined, you get your print statement. otherwise, it never shows up in the binary.
现在,如果你定义了debugmode,你会得到你的print语句。否则,它永远不会出现在二进制文件中。
#2
Using GCC, I really enjoy to add, per file:
使用GCC,我真的很喜欢添加,每个文件:
#if 0
#define TRACE(pattern,args...) fprintf(stderr,"%s:%s/%u" pattern "\n",__FILE__,__FUNCTION__,__LINE__,##args)
#else
#define TRACE(dummy,args...)
#endif
and then in the code:
然后在代码中:
i++;
TRACE("i=%d",i);
i
will be printed only when I activate the TRACE() macro in the top of the file. Works really great, plus it prints the source file, line and function it occurred.
只有当我激活文件顶部的TRACE()宏时才会打印。工作真的很棒,加上它打印出来的源文件,行和功能。
#3
if (MY_DEBUG_DEFINE) {
do_debug_stuff();
}
Any half decent compiler would optimize the block away. Note you need to define MY_DEBUG_DEFINE
as a boolean (ie 0 or not 0).
任何一半体面的编译器都会优化块。注意,您需要将MY_DEBUG_DEFINE定义为布尔值(即0或不为0)。
#define MY_DEBUG_DEFINE defined(NDEBUG)
If you happen to compile with maximum warning level, this trick avoids unreferenced argument.
如果您碰巧使用最大警告级别进行编译,则此技巧可避免未引用的参数。
#4
Workaround to get vararg with preprocessors that don't support it
使用不支持它的预处理器获取vararg的解决方法
#define DEBUG
#ifdef DEBUG
#define trace(args) printf args
#else
#define trace(args)
#endif
int dostuff(int value)
{
trace(("%d", value));
}
#5
You can define PRINTF_IF_DEBUGGING as
您可以将PRINTF_IF_DEBUGGING定义为
#ifndef NDEBUG
#define PRINTF_IF_DEBUGGING(X) printf(X)
#else
#define PRINTF_IF_DEBUGGING(X)
#endif
This will centralize the #ifdefs in only one place.
这将只在一个地方集中#ifdefs。
#6
Well, the problem with the PRINT_DEBUG type macros as given here, is that they only allow one parameter. For a proper printf() we'll need several, but C macros don't (presently) allow variable arguments.
那么,这里给出的PRINT_DEBUG类型宏的问题是它们只允许一个参数。对于正确的printf(),我们需要几个,但C宏(目前)不允许变量参数。
So, to pull this off, we've got to get creative.
所以,要实现这一目标,我们必须发挥创意。
#ifdef debugmode
#define PRINTF printf
#else
#define PRINTF 1 ? NULL : printf
#endif
Then when you write PRINTF("a = %i, b = %i", a, b);
, in non-debug mode, it will be renders as (effectively):
然后当你在非调试模式下写PRINTF(“a =%i,b =%i”,a,b);时,它将呈现为(有效):
if (true) NULL;
else printf("a = %i, b = %i", a, b);
The compiler is happy, but the printf is never execute, and if the compiler if bright (i.e, any modern C compiler), the code for the printf() will never be generated, as the compiler will recognize that path can never be taken.
编译器很高兴,但printf永远不会执行,如果编译器是明亮的(即任何现代C编译器),printf()的代码将永远不会生成,因为编译器将识别该路径永远不会被执行。
Note, however, that the parameters will still be evaluated, so if they have any side effects (i.e, ++x or a function call), they code may be generated (but not executed)
但请注意,参数仍将被评估,因此如果它们有任何副作用(即++ x或函数调用),它们的代码可能会生成(但不会被执行)
#7
I would also print some other C preprocessor flags that can help you track problems down
我还打印一些其他C预处理器标志,可以帮助您跟踪问题
printf("%s:%d {a=%i, b=%i}\n", __FILE__, __LINE__, a, b);