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
"), 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);
}
原文地址:https://www.cnblogs.com/Toya/p/14381630.html