Good day.
I have to use some external functions that produce a lot of debugging information to stdout (via std::cout
). I want to duplicate this information to some log file by redirecting cout
to boost tee_device
. I use the following example code:
typedef boost::iostreams::tee_device<ostream, ofstream> TeeDevice;
typedef boost::iostreams::stream<TeeDevice> TeeStream;
int main(int argc, char** argv) {
remove("file.log");
ofstream logFile;
logFile.open("file.log");
TeeDevice outputDevice(cout, logFile);
TeeStream logger(outputDevice);
cout.rdbuf(logger.rdbuf());
cout << "some log info";//this should print both to stdout and to file
logger.close();
}
However I have a segmentation fault when trying to run this. Why?
I know that I can do like
logger << "some debug log info";
but I need exactly to redirect cout
. How I can obtain this?
Thanks, Stanislav