实现TRACE宏功能(内联函数形式和宏形式),无MFC时打印到Output窗口

内联函数形式:

inline void trace(const char* format,...)
{
va_list ap;
va_start(ap,format);
int len=_vscprintf(format,ap)+1;//+1计入'/0'
char *pBuf=(char*)malloc(sizeof(char)*len);
vsprintf_s(pBuf,len,format,ap);
va_end(ap);
OutputDebugStringA(pBuf);
free(pBuf);
}

宏形式:

内联函数的形式存在缺陷,不能用来统计所在的函数和行号等

更好的方法应该使用宏的方式去实现

#define DBG_BUF_SIZE 1024
CHAR _buf_[DBG_BUF_SIZE];
#define TRACE(...) do{/
sprintf_s(_buf_,DBG_BUF_SIZE,__VA_ARGS__);
/
OutputDebugStringA(_buf_);
/
}
while(0)

VC6 不支持可变参数的宏,可以参考Warkaround

VC6 实现TRACE 文件、行号,__VA_ARGS__ Walkaround

http://blog.csdn.net/iamoyjj/archive/2011/02/15/6186935.aspx

原文地址:https://www.cnblogs.com/oyjj/p/2132902.html