Capturing stdout

Started by
7 comments, last by the_edd 12 years, 3 months ago
Is there a way to capture data sent to stdout, or for a general stream for that matter? By this I mean that when a message is sent to stdout, the program would be notified by this and it could then post the message to e.g. a graphical console window.
Advertisement
In which language? On what platform?
Oh sorry, I was a bit hasty. With C++ and I'm hoping that it would be possible platform independently.
That's basically a no. Specific platforms may or may not give you some options.
Presumably you mean the stdout of another process that your program is launching?

If so, the most portable way is to use something like this (which isn't an official boost library yet), or this (whose Windows implementation is still a bit sketchy, to say the least).

Presumably you mean the stdout of another process that your program is launching?


No. My game uses stdout to report load and game time messages. I would like that during the game, these messages could also be seen in a graphical console. So for that I would need to be able to capture the messages somehow and then draw them.

So lets say that in the game a player joins the server and I have something like


cout << "Player joined the game.\n";


I would like that this message could be drawn by a function, say, drawMessageToGraphicalConsole(string msg) which I have implemented, without changing every "cout" in the code.
If you only want to redirect std::cout, and not other uses of stdout, then you can use a custom streambuf object. edd²'s website has an example of how you can do that here.
I'll have a look at the streambuffers. Thanks for the advice, although I had hoped this would be more straight-forward :).
Take the caps_buffer example. You'll barely need need half of that to do what you want.

The boost iostreams library might be of help, also.

This topic is closed to new replies.

Advertisement