Pregunta

After upgrading to VS2013 I started receiving all my ATLTRACE2 messages in a "() : atlTraceGeneral - My output" format.

e.g.

ATLTRACE(_T("This is my data: %d\n"), 124);

... shown as

dllmain.cpp(1121) : atlTraceGeneral - This is my data: 124

I don't need any additional info. Is here some way to get back to the previous format so that the output would be just

This is my data: 124
¿Fue útil?

Solución

The only working fix is to undef ATLTRACE under _DEBUG macro and implement trace by yourself. Guys at Microsoft recommended the same.

The solution looks like this:

#ifdef _DEBUG
#ifdef ATLTRACE 
#undef ATLTRACE
#undef ATLTRACE2

#define ATLTRACE CustomTrace
#define ATLTRACE2 ATLTRACE
#endif // ATLTRACE
#endif // _DEBUG

with the following CustomTraces:

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);
}

Otros consejos

I went a different route -- I chose to edit the output like this (the message only gets shorter, so no allocation required):

#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'\0'
    if (nReportType == _CRT_WARN)
    {
        wchar_t* pwsz = wcsstr(wszMsg, wszTrace);
        if (pwsz != nullptr)
        {
            int ccBuf = wcslen(pwsz) + 1;       // remaining buffer size (include L'\0')
            wmemmove_s(pwsz, ccBuf, &pwsz[ccTrace], ccBuf - ccTrace);
        }
    }
    return FALSE;       // always keep processing
}
#endif

And in the CWinApp-derived constructor:

#ifdef _DEBUG
    _CrtSetReportHookW2(_CRT_RPTHOOK_INSTALL, crtReportHookW);
#endif

and CWinApp-derived destructor:

#ifdef _DEBUG
    _CrtSetReportHookW2(_CRT_RPTHOOK_REMOVE, crtReportHookW);
#endif

For some reason, both the MCBS and wide-character versions of the hook are called with the same message, so only the wide-character hook is necessary even in an MBCS app.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top