Writing a program run in a while loop maybe for some hours, so I'm trying to redirect my cout and cerr to save log files under directory named by the date when loop started.
Problem is I have some log that should be saved before while loop, so it logs before I redirect stdout and stderr.
prepareLoop();
std::cout("Some log"); // Log before redirection
while (true)
{
std::string date = getTodaysDate();
// redirect stdout and stderr to log file with date on filename.
std::ofstream logByDate { date + ".log", std::ios::app };
std::ofstream ErrByDate { date + ".err", std::ios::app };
std::cout.rdbuf(logByDate.rdbuf());
std::cerr.rdbuf(ErrByDate.rdbuf());
otherJobsToDo();
}
I thought of shell script redirecting stdout and stderr to file, like this
./myProgram.out 2>> ${date}.err 1>> ${date}.log
but it seems not working. Same output with when I execute it with no redirection on script.
Can I write those log before loop?
Summary: A program is running through days. I have to change log file when day has changed. So, I will keep redirecting stdout and stderr in loop. But there are some log outside the loop that have to be written on log. And redirecting when executing doesn't write them on log.