c - #define宏,用于在C中进行调试打印?

标签 c c-preprocessor

尝试创建定义DEBUG时可用于打印调试消息的宏,例如以下伪代码:

#define DEBUG 1
#define debug_print(args ...) if (DEBUG) fprintf(stderr, args)


如何使用宏完成此操作?

最佳答案

如果使用C99或更高版本的编译器

#define debug_print(fmt, ...) \
            do { if (DEBUG) fprintf(stderr, fmt, __VA_ARGS__); } while (0)


假定您使用的是C99(早期版本不支持变量参数列表符号)。 do { ... } while (0)习惯用法确保代码的行为像一条语句(函数调用)。对代码的无条件使用可确保编译器始终检查您的调试代码是否有效-但当DEBUG为0时,优化程序将删除该代码。

如果要使用#ifdef DEBUG,则更改测试条件:

#ifdef DEBUG
#define DEBUG_TEST 1
#else
#define DEBUG_TEST 0
#endif


然后在我使用DEBUG的地方使用DEBUG_TEST。

如果您坚持使用字符串文字作为格式字符串(无论如何可能是个好主意),则还可以在输出中引入__FILE____LINE____func__之类的东西,这可以改善诊断:

#define debug_print(fmt, ...) \
        do { if (DEBUG) fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \
                                __LINE__, __func__, __VA_ARGS__); } while (0)


这依赖于字符串连接来创建比程序员编写的格式更大的字符串。

如果您使用C89编译器

如果您对C89感到困惑,并且没有有用的编译器扩展,则没有一种特别干净的方法来处理它。我过去使用的技术是:

#define TRACE(x) do { if (DEBUG) dbg_printf x; } while (0)


然后,在代码中编写:

TRACE(("message %d\n", var));


双括号至关重要-这就是为什么在宏扩展中使用有趣的符号的原因。和以前一样,编译器总是检查代码的语法有效性(这很好),但是优化器仅在DEBUG宏的计算结果为非零时才调用打印功能。

这确实需要支持功能(在示例中为dbg_printf())来处理“ stderr”之类的事情。它要求您知道如何编写varargs函数,但这并不难:

#include <stdarg.h>
#include <stdio.h>

void dbg_printf(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
}


当然,您也可以在C99中使用此技术,但是__VA_ARGS__技术更整洁,因为它使用常规函数符号,而不是双括号。

为什么编译器始终看到调试代码至关重要?

[重新整理对另一个答案的评论。]

上面的C99和C89实现背后的一个中心思想是,适当的编译器始终会看到调试类似printf的语句。这对于长期的代码很重要,这种代码将持续十年或两年。

假设一段代码大多数年来一直处于休眠状态(稳定),但是现在需要进行更改。您可以重新启用调试跟踪-但是必须调试调试(跟踪)代码令人沮丧,因为在稳定的维护期间,调试代码涉及已重命名或重新键入的变量。如果编译器(后预处理器)始终看到print语句,则可以确保所有周围的更改都不会使诊断无效。如果编译器看不到打印语句,则它不能保护您免受自己的粗心(或同事或合作者的粗心)的伤害。请参阅Kernighan和Pike的“ The Practice of Programming”,尤其是第8章(另请参见TPOP上的Wikipedia)。

这是“到那里,就做完了”的经验-我本质上使用了其他答案中描述的技术,其中非调试版本多年来(超过十年)都看不到类似于printf的语句。但是我遇到了TPOP中的建议(请参阅我以前的评论),然后在几年后确实启用了一些调试代码,并遇到了上下文更改中断调试的问题。好几次,始终对打印进行验证使我免于以后的麻烦。

我使用NDEBUG仅控制声明,并使用单独的宏(通常为DEBUG)来控制是否在程序中内置了调试跟踪。即使内置了调试跟踪,我也经常不希望调试输出无条件显示,因此我有一种机制来控制是否显示输出(调试级别,而不是直接调用fprintf()而是调用调试打印函数,仅有条件地进行打印,因此基于程序选项可以打印或不打印相同版本的代码)。对于大型程序,我还有一个代码的“多个子系统”版本,因此我可以在运行时控制下,使程序的不同部分产生不同数量的跟踪。

我主张对于所有构建,编译器都应查看诊断语句。但是,除非启用了debug,否则编译器不会为调试跟踪语句生成任何代码。基本上,这意味着每次编译时,编译器都会检查所有代码-无论是发布还是调试。这是一件好事!

debug.h-版本1.2(1990-05-01)

/*
@(#)File:            $RCSfile: debug.h,v $
@(#)Version:         $Revision: 1.2 $
@(#)Last changed:    $Date: 1990/05/01 12:55:39 $
@(#)Purpose:         Definitions for the debugging system
@(#)Author:          J Leffler
*/

#ifndef DEBUG_H
#define DEBUG_H

/* -- Macro Definitions */

#ifdef DEBUG
#define TRACE(x)    db_print x
#else
#define TRACE(x)
#endif /* DEBUG */

/* -- Declarations */

#ifdef DEBUG
extern  int     debug;
#endif

#endif  /* DEBUG_H */


debug.h-版本3.6(2008-02-11)

/*
@(#)File:           $RCSfile: debug.h,v $
@(#)Version:        $Revision: 3.6 $
@(#)Last changed:   $Date: 2008/02/11 06:46:37 $
@(#)Purpose:        Definitions for the debugging system
@(#)Author:         J Leffler
@(#)Copyright:      (C) JLSS 1990-93,1997-99,2003,2005,2008
@(#)Product:        :PRODUCT:
*/

#ifndef DEBUG_H
#define DEBUG_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

/*
** Usage:  TRACE((level, fmt, ...))
** "level" is the debugging level which must be operational for the output
** to appear. "fmt" is a printf format string. "..." is whatever extra
** arguments fmt requires (possibly nothing).
** The non-debug macro means that the code is validated but never called.
** -- See chapter 8 of 'The Practice of Programming', by Kernighan and Pike.
*/
#ifdef DEBUG
#define TRACE(x)    db_print x
#else
#define TRACE(x)    do { if (0) db_print x; } while (0)
#endif /* DEBUG */

#ifndef lint
#ifdef DEBUG
/* This string can't be made extern - multiple definition in general */
static const char jlss_id_debug_enabled[] = "@(#)*** DEBUG ***";
#endif /* DEBUG */
#ifdef MAIN_PROGRAM
const char jlss_id_debug_h[] = "@(#)$Id: debug.h,v 3.6 2008/02/11 06:46:37 jleffler Exp $";
#endif /* MAIN_PROGRAM */
#endif /* lint */

#include <stdio.h>

extern int      db_getdebug(void);
extern int      db_newindent(void);
extern int      db_oldindent(void);
extern int      db_setdebug(int level);
extern int      db_setindent(int i);
extern void     db_print(int level, const char *fmt,...);
extern void     db_setfilename(const char *fn);
extern void     db_setfileptr(FILE *fp);
extern FILE    *db_getfileptr(void);

/* Semi-private function */
extern const char *db_indent(void);

/**************************************\
** MULTIPLE DEBUGGING SUBSYSTEMS CODE **
\**************************************/

/*
** Usage:  MDTRACE((subsys, level, fmt, ...))
** "subsys" is the debugging system to which this statement belongs.
** The significance of the subsystems is determined by the programmer,
** except that the functions such as db_print refer to subsystem 0.
** "level" is the debugging level which must be operational for the
** output to appear. "fmt" is a printf format string. "..." is
** whatever extra arguments fmt requires (possibly nothing).
** The non-debug macro means that the code is validated but never called.
*/
#ifdef DEBUG
#define MDTRACE(x)  db_mdprint x
#else
#define MDTRACE(x)  do { if (0) db_mdprint x; } while (0)
#endif /* DEBUG */

extern int      db_mdgetdebug(int subsys);
extern int      db_mdparsearg(char *arg);
extern int      db_mdsetdebug(int subsys, int level);
extern void     db_mdprint(int subsys, int level, const char *fmt,...);
extern void     db_mdsubsysnames(char const * const *names);

#endif /* DEBUG_H */


C99或更高版本的单参数变体

凯尔·勃兰特(Kyle Brandt)问:


  无论如何,即使没有参数,debug_print仍然可以工作?例如:

    debug_print("Foo");



有一个简单的老式hack:

debug_print("%s\n", "Foo");


下面显示的仅适用于GCC的解决方案也对此提供了支持。

但是,您可以通过使用以下方法对C99直系统进行操作:

#define debug_print(...) \
            do { if (DEBUG) fprintf(stderr, __VA_ARGS__); } while (0)


与第一个版本相比,您丢失了需要'fmt'参数的有限检查,这意味着有人可以尝试不带参数的调用'debug_print()'(但是参数列表中以fprintf()结尾的逗号会失败编译)。遗失支票是否根本是个问题尚有争议。

单个参数的特定于GCC的技术

一些编译器可能会提供扩展来处理宏中变长参数列表的其他方式。具体来说,如Hugo Ideler的注释中首先指出的那样,GCC允许您省略通常在宏的最后一个“固定”参数之后出现的逗号。它还允许您在宏替换文本中使用##__VA_ARGS__,当且仅当前一个标记是逗号时,它才会删除符号前的逗号:

#define debug_print(fmt, ...) \
            do { if (DEBUG) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)


该解决方案保留了要求format参数的优点,同时在格式之后接受可选参数。

Clang还为GCC兼容性支持此技术。



为什么执行do-while循环?


  do while的目的是什么?


您希望能够使用该宏,使其看起来像一个函数调用,这意味着后面将使用分号。因此,您必须包装宏主体以适合。如果您使用if语句而没有周围的do { ... } while (0),则将具有:

/* BAD - BAD - BAD */
#define debug_print(...) \
            if (DEBUG) fprintf(stderr, __VA_ARGS__)


现在,假设您编写:

if (x > y)
    debug_print("x (%d) > y (%d)\n", x, y);
else
    do_something_useful(x, y);


不幸的是,该缩进不能反映对流的实际控制,因为预处理器会生成与此等效的代码(缩进和大括号以强调实际含义):

if (x > y)
{
    if (DEBUG)
        fprintf(stderr, "x (%d) > y (%d)\n", x, y);
    else
        do_something_useful(x, y);
}


对该宏的下一次尝试可能是:

/* BAD - BAD - BAD */
#define debug_print(...) \
            if (DEBUG) { fprintf(stderr, __VA_ARGS__); }


现在,相同的代码片段会产生:

if (x > y)
    if (DEBUG)
    {
        fprintf(stderr, "x (%d) > y (%d)\n", x, y);
    }
; // Null statement from semi-colon after macro
else
    do_something_useful(x, y);


并且else现在是语法错误。 do { ... } while(0)循环避免了这两个问题。

还有另一种可能有用的宏编写方式:

/* BAD - BAD - BAD */
#define debug_print(...) \
            ((void)((DEBUG) ? fprintf(stderr, __VA_ARGS__) : 0))


这使程序片段显示为有效。 (void)强制转换禁止在需要值的上下文中使用它-但可以将其用作do { ... } while (0)版本不能使用的逗号运算符的左操作数。如果您认为应该能够将调试代码嵌入此类表达式中,则可能更喜欢这样做。如果您希望要求调试打印充当完整语句,则do { ... } while (0)版本更好。请注意,如果宏的主体包含任何分号(大致而言),则只能使用do { ... } while(0)表示法。它始终有效;表达式语句机制可能更难以应用。您可能还会从编译器收到带有您希望避免的表达式形式的警告;这将取决于编译器和您使用的标志。




TPOP先前位于http://plan9.bell-labs.com/cm/cs/tpophttp://cm.bell-labs.com/cm/cs/tpop,但现在(2015-08-10)都已损坏。




GitHub中的代码

如果您好奇,可以在GitHub中的SOQ中查看此代码(堆栈
溢出问题)存储库作为文件中的debug.cdebug.hmddebug.c
src/libsoq
子目录。

关于c - #define宏,用于在C中进行调试打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43743502/

相关文章:

c - C中main的返回值

c++ - 带线程的 C/C++ 数组——我需要使用互斥锁还是锁?

c++ - 运算符 '==' 没有左操作数

c-preprocessor - 是否有任何 C 预处理器作为独立程序?

c++ - 是否可以将 X-Macro 与 std::variant (或一般模板)一起使用?

c-preprocessor - C99 预处理器图灵完整吗?

c - 为什么这总是指向相似的值?

带有可变参数的 C printf 说明符。未定义的行为在哪一点上有问题?

c - 如何使用预处理器将枚举中的值记录到文件中

c - 作为迭代函数非递归运行 picoC