-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbb.timestamper
executable file
·215 lines (177 loc) · 6.75 KB
/
bb.timestamper
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/perl
#----------------------------------------------------------#
# Author: Douglas Senalik [email protected] #
#----------------------------------------------------------#
# "Black Box" program series
=bb
A pipe which will insert time stamps into the passing data
=cut bb
use strict;
use warnings;
use Getopt::Long; # for getting command line parameters
############################################################
# configuration variables
############################################################
my $defaultinterval = 5; # time in minutes
############################################################
# global variables
############################################################
my $ansiup = "\033[1A"; # terminal control
(my $prognopath = $0) =~ s/^.*[\/\\]//;
############################################################
# command line parameters
############################################################
my $interval = $defaultinterval;
my $prefix = "";
my $suffix = "";
my $noreturn = 0;
my $help = 0; # print help and exit
my $quiet = 0; # only show errors
my $debug = 0; # print extra debugging information
GetOptions (
"interval=s" => \$interval, # string
"prefix=s" => \$prefix, # string
"suffix=s" => \$suffix, # string
"noreturn" => \$noreturn, # flag
"help" => \$help, # flag
"quiet" => \$quiet, # flag
"debug" => \$debug); # flag
# debug implies not quiet
if ( $debug ) { $quiet = 0; }
# convert interval from minutes to seconds
unless ( $interval )
{ $help = 1; }
else
{ $interval = ( $interval * 60 ); }
debugmsg ( "Interval in seconds = \"$interval\"" );
############################################################
# print help screen
############################################################
if ( $help )
{
print "$prognopath
This program acts as a pipe, and will insert time stamps
in the stream
Optional parameters:
--interval=xxx minutes between time stamps, default=$defaultinterval
--prefix=xxx put this text in front of time, default=nothing
--suffix=xxx put this text in after time, default=nothing
--noreturn do not put a return after time
--help print this screen
--debug print extra debugging information
";
# --quiet only print error messages
exit 1;
} # if ( $help )
############################################################
# main loop
############################################################
my $starttime = time();
my $laststamp = 0;
my $INF = stdopen ( "<", "-" );
my $OUTF = stdopen ( ">", "-" );
binmode ( $INF ); # not sure if necessary, but shouldn't hurt
select($INF); $| = 1; # input unbuffered
select($OUTF); $| = 1; # output unbuffered
while ( ! eof($INF) )
{
# print out timestamp at desired interval. If interval not yet reached,
# then does nothing
my $currtime = time();
if ( ( $currtime - $laststamp ) > $interval )
{
# remove time in $interval increments so we don't accumulate weird intervals
if ( $laststamp <= 0 )
{ $laststamp = $currtime }
else
{
while ( ( $currtime - $laststamp ) > $interval )
{ $laststamp += $interval; }
}
print $OUTF $prefix, timestr($currtime), $suffix;
unless ( $noreturn ) { print $OUTF "\n"; }
}
# read in a chunk of unbuffered input data. Will block here waiting for input
# unless piping program terminates, then will return 0 bytes read
my $buf;
my $nread = sysread($INF, $buf, 1024);
debugmsg ( "Read $nread bytes from input stream" );
if ( $nread <= 0 )
{
debugmsg ( "\$nread=$nread is <= 0, ending" );
last;
}
print $OUTF $buf;
} # ( ! eof($INF) )
debugmsg ( "Closing file handles" );
stdclose ( $OUTF );
stdclose ( $INF );
exit 0;
############################################################
sub debugmsg { my ( $text, $noreturn, $nolinenum ) = @_;
############################################################
if ( $debug )
{
my ($package, $filename, $line, $sub) = caller(0);
unless ( $nolinenum ) { $text = "Line $line: " . $text; }
if ( ! ( $noreturn ) ) { $text .= "\n"; }
print $text;
} # if ( $debug )
} # sub debugmsg
###############################################################
sub timestr {
###############################################################
@_ = localtime(shift || time);
return(sprintf("%04d/%02d/%02d %02d:%02d:%02d", $_[5]+1900, $_[4]+1, $_[3], @_[2,1,0]));
} # sub timestr
###############################################################
sub commify {
###############################################################
# http://perldoc.perl.org/perlfaq5.html#How-can-I-output-my-numbers-with-commas
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
} # commify
###############################################################
sub stdopen { my ( $mode, $filename, $extratext ) = @_;
###############################################################
# a replacement for the three-parameter open which also allows
# the use of "-" as the file name to mean STDIN or STDOUT
my $fh; # the file handle
if ( $filename eq "-" ) # only exact match to "-" has special meaning
{
if ( $mode =~ m/>/ )
{ $fh = *STDOUT }
else
{ $fh = *STDIN }
}
else
{
# supplemental passed text for error messages, need one more space
if ( defined $extratext )
{ $extratext .= " " }
else
{ $extratext = "" }
my $text; # this is only used for error message
if ( $mode =~ m/^\+?>>/ ) # ">>" or "+>>"
{ $text = "append" }
elsif ( $mode =~ m/^\+?>/ ) # ">" or "+>"
{ $text = "output" }
elsif ( $mode =~ m/^\+?</ ) # "<" or "+<"
{ $text = "input" }
else
{ die "Error, unsupported file mode \"$mode\" specified to stdopen( $mode, $filename, $extratext )\n"; }
open ( $fh, $mode, $filename ) or die ( "Error opening ${extratext}file \"$filename\" for $text: $!\n" );
}
# return the opened file handle to the caller
return $fh;
} # stdopen
###############################################################
sub stdclose { my ( $fh ) = @_;
###############################################################
# same as built-in close, except in case of STDIN or STDOUT,
# and in this case the file handle is not closed
unless ( fileno($fh) <= 2 ) # if file number is this low, is stdin or stdout or stderr
{ close ( $fh ) or die ( "Error closing file handle: $!\n" ); }
} # sub stdclose
# eof