I'm working on a feature which requires writing a single log file(identified by its path) in multiple processes. Previously, each process used to call printf
to stream the log on terminal(standard output). Now I need to change the output destination to a file. So I tried using freopen
to redirect the stdout to the file in each process.
freopen(file_path, "a", stdout); //
But it seems it doesn't work well. Some log is missing. What's the common practice to achieve this ?
B.T.W In our requirement, the user should be allowed to switch logging destination between file and standard output, so the first argument "file_path" could be tty when switched back to terminal. Is that OK to call freopen(tty, "a", stdout)
?