KlingonJoe
KlingonJoe

Reputation: 835

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.

Upvotes: 5

Views: 2477

Answers (1)

KlingonJoe
KlingonJoe

Reputation: 835

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);

Upvotes: 4

Related Questions