使用TRACE调试很方便,但是在Unicode环境下确老是问题多多,经常会出现令人郁闷的识别问题。
_CrtDbgReport: String too long or IO Error
虽然可以通过setlocale函数来设置语言页,但是仅支持一种语言,如果需要同时显示多种语言就很令人无语。,此外TRACE有最大输出长度的限制(1K)
class XCLILocale
{
public:
XCLILocale(void)
{ m_strLocale = _tsetlocale(LC_ALL,_T("")); }
virtual ~XCLILocale(void)
{ _tsetlocale(LC_ALL,m_strLocale); }
public:
LPCTSTR m_strLocale;
};
void main(void)
{
XCLILocale locale;
TRACE(_T("测试Test\n"));
}
Windows提供了一个API函数OutputDebugString用来向output窗口输出信息,该函数有OutputDebugStringW (Unicode) and OutputDebugStringA (ANSI)两个版本。
字符串长度无限制(至少我输出了一个约25K的XML文件就没啥问题,用TRACE最多显示一部分)。
不过使用OutputDebugString函数不支持printf 风格的格式化。需要预先格式化字符串。不过可以用CString类的FormatV函数封装一下,封装代码如下:
void __cdecl XTrace(LPCTSTR strFormat, ...)
{
va_list argList;
va_start(argList, strFormat);
CString str;
str.FormatV(strFormat,argList);
va_end(argList);
OutputDebugString(str);
}
至此,使用XTrace函数终于可以替代TRACE宏,在output窗口中自由输出测试信息了