Redirect program output to log files
When we perform batch tests on developed algorithms, it is helpful to save the messages and data printed out by the program to a log file for backup and further analysis, while still keeping them displayed in the console for real time monitoring. This can be achieved via output redirection on Linux.
We know that the standard file descriptors on Linux are:
- Standard input: 0
- Standard output: 1
- Standard error: 2
If using Octave, we can get them by calling stdin
, stdout
and stderr
. In C++, they are std::cin
, std::cout
and std::cerr
respectively. If we execute an Octave script directly from the console instead of in a session, the following ways can be used.
-
Standard output and standard error directed to a same file without being displayed in the console.
$ octave script_name.m &> logfile.log
&>
means1>logfile.log 2>logfile.log
but withlogfile.log
opened only once. -
Standard output and standard error directed to different files without being displayed in the console.
$ octave script_name.m > logfile.log 2> logfile.err
> logfile.log
is short for1>logfile.log
. -
If both console message display and log file record are needed, the command
tee
should be used.$ octave script_name.m 2>&1 | tee logfile.log &
2>&1
redirects the standard error to the standard output.
Backlinks: 《Perform numerical experiments in Emacs Org mode》