Implement a buffer
#include <streambuf>
#include <vector>
template<class charT,
class traits = std::char_traits<charT> >
class logoutbuf :
public std::basic_streambuf<charT, traits>
{
private:
typedef std::vector<charT> buffer_type;
buffer_type buffer_;
...
};
Josuttis has an example of a buffered stream buffer. However, his example uses a fixed array for a buffer that gets flushed when it is full. The logging stream should only flush the buffer when instructed to do so, with a std::endl or a call to flush. To accomplish this, the fixed array can be replaced with a std::vector.