Jump to content
  • Advertisement
Sign in to follow this  
DrjonesDW3d

capture stdout from within a program?

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am curious if anyone knows of a way to capture everything that is being sent to stdout in a c++ program into another iostream. Mainly, so a mirrior of the output can be sent to a file. I know it's possible using a shell to save the output, but sometimes that get's confusing since programs with input won't see the output. So anyways, is there a way to attach one ostream to another or something, so that all data gets sent to one gets sent to the other (without explicitly having to tell it to send it to them both)?

Share this post


Link to post
Share on other sites
Advertisement
With standard C++, you can redirect a stream by reassigning the stream buffer.
If you use Boost's new iostreams library, it is possible to use a tee device, which will send anything it receives to a pair of sinks (e.g. standard output stream and a file stream).

Share this post


Link to post
Share on other sites
If your standard output includes non-iostream output (ex: from a library function that outputs with printf()) then one approach is to create a pipe that outputs to a child process that just a while loop that reads on standard input and outputs on standard output and to a file. Or if you're application is multi-threaded by creating a pipe that redirects standard output, then having a thread read on the pipe and outputs to standard output an a file like the child process.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!