Add timestamps to stdout
I spent some time trying to get timestamps added to C++ printing, e.g .through cout
.
I naive approach is to write a function get_current_time()
and put it before all printing statements e.g.:
cout << get_current_time() << "Message" << endl;
This requires changing all logging statements. Then my googling stumbled upon this question which had an elegant solution incorporating a decorator object.
Further down the page however I came upon a much nicer solution that transcends languages and programs and can be applied to running shell commands. The answer I ended up stealing and adding to my .zshrc is as follows:
function add_timestamps() {
awk '{print "["strftime()"]: "$0}'
}
This can be used after a program is run e.g. a.out | add_timestamps
to add the current time before every line printed out by a.out
.
Even top
can be passed through this function, but with predictably nasty results!