Search code examples
c++visual-studiounit-testingmstest

C++ / Visual Studio - How to output to Test Output Window?


I'm aware of the logger in Visual Studio, i.e.:

Logger::WriteMessage("foo");

This works great for the test code, less practical for the code inside the application. So, I'm looking for a standard solution to redirect the stdout messages inside the code.


Solution

  • The stream in std-out can be reassigned, for example:

    // REDIRECT STD STREAM
    streambuf * backup;
    backup = cout.rdbuf();
    stringstream ss;
    cout.rdbuf(ss.rdbuf());    
    
    // DO SOMETHING
    cout << "foo\n";
    
    // PRINT STREAM TO LOGGER
    Logger::WriteMessage(ss.str().c_str());
    
    // ASSIGN COUT BACK TO STDOUT
    cout.rdbuf(backup);