In a program which involves several output streams, the developer often wants to have the same content sent to two distinct streams. This usually happens when log messages, which are typically sent to a file, need also appear on the user's console, when the program's "verbosity" flag is on.
In short, we would like to have a "tee" stream which forwards its incoming content to two different output streams. For example, the following piece of code, should print the line "Zapp Branigan" to the standard output (cout) as well as to a file ("futurama.txt"):

ofstream f("futurama.txt");
TeeStream ts(cout, f);
ts << "Zapp Branigan" << endl; // "Zapp Branigan" is sent to TWO streams
It turns out that ahieving this end, using C++'s stream library, is not trivial mainly because the basic_ostream class template offers no virtual functions which can be overriden. Alternatively, class basic_streambuf (used by basic_ostream) can be customized thru subclassing/overriding, but this is an inadequate approach: The data that is passed to a basic_streambuf object contains only the raw text, exlcuding any formatting manipulators (such as: std::endl, std::width, etc.) thus discarding an important service of the stream library.

The solution: class basic_TeeStream

#include <iostream>
#include <fstream>

template<typename Elem, typename Traits = std::char_traits<Elem> >
struct basic_TeeStream : std::basic_ostream<Elem,Traits>
   typedef std::basic_ostream<Elem,Traits> SuperType;
   basic_TeeStream(std::ostream& o1, std::ostream& o2) 
      :  SuperType(o1.rdbuf()), o1_(o1), o2_(o2) { }

   basic_TeeStream& operator<<(SuperType& (__cdecl *manip)(SuperType& ))
      o1_ << manip;
      o2_ << manip;
      return *this;
   template<typename T>
   basic_TeeStream& operator<<(const T& t)
      o1_ << t;
      o2_ << t;
      return *this;
   std::ostream& o1_;
   std::ostream& o2_;