Helper class to redirect std::ostream to a functor.
This object takes an std::ostream
and redirects its input to a stream buffer owned by this class. When the stream syncs, it forwards the resulting strings views to a functor. It has the effect to redirect the output stream to the functor.
This std::ostream
does not directly output data, but forwards any input to its associated functor. It can operate in two different modes:
- In line-buffered mode, the buffer will only output complete lines that have been terminated by a newline character. In particular, this mode will ignore any explicit flushing of the C++ stream. This mode is capable of exactly reproducing the original output layout as designed by the user of the
std::ostream
, but messages may appear later than expected when the user explicitly flushes the C++ stream.
- In unbuffered mode, the buffer will always forward all pending data every time the C++ stream is flushed. As most logging sinks are line-oriented and insert an additional newline after each log message, this will not correctly reproduce the original layout of the output. As a lot of people use
std::endl
instead of just "\n"
for ending their lines, this mode will not forward empty lines to the logging system to avoid empty lines after every regular line printed to the C++ stream.
- Author
- Steffen Müthing
- Copyright
- BSD-2-Clause