正在清除ATLTCRACE输出中显示的atlTraceGeneral类别

Getting rid of atlTraceGeneral category shown in ATLTRACE output

本文关键字:显示 atlTraceGeneral 类别 输出 清除 ATLTCRACE      更新时间:2023-10-16

升级到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应用程序中,也只需要宽字符钩子。