In my fairly large C/C++ console application, I wish to redirect the output of printf, fprintf(stderr,...), cout, and cerr to screen as well as a log file.
I could do something like this using "tee" or redirecting the output when launching the program by using redirection (How can I redirect and append both stdout and stderr to a file with Bash?).
However, I want to do it programmatically from inside my main routine without modifying existing code. I was hoping to add some code at the start of my main() function to appropriately set the streams so that all subsequent calls to printf, fprintf(stdout,...), fprintf(stderr,...), cout, cerr, etc. print their output on screen as usual, but additionally also log the output into a file.
There may be some issue with the sequence due to stdout being buffered and stderr being not buffered, but that is OK. It would be even better, if it can be done in a cross-platform manner.