4

I always thought the output window for a MSVC++ app running in the debugger was mapped to one of the standard output streams. But When I run this code in a Windows MFC app, nothing is seen:

std::cerr << "cerr"<<std::endl;
std::cout << "cout"<<std::endl;
std::clog << "clog"<<std::endl;

Is this a Windows thing or a VC++ thing? How are functions/macros like TRACE and OutputDebugString writing to this window, and shouldn't I be able to do so without using them?

Mr. Boy
  • 57,008
  • 88
  • 294
  • 540

4 Answers4

5

There is no standard style stream mapped to the Visual Studio output window. The function that accomplishes this is OutputDebugString().

The closest thing you can get to something like this is create your own wrapper class that behaves like an ostream, and underneath calls OutputDebugStream.

Ramon Zarazua B.
  • 6,865
  • 4
  • 19
  • 25
  • Does OutputDebugStream, as the name suggests, only function when _DEBUG is defined? If so, it's not much use for debugging release-mode-with-debug-info .exes... neither is TRACE for that matter. – Mr. Boy Nov 12 '09 at 10:23
  • OutputDebugString is a Windows API function and so available in both debug and release builds. Typically macros like TRACE are debug only, so you'll want to explicitly call OutputDebugString if you wish to continue writing the messages in a release build. – Stephen Nutt Nov 12 '09 at 12:49
  • You can further customize this by enabling or disabling this sort of calls yourself in a custom wrapper http://stackoverflow.com/questions/1389538/cancelling-stdcout-code-lines-using-preprocessor/1389813#1389813 – Ramon Zarazua B. Nov 13 '09 at 01:00
  • Depending on the project settings, the standard streams CAN be mapped to the output window, but this doesn't work for e.g. a DLL project that is run by a third-party host application. Leave it to Microsoft to ignore 16.7% of code in 'helloworld.cpp' as a requirement. –  Dec 06 '12 at 05:34
2

Here: Capturing cout in Visual Studio 2005 output window? Also here: http://www.codeproject.com/KB/debug/STLDebugLogger.aspx

Community
  • 1
  • 1
Captain Comic
  • 14,954
  • 43
  • 105
  • 144
0

You can use the RTP Macro:

std:string myString = "SOMETHING";
_RPT1(_CRT_WARN, "%s\n", myString.c_str());

or OutputDebugString

std:string myString = "SOMETHING";
OutputDebugString(savedStatementText.c_str());
Guillermo Hernandez
  • 1,014
  • 1
  • 7
  • 5
0

This is stream-friendly approach, which I've found on the net:

class debug_stream : public std::ostringstream
{
public:
    template<typename T>
    friend debug_stream& operator<<(debug_stream& os, T&& s);
};

template<typename T>
debug_stream& operator<<(debug_stream& os, T&& s)
{
    (ostringstream&amp;)os << s;
    PrintToDebug(os.str());
    os.str("");
    return os;
}

class dbgview_buffer : public std::stringbuf
{
public:
    ~dbgview_buffer()
    {
        sync(); // can be avoided
    }

    int sync()
    {
        OutputDebugString(str().c_str());
        str("");
        return 0;
    }
};

Use it this way:

dbgview_buffer buf;
ostream dbgview(&buf);
dbgview << "test" << endl;
Stepan Yakovenko
  • 7,404
  • 22
  • 105
  • 187