-
Notifications
You must be signed in to change notification settings - Fork 6
/
vnl-ts
executable file
·188 lines (128 loc) · 4.82 KB
/
vnl-ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use autodie;
use FindBin '$RealBin';
use lib "$RealBin/lib";
use Vnlog::Util qw(parse_options read_and_preparse_input ensure_all_legends_equivalent reconstruct_substituted_command);
my @specs = ( qw(i s m r),
"vnl-field=s",
"vnl-tool=s",
"help|h");
my ($filenames,$options,$nondash_options) = parse_options(\@ARGV, \@specs, 1, <<EOF);
$0 [-i | -s] [-m] [--vnl-field t] format < pipe
This wraps the 'ts' utility: a tool that prepends timestamps to streaming input.
Unlike the 'ts' utility, this tool REQUIRES an explicit format to be given, and
this tool will barf if this format contains whitespace. The 'ts' -r option is
not supported.
The options are (mostly from the 'ts' manpage)
-i Instead of reporting the absolute time, report the elapsed time since the
last record.
-s Instead of reporting the absolute time, report the elapsed time since the
legend line was received.
-m Use monotonic clock
--vnl-field t
Specifies the name of the new field. If omitted, a sensible default will
be used
--vnl-tool tool
Specifies the path to the tool we're wrapping. By default we wrap 'ts',
so most people can omit this
EOF
$options->{'vnl-tool'} //= 'ts';
if( defined $options->{'vnl-field'} && $options->{'vnl-field'} =~ /\s/ )
{
my $fieldname = $options->{'vnl-field'};
say "Error: the new field name MUST be free of whitespace. Instead got '$fieldname'";
exit 1;
}
my $format = $nondash_options->[0];
if( $format =~ /\s/ )
{
say "Error: the format MUST be free of whitespace. Instead got '$format'";
exit 1;
}
if( defined $options->{r} )
{
say "Error: 'vnl-ts -r' is not supported: it assumes input timestamps with whitespace, which is incompatible with vnlog";
exit 1;
}
my $inputs = read_and_preparse_input($filenames, undef, 1);
ensure_all_legends_equivalent($inputs);
if(!defined $options->{'vnl-field'})
{
# default field names
if ($options->{i}) { $options->{'vnl-field'} = "time-diff"; }
elsif($options->{s}) { $options->{'vnl-field'} = "time-rel"; }
else { $options->{'vnl-field'} = "time"; }
}
say "# " .$options->{'vnl-field'} . " " . join(' ', @{$inputs->[0]{keys}});
my $ARGV_new = reconstruct_substituted_command($inputs, $options, [$format], \@specs);
exec $options->{'vnl-tool'}, @$ARGV_new;
__END__
=head1 NAME
vnl-ts - add a timestamp to a vnlog stream
=head1 SYNOPSIS
$ read_temperature
# temperature
29.5
30.4
28.3
22.1
... continually produces data at 1Hz
$ read_temperature | vnl-ts -s %.s
# time-rel temperature
0.013893 30.2
1.048695 28.6
2.105592 29.3
3.162873 22.0
...
=head1 DESCRIPTION
Usage: vnl-ts [-i | -s] [-m] [--vnl-field t] format < pipe
This tool runs C<ts> on given vnlog streams. C<vnl-ts> is a wrapper around the
C<ts> tool from Joey Hess's L<moreutils|https://joeyh.name/code/moreutils/>
toolkit. Since this is a wrapper, most commandline options and behaviors of the
C<ts> tool are present; consult the L<ts(1)> manpage for details. The
differences from C<ts> are
=over
=item *
The input and output to this tool are vnlog files, complete with a legend
=item *
The format I<must> be passed-in by the user; no default is assumed.
=item *
The given format I<must not> contain whitespace, so that it fits a single vnlog
field.
=item *
C<-r> is not supported: it assumes input timestamps with whitespace, which is
incompatible with vnlog
=item *
A C<vnl-ts>-specific option C<--vnl-field> is available to set the name of the
new field. If omitted, a reasonable default will be used.
=item *
By default we call the C<ts> tool to do the actual work. If the underlying tool
has a different name or lives in an odd path, this can be specified by passing
C<--vnl-tool TOOL>
=back
Past that, everything C<ts> does is supported, so see that man page for
detailed documentation.
=head1 COMPATIBILITY
By default this calls the tool named C<ts>. At least on FreeBSD, it's called
C<moreutils-ts>, so on such systems you should invoke C<vnl-ts --vnl-tool
moreutils-ts ...>
I use GNU/Linux-based systems exclusively, but everything has been tested
functional on FreeBSD and OSX in addition to Debian, Ubuntu and CentOS. I can
imagine there's something I missed when testing on non-Linux systems, so please
let me know if you find any issues.
=head1 SEE ALSO
L<ts(1)>
=head1 REPOSITORY
https://github.com/dkogan/vnlog/
=head1 AUTHOR
Dima Kogan C<< <[email protected]> >>
=head1 LICENSE AND COPYRIGHT
Copyright 2018 Dima Kogan C<< <[email protected]> >>
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.
=cut