2013-12-11 16 views
6

升級到VS2013後,我開始以「():atlTraceGeneral - My output」格式接收所有ATLTRACE2消息。擺脫ATLTRACE輸出中顯示的atlTraceGeneral類別

例如

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

...顯示爲

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

我不需要任何額外的信息。這裏是一些方式來找回以前的格式,使輸出將只是

This is my data: 124 
+0

假設您可以使用ATL/MFC跟蹤工具來禁用「類別和名稱」跟蹤。如果這能夠以交互方式進行,您可以在代碼中以編程方式執行相同的操作 - 更新應用程序的初始狀態。 –

+0

我在當前示例中找不到ATL/MFC跟蹤工具。 –

+0

VS 2012在'C:\ Program Files文件(x86)\ Microsoft Visual Studio 11.0 \ Common7 \ Tools \ AtlTraceTool8.exe'中有推測,VS 2013也有它。它也可以從IDE菜單Tools中啓動。 –

回答

7

唯一的工作解決方法是在_DEBUG宏民主基金ATLTRACE,並通過自己實現跟蹤。微軟的人推薦the same

該解決方案是這樣的:

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

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

具有以下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); 
} 
+0

我相信倒數第二行應該是'CustomTrace(...'而不是'Hooktrace(...' – etienne

0

我走了不同的路線 - 我選擇了編輯這樣的(該消息只得到更短的輸出,所以不需要分配):

#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 

而在CWinApp派生的構造函數中:

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

和CWinApp派生的析構函數:

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

出於某種原因,這兩個鉤的MCBS和寬字符版本被稱爲具有相同的消息,因此只有寬字符鉤是即使在MBCS應用程序中也是必需的。