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

VC6 不支持C99 没有可变长的宏参数 __VA_ARGS__

可以使用下面的Walkaround来解决

采用的方法比较巧妙,使用匿名对象和重载()操作符来达到和TRACE函数相同的效果,同时还能正确打印文件和行号。

#include <stdio.h>
#include
<windows.h>

#define _DEBUG_BUF_SIZE_ 2048
static char _buf_[_DEBUG_BUF_SIZE_];

class trace_impl
{
public:
trace_impl(
const char* file,size_t line):file_(file),line_(line)
{
}
inline
void operator() (const char* format,...) const
{
va_list ap;
va_start(ap,format);
vsprintf(_buf_,format,ap);
va_end(ap);
OutputDebugStringA(_buf_);
sprintf(_buf_,
" in %s(%d)/n",file_,line_);
OutputDebugStringA(_buf_);
}
private:
const char* file_;
size_t line_;
};

#define TRACE trace_impl(__FILE__,__LINE__)

  

代码的实现参考台湾的一篇博客,

更详细的信息请参考:http://www.jeffhung.net/blog/articles/jeffhung/1012/

posted @ 2011-02-15 22:30  OYJJ  阅读(1080)  评论(0编辑  收藏  举报