App-Prefix
view release on metacpan or search on metacpan
# my $info = MemInfo::get_meminfo();
# my ($free, $cached, $swapped) = ($info->{MemFree}, $info->{Cached}, $info->{SwapCached});
# my $str = sprintf( "%s free, %s cached, %s swapped",
# convert_bytes_to_human_size( $free ),
# convert_bytes_to_human_size( $cached ),
# convert_bytes_to_human_size( $swapped ) );
# push(@prefixes, $str);
#}
my $out = "";
if (@prefixes) {
if ($suffix) { # not prefix, suffix
$out .= $_;
$out .= " " if $space;
}
$out .= join(" ", @prefixes);
if (!$suffix) { # yes suffix
$out .= " " if $space;
$out .= $_;
}
} else {
$out = $_;
}
print $out, "\n";
$lasttime = $time;
}
}
sub getdatetime {
my $t = shift || time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t);
#return sprintf("%04d-%02d-%02d %02d:%02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min, $sec);
return POSIX::strftime( "%Y-%m-%d %H:%M:%S", $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
}
############################################
# converts seconds to human-readable.
# I couldn't find a module on cpan that did (exactly) this :)
# the criteria is a short string describing the time duration, that's easy to parse.
# (DateTime::Format::Human::Duration is similar, but won't show fractional durations like "1.2 mins"
sub seconds_for_display {
my $t = shift;
my $precision = shift || 2;
my $format = '%1.' . ${precision} . "f";
if ($raw) {
if (int($t) == $t) {
return "$t secs";
} else {
return sprintf( $format, $t ) . " secs";
}
}
# start from largest unit, a year, and work towards smaller units
if (abs($t) >= 86400 * 365.25) {
# this is not exact because we ignore leap years
return sprintf($format, $t/(86400 * 365.25)) . " years";
}
# Considered, but removed, months entries in seconds_for_display()
# 1) hard to abbreviate months in 4 chars. mnths?
# 2) 2.1 months looks especially weird
# 3) months are much more variable-sized than any other time unit
# therefore more ambiguous and complex to compute (Ie, 28 vs 31 days)
#my $seconds_per_month = (365.25 / 12) * 86400; # mythical equal-sized months
#if (abs($t) >= $seconds_per_month) {
# return sprintf($format, $t/$seconds_per_month) . " mnths";
#}
if (abs($t) >= 86400) {
return sprintf($format, $t/86400) . " days";
}
if (abs($t) >= 60*60) {
return sprintf($format, $t/3600) . " hrs";
}
if (abs($t) >= 60) {
return sprintf($format, $t/60) . " mins";
}
# now from 1/100th of a second and smaller...
if (abs($t) <= 0.01) {
return sprintf($format, $t*1000) . " ms";
}
# then between 0.01 and 0.1 ...
if (abs($t) <= 0.1) { # note that we ignore $format here
my $prec = max(2, $precision);
return sprintf("%0.${prec}f", $t) . " secs";
}
# for abs($t) between 0.1 and 60
return sprintf("$format secs", $t);
}
=pod
=head1 NAME
prefix - adds hostname, time information, or more to lines from stdin (or read from files)
=head1 SYNOPSIS
% tail -f /var/log/some.log | prefix -host -timestamp
tails a file, showing each line with a hostname and a timestamp like.
So if we were tailing a growing file with lines like:
OK: System operational
Warning: Disk bandwidth saturated
we would get real-time output like:
host.example.com 2013-10-13 16:51:26 OK: System operational
host.example.com 2013-10-13 16:55:47 Warning: Disk bandwidth saturated
host.example.com 2013-10-13 16:55:49 Warning: Things are wonky: disks spinning backwards
host.example.com 2013-10-13 16:55:50 Error: Data read wackbards
host.example.com 2013-10-13 16:56:10 OK: Spacetime reversal complete
Note that the hostname (host.example.com) and the date have been prepended to the lines from the file
The prefix program also supports adding arbitrary text, current time to the microsecond,
time elapsed since invocation, time since last line output,
( run in 3.043 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )