0

I would like to see a log file that will contain exactly the same data as the std::cout prints to console (without hiding output), so anything displayed in windows console/linux terminal will be copied to file output.log, without using OS-specific tools (like operator > in bash or tee).

The only one solution i see is to create class that will "cache" messages storing them temporarily in stringstream and then "flush" it to both (or more) streams, but this has disadvantage for my case and not suitable because i need my << calls to output cout exactly at the moment of their execution.

Is this possible? I don't see anything in standard library suitable for this task.

Croll
  • 3,213
  • 6
  • 24
  • 54
  • Have you already had a look at `std::streambuf` and `std::cout.rdbuf`? You can make your own implementation of `std::streambuf` and then redirect `std::cout` to that. – Cromon Mar 19 '16 at 21:26
  • You could create a subclass of `std::ostream` that overrides the `operator < – Drew McGowen Mar 19 '16 at 21:27
  • Method from http://stackoverflow.com/a/14155788/3367446 suits my needs. Didn't find it unfortunately then was searching solution before asking. – Croll Mar 19 '16 at 21:48
  • @P0W: note that the solution you pointed at is actually pretty bad. In particular, it doesn't allow replication of `std::cout`'s output from components which can't be changed. A much better approach is to use a replicating stream buffer as is shown by [this](http://stackoverflow.com/a/12443391/1120273) answer. – Dietmar Kühl Mar 20 '16 at 00:42

0 Answers0