summaryrefslogtreecommitdiffstats
path: root/scripts/ninja-ts
blob: 7c6b1cf1f71aabb368e77c604f03c6436bbeb91c (plain)
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
#!/usr/bin/perl
# ninja-ts - wsa's Add A Time Stamp Filter V1.1
# written by Wolfram Sang, (C) 2009 Pengutronix, (C) 2022 Sang Engineering
# free software - no warranty - WTFPL V2, see http://sam.zoy.org/wtfpl/

use warnings;
use strict;
use Time::HiRes qw(gettimeofday tv_interval);

my $arg = defined($ARGV[0]) ? $ARGV[0] : '(?=foo)bar'; # false-branch is a regexp that never matches
if ($arg eq '--help' or $arg eq '-h') {
	print "ninja-ts [regexp] - a filter which prepends a timestamp to every line of STDOUT; time will be reset if [regexp] matches\n";
	print "  Example: picocom <picocom_options> | ninja-ts 'Starting kernel ...'\n";
	exit 0;
}

my $old;
my $base;
$| = 1; # Flush output immediately

sub reset_time {
	$old = 0;
	$base = [gettimeofday()];
}

reset_time;
while (<STDIN>) {
	reset_time if (/$arg/o);
	my $new = tv_interval($base);
	my $diff = $new - $old;
	printf("[%10.6f] <%10.6f> $_", $new, $diff);
	$old = $new;
}