正在清除ATLTCRACE输出中显示的atlTraceGeneral类别
Getting rid of atlTraceGeneral category shown in ATLTRACE output
升级到VS2013后,我开始以"():atlTraceGeneral-my output"格式接收所有ATLTCRACE2消息。
例如
ATLTRACE(_T("This is my data: %dn"), 124);
显示为
dllmain.cpp(1121) : atlTraceGeneral - This is my data: 124
我不需要任何其他信息。这里有没有办法回到以前的格式,这样输出就只是
This is my data: 124
唯一有效的解决方案是在_DEBUG宏下取消ATLTRACE的定义并自己实现跟踪。微软的同事们也推荐了同样的方法。
解决方案如下:
#ifdef _DEBUG
#ifdef ATLTRACE
#undef ATLTRACE
#undef ATLTRACE2
#define ATLTRACE CustomTrace
#define ATLTRACE2 ATLTRACE
#endif // ATLTRACE
#endif // _DEBUG
具有以下自定义跟踪:
void CustomTrace(const wchar_t* format, ...)
{
const int TraceBufferSize = 1024;
wchar_t buffer[TraceBufferSize];
va_list argptr; va_start(argptr, format);
vswprintf_s(buffer, format, argptr);
va_end(argptr);
::OutputDebugString(buffer);
}
void CustomTrace(int dwCategory, int line, const wchar_t* format, ...)
{
va_list argptr; va_start(argptr, format);
CustomTrace(format, argptr);
va_end(argptr);
}
我走了另一条路——我选择这样编辑输出(消息只会变短,所以不需要分配):
#ifdef _DEBUG
static int __cdecl crtReportHookW(int nReportType, wchar_t* wszMsg, int* pnRet)
{
const wchar_t wszTrace[] = L"atlTraceGeneral - ";
const int ccTrace = _countof(wszTrace) - 1; // exclude L' '
if (nReportType == _CRT_WARN)
{
wchar_t* pwsz = wcsstr(wszMsg, wszTrace);
if (pwsz != nullptr)
{
int ccBuf = wcslen(pwsz) + 1; // remaining buffer size (include L' ')
wmemmove_s(pwsz, ccBuf, &pwsz[ccTrace], ccBuf - ccTrace);
}
}
return FALSE; // always keep processing
}
#endif
在CWinApp派生的构造函数中:
#ifdef _DEBUG
_CrtSetReportHookW2(_CRT_RPTHOOK_INSTALL, crtReportHookW);
#endif
和CWinApp派生的析构函数:
#ifdef _DEBUG
_CrtSetReportHookW2(_CRT_RPTHOOK_REMOVE, crtReportHookW);
#endif
出于某种原因,MCBS和宽字符版本的钩子都是用同一条消息调用的,因此即使在MBCS应用程序中,也只需要宽字符钩子。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1