App-Prefix
view release on metacpan or search on metacpan
if ($utimestamp) {
my $frac = sprintf("%0.5f", $time - int($time));
$frac =~ s/^0//; # remove leading 0, but not the .
$str .= $frac;
}
push( @prefixes, $str );
}
if ($elapsedstamp) {
push(@prefixes,
sprintf("%s elapsed", seconds_for_display( $time - $startt, 5 ) ) );
}
if ($diffstamp) {
push(@prefixes,
sprintf("%s diff", seconds_for_display( $time - $lasttime, 5 ) ) );
}
#if ($loadstamp) { # DEFERRED FEATURE
# require CPULoad;
# my @loads = CPULoad::get_loads();
# push(@prefixes, sprintf ("load:%0.2f", $loads[0]));
#}
#if ($memstamp) { # DEFERRED FEATURE
# require MemInfo;
# 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)
( run in 0.737 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )