I'd like to compose two (or more) streams into one. My goal is that any output directed to cout
, cerr
, and clog
also be outputted into a file, along with the original stream. (For when things are logged to the console, for example. After closing, I'd like to still be able to go back and view the output.)
I was thinking of doing something like this:
class stream_compose : public streambuf, private boost::noncopyable
{
public:
// take two streams, save them in stream_holder,
// this set their buffers to `this`.
stream_compose;
// implement the streambuf interface, routing to both
// ...
private:
// saves the streambuf of an ios class,
// upon destruction restores it, provides
// accessor to saved stream
class stream_holder;
stream_holder mStreamA;
stream_holder mStreamB;
};
Which seems straight-forward enough. The call in main then would be something like:
// anything that goes to cout goes to both cout and the file
stream_compose coutToFile(std::cout, theFile);
// and so on
I also looked at boost::iostreams
, but didn't see anything related.
Are there any other better/simpler ways to accomplish this?
You mention having not found anything in Boost.IOStreams. Did you consider tee_device?
You do have the right design—if you want to do this purely within the stdlib.
One thing: instead of teeing to each streambuf on every output, implement it to use the same put area as one of the streambufs it's given, and copy to the others on overflow and sync. This will minimize virtual calls, which is one of the goals of how streambufs work.
Alternatively, and if you want to only handle stdout & stderr (which is common), run your program through the standard Unix
tee
program (or the equivalent on your platform), either by doing it yourself when invoking the program, or within the program by forking, setting up the streams as appropriate, etc.Edit: You got me thinking, and I should know how to get this right. Here's my first approximation. (When this breaks, you get to keep both pieces.)
Now, this test is far from complete, but it seems to work:
Put it together with a file:
I would write a custom stream buffer that just forwards data to the buffers of all your linked streams.