Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add time information to STDERR output in Perl?

Tags:

perl

Is there an easy way to time tag errors going to stderr? In order to troubleshoot problems, I need to know when an error occurred.

Example:

Dec 10 12:00:00 Can't call method "str" on an undefined value

Thanks!

like image 856
macbus Avatar asked Nov 27 '22 22:11

macbus


2 Answers

Define custom handlers for handling warnings and fatal errors:

use strict;
use warnings;

$SIG{__WARN__} = sub { warn sprintf("[%s] ", scalar localtime), @_ };
$SIG{__DIE__}  = sub { die  sprintf("[%s] ", scalar localtime), @_ };

warn;
die;

Output:

[Fri Dec 11 14:35:37 2009] Warning: something's wrong at c:\temp\foo.pl line 7.
[Fri Dec 11 14:35:37 2009] Died at c:\temp\foo.pl line 8.

You might want to use gmtime instead of localtime.

like image 182
Michael Carman Avatar answered Jan 03 '23 11:01

Michael Carman


__WARN__ handlers only catch warnings. They don't catch print STDERR. If you want to catch everything, you have to tie STDERR.

#!/usr/bin/perl -w

package Tie::Handle::Timestamp;

use strict;
use warnings;

use base 'Tie::Handle';

sub stamp {
    return scalar localtime() .": ";
}

sub TIEHANDLE {
    my $class = shift;
    my $handle = shift;

    # Dup the filehandle otherwise we'll be calling ourself
    open my $fh, ">&", $handle or die $!;

    return bless $fh, $class;
}

sub PRINT {
    my $self = shift;
    print { $self } $self->stamp, @_;
}

sub PRINTF {
    my $self = shift;
    my $format = shift;
    printf { $self } $self->stamp . $format, @_;
}

sub CLOSE {
    my $self = shift;
    close $self;
}


package main;

# Tie it at compile time to catch compile time warnings
BEGIN {
    tie *STDERR, "Tie::Handle::Timestamp", *STDERR or die;
}

print STDERR "Foo\n";
warn "Bar";
undef == 2;

This has its own perils as you're now relying on how good your tie implementation is. I'd be surprised there's not already a CPAN module for this, but I don't know of any one I could recommend.

like image 24
Schwern Avatar answered Jan 03 '23 13:01

Schwern