Log/Report.pm

filter: Statementstmt 75.8% 213 / 281 Branchbran 52.1% 99 / 190 Conditioncond 54.7% 46 / 84 MC/DCmcdc 24.4% 12 / 49 Subroutinesub 65.8% 27 / 41 Podpod 100% 21 / 21 totaltotal 62.7% 418 / 666 CC 138 SCAR 44.1
CC
138
Cov
65%
dispatcher
152.6
report
134.4
import
65.6
CRAP 82.1
↑ Summary Log/Report/Domain.pm »
1 # This code is part of Perl distribution Log-Report version 1.46.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2007-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Log::Report;{
13 17 our $VERSION = '1.46';
14 }
15  
16 17 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 17 ▶use Log::Report::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 my $lrm = 'Log::Report::Message';
26  
27 ### if you change anything here, you also have to change Log::Report::Minimal
28 17 my @make_msg = qw/__ __x __n __nx __xn N__ N__n N__w __p __px __np __npx/;
29 17 my @functions = qw/report dispatcher try textdomain default_dispatcher_mode/;
30 17 my @reason_functions = qw/trace assert info notice warning mistake error fault alert failure panic/;
31  
32 17 our @EXPORT_OK = (@make_msg, @functions, @reason_functions);
33  
34 sub _whats_needed(); sub dispatcher($@); sub textdomain(@);
35 sub trace(@); sub assert(@); sub info(@); sub notice(@); sub warning(@);
36 sub mistake(@); sub error(@); sub fault(@); sub alert(@); sub failure(@);
37 sub panic(@);
38 sub __($); sub __x($@); sub __n($$$@); sub __nx($$$@); sub __xn($$$@);
39 sub N__($); sub N__n($$); sub N__w(@);
40 sub __p($$); sub __px($$@); sub __np($$$$); sub __npx($$$$@);
41  
42 #
43 # Some initiations
44 #
45  
46 17 my $reporter = {};
47 17 my $default_mode = 0;
48 17 my @nested_tries;
49  
50 # we can only load these after Log::Report has compiled, because
51 # they use this module themselves as well.
52  
53 17 require Log::Report::Die;
54 17 require Log::Report::Domain;
55 17 require Log::Report::Message;
56 17 require Log::Report::Exception;
57 17 require Log::Report::Dispatcher;
58 17 require Log::Report::Dispatcher::Try;
59  
60 17 textdomain 'log-report';
61  
62 17 my $default_dispatcher = dispatcher PERL => 'default', accept => 'NOTICE-';
63  
64 #--------------------
65  
66 sub report($@)
67 42 ▶{ my $opts = ref $_[0] eq 'HASH' ? +{ %{ (shift) } } : {};
Subroutine report: called CC=50 SCAR=49.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 38 42
68 42 my ($reason, $message) = (shift, shift);
69  
70 42 ▶ my $stop = exists $opts->{is_fatal} ? $opts->{is_fatal} : is_fatal $reason;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'is_fatal'} ? : 2 40 42
71 42 my $try = $nested_tries[-1]; # WARNING: overloaded boolean, use 'defined'
72  
73 42 my @disp;
74 42 ▶ if(defined $try)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $try) { } 11 31 42
75 11 ▶ { push @disp, @{$reporter->{needs}{$reason} || []}
Branch: 1 branch
BranchTrueFalseTotal
unless $stop or $try->hides($reason) 3 8 11
Condition80%4/5
$stop or $try->hides($reason)
l!l&&r!l&&!r
803
$reporter->{'needs'}{$reason} || []
l!l
12
MC/DC50%1/2
$stop or $try->hides($reason)
$stop $try->hides($reason)
MC/DC100%1/1
$reporter->{'needs'}{$reason} || []
$reporter->{'needs'}{$reason}
Truth table66%2/3
A or B
A= $stopB= $try->hides($reason)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $reporter->{'needs'}{$reason}
Aresult
11
00
76 unless $stop || $try->hides($reason);
77  
78 push @disp, $try
79 11 ▶ if $try->needs($reason) || $opts->{is_fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $try->needs($reason) or $opts->{"is_fatal"} 11 0 11
Condition66%2/3
$try->needs($reason) or $opts->{"is_fatal"}
l!l&&r!l&&!r
1010
MC/DC0%0/2
$try->needs($reason) or $opts->{"is_fatal"}
$try->needs($reason) $opts->{"is_fatal"}
Truth table66%2/3
A or B
A= $try->needs($reason)B= $opts->{"is_fatal"}
ABresult
1X1
011
000
80 }
81 else
82 31 ▶ { @disp = @{$reporter->{needs}{$reason} || []};
Condition100%2/2
$reporter->{'needs'}{$reason} || []
l!l
922
MC/DC100%1/1
$reporter->{'needs'}{$reason} || []
$reporter->{'needs'}{$reason}
Truth table100%2/2
A
A= $reporter->{'needs'}{$reason}
Aresult
11
00
83 }
84  
85 42 ▶ is_reason $reason
Branch: 1 branch
BranchTrueFalseTotal
&is_reason($reason) or &error(&__x("token '{token UNKNOWN}' not recognized as reason.", "token", $reason)) 42 0 42
86 or error __x"token '{token UNKNOWN}' not recognized as reason.", token => $reason;
87  
88 # return when no-one needs it: skip unused trace() fast!
89 42 ▶ @disp || $stop
Branch: 1 branch
BranchTrueFalseTotal
@disp or $stop or return 20 22 42
Condition66%2/3
@disp or $stop
l!l&&r!l&&!r
20022
MC/DC50%1/2
@disp or $stop
@disp $stop
Truth table66%2/3
A or B
A= @dispB= $stop
ABresult
1X1
011
000
90 or return;
91  
92 20 my $to = delete $opts->{to};
93 20 ▶ if($to)
Branch: 1 branch
BranchTrueFalseTotal
if ($to) 0 20 20
94 { # explicit destination, still disp may not need it.
95 0 ▶ if(ref $to eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to eq 'ARRAY') { } 0 0 0
96 0 { my %disp = map +($_->name => $_), @disp;
97 0 @disp = grep defined, @disp{@$to};
98 }
99 else
100 0 { @disp = grep $_->name eq $to, @disp;
101 }
102 0 ▶ push @disp, $try if defined $try;
Branch: 1 branch
BranchTrueFalseTotal
if defined $try 0 0 0
103  
104 0 ▶ @disp || $stop
Branch: 1 branch
BranchTrueFalseTotal
@disp or $stop or return 0 0 0
Condition0%0/3
@disp or $stop
l!l&&r!l&&!r
000
MC/DC0%0/2
@disp or $stop
@disp $stop
Truth table0%0/3
A or B
A= @dispB= $stop
ABresult
1X1
011
000
105 or return;
106 }
107  
108 20 ▶ unless(Log::Report::Dispatcher->can('collectLocation'))
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Report::Dispatcher"->can("collectLocation")) 0 20 20
109 { # internal Log::Report error can result in "deep recursions".
110 0 eval "require Carp"; Carp::confess($message);
111 }
112 20 ▶ $opts->{location} ||= Log::Report::Dispatcher->collectLocation;
Condition66%2/3
$opts->{"location"} ||= "Log::Report::Dispatcher"->collectLocation
l!l&&r!l&&!r
3170
MC/DC0%0/2
$opts->{"location"} ||= "Log::Report::Dispatcher"->collectLocation
$opts->{"location"} "Log::Report::Dispatcher"->collectLocation
Truth table66%2/3
A ||= B
A= $opts->{"location"}B= "Log::Report::Dispatcher"->collectLocation
ABresult
1X1
011
000
113  
114 20 my $exception;
115 20 ▶ if(!blessed $message)
Branch: 3 branches
BranchTrueFalseTotal
if (not builtin::blessed($message)) { } 12 8 20
elsif ($message->isa('Log::Report::Exception')) { } 3 5 8
elsif ($message->isa('Log::Report::Message')) { } 4 1 5
116 { # untranslated message into object
117 12 ▶ @_%2 and error __x"odd length parameter list with '{msg}'.", msg => $message;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and &error(&__x("odd length parameter list with '{msg}'.", "msg", $message)) 0 12 12
118 12 $message = $lrm->new(_prepend => $message, @_);
119 }
120 elsif($message->isa('Log::Report::Exception'))
121 3 { $exception = $message;
122 3 $message = $exception->message;
123 }
124 elsif($message->isa('Log::Report::Message'))
125 4 ▶ { @_==0 or error __x"a message object is reported with more parameters.";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or &error(&__x("a message object is reported with more parameters.")) 4 0 4
126 }
127 else
128 { # foreign object
129 1 my $text = "$message"; # hope stringification is overloaded
130 1 $text =~ s/\s*$//gs;
131 1 ▶ @_%2 and error __x"odd length parameter list with object '{msg}'.", msg => $text;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and &error(&__x("odd length parameter list with object '{msg}'.", "msg", $text)) 0 1 1
132 1 $message = $lrm->new(_prepend => $text, @_);
133 }
134  
135 20 ▶ $message->to($to) if $to; # overrule destination of message
Branch: 1 branch
BranchTrueFalseTotal
if $to 0 20 20
136 20 ▶ if(my $disp_names = $message->to)
Branch: 1 branch
BranchTrueFalseTotal
if (my $disp_names = $message->to) 0 20 20
137 0 ▶ { my %select = map +($_ => 1), ref $disp_names eq 'ARRAY' ? @$disp_names : $disp_names;
Branch: 1 branch
BranchTrueFalseTotal
ref $disp_names eq 'ARRAY' ? : 0 0 0
138 0 @disp = grep $select{$_->name}, @disp;
139 0 ▶ push @disp, $try if defined $try && !$select{try};
Branch: 1 branch
BranchTrueFalseTotal
if defined $try and not $select{"try"} 0 0 0
Condition0%0/3
defined $try and not $select{"try"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $try and not $select{"try"}
defined $try not $select{"try"}
Truth table0%0/3
A and not(B)
A= defined $tryB= not $select{"try"}
ABresult
0X0
100
111
140 0 ▶ @disp or return;
Branch: 1 branch
BranchTrueFalseTotal
@disp or return 0 0 0
141 }
142  
143 20 ▶ $opts->{errno} //= $message->errno //
Branch: 2 branches
BranchTrueFalseTotal
&use_errno($reason) ? : 1 16 17
&is_fatal($reason) ? : 5 11 16
Condition66%6/9
$opts->{"errno"} //= $message->errno // (&use_errno($reason) ? $! + 0 || $? : (&is_fatal($reason) ? 1 : undef))
l!l&&r!l&&!r
3611
$message->errno // (&use_errno($reason) ? $! + 0 || $? : (&is_fatal($reason) ? 1 : undef))
l!l&&r!l&&!r
0611
$! + 0 or $?
l!l&&r!l&&!r
100
MC/DC0%0/3
$opts->{"errno"} //= $message->errno // (&use_errno($reason) ? $! + 0 || $? : (&is_fatal($reason) ? 1 : undef))
$opts->{"errno"} $message->errno (&use_errno($reason) ? $! + 0 || $? : (&is_fatal($reason) ? 1 : undef))
MC/DC0%0/2
$! + 0 or $?
$! + 0 $?
Truth table0%0/4
A //= B // C
A= $opts->{"errno"}B= $message->errnoC= (&use_errno($reason) ? $! + 0 || $? : (&is_fatal($reason) ? 1 : undef))
ABCresult
1XX1
01X1
0011
0000
Truth table33%1/3
A or B
A= $! + 0B= $?
ABresult
1X1
011
000
144 (use_errno($reason) ? ($!+0 || $?) : is_fatal($reason) ? 1 : undef);
145  
146 20 my $domain = $message->domain;
147 20 ▶ if(my $filters = $reporter->{filters})
Branch: 1 branch
BranchTrueFalseTotal
if (my $filters = $reporter->{'filters'}) { } 1 19 20
148 {
149 DISPATCHER:
150 1 foreach my $d (@disp)
151 1 { my ($r, $m) = ($reason, $message);
152 1 foreach my $filter (@$filters)
153 2 ▶ { next if keys %{$filter->[1]} && !$filter->[1]{$d->name};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$filter[1];} and not $filter->[1]{$d->name} 1 1 2
Condition66%2/3
keys %{$$filter[1];} and not $filter->[1]{$d->name}
!ll&&!rl&&r
011
MC/DC50%1/2
keys %{$$filter[1];} and not $filter->[1]{$d->name}
keys %{$$filter[1];} not $filter->[1]{$d->name}
Truth table66%2/3
A and not(B)
A= keys %{$$filter[1];}B= not $filter->[1]{$d->name}
ABresult
0X0
100
111
154 1 ($r, $m) = $filter->[0]->($d, $opts, $r, $m, $domain);
155 1 ▶ $r or next DISPATCHER;
Branch: 1 branch
BranchTrueFalseTotal
$r or next DISPATCHER 1 0 1
156 }
157 1 $d->log($opts, $r, $m, $domain);
158 }
159 }
160 else
161 19 { $_->log($opts, $reason, $message, $domain) for @disp;
162 }
163  
164 20 ▶ if($stop)
Branch: 1 branch
BranchTrueFalseTotal
if ($stop) 9 11 20
165 { # $^S = $EXCEPTIONS_BEING_CAUGHT; parse: undef, eval: 1, else 0
166 9 ▶ (defined($^S) ? $^S : 1) or exit($opts->{errno} || 0);
Branch: 2 branches
BranchTrueFalseTotal
defined $^S ? $^S : 1 or exit($opts->{"errno"} || 0) 9 0 9
defined $^S ? : 9 0 9
Condition0%0/2
$opts->{'errno'} || 0
l!l
00
MC/DC0%0/1
$opts->{'errno'} || 0
$opts->{'errno'}
Truth table0%0/2
A
A= $opts->{'errno'}
Aresult
11
00
167  
168 9 ▶ $! = $opts->{errno} || 0;
Condition100%2/2
$opts->{'errno'} || 0
l!l
81
MC/DC100%1/1
$opts->{'errno'} || 0
$opts->{'errno'}
Truth table100%2/2
A
A= $opts->{'errno'}
Aresult
11
00
169 9 ▶ $@ = $exception || Log::Report::Exception->new(report_opts => $opts, reason => $reason, message => $message);
Condition66%2/3
$exception || 'Log::Report::Exception'->new('report_opts', $opts, 'reason', $reason, 'message', $message)
l!l&&r!l&&!r
270
MC/DC0%0/2
$exception || 'Log::Report::Exception'->new('report_opts', $opts, 'reason', $reason, 'message', $message)
$exception 'Log::Report::Exception'->new('report_opts', $opts, 'reason', $reason, 'message', $message)
Truth table66%2/3
A || B
A= $exceptionB= 'Log::Report::Exception'->new('report_opts', $opts, 'reason', $reason, 'message', $message)
ABresult
1X1
011
000
170 9 die; # $@->PROPAGATE() will be called, some eval will catch this
171 }
172  
173 11 @disp;
174 }
175  
176  
177 17 my %disp_actions = map +($_ => 1), qw/close find list disable enable mode needs filter active-try do-not-reopen/;
178 17 my $reopen_disp = 1;
179  
180 sub dispatcher($@)
181 36 ▶{ if(! $disp_actions{$_[0]})
Subroutine dispatcher: called CC=38 SCAR=50.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ($disp_actions{$_[0]}) 22 14 36
182 22 { my ($type, $name) = (shift, shift);
183  
184 # old dispatcher with same name will be closed in DESTROY
185 22 my $disps = $reporter->{dispatchers};
186  
187 22 ▶ if(!$reopen_disp)
Branch: 1 branch
BranchTrueFalseTotal
unless ($reopen_disp) 17 5 22
188 0 ▶ { my $has = first {$_->name eq $name} @$disps;
Subroutine __ANON__: not called CC=1 SCAR=1.2
189 17 ▶ if(defined $has && $has ne $default_dispatcher)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $has and $has ne $default_dispatcher) 0 17 17
Condition33%1/3
defined $has and $has ne $default_dispatcher
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $has and $has ne $default_dispatcher
defined $has $has ne $default_dispatcher
Truth table33%1/3
A and B
A= defined $hasB= $has ne $default_dispatcher
ABresult
0X0
100
111
190 0 ▶ { my $default = $name eq 'default' ? ' (refreshing configuration instead)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'default' ? : 0 0 0
191 0 trace "not reopening $name$default.";
192 0 return $has;
193 }
194 }
195  
196 22 my @disps = grep $_->name ne $name, @$disps;
197 22 ▶ trace "reopening dispatcher $name" if @disps != @$disps;
Branch: 1 branch
BranchTrueFalseTotal
if @disps != @$disps 0 22 22
198  
199 22 my $disp = Log::Report::Dispatcher ->new($type, $name, mode => $default_mode, @_);
200  
201 22 ▶ push @disps, $disp if $disp;
Branch: 1 branch
BranchTrueFalseTotal
if $disp 22 0 22
202 22 $reporter->{dispatchers} = \@disps;
203  
204 22 _whats_needed;
205 22 ▶ return $disp ? ($disp) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$disp ? : 22 0 22
206 }
207  
208 14 my $command = shift;
209 14 ▶ if($command eq 'list')
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "list") 7 7 14
210 7 ▶ { @_ and mistake __"the 'list' sub-command doesn't expect additional parameters.";
Branch: 1 branch
BranchTrueFalseTotal
@_ and &mistake(&__("the 'list' sub-command doesn't expect additional parameters.")) 0 7 7
211 7 my @disp = @{$reporter->{dispatchers}};
212 7 ▶ push @disp, $nested_tries[-1] if @nested_tries;
Branch: 1 branch
BranchTrueFalseTotal
if @nested_tries 1 6 7
213 7 return @disp;
214 }
215 7 ▶ if($command eq 'needs')
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "needs") 0 7 7
216 0 ▶ { my $reason = shift || 'undef';
Condition0%0/2
shift() || 'undef'
l!l
00
MC/DC0%0/1
shift() || 'undef'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
217 0 ▶ is_reason $reason
Branch: 1 branch
BranchTrueFalseTotal
&is_reason($reason) or &error(&__x("the 'needs' sub-command parameter '{need}' is not a reason.", "need", $reason)) 0 0 0
218 or error __x"the 'needs' sub-command parameter '{need}' is not a reason.", need => $reason;
219 0 my $disp = $reporter->{needs}{$reason};
220 0 ▶ return $disp ? @$disp : ();
Branch: 1 branch
BranchTrueFalseTotal
$disp ? : 0 0 0
221 }
222 7 ▶ if($command eq 'filter')
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "filter") 2 5 7
223 2 { my $code = shift;
224 2 ▶ ref $code eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $code eq "CODE" or &error(&__("the 'filter' sub-command needs a CODE reference.")) 2 0 2
225 or error __"the 'filter' sub-command needs a CODE reference.";
226 2 my %names = map +($_ => 1), @_;
227 2 push @{$reporter->{filters}}, [ $code, \%names ];
228 2 return ();
229 }
230 5 ▶ if($command eq 'active-try')
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "active-try") 0 5 5
231 0 { return $nested_tries[-1];
232 }
233 5 ▶ if($command eq 'do-not-reopen')
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "do-not-reopen") 0 5 5
234 0 { $reopen_disp = 0;
235 0 return ();
236 }
237  
238 5 ▶ my $mode = $command eq 'mode' ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
$command eq 'mode' ? : 0 5 5
239  
240 5 ▶ my $all_disp = @_==1 && $_[0] eq 'ALL';
Condition33%1/3
@_ == 1 && $_[0] eq 'ALL'
!ll&&!rl&&r
050
MC/DC0%0/2
@_ == 1 && $_[0] eq 'ALL'
@_ == 1 $_[0] eq 'ALL'
Truth table33%1/3
A && B
A= @_ == 1B= $_[0] eq 'ALL'
ABresult
0X0
100
111
241 5 my $disps = $reporter->{dispatchers};
242 5 my @disps;
243 0 ▶ if($all_disp) { @disps = @$disps }
Branch: 1 branch
BranchTrueFalseTotal
if ($all_disp) { } 0 5 5
244 else
245 { # take the dispatchers in the specified order. Both lists
246 # are small, so O(x²) is small enough
247 5 for my $n (@_) { push @disps, grep $_->name eq $n, @$disps }
248 }
249  
250 5 ▶ wantarray || !defined wantarray || @disps < 2
Branch: 1 branch
BranchTrueFalseTotal
wantarray or not defined wantarray or @disps < 2 or &error(&__("only one dispatcher name accepted in SCALAR context.")) 5 0 5
Condition33%2/6
wantarray or not defined wantarray or @disps < 2
l!l&&r!l&&!r
500
wantarray or not defined wantarray
l!l&&r!l&&!r
050
MC/DC0%0/3
wantarray or not defined wantarray or @disps < 2
wantarray not defined wantarray @disps < 2
Truth table25%1/4
A or not(B) or C
A= wantarrayB= not defined wantarrayC= @disps < 2
ABCresult
1XX1
01X1
0011
0000
251 or error __"only one dispatcher name accepted in SCALAR context.";
252  
253 5 ▶ if($command eq 'close')
Branch: 4 branches
BranchTrueFalseTotal
if ($command eq 'close') { } 5 0 5
elsif ($command eq 'enable') { } 0 0 0
elsif ($command eq 'disable') { } 0 0 0
elsif ($command eq 'mode') { } 0 0 0
254 5 { my %kill = map +($_->name => 1), @disps;
255 5 @$disps = grep !$kill{$_->name}, @$disps;
256  
257 5 $_->close for @disps;
258 }
259 0 elsif($command eq 'enable') { $_->_disabled(0) for @disps }
260 0 elsif($command eq 'disable') { $_->_disabled(1) for @disps }
261 elsif($command eq 'mode')
262 0 ▶ { Log::Report::Dispatcher->defaultMode($mode) if $all_disp;
Branch: 1 branch
BranchTrueFalseTotal
if $all_disp 0 0 0
263 0 $_->_set_mode($mode) for @disps;
264 }
265  
266 # find does require reinventarization
267 5 ▶ _whats_needed if $command ne 'find';
Branch: 1 branch
BranchTrueFalseTotal
if $command ne "find" 5 0 5
268  
269 5 ▶ wantarray ? @disps : $disps[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 5 5
270 }
271  
272 17 ▶END { $_->close for @{$reporter->{dispatchers}} }
Subroutine END: called CC=2 SCAR=6.9
273  
274 # _whats_needed
275 # Investigate from all dispatchers which reasons will need to be
276 # passed on. After dispatchers are added, enabled, or disabled,
277 # this method shall be called to re-investigate the back-ends.
278  
279 sub _whats_needed()
280 27 ▶{ my %needs;
Subroutine _whats_needed: called CC=3 SCAR=11.0
281 27 foreach my $disp (@{$reporter->{dispatchers}})
282 31 { push @{$needs{$_}}, $disp for $disp->needs;
283 }
284 27 $reporter->{needs} = \%needs;
285 }
286  
287  
288 sub try(&@)
289 21 ▶{ my $code = shift;
Subroutine try: called CC=13 SCAR=25.7
Pod documented
290  
291 21 ▶ @_ % 2 and report +{ location => [caller 0] },
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and &report({"location", [caller 0]}, "PANIC", &__("odd length parameter list for try(): forgot the terminating ';'?")) 0 21 21
292 PANIC => __"odd length parameter list for try(): forgot the terminating ';'?";
293  
294 unshift @_, mode => 'DEBUG'
295 21 ▶ if $reporter->{needs}{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $reporter->{"needs"}{"TRACE"} 2 19 21
296  
297 21 my $disp = Log::Report::Dispatcher::Try->new(TRY => 'try', @_);
298  
299 # L::R native messages are logged directly in $disp via @nested_tries
300 21 push @nested_tries, $disp;
301  
302 # user's __DIE__ handlers would frustrate the exception mechanism
303 21 local $SIG{__DIE__};
304  
305 21 my ($ret, @ret);
306 21 ▶ if(!defined wantarray) { eval { $code->() } } # VOID context
Branch: 2 branches
BranchTrueFalseTotal
if (not defined wantarray) { } 13 8 21
elsif (wantarray) { } 1 7 8
307 1 elsif(wantarray) { @ret = eval { $code->() } } # LIST context
308 7 else { $ret = eval { $code->() } } # SCALAR context
309  
310 21 my $err = $@;
311 21 pop @nested_tries; # remove $disp
312  
313 21 ▶ my $is_exception = blessed $err && $err->isa('Log::Report::Exception');
Condition100%3/3
builtin::blessed($err) && $err->isa('Log::Report::Exception')
!ll&&!rl&&r
1218
MC/DC100%2/2
builtin::blessed($err) && $err->isa('Log::Report::Exception')
builtin::blessed($err) $err->isa('Log::Report::Exception')
Truth table100%3/3
A && B
A= builtin::blessed($err)B= $err->isa('Log::Report::Exception')
ABresult
0X0
100
111
314 21 ▶ if(!$is_exception && $err && !$disp->wasFatal)
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_exception and $err and not $disp->wasFatal) 7 14 21
Condition83%5/6
not $is_exception and $err and not $disp->wasFatal
!ll&&!rl&&r
1407
not $is_exception and $err
!ll&&!rl&&r
867
MC/DC66%2/3
not $is_exception and $err and not $disp->wasFatal
not $is_exception $err not $disp->wasFatal
Truth table75%3/4
not(A) and B and not(C)
A= not $is_exceptionB= $errC= not $disp->wasFatal
ABCresult
0XX0
10X0
1100
1111
315 { # Decode errors which do not origin from Log::Report reports
316 # Native exceptions are already logged.
317 7 ▶ my ($opts, $reason, $text, $tags) = blessed $err
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) ? : 1 6 7
318 ? Log::Report::Die::exception_decode($err)
319 : Log::Report::Die::die_decode($err, on_die => $disp->die2reason);
320  
321 7 $disp->log($opts, $reason, __x($text, _tags => $tags));
322 }
323  
324 21 ▶ $disp->died($err)
Branch: 2 branches
BranchTrueFalseTotal
if $is_exception ? $err->isFatal : $err 15 6 21
$is_exception ? : 8 13 21
325 if $is_exception ? $err->isFatal : $err;
326  
327 21 $@ = $disp;
328  
329 21 ▶ wantarray ? @ret : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 20 21
330 }
331  
332 #--------------------
333  
334 23 ▶sub trace(@) {report TRACE => @_}
Subroutine trace: called CC=1 SCAR=0.0
Pod documented
335 1 ▶sub assert(@) {report ASSERT => @_}
Subroutine assert: called CC=1 SCAR=0.0
Pod documented
336 1 ▶sub info(@) {report INFO => @_}
Subroutine info: called CC=1 SCAR=0.0
Pod documented
337 4 ▶sub notice(@) {report NOTICE => @_}
Subroutine notice: called CC=1 SCAR=0.0
Pod documented
338 3 ▶sub warning(@) {report WARNING => @_}
Subroutine warning: called CC=1 SCAR=0.0
Pod documented
339 0 ▶sub mistake(@) {report MISTAKE => @_}
Subroutine mistake: not called CC=1 SCAR=6.9
Pod documented
340 4 ▶sub error(@) {report ERROR => @_}
Subroutine error: called CC=1 SCAR=0.0
Pod documented
341 0 ▶sub fault(@) {report FAULT => @_}
Subroutine fault: not called CC=1 SCAR=6.9
Pod documented
342 0 ▶sub alert(@) {report ALERT => @_}
Subroutine alert: not called CC=1 SCAR=6.9
Pod documented
343 1 ▶sub failure(@) {report FAILURE => @_}
Subroutine failure: called CC=1 SCAR=0.0
Pod documented
344 0 ▶sub panic(@) {report PANIC => @_}
Subroutine panic: not called CC=1 SCAR=6.9
Pod documented
345  
346 #--------------------
347  
348  
349 sub __($)
350 23 ▶{ my ($cpkg, $fn, $linenr) = caller;
Subroutine __: called CC=1 SCAR=0.0
351 23 $lrm->new(_msgid => shift, _domain => pkg2domain($cpkg), _use => "$fn line $linenr");
352 }
353  
354  
355 # label "msgid" added before first argument
356 sub __x($@)
357 68 ▶{ my ($cpkg, $fn, $linenr) = caller;
Subroutine __x: called CC=2 SCAR=7.0
358 68 ▶ @_%2 or error __x"even length parameter list for __x at {where}", where => "$fn line $linenr";
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 or &error(&__x("even length parameter list for __x at {where}", "where", "$fn line $linenr")) 68 0 68
359  
360 68 my $msgid = shift;
361 68 $lrm->new(_msgid => $msgid, _expand => 1, _domain => pkg2domain($cpkg), _use => "$fn line $linenr", @_);
362 }
363  
364  
365 sub __n($$$@)
366 4 ▶{ my ($single, $plural, $count) = (shift, shift, shift);
Subroutine __n: called CC=1 SCAR=0.0
367 4 my ($cpkg, $fn, $linenr) = caller;
368 4 $lrm->new(_msgid => $single, _plural => $plural, _count => $count, _domain => pkg2domain($cpkg),
369 _use => "$fn line $linenr" , @_);
370 }
371  
372  
373 sub __nx($$$@)
374 13 ▶{ my ($single, $plural, $count) = (shift, shift, shift);
Subroutine __nx: called CC=1 SCAR=0.0
375 13 my ($cpkg, $fn, $linenr) = caller;
376 13 $lrm->new(_msgid => $single, _plural => $plural, _count => $count, _expand => 1,
377 _domain => pkg2domain($cpkg), _use => "$fn line $linenr", @_);
378 }
379  
380  
381 sub __xn($$$@) # repeated for prototype
382 3 ▶{ my ($single, $plural, $count) = (shift, shift, shift);
Subroutine __xn: called CC=1 SCAR=0.0
383 3 my ($cpkg, $fn, $linenr) = caller;
384 3 $lrm->new(_msgid => $single, _plural => $plural, _count => $count, _expand => 1,
385 _domain => pkg2domain($cpkg), _use => "$fn line $linenr", @_);
386 }
387  
388  
389 13 ▶sub N__($) { $_[0] }
Subroutine N__: called CC=1 SCAR=0.0
Pod documented
390  
391  
392 1 ▶sub N__n($$) {@_}
Subroutine N__n: called CC=1 SCAR=0.0
Pod documented
393  
394  
395 0 ▶sub N__w(@) {split " ", $_[0]}
Subroutine N__w: not called CC=1 SCAR=6.9
Pod documented
396  
397  
398 #--------------------
399  
400 0 ▶sub __p($$) { __($_[0])->_msgctxt($_[1]) }
Subroutine __p: not called CC=1 SCAR=6.9
401  
402 sub __px($$@)
403 0 ▶{ my ($ctxt, $msgid) = (shift, shift);
Subroutine __px: not called CC=1 SCAR=6.9
404 0 __x($msgid, @_)->_msgctxt($ctxt);
405 }
406  
407 sub __np($$$$)
408 0 ▶{ my ($ctxt, $msgid, $plural, $count) = @_;
Subroutine __np: not called CC=1 SCAR=6.9
409 0 __n($msgid, $msgid, $plural, $count)->_msgctxt($ctxt);
410 }
411  
412 sub __npx($$$$@)
413 0 ▶{ my ($ctxt, $msgid, $plural, $count) = splice @_, 0, 4;
Subroutine __npx: not called CC=1 SCAR=6.9
414 0 __nx($msgid, $msgid, $plural, $count, @_)->_msgctxt($ctxt);
415 }
416  
417 #--------------------
418  
419 sub import(@)
420 142 ▶{ my $class = shift;
Subroutine import: called CC=21 SCAR=41.8
421  
422 142 ▶ if($INC{'Log/Report/Minimal.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Log/Report/Minimal.pm"}) 0 142 142
423 0 { my ($pkg, $fn, $line) = caller; # do not report on LR:: modules
424 0 ▶ if(index($pkg, 'Log::Report::') != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index($pkg, "Log::Report::") != 0) 0 0 0
425 { # @pkgs empty during release testings of L::R distributions
426 0 my @pkgs = Log::Report::Optional->usedBy;
427 0 ▶ die "Log::Report loaded too late in $fn line $line, put in $pkg before ", (join ',', @pkgs) if @pkgs;
Branch: 1 branch
BranchTrueFalseTotal
if @pkgs 0 0 0
428 }
429 }
430  
431 142 ▶ my $to_level = ($_[0] && $_[0] =~ m/^\+\d+$/ ? shift : undef) || 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $_[0] =~ /^\+\d+$/ ? : 0 142 142
Condition60%3/5
($_[0] && $_[0] =~ /^\+\d+$/ ? shift() : undef) || 0
l!l
0142
$_[0] and $_[0] =~ /^\+\d+$/
!ll&&!rl&&r
161260
MC/DC0%0/2
($_[0] && $_[0] =~ /^\+\d+$/ ? shift() : undef) || 0
$_[0] $_[0] =~ /^\+\d+$/
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ /^\+\d+$/
ABresult
111
0X0
100
432 142 ▶ my $textdomain = @_%2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 132 10 142
433 142 my %opts = @_;
434  
435 142 my ($pkg, $fn, $linenr) = caller $to_level;
436  
437 ### Log::Report options
438  
439 142 ▶ if(my $msg_class = delete $opts{message_class})
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg_class = delete $opts{"message_class"}) 0 142 142
440 0 ▶ { $msg_class->isa($lrm)
Branch: 1 branch
BranchTrueFalseTotal
$msg_class->isa($lrm) or &error(&__x("message_class {class} does not extend {base}", "base", $lrm, "class", $msg_class)) 0 0 0
441 or error __x"message_class {class} does not extend {base}", base => $lrm, class => $msg_class;
442 0 $lrm = $msg_class;
443 }
444  
445 142 ▶ if(exists $opts{mode})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"mode"}) 0 142 142
446 0 ▶ { $default_mode = delete $opts{mode} || 0;
Condition0%0/2
delete $opts{'mode'} || 0
l!l
00
MC/DC0%0/1
delete $opts{'mode'} || 0
delete $opts{'mode'}
Truth table0%0/2
A
A= delete $opts{'mode'}
Aresult
11
00
447 0 Log::Report::Dispatcher->defaultMode($default_mode);
448 0 dispatcher mode => $default_mode, 'ALL';
449 }
450  
451 142 my @export;
452 142 ▶ if(my $in = delete $opts{import})
Branch: 1 branch
BranchTrueFalseTotal
if (my $in = delete $opts{'import'}) { } 125 17 142
453 125 ▶ { push @export, ref $in eq 'ARRAY' ? @$in : $in;
Branch: 1 branch
BranchTrueFalseTotal
ref $in eq 'ARRAY' ? : 125 0 125
454 }
455 else
456 17 { push @export, @functions, @make_msg;
457  
458 17 ▶ my $syntax = delete $opts{syntax} || 'SHORT';
Condition100%2/2
delete $opts{'syntax'} || 'SHORT'
l!l
512
MC/DC100%1/1
delete $opts{'syntax'} || 'SHORT'
delete $opts{'syntax'}
Truth table100%2/2
A
A= delete $opts{'syntax'}
Aresult
11
00
459 17 ▶ if($syntax eq 'SHORT')
Branch: 2 branches
BranchTrueFalseTotal
if ($syntax eq 'SHORT') { } 17 0 17
elsif ($syntax ne 'REPORT' and $syntax ne 'LONG') { } 0 0 0
460 17 { push @export, @reason_functions
461 }
462 ▶ elsif($syntax ne 'REPORT' && $syntax ne 'LONG')
Condition0%0/3
$syntax ne "REPORT" and $syntax ne "LONG"
!ll&&!rl&&r
000
MC/DC0%0/2
$syntax ne "REPORT" and $syntax ne "LONG"
$syntax ne "REPORT" $syntax ne "LONG"
Truth table0%0/3
A and B
A= $syntax ne "REPORT"B= $syntax ne "LONG"
ABresult
0X0
100
111
463 0 { error __x"syntax flag must be either SHORT or REPORT, not `{flag}' in {fn} line {line}", flag => $syntax, fn => $fn, line => $linenr;
464 }
465 }
466  
467 142 $class->export_to_level(1+$to_level, undef, @export);
468  
469 ### Log::Report::Domain configuration
470  
471 142 ▶ if(defined $textdomain)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $textdomain) { } 126 16 142
elsif (keys %opts) { } 0 16 16
472 126 { pkg2domain $pkg, $textdomain, $fn, $linenr;
473 126 my $domain = textdomain $textdomain;
474 126 ▶ $domain->configure(%opts, where => [$pkg, $fn, $linenr ]) if keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if keys %opts 0 126 126
475 }
476 elsif(keys %opts)
477 0 { error __x"no domain for configuration options in {fn} line {line}", fn => $fn, line => $linenr;
478 }
479 }
480  
481  
482 0 ▶sub default_dispatcher_mode() { $default_mode }
Subroutine default_dispatcher_mode: not called CC=1 SCAR=6.9
Pod documented
483  
484 # deprecated, since we have a ::Domain object in 1.00
485 sub translator($;$$$$)
486 0 ▶{ # replaced by (textdomain $domain)->configure
Subroutine translator: not called CC=5 SCAR=34.0
Pod documented
487  
488 0 my ($class, $name) = (shift, shift);
489 0 ▶ my $domain = textdomain $name
Branch: 1 branch
BranchTrueFalseTotal
my $domain = &textdomain($name) or &error(&__x("textdomain `{domain}' for translator not defined", "domain", $name)) 0 0 0
490 or error __x"textdomain `{domain}' for translator not defined", domain => $name;
491  
492 0 ▶ @_ or return $domain->translator;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $domain->translator 0 0 0
493  
494 0 my ($translator, $pkg, $fn, $line) = @_;
495 0 ▶ ($pkg, $fn, $line) = caller # direct call, not via import
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pkg 0 0 0
496 unless defined $pkg;
497  
498 0 ▶ $translator->isa('Log::Report::Translator')
Branch: 1 branch
BranchTrueFalseTotal
$translator->isa("Log::Report::Translator") or &error(&__x("translator must be a {pkg} object for {domain}", "pkg", "Log::Report::Translator", "domain", $name)) 0 0 0
499 or error __x"translator must be a {pkg} object for {domain}", pkg => 'Log::Report::Translator', domain => $name;
500  
501 0 $domain->configure(translator => $translator, where => [$pkg, $fn, $line]);
502 }
503  
504  
505 sub textdomain(@)
506 238 ▶{
Subroutine textdomain: called CC=11 SCAR=34.2
Pod documented
507 238 ▶ if(@_==1 && blessed $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and builtin::blessed($_[0])) 0 238 238
Condition33%1/3
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
02380
MC/DC0%0/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1 builtin::blessed($_[0])
Truth table33%1/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
ABresult
0X0
100
111
508 0 { my $domain = shift;
509 0 ▶ $domain->isa('Log::Report::Domain') or panic;
Branch: 1 branch
BranchTrueFalseTotal
$domain->isa("Log::Report::Domain") or &panic() 0 0 0
510 0 return $reporter->{textdomains}{$domain->name} = $domain;
511 }
512  
513 238 ▶ if(@_==2)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 238 238
514 { # used for 'maintenance' and testing
515 0 ▶ return delete $reporter->{textdomains}{$_[0]} if $_[1] eq 'DELETE';
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "DELETE" 0 0 0
516 0 ▶ return $reporter->{textdomains}{$_[0]} if $_[1] eq 'EXISTS';
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "EXISTS" 0 0 0
517 }
518  
519 238 ▶ my $name = (@_%2 ? shift : pkg2domain((caller)[0])) || 'default';
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 238 0 238
Condition100%2/2
(@_ % 2 ? shift() : &pkg2domain(scalar((caller)[0]))) || 'default'
l!l
22810
MC/DC100%1/1
(@_ % 2 ? shift() : &pkg2domain(scalar((caller)[0]))) || 'default'
(@_ % 2 ? shift() : &pkg2domain(scalar((caller)[0])))
Truth table100%2/2
A
A= (@_ % 2 ? shift() : &pkg2domain(scalar((caller)[0])))
Aresult
11
00
520 238 ▶ my $domain = $reporter->{textdomains}{$name} ||= Log::Report::Domain->new(name => $name);
Condition66%2/3
$reporter->{"textdomains"}{$name} ||= "Log::Report::Domain"->new("name", $name)
l!l&&r!l&&!r
210280
MC/DC0%0/2
$reporter->{"textdomains"}{$name} ||= "Log::Report::Domain"->new("name", $name)
$reporter->{"textdomains"}{$name} "Log::Report::Domain"->new("name", $name)
Truth table66%2/3
A ||= B
A= $reporter->{"textdomains"}{$name}B= "Log::Report::Domain"->new("name", $name)
ABresult
1X1
011
000
521 238 ▶ $domain->configure(@_, where => [caller]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 238 238
522 238 $domain;
523 }
524  
525 #--------------------
526  
527 sub needs(@)
528 0 ▶{ my $thing = shift;
Subroutine needs: not called CC=2 SCAR=17.9
Pod documented
529 0 ▶ my $self = ref $thing ? $thing : $reporter;
Branch: 1 branch
BranchTrueFalseTotal
ref $thing ? : 0 0 0
530 0 ▶ first {$self->{needs}{$_}} @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
531 }
532  
533 #--------------------
534  
535 17 1;
↑ Summary Log/Report/Domain.pm »