To put it simply, I'm trying to create one stream that links to a file std::ofstream outFile{pathToFile, std::ios_base::app};
. This file is a log file that, ideally, would receive copies of both stderr and stdout. When an error would occur, for example, the error would be printed in the console and in the file.
I've tried using freopen(pathToFile, "a+", stderr);
as well as std::cerr.rdbuf(outFile.rdbuf());
however, both of these completely redirect the output, similar to a pipe in bash. I also want to see the error in the console.
Being somewhat new to C++ streams, I'm not quite sure how I would achieve this. The way I would see it being done in another language would be "subscribing" to stderr, so my original stream would be notified everytime stderr changes and update itself. Another way would be to override stderr, log to my file and call the original one, but since stderr is usually not called by the programmer itself, I'm not sure how I could do that.
It should be noted that I'm using windows, so I don't have access to unistd.h
. I would also like to avoid using STL
or third party libraries.