vs下調試程序時,把信息打印到輸出窗口


轉載:https://blog.csdn.net/cwj066/article/details/82423627

     https://stackoverflow.com/questions/20508086/getting-rid-of-atltracegeneral-category-shown-in-atltrace-output

方法一:寫一個變參函數,把想要打印到輸出窗口的信息傳給函數,函數內部調用系統函數OutputDebugString(),就可以把調試信息打印到輸出窗口。

void OutputDebugPrintf(const char* strOutputString,...)
{
    char strBuffer[4096] = {0};

    va_list vlArgs;
    va_start(vlArgs, strOutputString);
    _vsnprintf_s(strBuffer, sizeof(strBuffer) - 1, strOutputString, vlArgs);
    
    va_end(vlArgs);
    OutputDebugString(CA2W(strBuffer));
}

方法二:調用系統自帶的庫函數TRACE(),這個函數其實和printf()類似,只不過printf()這個是把信息輸出到控制台窗口,而TRACE()這個函數是把信息輸出到vs2010的輸出窗口,方便編程人員調試用。

TRACE(“%s”, "no error, no warning");

TRACE(“%d”, 1024);

TRACE(“%d”, 520.1314);

TRACE(“%c”, 'U');

頭文件:

#include<afx.h>

 

未驗證方法1

其它:不想要這個信息

采用下面的方法,顯示的信息變成:

問題:

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

回答

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:

1 #ifdef _DEBUG
2 #ifdef ATLTRACE 
3 #undef ATLTRACE
4 #undef ATLTRACE2
5 
6 #define ATLTRACE CustomTrace
7 #define ATLTRACE2 ATLTRACE
8 #endif // ATLTRACE
9 #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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM