Getting rid of atlTraceGeneral category shown in ATLTRACE output
Asked Answered
A

2

12

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
Aegisthus answered 11/12, 2013 at 0:16 Comment(6)
Supposedly, you can use ATL/MFC Trace Tool to disable "Category and Fucntion Name" tracing. If this works out interactively, you can do the same programmatically in your code as well - to update initial state on your app.Abacus
I couldn't find ATL/MFC Trace Tool in current samples.Aegisthus
VS 2012 has it in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\AtlTraceTool8.exe Presumably, VS 2013 has it there as well. It can also be started from IDE, from menu Tools.Abacus
Thanks for the pointing out the tool. I had no idea that now it's included in VS tools. However, it manages ATL just for VS2012, not VS2013 and I need to get fixed in VS2013Aegisthus
Well, I don't have VS2013 to check - this is why I refered to VS2012 tool. It was there in VS 2010, VS 2008 and before. I assumed the same thing is in 2013 as well. The tool changes tracing interactively, and you of course can do the same programmatically as well using AtlTraceXxx functions.Abacus
That's the core issue, the tool was removed in VS2013. And along with it, any option to suppress the extraneous chatter.Burack
A
14

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);
}
Aegisthus answered 5/1, 2014 at 7:16 Comment(2)
I believe the second to last line should read CustomTrace(... instead of Hooktrace(...Phelps
@Anton, thanks it helped. I though there was some magic code w/ ATLTRACE but actually not.Eutherian
C
3

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.

Chestonchest answered 22/8, 2016 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.