Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect stdout and stderr to the output debug console of microsoft visual studio

I am using microsoft visual studio to do C++. I don't see std::err and std::out in the output console of the IDE. Is there a way to redirect them ?

like image 901
0x26res Avatar asked Apr 21 '10 13:04

0x26res


People also ask

How do I redirect console output to a file in Visual Studio?

You can use > to redirect the output of the command run by Visual Studio. Add it to the command arguments by selecting your project in the solution explorer and clicking PROJECT->Properties->Configuration Properties->Debugging. Then enter > output. txt into the Command Arguments.

How do I redirect stderr and stdout to a file?

The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator. We have created a file named “sample. txt” to store the redirected output in our current directory.

How do I show debug output in Visual Studio?

To see the debug output window, in Microsoft Visual Studio, click View, click Other Windows, and then click Output. You can view the debug output in this window only if the debugger is attached to the process that is writing to the output window.

How would you redirect a commands stderr to the stdout?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


2 Answers

You can indeed redirect std::out and std::err. Simply right click on your project in the solution explorer and select Properties. Then select Configuration Properties -> Debugging and put the appropriate arguments into the Command Arguments field. For example, to redirect std::err to a file, I would type in 2> ErrorLog.txt.

The things you type in Command Arguments simply get appended as command line arguments when Visual Studio runs your program, just like you had manually typed them in to the console. So, the above example simply tells VisualStudio to run your program with the command <programName>.exe 2> ErrorLog.txt instead of just <programName>.exe.

like image 59
Brandon Avatar answered Oct 18 '22 14:10

Brandon


I know this is an old thread but I can't help but giving the answer since I can't believe there still is no real answer. What you can do is redirect the cout to a ostringstream of your choice. To do this, derive a new class from streambuf that will send the stream to OutputDebugString (Lets call this class OutputDebugStream) and create an instance of the class, myStream. Now call:

cout.rdbuf(&myStream)

I used cout for an example. This same technique can be used with cerr, just call

cerr.rdbuf(&myStream).  

Stdout is a little more difficult if you are not using cout. You can redirect stdout during runtime by using freopen(), but it must be to a file. To get this to redirect to the Debug screen is a little more difficult. One way is to use fmemopen() if it is available (it is not standard) and write a streambuf to output this data to the Debug screen. Alternatively, you can redirect to a file and write a stream to open as input and redirect to the debug stream. A bit more work, but I think it is possible.

like image 34
Dan Avatar answered Oct 18 '22 12:10

Dan