I suggest a very simple solution, but c++14 is recommended.
I would wrap QTextStream this way (output only as example):
#include <QTextStream>
#include <QFile>
class OutTeeTextStream
{
public:
OutTeeTextStream()
{
logfilestream = 0;
stdoutstream = new QTextStream(stdout);
file.setFileName("/tmp/outlog.log");
if(file.open(QFile::Append))
{
logfilestream = new QTextStream(&file);
}
}
~OutTeeTextStream()
{
file.close();
delete stdoutstream;
delete logfilestream;
}
OutTeeTextStream &operator<<(auto data)
{
(*stdoutstream) << data;
if(logfilestream != 0)
{
(*logfilestream) << data;
}
return (*this);
}
private:
QTextStream * stdoutstream;
QTextStream * logfilestream;
QFile file;
};
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().