I have a Qt console application. All communication with the user goes over two streams:
QTextStream in(stdin);
QTextStream out(stdout);
Now I want to log the whole session to a file, but I don't want to add a log output on every location where the streams are used.
Is there an easy way to "tee" the data of both streams to the console and to a file?
The app has to run on Windows.
I suggest a very simple solution, but c++14 is recommended. I would wrap QTextStream this way (output only as example):
As you can see, the wrapper holds two QTextStream instances and provides a stream insertion operator, which basically broadcasts data to each of them.
Not using c++14 (e.g. c++11) will result in complaints about use of ‘auto’ in parameter declaration. In this case, one had to declare/define every insertion (and extraction) operator overload (about fifteen / seventeen in QTextStream).
Refactoring would consist of replacing every QTextStream(stdout) with OutTeeTextStream().