Clever timestamps but is it a good idea?
At work I have a collection of long-running Perl programs exec-ed by a rc script that have their noise redirected to a single intermingled log. The scripts themselves and the modules they use are littered with carps, warns and print STDERRs depending upon when and by whom they were written. I can differentiate their output (more or less) but I’d like it timestamped for forensic purposes.
My first thought, heaven help me, involving tying filehandles is exampled by this hack:
!/usr/local/bin/perl -w use Tie::Handle; use Carp; use strict; package TimeStamper; @ISA = qw(Tie::Handle); sub wrap { my $fh = shift; my $fh_name = $fh; $fh_name =~ s/^\*//; local *MYFH; open(MYFH, “>&$fh_name”) or die(“Failed to dupe [$fh_name]: $!”); tie($fh,‘TimeStamper’,*MYFH); } sub TIEHANDLE { my ($class,$fh) = @_; my $obj = bless [$fh],$class; return $obj; } sub PRINT { my $self = shift; my $fh = $self->0; myt = localtime();
print sprintf("[%4d/%02d/%02d %02d:%02d:%02d] ",
$t[5]+1900,t[4,3,2,1,0]),@_;
}
package main;
TimeStamper::wrap(*STDERR);
print STDERR “This is timestamped.\n”;
warn(‘This is timestamped.’);
carp(‘This is timestamped.’);
print “This is not timestamped.\n”;
Neat. Even half clever. But I’m not sure it’s a good idea.
Comments [1]
Commenting is closed for this article.
Ross Lonstein