From: Dobrica Pavlinusic Date: Sun, 12 Feb 2012 20:02:59 +0000 (+0100) Subject: don't timestamp lines which allready have timestamp at beginning X-Git-Url: http://git.rot13.org/?p=cloudstore.git;a=commitdiff_plain;h=cc416fce649a9482e1eb91a86148460d810b6b44 don't timestamp lines which allready have timestamp at beginning --- diff --git a/lib/WarnColor.pm b/lib/WarnColor.pm index 1a40fee..4f1b7b8 100644 --- a/lib/WarnColor.pm +++ b/lib/WarnColor.pm @@ -26,13 +26,19 @@ sub BEGIN { $log .= $ENV{NAME} ? $ENV{NAME} : $1 if $0 =~ m{([^/]+)$}; - my $line = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s\n", $yyyy,$m,$d, $hh,$mm,$ss, join(' ',@_)); + my $line = join(' ', @_); + if ( $line =~ s{^(?:\e\[\d+m)+(\d\d\d\d)\W(\d\d)\W(\d\d) (\d\d:\d\d:\d\d)}{$1-$2-$3 $4} ) { + # have timestamp allready + } else { + my $time = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $yyyy,$m,$d, $hh,$mm,$ss); + $line = "$time $line"; + } + $line =~ s/[\r\n]+$/\n/; print STDERR "creating $log\n" unless -e $log; - open(my $fh, '>>', $log); - if ( $fh ) { + if ( open(my $fh, '>>', $log) ) { print $fh $line; close $fh; } else { diff --git a/t/WarnColor.t b/t/WarnColor.t index 21f70d0..250c1de 100755 --- a/t/WarnColor.t +++ b/t/WarnColor.t @@ -2,7 +2,7 @@ use strict; use warnings; -use Test::More tests => 4; +use Test::More tests => 5; use File::Path qw(remove_tree); use Data::Dump qw(dump); @@ -23,4 +23,4 @@ ok warn('slice warn'), 'with ENV'; ok glob('/tmp/slice/log/*'), 'log created'; - +ok warn('2012/02/12 11:22:13 rsync timestamp'), 'rsync timestamp';