I ended up writing a quick little script for this in NodeJS, but I was wondering if there was a utility you could feed text into which would prepend each line with some text -- in my specific case, the time elapsed in milliseconds since the start of the programm. Ideally, the use would be something like:
longrunningprocess | prepend-timestamp
This could help do some fast debugging.
This question looks very similar to Is there a Unix utility to prepend timestamps to stdin?, but is not the same, I would like to prepend milliseconds since the start of the program, not the current timestamp.
Here is my version using NodeJS:
#!/bin/env node
var moment = require("moment");
process.stdin.resume()
var fullData = "";
var t = +new Date();
process.stdin.on('data', function(data) {
fullData += data;
splitted = fullData.split("\n");
splitted.forEach(function (part) {
if (part === "" ) {
return process.stdout.write("\n");
}
process.stdout.write( (+new Date() - t) + " " + part + "\n");
})
fullData = splitted[splitted.length-1];
});
process.stdout.on('error', function(err) {
if (err.code === 'EPIPE') return process.exit()
process.emit('error', err)
});
Bonus points if the ANSI escapes are kept (eg to see the colors of the initial command),
It would be even better if the precision (eg number of decimals) could be configurable.