如何创建带有可变参数列表的仅调试函数?就像 printf()
-
08-06-2019 - |
题
我想制作一个具有相同参数的调试日志记录功能 printf
. 。但在优化构建期间可以被预处理器删除。
例如:
Debug_Print("Warning: value %d > 3!\n", value);
我研究过可变参数宏,但它们并非在所有平台上都可用。 gcc
支持他们, msvc
才不是。
解决方案
我仍然采用旧方法,定义一个宏(XTRACE,如下),该宏与无操作或带有变量参数列表的函数调用相关。在内部,调用 vsnprintf 这样你就可以保留 printf 语法:
#include <stdio.h>
void XTrace0(LPCTSTR lpszText)
{
::OutputDebugString(lpszText);
}
void XTrace(LPCTSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
int nBuf;
TCHAR szBuffer[512]; // get rid of this hard-coded buffer
nBuf = _vsnprintf(szBuffer, 511, lpszFormat, args);
::OutputDebugString(szBuffer);
va_end(args);
}
然后是一个典型的 #ifdef 开关:
#ifdef _DEBUG
#define XTRACE XTrace
#else
#define XTRACE
#endif
嗯,这可以清理很多,但这是基本的想法。
其他提示
这就是我在 C++ 中调试打印输出的方法。像这样定义“dout”(调试输出):
#ifdef DEBUG
#define dout cout
#else
#define dout 0 && cout
#endif
在代码中,我使用“dout”,就像“cout”一样。
dout << "in foobar with x= " << x << " and y= " << y << '\n';
如果预处理器将“dout”替换为“0 && cout”,请注意 << 的优先级高于 &&,并且 && 的短路计算会使整行计算为 0。由于未使用 0,编译器根本不会为该行生成任何代码。
这是我用 C/C++ 做的事情。首先,您编写一个使用可变参数的函数(请参阅 Stu 的帖子中的链接)。然后做这样的事情:
int debug_printf( const char *fmt, ... );
#if defined( DEBUG )
#define DEBUG_PRINTF(x) debug_printf x
#else
#define DEBUG_PRINTF(x)
#endif
DEBUG_PRINTF(( "Format string that takes %s %s\n", "any number", "of args" ));
您需要记住的是在调用调试函数时使用双括号,并且整行将在非调试代码中被删除。
消除可变参数函数的另一种有趣方法是:
#define function sizeof
@编码轮:
您的方法有一个小问题。考虑这样的调用
XTRACE("x=%d", x);
这在调试版本中工作正常,但在发布版本中它将扩展为:
("x=%d", x);
这是完全合法的 C 语言,可以编译并运行,通常不会产生副作用,但会生成不必要的代码。我通常用来消除该问题的方法是:
让XTrace函数返回一个int(只返回0,返回值无所谓)
将#else 子句中的#define 更改为:
0 && XTrace
现在发布版本将扩展为:
0 && XTrace("x=%d", x);
任何像样的优化器都会抛弃整个事情,因为短路评估会阻止 && 之后的任何事情被执行。
当然,正如我写下最后一句话一样,我意识到也许原始形式也可能被优化掉,并且在出现副作用的情况下,例如作为参数传递给 XTrace 的函数调用,它可能是一个更好的解决方案,因为它会确保调试版本和发布版本的行为相同。
在 C++ 中,您可以使用流运算符来简化事情:
#if defined _DEBUG
class Trace
{
public:
static Trace &GetTrace () { static Trace trace; return trace; }
Trace &operator << (int value) { /* output int */ return *this; }
Trace &operator << (short value) { /* output short */ return *this; }
Trace &operator << (Trace &(*function)(Trace &trace)) { return function (*this); }
static Trace &Endl (Trace &trace) { /* write newline and flush output */ return trace; }
// and so on
};
#define TRACE(message) Trace::GetTrace () << message << Trace::Endl
#else
#define TRACE(message)
#endif
并像这样使用它:
void Function (int param1, short param2)
{
TRACE ("param1 = " << param1 << ", param2 = " << param2);
}
然后,您可以以与输出到的方式大致相同的方式为类实现自定义跟踪输出 std::cout
.
啊,vsprintf() 是我缺少的东西。我可以使用它将变量参数列表直接传递给 printf():
#include <stdarg.h>
#include <stdio.h>
void DBG_PrintImpl(char * format, ...)
{
char buffer[256];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
printf("%s", buffer);
va_end(args);
}
然后将整个事情包装在宏中。
它们在哪些平台上不可用?stdarg 是标准库的一部分:
http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html
任何不提供它的平台都不是标准的 C 实现(或者非常非常古老)。对于这些,你必须使用可变参数:
http://opengroup.org/onlinepubs/007908775/xsh/varargs.h.html
这种功能的部分问题是,通常需要变异宏。这些最近是标准化的(C99),许多旧的C编译器不支持标准,或者有自己的特殊工作。
下面是我编写的一个调试头,它有几个很酷的功能:
- 支持调试宏的 C99 和 C89 语法
- 根据函数参数启用/禁用输出
- 输出到文件描述符(文件io)
笔记:由于某种原因,我遇到了一些轻微的代码格式问题。
#ifndef _DEBUG_H_
#define _DEBUG_H_
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include "stdarg.h"
#include "stdio.h"
#define ENABLE 1
#define DISABLE 0
extern FILE* debug_fd;
int debug_file_init(char *file);
int debug_file_close(void);
#if HAVE_C99
#define PRINT(x, format, ...) \
if ( x ) { \
if ( debug_fd != NULL ) { \
fprintf(debug_fd, format, ##__VA_ARGS__); \
} \
else { \
fprintf(stdout, format, ##__VA_ARGS__); \
} \
}
#else
void PRINT(int enable, char *fmt, ...);
#endif
#if _DEBUG
#if HAVE_C99
#define DEBUG(x, format, ...) \
if ( x ) { \
if ( debug_fd != NULL ) { \
fprintf(debug_fd, "%s : %d " format, __FILE__, __LINE__, ##__VA_ARGS__); \
} \
else { \
fprintf(stderr, "%s : %d " format, __FILE__, __LINE__, ##__VA_ARGS__); \
} \
}
#define DEBUGPRINT(x, format, ...) \
if ( x ) { \
if ( debug_fd != NULL ) { \
fprintf(debug_fd, format, ##__VA_ARGS__); \
} \
else { \
fprintf(stderr, format, ##__VA_ARGS__); \
} \
}
#else /* HAVE_C99 */
void DEBUG(int enable, char *fmt, ...);
void DEBUGPRINT(int enable, char *fmt, ...);
#endif /* HAVE_C99 */
#else /* _DEBUG */
#define DEBUG(x, format, ...)
#define DEBUGPRINT(x, format, ...)
#endif /* _DEBUG */
#endif /* _DEBUG_H_ */
今天遇到这个问题,我的解决方案是以下宏:
static TCHAR __DEBUG_BUF[1024]
#define DLog(fmt, ...) swprintf(__DEBUG_BUF, fmt, ##__VA_ARGS__); OutputDebugString(__DEBUG_BUF)
然后您可以像这样调用该函数:
int value = 42;
DLog(L"The answer is: %d\n", value);
这就是我使用的:
inline void DPRINTF(int level, char *format, ...)
{
# ifdef _DEBUG_LOG
va_list args;
va_start(args, format);
if(debugPrint & level) {
vfprintf(stdout, format, args);
}
va_end(args);
# endif /* _DEBUG_LOG */
}
当 _DEBUG_LOG 标志关闭时,这在运行时绝对没有任何成本。
这是用户答案的 TCHAR 版本,因此它将作为 ASCII 工作(普通的),或 Unicode 模式(或多或少)。
#define DEBUG_OUT( fmt, ...) DEBUG_OUT_TCHAR( \
TEXT(##fmt), ##__VA_ARGS__ )
#define DEBUG_OUT_TCHAR( fmt, ...) \
Trace( TEXT("[DEBUG]") #fmt, \
##__VA_ARGS__ )
void Trace(LPCTSTR format, ...)
{
LPTSTR OutputBuf;
OutputBuf = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, \
(size_t)(4096 * sizeof(TCHAR)));
va_list args;
va_start(args, format);
int nBuf;
_vstprintf_s(OutputBuf, 4095, format, args);
::OutputDebugString(OutputBuf);
va_end(args);
LocalFree(OutputBuf); // tyvm @sam shaw
}
我说“或多或少”,因为它不会自动将 ASCII 字符串参数转换为 WCHAR,但它应该可以帮助您摆脱大多数 Unicode 错误,而不必担心将格式字符串包装在 TEXT() 中或在其前面加上 L 。
很大程度上源自 微软软件定义网络:检索最后一个错误代码