Dispatch/Output.pm

filter: Statementstmt 83.7% 72 / 86 Branchbran 60.0% 12 / 20 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 85.7% 18 / 21 Podpod 83.3% 5 / 6 totaltotal 77.0% 114 / 148 CC 14 SCAR 15.2
CC
14
Cov
77%
_level_as_name
20.0
new
6.0
_basic_init
5.0
CRAP 4.6
« Dispatch/File.pm ↑ Summary Dispatch/Email.pm »
1 package Log::Dispatch::Output;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 our $VERSION = '2.71';
7  
8 33 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Log::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use Log::Dispatch::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 ▶use Log::Dispatch::Vars qw( @OrderedLevels );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 33 ▶use base qw( Log::Dispatch::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
18 0 my $proto = shift;
19 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
20  
21 0 die "The new method must be overridden in the $class subclass";
22 }
23  
24 {
25 33 my $validator = validation_for(
26 params => {
27 level => { type => t('LogLevel') },
28  
29 # Pre-PVC we accepted empty strings, which is weird, but we don't
30 # want to break back-compat. See
31 # https://github.com/houseabsolute/Log-Dispatch/issues/38.
32 message => { type => t('Str') },
33 },
34 slurpy => 1,
35 );
36  
37 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
38 3 ▶ sub log {
Subroutine log: called CC=3 SCAR=11.2
Pod documented
39 3 my $self = shift;
40 3 my %p = $validator->(@_);
41  
42 3 my $level_num = $self->_level_as_number( $p{level} );
43 3 ▶ return unless $self->_should_log($level_num);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_log($level_num) 0 3 3
44  
45 3 local $! = undef;
46 $p{message} = $self->_apply_callbacks(%p)
47 3 ▶ if $self->{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"} 0 3 3
48  
49 3 $self->log_message(%p);
50 }
51  
52 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
53 136 ▶ sub _log_with_num {
Subroutine _log_with_num: called CC=3 SCAR=11.0
54 136 my $self = shift;
55 136 my $level_num = shift;
56 136 my %p = @_;
57  
58 136 ▶ return unless $self->_should_log($level_num);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_log($level_num) 1 135 136
59  
60 135 local $! = undef;
61 $p{message} = $self->_apply_callbacks(%p)
62 135 ▶ if $self->{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"} 75 60 135
63  
64 135 $self->log_message(%p);
65 }
66 ## use critic
67 }
68  
69 {
70 33 my $validator = validation_for(
71 params => {
72 name => {
73 type => t('NonEmptyStr'),
74 optional => 1,
75 },
76 min_level => { type => t('LogLevel') },
77 max_level => {
78 type => t('LogLevel'),
79 optional => 1,
80 },
81 callbacks => {
82 type => t('Callbacks'),
83 optional => 1,
84 },
85 newline => {
86 type => t('Bool'),
87 default => 0,
88 },
89 },
90  
91 # This is primarily here for the benefit of outputs outside of this
92 # distro which may be passing who-knows-what to this method.
93 slurpy => 1,
94 );
95  
96 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
97 111 ▶ sub _basic_init {
Subroutine _basic_init: called CC=5 SCAR=16.1
98 111 my $self = shift;
99 111 my %p = $validator->(@_);
100  
101 111 $self->{level_names} = \@OrderedLevels;
102  
103 111 ▶ $self->{name} = $p{name} || $self->_unique_name();
Condition66%2/3
$p{'name'} || $self->_unique_name
l!l&&r!l&&!r
57540
MC/DC0%0/2
$p{'name'} || $self->_unique_name
$p{'name'} $self->_unique_name
Truth table66%2/3
A || B
A= $p{'name'}B= $self->_unique_name
ABresult
1X1
011
000
104  
105 111 $self->{min_level} = $self->_level_as_number( $p{min_level} );
106  
107 # Either use the parameter supplied or just the highest possible level.
108 $self->{max_level} = (
109 exists $p{max_level}
110 ? $self->_level_as_number( $p{max_level} )
111 111 ▶ : $#{ $self->{level_names} }
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'max_level'} ? : 16 95 111
112 );
113  
114 111 ▶ $self->{callbacks} = $p{callbacks} if $p{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"callbacks"} 2 109 111
115  
116 111 ▶ if ( $p{newline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"newline"}) 61 50 111
117 61 push @{ $self->{callbacks} }, \&_add_newline_callback;
118 }
119 }
120 }
121  
122 33 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
123 222 my $self = shift;
124  
125 222 return $self->{name};
126 }
127  
128 0 ▶sub min_level {
Subroutine min_level: not called CC=1 SCAR=6.9
Pod documented
129 0 my $self = shift;
130  
131 0 return $self->{level_names}[ $self->{min_level} ];
132 }
133  
134 1 ▶sub max_level {
Subroutine max_level: called CC=1 SCAR=0.0
Pod documented
135 1 my $self = shift;
136  
137 1 return $self->{level_names}[ $self->{max_level} ];
138 }
139  
140 1 ▶sub accepted_levels {
Subroutine accepted_levels: called CC=1 SCAR=0.0
Pod documented
141 1 my $self = shift;
142  
143 1 return @{ $self->{level_names} }
144 1 [ $self->{min_level} .. $self->{max_level} ];
145 }
146  
147 372 ▶sub _should_log {
Subroutine _should_log: called CC=2 SCAR=6.9
148 372 my $self = shift;
149 372 my $level_num = shift;
150  
151 return ( ( $level_num >= $self->{min_level} )
152 372 ▶ && ( $level_num <= $self->{max_level} ) );
Condition100%3/3
$level_num >= $self->{'min_level'} && $level_num <= $self->{'max_level'}
!ll&&!rl&&r
4649277
MC/DC100%2/2
$level_num >= $self->{'min_level'} && $level_num <= $self->{'max_level'}
$level_num >= $self->{'min_level'} $level_num <= $self->{'max_level'}
Truth table100%3/3
A && B
A= $level_num >= $self->{'min_level'}B= $level_num <= $self->{'max_level'}
ABresult
0X0
100
111
153 }
154  
155 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
156 0 ▶sub _level_as_name {
Subroutine _level_as_name: not called CC=4 SCAR=30.0
157 0 my $self = shift;
158 0 my $level = shift;
159  
160 0 ▶ unless ( defined $level ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level) 0 0 0
161 0 Carp::croak 'undefined value provided for log level';
162 }
163  
164 0 my $canonical_level;
165 0 ▶ unless ( $canonical_level = Log::Dispatch->level_is_valid($level) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($canonical_level = "Log::Dispatch"->level_is_valid($level)) 0 0 0
166 0 Carp::croak "$level is not a valid Log::Dispatch log level";
167 }
168  
169 0 ▶ return $canonical_level unless $level =~ /\A[0-7]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $level =~ /\A[0-7]+\z/ 0 0 0
170  
171 0 return $self->{level_names}[$level];
172 }
173 ## use critic
174  
175 33 my $_unique_name_counter = 0;
176  
177 54 ▶sub _unique_name {
Subroutine _unique_name: called CC=1 SCAR=0.0
178 54 my $self = shift;
179  
180 54 return '_anon_' . $_unique_name_counter++;
181 }
182  
183 73 ▶sub _add_newline_callback {
Subroutine _add_newline_callback: called CC=1 SCAR=0.0
184  
185 # This weird construct is an optimization since this might be called a lot
186 # - see https://github.com/autarch/Log-Dispatch/pull/7
187 73 +{@_}->{message} . "\n";
188 }
189  
190 33 1;
191  
192 # ABSTRACT: Base class for all Log::Dispatch::* objects
193  
194 __END__
« Dispatch/File.pm ↑ Summary Dispatch/Email.pm »