Codebase list liblog-report-perl / v1.03
distribution Log-Report-1.03.tar.gz Mark Overmeer authored 10 years ago Mark Overmeer committed 6 years ago
9 changed file(s) with 56 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
55
66 TODO:
77 . connect to Message::Passing framework
8
9 version 1.03: Thu May 22 11:54:24 CEST 2014
10
11 Fixes:
12 - float serialization under locale in test [cpantesters]
13 - non-errors and ::Dispatcher::Perl
14
15 Improvements:
16 - shorted display of string parameters in stack-trace to max 80 chars
17 - Log4perl log-lines sometimes show dispatcher as source, skip them.
18 - disable 'mode switch' trace for try()
819
920 version 1.02: Mon Mar 10 16:03:13 CET 2014
1021
11
22 use 5.008;
33
4 my $version = '1.02';
4 my $version = '1.03';
55
66 my %prereq =
77 ( Test::More => '0.86'
5050 sub die_decode($)
5151 { my @text = split /\n/, $_[0];
5252 @text or return ();
53
54 $text[0] =~ s/\.$//; # inconsequently used
5553 chomp $text[-1];
5654
5755 my %opt = (errno => $! + 0);
5856 my $err = "$!";
5957
6058 my $dietxt = $text[0];
61 if($text[0] =~ s/ at (.+) line (\d+)$// )
59 if($text[0] =~ s/ at (.+) line (\d+)\.?$// )
6260 { $opt{location} = [undef, $1, $2, undef];
6361 }
6462 elsif(@text > 1 && $text[1] =~ m/^\s*at (.+) line (\d+)\.?$/ )
65 { $opt{location} = [undef, $1, $2, undef];
63 { # sometimes people carp/confess with \n, folding the line
64 $opt{location} = [undef, $1, $2, undef];
6665 splice @text, 1, 1;
6766 }
6867
69 $text[0] =~ s/\s*[.:;]?\s*$err\s*$//
68 $text[0] =~ s/\s*[.:;]?\s*$err\s*$// # the $err is translation sensitive
7069 or delete $opt{errno};
7170
7271 my $msg = shift @text;
2525 @reasons==keys %default_reasonToLevel
2626 or panic __"Not all reasons have a default translation";
2727
28 # Do not show these as source of the error: one or more caller frames up
29 Log::Log4perl->wrapper_register($_) for qw/
30 Log::Report
31 Log::Report::Dispatcher
32 Log::Report::Dispatcher::Try
33 /;
34
2835 =chapter NAME
2936 Log::Report::Dispatcher::Log4perl - send messages to Log::Log4perl back-end
3037
3838
3939 sub log($$$$)
4040 { my ($self, $opts, $reason, $message, $domain) = @_;
41 my $text = $self->translate($opts, $reason, $message);
42
43 if($opts->{is_fatal})
44 { $! = $opts->{errno};
45 die $text;
46 }
47 else
48 { warn $text;
49 }
41 print STDERR $self->translate($opts, $reason, $message);
5042 }
5143
5244 1;
7373 , '""' => 'showStatus'
7474 , fallback => 1;
7575
76 #-----------------
7677 =chapter METHODS
7778
7879 =section Constructors
107108 $self;
108109 }
109110
111 #-----------------
110112 =section Accessors
111113
112114 =method died [STRING]
126128
127129 sub exceptions() { @{shift->{exceptions}} }
128130
131 #-----------------
129132 =section Logging
130133
131134 =method log $opts, $reason, $message
173176 M<Log::Report::Exception::throw()> which does the job.
174177 =cut
175178
176 sub reportFatal(@) { $_->throw(@_) for shift->wasFatal }
177 sub reportAll(@) { $_->throw(@_) for shift->exceptions }
178
179
180 #-----------------
181
179 sub reportFatal(@) { $_->throw(@_) for shift->wasFatal }
180 sub reportAll(@) { $_->throw(@_) for shift->exceptions }
181
182 #-----------------
182183 =section Status
183184
184185 =method failed
208208
209209 $self->{needs} = [ expand_reasons $default_accept[$mode] ];
210210
211 trace __x"switching to run mode {mode}, accept {accept}"
212 , mode => $mode, accept => $default_accept[$mode];
211 trace __x"switching to run mode {mode} for {pkg}, accept {accept}"
212 , mode => $mode, pkg => ref $self, accept => $default_accept[$mode]
213 unless $self->isa('Log::Report::Dispatcher::Try');
213214
214215 $mode;
215216 }
424425 $max -= @params * 2 - length($listtail); # \( ( \,[ ] ){n-1} \)
425426
426427 my $calling = $thing->stackTraceCall(\%args, $abstract, $call, $obj);
427 my @out = map {$thing->stackTraceParam(\%args, $abstract, $_)} @params;
428 my @out = map $thing->stackTraceParam(\%args, $abstract, $_), @params;
428429 my $total = sum map {length $_} $calling, @out;
429430
430431 ATTEMPT:
476477 return $param # int or float
477478 if $param =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
478479
479 '"' . escape_chars($param) . '"';
480 my $escaped = escape_chars $param;
481 if(length $escaped > 80)
482 { $escaped = substr($escaped, 0, 30)
483 . '...['. (length($escaped) -80) .' chars more]...'
484 . substr($escaped, -30);
485 }
486
487 qq{"$escaped"};
480488 }
481489
482490 =chapter DETAILS
225225 my $stop = exists $opts->{is_fatal} ? $opts->{is_fatal} : is_fatal $reason;
226226
227227 my $try = $nested_tries[-1];
228 my @disp = ($stop && $try) ? () : @{$reporter->{needs}{$reason} || []};
228 my @disp = defined $try && $stop ? () : @{$reporter->{needs}{$reason}||[]};
229229 push @disp, $try if defined $try && $try->needs($reason);
230230
231231 # return when no-one needs it: skip unused trace() fast!
277277 @disp or return;
278278 }
279279
280 my @last_call; # call Perl dispatcher always last, it calls real die
281280 my $domain = $message->domain;
282281 if(my $filters = $reporter->{filters})
283282 {
289288 ($r, $m) = $filter->[0]->($d, $opts, $r, $m, $domain);
290289 $r or next DISPATCHER;
291290 }
292
293 if($d->isa('Log::Report::Dispatcher::Perl'))
294 { @last_call = ($d, { %$opts }, $r, $m, $domain) }
295 else { $d->log($opts, $r, $m, $domain) }
291 $d->log($opts, $r, $m, $domain);
296292 }
297293 }
298294 else
299 { foreach my $d (@disp)
300 { if($d->isa('Log::Report::Dispatcher::Perl'))
301 { @last_call = ($d, { %$opts }, $reason, $message, $domain) }
302 else { $d->log($opts, $reason, $message, $domain) }
303 }
304 }
305
306 if(@last_call && !$^S)
307 { # the PERL dispatcher may terminate the program
308 shift(@last_call)->log(@last_call);
295 { $_->log($opts, $reason, $message, $domain) for @disp;
309296 }
310297
311298 if($stop)
872859 if($INC{'Log/Report/Minimal.pm'})
873860 { my ($pkg, $fn, $line) = caller; # do not report on LR:: modules
874861 if(index($pkg, 'Log::Report::') != 0)
875 { my @pkgs = Log::Report::Optional->usedBy;
862 { # @pkgs empty during release testings of L::R distributions
863 my @pkgs = Log::Report::Optional->usedBy;
876864 die "Log::Report loaded too late in $fn line $line, "
877865 . "put in $pkg before ", (join ',', @pkgs) if @pkgs;
878866 }
1414
1515 sub ol_is($$;$)
1616 { # since Test::More 0.95_01, is() does not stringify its arguments.
17 # This means that overloading does not quick in. How to test
17 # This means that overloading does not kick in. How to test
1818 # overloading now?
1919 my ($f, $s, $comment) = @_;
2020 overload::Overloaded($f) || overload::Overloaded($s)
139139
140140 use constant PI => 4 * atan2(1, 1);
141141 my $approx = 'approx pi: 3.141593';
142 is((sprintf "approx pi: %.6f", PI), $approx);
143 ol_is((__x "approx pi: {approx}", approx => sprintf("%.6f", PI)), $approx);
144 ol_is((__x "approx pi: {pi%.6f}", pi => PI), $approx);
142 is((sprintf "approx pi: %.6f", PI), $approx, 'sprintf');
143 ol_is((__x "approx pi: {approx}", approx => sprintf("%.6f", PI)), $approx,
144 'sprintf nested');
145
146 my $app = __x "approx pi: {pi%.6f}", pi => PI;
147 $app =~ s/\,/./g; # translated under locale, which may use ','
148 ol_is($app, $approx, 'interpolated format');
145149
146150 ol_is((__x "{perms} {links%2d} {user%-8s} {size%8d} {fn}"
147151 , perms => '-rw-r--r--', links => 1, user => 'superman'