Reporter.pm

filter: Statementstmt 90.1% 101 / 112 Branchbran 83.3% 40 / 48 Conditioncond 43.7% 7 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 90.9% 20 / 22 Podpod 92.3% 12 / 13 totaltotal 81.9% 181 / 221 SCAR 35.2
CC
30
Cov
84%
log
10.1
report
7.1
notImplemented
6.0
CRAP 33.6
« Box/Parser.pm ↑ Summary Message/Head.pm »
1 # Copyrights 2001-2023 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution Mail-Message. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Reporter;
10 56 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 56 $VERSION = '3.013';
12  
13  
14 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 56 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 56 ▶use Scalar::Util 'dualvar';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 56 my @levelname = (undef, qw(DEBUG NOTICE PROGRESS WARNING ERROR NONE INTERNAL));
22  
23 56 my %levelprio = (ERRORS => 5, WARNINGS => 4, NOTICES => 2);
24 56 ▶for(my $l = 1; $l < @levelname; $l++)
Branch: 1 branch
BranchTrueFalseTotal
if ($l < @levelname) 392 56 448
25 392 { $levelprio{$levelname[$l]} = $l;
26 392 $levelprio{$l} = $l;
27 }
28  
29 sub new(@)
30 863 ▶{ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31 #confess "Parameter list has odd length: @_" if @_ % 2;
32 863 (bless {MR_log => 1, MR_trace => 1}, $class)->init({@_});
33 }
34  
35 56 my($default_log, $default_trace, $trace_callback);
36 sub init($)
37 845 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=3 SCAR=11.2
Pod undocumented
38 845 ▶ $self->{MR_log} = $levelprio{$args->{log} || $default_log};
Condition66%2/3
$args->{'log'} || $default_log
l!l&&r!l&&!r
2356100
MC/DC0%0/2
$args->{'log'} || $default_log
$args->{'log'} $default_log
Truth table66%2/3
A || B
A= $args->{'log'}B= $default_log
ABresult
1X1
011
000
39 845 ▶ $self->{MR_trace} = $levelprio{$args->{trace} || $default_trace};
Condition66%2/3
$args->{'trace'} || $default_trace
l!l&&r!l&&!r
2356100
MC/DC0%0/2
$args->{'trace'} || $default_trace
$args->{'trace'} $default_trace
Truth table66%2/3
A || B
A= $args->{'trace'}B= $default_trace
ABresult
1X1
011
000
40 845 $self;
41 }
42  
43 #------------------------------------------
44  
45  
46 sub _trace_warn($$$)
47 2 ▶{ my ($who, $level, $text) = @_;
Subroutine _trace_warn: called CC=1 SCAR=0.0
48 2 warn "$level: $text\n";
49 }
50  
51 sub defaultTrace(;$$)
52 61 ▶{ my $thing = shift;
Subroutine defaultTrace: called CC=5 SCAR=16.1
Pod documented
53  
54 61 ▶ return ($default_log, $default_trace)
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 60 61
55 unless @_;
56  
57 60 my $level = shift;
58 60 ▶ my $prio = $thing->logPriority($level)
Branch: 1 branch
BranchTrueFalseTotal
my $prio = $thing->logPriority($level) or croak("Unknown trace-level $level.") 0 60 60
59 or croak "Unknown trace-level $level.";
60  
61 60 ▶ if( ! @_)
Branch: 2 branches
BranchTrueFalseTotal
if (not @_) { } 57 3 60
elsif (ref $_[0]) { } 1 2 3
62 57 { $default_log = $default_trace = $prio;
63 57 $trace_callback = \&_trace_warn;
64 }
65 elsif(ref $_[0])
66 1 { $default_log = $thing->logPriority('NONE');
67 1 $default_trace = $prio;
68 1 $trace_callback = shift;
69 }
70 else
71 2 { $default_log = $prio;
72 2 $default_trace = $thing->logPriority(shift);
73 2 $trace_callback = \&_trace_warn;
74 }
75  
76 60 ($default_log, $default_trace);
77 }
78  
79 56 __PACKAGE__->defaultTrace('WARNINGS');
80  
81 #------------------------------------------
82  
83  
84 sub trace(;$$)
85 4 ▶{ my $self = shift;
Subroutine trace: called CC=3 SCAR=11.0
Pod documented
86  
87 return $self->logPriority($self->{MR_trace})
88 4 ▶ unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 1 4
89  
90 1 my $level = shift;
91 1 ▶ my $prio = $levelprio{$level}
Branch: 1 branch
BranchTrueFalseTotal
my $prio = $levelprio{$level} or croak("Unknown trace-level $level.") 0 1 1
92 or croak "Unknown trace-level $level.";
93  
94 1 $self->{MR_trace} = $prio;
95 }
96  
97 #------------------------------------------
98  
99  
100 # Implementation detail: the Mail::Box::Parser::C code avoids calls back
101 # to Perl by checking the trace-level itself. In the perl code of this
102 # module however, just always call the log() method, and let it check
103 # whether or not to display it.
104  
105 sub log(;$@)
106 57 ▶{ my $thing = shift;
Subroutine log: called CC=10 SCAR=23.2
Pod documented
107  
108 57 ▶ if(ref $thing) # instance call
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing) { } 55 2 57
109 { return $thing->logPriority($thing->{MR_log})
110 55 ▶ unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 52 55
111  
112 52 my $level = shift;
113 52 ▶ my $prio = $levelprio{$level}
Branch: 1 branch
BranchTrueFalseTotal
my $prio = $levelprio{$level} or croak("Unknown log-level $level") 0 52 52
114 or croak "Unknown log-level $level";
115  
116 52 ▶ return $thing->{MR_log} = $prio
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 52 52
117 unless @_;
118  
119 52 my $text = join '', @_;
120 $trace_callback->($thing, $level, $text)
121 52 ▶ if $prio >= $thing->{MR_trace};
Branch: 1 branch
BranchTrueFalseTotal
if $prio >= $thing->{"MR_trace"} 2 50 52
122 56 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 52 ▶$thing->{MR_trace} or confess;
Branch: 1 branch
BranchTrueFalseTotal
$thing->{"MR_trace"} or confess() 0 52 52
124  
125 6 push @{$thing->{MR_report}[$prio]}, $text
126 52 ▶ if $prio >= $thing->{MR_log};
Branch: 1 branch
BranchTrueFalseTotal
if $prio >= $thing->{"MR_log"} 6 46 52
127 }
128 else # class method
129 2 { my $level = shift;
130 2 ▶ my $prio = $levelprio{$level}
Branch: 1 branch
BranchTrueFalseTotal
my $prio = $levelprio{$level} or croak("Unknown log-level $level") 0 2 2
131 or croak "Unknown log-level $level";
132  
133 2 ▶ $trace_callback->($thing, $level, join('',@_))
Branch: 1 branch
BranchTrueFalseTotal
if $prio >= $default_trace 1 1 2
134 if $prio >= $default_trace;
135 }
136  
137 54 $thing;
138 }
139  
140  
141 #------------------------------------------
142  
143  
144 sub report(;$)
145 11 ▶{ my $self = shift;
Subroutine report: called CC=7 SCAR=19.6
Pod documented
146 11 ▶ my $reports = $self->{MR_report} || return ();
Condition50%1/2
$self->{'MR_report'} || (return ())
l!l
110
MC/DC0%0/1
$self->{'MR_report'} || (return ())
$self->{'MR_report'}
Truth table50%1/2
A
A= $self->{'MR_report'}
Aresult
11
00
147  
148 11 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 4 11
149 7 { my $level = shift;
150 7 ▶ my $prio = $levelprio{$level}
Branch: 1 branch
BranchTrueFalseTotal
my $prio = $levelprio{$level} or croak("Unknown report level $level.") 0 7 7
151 or croak "Unknown report level $level.";
152  
153 7 ▶ return $reports->[$prio] ? @{$reports->[$prio]} : ();
Branch: 1 branch
BranchTrueFalseTotal
$reports->[$prio] ? : 7 0 7
154 }
155  
156 4 my @reports;
157 4 ▶ for(my $prio = 1; $prio < @$reports; $prio++)
Branch: 1 branch
BranchTrueFalseTotal
if ($prio < @$reports) 20 4 24
158 20 ▶ { next unless $reports->[$prio];
Branch: 1 branch
BranchTrueFalseTotal
unless $reports->[$prio] 11 9 20
159 9 my $level = $levelname[$prio];
160 9 push @reports, map { [ $level, $_ ] } @{$reports->[$prio]};
161 }
162  
163 4 @reports;
164 }
165  
166 #-------------------------------------------
167  
168  
169 sub addReport($)
170 13 ▶{ my ($self, $other) = @_;
Subroutine addReport: called CC=4 SCAR=13.9
Pod documented
171 13 ▶ my $reports = $other->{MR_report} || return ();
Condition100%2/2
$other->{'MR_report'} || (return ())
l!l
112
MC/DC100%1/1
$other->{'MR_report'} || (return ())
$other->{'MR_report'}
Truth table100%2/2
A
A= $other->{'MR_report'}
Aresult
11
00
172  
173 1 ▶ for(my $prio = 1; $prio < @$reports; $prio++)
Branch: 1 branch
BranchTrueFalseTotal
if ($prio < @$reports) 5 1 6
174 5 ▶ { push @{$self->{MR_report}[$prio]}, @{$reports->[$prio]}
Branch: 1 branch
BranchTrueFalseTotal
if exists $reports->[$prio] 3 2 5
175 if exists $reports->[$prio];
176 }
177 1 $self;
178 }
179
180 #-------------------------------------------
181  
182  
183 sub reportAll(;$)
184 2 ▶{ my $self = shift;
Subroutine reportAll: called CC=1 SCAR=0.0
Pod documented
185 2 map { [ $self, @$_ ] } $self->report(@_);
186 }
187  
188 #-------------------------------------------
189  
190  
191 1 ▶sub errors(@) {shift->report('ERRORS')}
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
192  
193 #-------------------------------------------
194  
195  
196 1 ▶sub warnings(@) {shift->report('WARNINGS')}
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
197  
198 #-------------------------------------------
199  
200  
201 sub notImplemented(@)
202 0 ▶{ my $self = shift;
Subroutine notImplemented: not called CC=2 SCAR=17.9
Pod documented
203 0 ▶ my $package = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
204 0 my $sub = (caller 1)[3];
205  
206 0 $self->log(ERROR => "Package $package does not implement $sub.");
207 0 confess "Please warn the author, this shouldn't happen.";
208 }
209  
210 #------------------------------------------
211  
212  
213 sub logPriority($)
214 74 ▶{ my $level = $levelprio{$_[1]} or return undef;
Subroutine logPriority: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $level = $levelprio{$_[1]} or return undef 2 72 74
215 72 dualvar $level, $levelname[$level];
216 }
217  
218 #-------------------------------------------
219  
220  
221 sub logSettings()
222 173 ▶{ my $self = shift;
Subroutine logSettings: called CC=1 SCAR=0.0
Pod documented
223 173 (log => $self->{MR_log}, trace => $self->{MR_trace});
224 }
225  
226 #-------------------------------------------
227  
228  
229 sub AUTOLOAD(@)
230 0 ▶{ my $thing = shift;
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
231 0 our $AUTOLOAD;
232 0 ▶ my $class = ref $thing || $thing;
Condition0%0/3
ref $thing || $thing
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $thing || $thing
ref $thing $thing
Truth table0%0/3
A || B
A= ref $thingB= $thing
ABresult
1X1
011
000
233 0 (my $method = $AUTOLOAD) =~ s/^.*\:\://;
234  
235 0 $Carp::MaxArgLen=20;
236 0 confess "Method $method() is not defined for a $class.\n";
237 }
238  
239 #-------------------------------------------
240  
241  
242 #-------------------------------------------
243  
244  
245 3717 ▶sub DESTROY {shift}
Subroutine DESTROY: called CC=1 SCAR=0.0
246  
247 56 1;
« Box/Parser.pm ↑ Summary Message/Head.pm »