-
Notifications
You must be signed in to change notification settings - Fork 10
/
wtimestamp
executable file
·39 lines (39 loc) · 1.35 KB
/
wtimestamp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env perl
# wtimestamp (part of ossobv/vcutil) // wdoekes/2015,2019 // Public Domain
#
# Reads lines from stdin and prepends them with the current time.
# Use this for applications that provide output but fail to add a timestamp.
#
# Usage:
#
# $ application | wtimestamp
# (outputs stdout from application with a nicely formatted time)
#
# Example:
#
# $ for x in `seq 3`; do sleep 1.5; echo "line $x"; done | wtimestamp
# 2019-04-12 15:32:39.658942+0200: line 1
# 2019-04-12 15:32:41.161355+0200: line 2
# 2019-04-12 15:32:42.663128+0200: line 3
#
# Previous version, in sh:
#
# while read -r line; do
# echo "`date +'%Y-%m-%d %H:%M:%S%z'`: $line"
# done
#
# Replaced by perl because of all the PID spawning overhead sometimes is
# too much (e.g. when we're monitoring execve's).
#
# See also: tai64n <https://cr.yp.to/daemontools/tai64n.html>
#
use POSIX 'strftime';use Time::HiRes 'gettimeofday';
$|++;my($prev,$cont,$in)=("",1,"");
while(($cont&&($cont=sysread(STDIN,$in,4096)))||$prev){
my @a=split /\n/s,$prev.$in,-1;$prev=$in="";$prev=pop @a if $cont;
# It's actually quite horrible that all of the above is needed for a simple
# line-based buffer reading. Surely this could be simpler in perl?
my @v=gettimeofday();
my $f=sprintf '%%Y-%%m-%%d %%H:%%M:%%S.%06d%%z',$v[1];
my $t=strftime($f,,localtime $v[0]);
foreach (@a){$_.="\n";print("$t: $_");}}