Dispatch.pm

filter: Statementstmt 84.6% 127 / 150 Branchbran 75.0% 30 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 72.0% 31 / 43 Podpod 50.0% 12 / 24 totaltotal 74.9% 203 / 271 CC 30 SCAR 11.3
CC
30
Cov
80%
_log_to
12.0
new
7.0
log_to
6.0
CRAP 3.1
« Dispatch/Syslog.pm ↑ Summary Dispatch/ApacheLog.pm »
1 package Log::Dispatch;
2  
3 33 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 33 our $VERSION = '2.71';
9  
10 33 ▶use Carp ();
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( %CanonicalLevelNames %LevelNamesToNumbers );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶use Module::Runtime qw( use_package_optimistically );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 33 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 33 ▶use base qw( Log::Dispatch::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 33 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
19 33 for my $l ( keys %CanonicalLevelNames ) {
20 396 my $level_num = $LevelNamesToNumbers{$l};
21 176 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
22 176 my $self = shift;
23 $self->_log_with_num(
24 $level_num,
25 176 ▶ level => $CanonicalLevelNames{$l},
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 97 79 176
26 message => @_ > 1 ? "@_" : $_[0],
27 );
28 396 };
29  
30 ## no critic (TestingAndDebugging::ProhibitNoStrict)
31 33 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 396 *{$l} = $sub;
33 }
34 }
35  
36 {
37 33 my $validator = validation_for(
38 params => {
39 outputs => {
40 type => t('ArrayRef'),
41 optional => 1,
42 },
43 callbacks => {
44 type => t('Callbacks'),
45 optional => 1,
46 },
47 },
48 );
49  
50 104 ▶ sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
51 104 my $class = shift;
52 104 my %p = $validator->(@_);
53  
54 103 my $self = bless {}, $class;
55  
56 $self->{callbacks} = $p{callbacks}
57 103 ▶ if $p{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"callbacks"} 6 97 103
58  
59 103 ▶ if ( my $outputs = $p{outputs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $outputs = $p{"outputs"}) 66 37 103
60 66 ▶ if ( ref $outputs->[1] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $outputs->[1] eq 'HASH') { } 1 65 66
61  
62 # 2.23 API
63 # outputs => [
64 # File => { min_level => 'debug', filename => 'logfile' },
65 # Screen => { min_level => 'warning' }
66 # ]
67 1 ▶ while ( my ( $class, $params ) = splice @$outputs, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$outputs, 0, 2)) 2 1 3
68 2 $self->_add_output( $class, %$params );
69 }
70 }
71 else {
72  
73 # 2.24+ syntax
74 # outputs => [
75 # [ 'File', min_level => 'debug', filename => 'logfile' ],
76 # [ 'Screen', min_level => 'warning' ]
77 # ]
78 65 for my $arr ( @{$outputs} ) {
79 67 ▶ die "expected arrayref, not '$arr'"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arr eq "ARRAY" 1 66 67
80 unless ref $arr eq 'ARRAY';
81 66 $self->_add_output( @{$arr} );
82 }
83 }
84 }
85  
86 102 return $self;
87 }
88 }
89  
90 33 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.2
Pod documented
91 1 my $self = shift;
92  
93 my %clone = (
94 1 ▶ callbacks => [ @{ $self->{callbacks} || [] } ],
Condition50%1/2
$self->{'callbacks'} || []
l!l
10
MC/DC0%0/1
$self->{'callbacks'} || []
$self->{'callbacks'}
Truth table50%1/2
A
A= $self->{'callbacks'}
Aresult
11
00
95 1 ▶ outputs => { %{ $self->{outputs} || {} } },
Condition50%1/2
$self->{'outputs'} || {}
l!l
10
MC/DC0%0/1
$self->{'outputs'} || {}
$self->{'outputs'}
Truth table50%1/2
A
A= $self->{'outputs'}
Aresult
11
00
96 );
97  
98 1 return bless \%clone, ref $self;
99 }
100  
101 68 ▶sub _add_output {
Subroutine _add_output: called CC=2 SCAR=6.9
102 68 my $self = shift;
103 68 my $class = shift;
104  
105 68 ▶ my $full_class
Branch: 1 branch
BranchTrueFalseTotal
substr($class, 0, 1) eq '+' ? : 2 66 68
106 = substr( $class, 0, 1 ) eq '+'
107 ? substr( $class, 1 )
108 : "Log::Dispatch::$class";
109  
110 68 use_package_optimistically($full_class);
111  
112 68 $self->add( $full_class->new(@_) );
113 }
114  
115 108 ▶sub add {
Subroutine add: called CC=3 SCAR=12.7
Pod documented
116 108 my $self = shift;
117 108 my $object = shift;
118  
119 # Once 5.6 is more established start using the warnings module.
120 108 ▶ if ( exists $self->{outputs}{ $object->name } && $^W ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"outputs"}{$object->name} and $^W) 0 108 108
Condition33%1/3
exists $self->{"outputs"}{$object->name} and $^W
!ll&&!rl&&r
10800
MC/DC0%0/2
exists $self->{"outputs"}{$object->name} and $^W
exists $self->{"outputs"}{$object->name} $^W
Truth table33%1/3
A and B
A= exists $self->{"outputs"}{$object->name}B= $^W
ABresult
0X0
100
111
121 0 Carp::carp(
122 'Log::Dispatch::* object ', $object->name,
123 ' already exists.'
124 );
125 }
126  
127 108 $self->{outputs}{ $object->name } = $object;
128 }
129  
130 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
131 0 my $self = shift;
132 0 my $name = shift;
133  
134 0 return delete $self->{outputs}{$name};
135 }
136  
137 3 ▶sub outputs {
Subroutine outputs: called CC=1 SCAR=0.0
Pod documented
138 3 my $self = shift;
139  
140 3 return values %{ $self->{outputs} };
141 }
142  
143 3 ▶sub callbacks {
Subroutine callbacks: called CC=1 SCAR=0.0
Pod documented
144 3 my $self = shift;
145  
146 3 return @{ $self->{callbacks} };
147 }
148  
149 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
150 48 ▶sub log {
Subroutine log: called CC=2 SCAR=7.0
Pod documented
151 48 my $self = shift;
152 48 my %p = @_;
153  
154 48 my $level_num = $self->_level_as_number( $p{level} );
155 47 ▶ return unless defined $level_num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level_num 0 47 47
156  
157 47 return $self->_log_with_num( $level_num, %p );
158  
159 }
160 ## use critic
161  
162 223 ▶sub _log_with_num {
Subroutine _log_with_num: called CC=3 SCAR=11.0
163 223 my $self = shift;
164 223 my $level_num = shift;
165 223 my %p = @_;
166  
167 223 ▶ return unless $self->_would_log($level_num);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_would_log($level_num) 91 132 223
168  
169 132 $p{message} = $self->_prepare_message(%p);
170 132 $_->_log_with_num( $level_num, %p ) for values %{ $self->{outputs} };
171  
172 132 return;
173 }
174  
175 134 ▶sub _prepare_message {
Subroutine _prepare_message: called CC=3 SCAR=11.0
176 134 my $self = shift;
177 134 my %p = @_;
178  
179 $p{message} = $p{message}->()
180 134 ▶ if ref $p{message} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $p{"message"} eq "CODE" 3 131 134
181  
182 $p{message} = $self->_apply_callbacks(%p)
183 134 ▶ if $self->{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"} 15 119 134
184  
185 134 return $p{message};
186 }
187  
188 2 ▶sub _log_to_outputs {
Subroutine _log_to_outputs: called CC=2 SCAR=6.9
189 2 my $self = shift;
190 2 my %p = @_;
191  
192 2 for ( values %{ $self->{outputs} } ) {
193 2 $_->log(%p);
194 }
195 }
196  
197 2 ▶sub log_and_die {
Subroutine log_and_die: called CC=2 SCAR=7.0
Pod documented
198 2 my $self = shift;
199 2 my %p = @_;
200  
201 2 $p{message} = $self->_prepare_message(%p);
202  
203 2 ▶ $self->_log_to_outputs(%p) if $self->would_log( $p{level} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->would_log($p{"level"}) 2 0 2
204  
205 2 $self->_die_with_message(%p);
206 }
207  
208 1 ▶sub log_and_croak {
Subroutine log_and_croak: called CC=1 SCAR=0.0
Pod documented
209 1 my $self = shift;
210  
211 1 $self->log_and_die(@_);
212 }
213  
214 2 ▶sub _die_with_message {
Subroutine _die_with_message: called CC=3 SCAR=12.0
215 2 my $self = shift;
216 2 my %p = @_;
217  
218 2 my $msg = $p{message};
219  
220 local $Carp::CarpLevel = ( $Carp::CarpLevel || 0 ) + $p{carp_level}
221 2 ▶ if exists $p{carp_level};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"carp_level"} 0 2 2
Condition0%0/2
$Carp::CarpLevel || 0
l!l
00
MC/DC0%0/1
$Carp::CarpLevel || 0
$Carp::CarpLevel
Truth table0%0/2
A
A= $Carp::CarpLevel
Aresult
11
00
222  
223 2 Carp::croak($msg);
224 }
225  
226 0 ▶sub log_to {
Subroutine log_to: not called CC=2 SCAR=17.9
Pod documented
227 0 my $self = shift;
228 0 my %p = @_;
229  
230 $p{message} = $self->_apply_callbacks(%p)
231 0 ▶ if $self->{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callbacks"} 0 0 0
232  
233 0 $self->_log_to(%p);
234 }
235  
236 0 ▶sub _log_to {
Subroutine _log_to: not called CC=3 SCAR=24.8
237 0 my $self = shift;
238 0 my %p = @_;
239 0 my $name = $p{name};
240  
241 0 ▶ if ( exists $self->{outputs}{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'outputs'}{$name}) { } 0 0 0
elsif ($^W) { } 0 0 0
242 0 $self->{outputs}{$name}->log(@_);
243 }
244 elsif ($^W) {
245 0 Carp::carp(
246 "Log::Dispatch::* object named '$name' not in dispatcher\n");
247 }
248 }
249  
250 17 ▶sub output {
Subroutine output: called CC=2 SCAR=6.9
Pod documented
251 17 my $self = shift;
252 17 my $name = shift;
253  
254 17 ▶ return unless exists $self->{outputs}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"outputs"}{$name} 1 16 17
255  
256 16 return $self->{outputs}{$name};
257 }
258  
259 10 ▶sub would_log {
Subroutine would_log: called CC=2 SCAR=6.9
Pod documented
260 10 my $self = shift;
261 10 my $level = shift;
262  
263 10 my $level_num = $self->_level_as_number($level);
264 10 ▶ return 0 unless defined $level_num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level_num 1 9 10
265  
266 9 return $self->_would_log($level_num);
267 }
268  
269 232 ▶sub _would_log {
Subroutine _would_log: called CC=3 SCAR=11.0
270 232 my $self = shift;
271 232 my $level_num = shift;
272  
273 232 for ( values %{ $self->{outputs} } ) {
274 233 ▶ return 1 if $_->_should_log($level_num);
Branch: 1 branch
BranchTrueFalseTotal
if $_->_should_log($level_num) 139 94 233
275 }
276  
277 93 return 0;
278 }
279  
280 1 ▶sub is_debug { $_[0]->would_log('debug') }
Subroutine is_debug: called CC=1 SCAR=0.0
Pod undocumented
281 0 ▶sub is_info { $_[0]->would_log('info') }
Subroutine is_info: not called CC=1 SCAR=6.9
Pod undocumented
282 0 ▶sub is_notice { $_[0]->would_log('notice') }
Subroutine is_notice: not called CC=1 SCAR=6.9
Pod undocumented
283 1 ▶sub is_warning { $_[0]->would_log('warning') }
Subroutine is_warning: called CC=1 SCAR=0.0
Pod undocumented
284 0 ▶sub is_warn { $_[0]->would_log('warn') }
Subroutine is_warn: not called CC=1 SCAR=6.9
Pod undocumented
285 0 ▶sub is_error { $_[0]->would_log('error') }
Subroutine is_error: not called CC=1 SCAR=6.9
Pod undocumented
286 0 ▶sub is_err { $_[0]->would_log('err') }
Subroutine is_err: not called CC=1 SCAR=6.9
Pod undocumented
287 0 ▶sub is_critical { $_[0]->would_log('critical') }
Subroutine is_critical: not called CC=1 SCAR=6.9
Pod undocumented
288 1 ▶sub is_crit { $_[0]->would_log('crit') }
Subroutine is_crit: called CC=1 SCAR=0.0
Pod undocumented
289 0 ▶sub is_alert { $_[0]->would_log('alert') }
Subroutine is_alert: not called CC=1 SCAR=6.9
Pod undocumented
290 0 ▶sub is_emerg { $_[0]->would_log('emerg') }
Subroutine is_emerg: not called CC=1 SCAR=6.9
Pod undocumented
291 0 ▶sub is_emergency { $_[0]->would_log('emergency') }
Subroutine is_emergency: not called CC=1 SCAR=6.9
Pod undocumented
292  
293 33 1;
294  
295 # ABSTRACT: Dispatches messages to one or more outputs
296  
297 __END__
« Dispatch/Syslog.pm ↑ Summary Dispatch/ApacheLog.pm »