title: How can I compose output streams, so output goes multiple places at once?
body: <p>I'd like to compose two (or more) streams into one. My goal is that any output directed to <code>cout</code>, <code>cerr</code>, and <code>clog</code> 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.)</p>
  
  <p>I was thinking of doing something like this:</p>
  
  <pre><code>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;
  };
  </code></pre>
  
  <p>Which seems straight-forward enough. The call in main then would be something like:</p>
  
  <pre><code>// anything that goes to cout goes to both cout and the file
  stream_compose coutToFile(std::cout, theFile);
  // and so on
  </code></pre>
  
  <p>I also looked at <code>boost::iostreams</code>, but didn't see anything related.</p>
  
  <p>Are there any other better/simpler ways to accomplish this?</p>