Daemontools/Config.pm

filter: Statementstmt 88.1% 215 / 244 Branchbran 70.9% 122 / 172 Conditioncond 51.6% 32 / 62 MC/DCmcdc 17.6% 6 / 34 Subroutinesub 87.8% 36 / 41 Podpod 85.0% 17 / 20 totaltotal 74.6% 428 / 573 CC 119 SCAR 26.4
CC
119
Cov
77%
init
47.0
log_level_min
20.0
log_level_max
20.0
CRAP 14.1
↑ Summary Daemontools.pm »
1 package Log::Any::Adapter::Daemontools::Config;
2 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Dynamic configuration settings used by Daemontools logging adapters
6 29 our $VERSION = '0.102'; # VERSION
7  
8  
9 29 ▶use Log::Any::Adapter::Util 'numeric_level', ':levels';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # At top of file where lexical scope is the cleanest
12 53 ▶sub _build_writer_eval_in_clean_scope {
Subroutine _build_writer_eval_in_clean_scope: called CC=2 SCAR=7.0
13 # Args: $self, $code, \$err
14 53 local $@;
15 53 my $output= $_[0]->output; # Needs to be in scope
16 53 my $format= $_[0]->format; # of the eval
17 53 my $coderef= eval $_[1];
18 53 ▶ ${ $_[2] }= $@ if defined $_[0]; # Save error because $@ is localized
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 53 0 53
19 53 return $coderef;
20 }
21  
22 29 ▶use Scalar::Util 'weaken', 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Lazy-load carp, and also remove any Log::Any infrastructure from the trace
25 29 our @CARP_NOT= qw( Log::Any::Adapter::Base Log::Any::Adapter Log::Any::Proxy Log::Any );
26 4 ▶sub _carp_exclude {
Subroutine _carp_exclude: called CC=2 SCAR=6.9
27 4 my $i= 1;
28 4 ▶ ++$i while caller($i) =~ /^Log::Any/;
Branch: 1 branch
BranchTrueFalseTotal
caller($i) =~ /^Log::Any/ and do { ++$i } 28 4 32
29 4 return $i;
30 }
31 4 ▶sub carp { require Carp; local $Carp::CarpLevel= _carp_exclude; &Carp::carp; }
Subroutine carp: called CC=1 SCAR=0.0
Pod undocumented
32 0 ▶sub croak { require Carp; local $Carp::CarpLevel= _carp_exclude; &Carp::croak; }
Subroutine croak: not called CC=1 SCAR=6.9
Pod undocumented
33  
34  
35 29 my (%log_level_name, %log_level_num);
36 29 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
37 29 %log_level_num= (
38 none => EMERGENCY-1,
39 emergency => EMERGENCY,
40 alert => ALERT,
41 critical => CRITICAL,
42 error => ERROR,
43 warning => WARNING,
44 notice => NOTICE,
45 info => INFO,
46 debug => DEBUG,
47 trace => TRACE,
48 all => TRACE
49 );
50 29 %log_level_name= reverse %log_level_num;
51
52 # Add the aliases to the name-to-value mapping
53 29 my %aliases= Log::Any::Adapter::Util::log_level_aliases();
54 29 for (keys %aliases) {
55 145 $log_level_num{$_}= $log_level_num{ $aliases{$_} };
56 }
57 }
58  
59 42 ▶sub _parse_log_level {
Subroutine _parse_log_level: called CC=8 SCAR=22.3
60 42 my ($spec, $base, $min, $max)= @_;
61 my $lev= $spec =~ /^-?\d+$/? $spec # plain level
62 : $spec =~ /^([-+])= (-?\d+)$/? $base + "${1}1" * $2 # += notation
63 42 ▶ : $log_level_num{$spec}; # else a level name
Branch: 2 branches
BranchTrueFalseTotal
$spec =~ /^-?\d+$/ ? : 19 23 42
$spec =~ /^([-+])= (-?\d+)$/ ? : 0 23 23
64 42 ▶ defined $lev or croak "Invalid log level '$spec'";
Branch: 1 branch
BranchTrueFalseTotal
defined $lev or croak("Invalid log level '${spec}'") 42 0 42
65 42 ▶ $min= EMERGENCY-1 unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 42 42
66 42 ▶ $lev= $min unless $lev >= $min;
Branch: 1 branch
BranchTrueFalseTotal
unless $lev >= $min 0 42 42
67 42 ▶ $max= TRACE unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 42 42
68 42 ▶ $lev= $max unless $lev <= $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $lev <= $max 0 42 42
69 42 return $lev;
70 }
71  
72 45 ▶sub log_level {
Subroutine log_level: called CC=4 SCAR=13.9
Pod documented
73 45 my $self= shift;
74 45 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 42 3 45
75 42 ▶ croak "extra arguments" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 42 42
76 42 my $l= _parse_log_level($_[0], $self->{log_level_num}, $self->{log_level_min_num}, $self->{log_level_max_num});
77  
78 # If log level changes, reset the cache
79 42 ▶ if ($l != $self->{log_level_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l != $self->{"log_level_num"}) 37 5 42
80 37 $self->{log_level_num}= $l;
81 37 $self->_reset_cached_adapters;
82 }
83 }
84 45 $log_level_name{ $self->{log_level_num} };
85 }
86  
87 0 ▶sub log_level_min {
Subroutine log_level_min: not called CC=4 SCAR=30.0
Pod documented
88 0 my $self= shift;
89 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
90 0 ▶ croak "extra arguments" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
91
92 # If log level changes as a result, reset the cache
93 0 $self->{log_level_min_num}= _parse_log_level($_[0], $self->{log_level_min_num}, EMERGENCY-1, $self->{log_level_max_num});
94 0 ▶ if ($self->{log_level_min_num} > $self->{log_level_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log_level_min_num"} > $self->{"log_level_num"}) 0 0 0
95 0 $self->{log_level_num}= $self->{log_level_min_num};
96 0 $self->_reset_cached_adapters;
97 }
98 }
99 0 $log_level_name{ $self->{log_level_min_num} };
100 }
101  
102 0 ▶sub log_level_max {
Subroutine log_level_max: not called CC=4 SCAR=30.0
Pod documented
103 0 my $self= shift;
104 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
105 0 ▶ croak "extra arguments" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
106 0 $self->{log_level_max_num}= _parse_log_level($_[0], $self->{log_level_max_num}, $self->{log_level_min_num}, TRACE);
107
108 # If log level changes as a result, reset the cache
109 0 ▶ if ($self->{log_level_max_num} < $self->{log_level_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log_level_max_num"} < $self->{"log_level_num"}) 0 0 0
110 0 $self->{log_level_num}= $self->{log_level_max_num};
111 0 $self->_reset_cached_adapters;
112 }
113 }
114 0 $log_level_name{ $self->{log_level_max_num} };
115 }
116  
117  
118 139 ▶sub output {
Subroutine output: called CC=9 SCAR=22.9
Pod documented
119 139 my $self= shift;
120 139 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 133 139
121 6 ▶ croak "extra arguments" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 6 6
122 6 ▶ defined $_[0] && (ref $_[0] eq 'GLOB' || ref $_[0] eq 'CODE' || ref($_[0])->can('print'))
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref $_[0] eq "GLOB" || ref $_[0] eq "CODE" || ref($_[0])->can("print") or croak("Argument must be file handle or coderef") 6 0 6
Condition66%6/9
defined $_[0] and ref $_[0] eq "GLOB" || ref $_[0] eq "CODE" || ref($_[0])->can("print")
!ll&&!rl&&r
006
ref $_[0] eq "GLOB" or ref $_[0] eq "CODE" or ref($_[0])->can("print")
l!l&&r!l&&!r
420
ref $_[0] eq "GLOB" or ref $_[0] eq "CODE"
l!l&&r!l&&!r
222
MC/DC0%0/4
defined $_[0] and ref $_[0] eq "GLOB" || ref $_[0] eq "CODE" || ref($_[0])->can("print")
defined $_[0] ref $_[0] eq "GLOB" ref $_[0] eq "CODE" ref($_[0])->can("print")
Truth table60%3/5
A and B or C or D
A= defined $_[0]B= ref $_[0] eq "GLOB"C= ref $_[0] eq "CODE"D= ref($_[0])->can("print")
ABCDresult
0XXX0
10000
11XX1
101X1
10011
123 or croak "Argument must be file handle or coderef";
124 6 $self->{output}= $_[0];
125 6 delete $self->{_writer_cache};
126 6 ▶ $self->_reset_cached_adapters unless $self->{writer};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 6 0 6
127 }
128 139 ▶ return defined $self->{output}? $self->{output} : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'output'} ? : 24 115 139
129 }
130  
131  
132 107 ▶sub format {
Subroutine format: called CC=11 SCAR=26.4
Pod documented
133 107 my $self= shift;
134 107 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 93 107
135 14 ▶ @_ == 1 && defined $_[0] && (!ref $_[0] or ref $_[0] eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 and defined $_[0] and !ref($_[0]) || ref $_[0] eq "CODE" or croak("Expected string or coderef") 14 0 14
Condition44%4/9
@_ == 1 and defined $_[0] and !ref($_[0]) || ref $_[0] eq "CODE"
!ll&&!rl&&r
0014
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0014
not ref $_[0] or ref $_[0] eq "CODE"
l!l&&r!l&&!r
1310
MC/DC0%0/4
@_ == 1 and defined $_[0] and !ref($_[0]) || ref $_[0] eq "CODE"
@_ == 1 defined $_[0] not ref $_[0] ref $_[0] eq "CODE"
Truth table40%2/5
A and B and not(C) or D
A= @_ == 1B= defined $_[0]C= not ref $_[0]D= ref $_[0] eq "CODE"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
136 or croak "Expected string or coderef";
137 14 ▶ if (!ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 13 1 14
138 # Test their supplied code right away, so the error happens in a
139 # place where its easy to fix
140 13 my $x= $self->_build_writer_eval_in_clean_scope(
141 "sub { "
142 .' my ($category, $level, $level_prefix, $file, $file_brief, $line, $package);'
143 ." $_[0]; "
144 ."}",
145 \my $err
146 );
147 13 ▶ defined $x && ref $x eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
defined $x and ref $x eq "CODE" or croak("Invalid format (make sure you wrote *code* that returns a string): $err") 13 0 13
Condition33%1/3
defined $x and ref $x eq "CODE"
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $x and ref $x eq "CODE"
defined $x ref $x eq "CODE"
Truth table33%1/3
A and B
A= defined $xB= ref $x eq "CODE"
ABresult
0X0
100
111
148 or croak "Invalid format (make sure you wrote *code* that returns a string): $err";
149 }
150 14 $self->{format}= $_[0];
151 14 delete $self->{_writer_cache};
152 14 ▶ $self->_reset_cached_adapters unless $self->{writer};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 14 0 14
153 }
154 107 ▶ defined $self->{format}? $self->{format} : '"$level_prefix$_\n"';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'format'} ? : 54 53 107
155 }
156  
157  
158 1 ▶sub writer {
Subroutine writer: called CC=5 SCAR=18.9
Pod documented
159 1 my $self= shift;
160 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
161 1 ▶ @_ == 1 && (!defined $_[0] || ref $_[0] eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 and !defined($_[0]) || ref $_[0] eq "CODE" or croak("Expected coderef or undef") 1 0 1
Condition33%2/6
@_ == 1 and !defined($_[0]) || ref $_[0] eq "CODE"
!ll&&!rl&&r
001
not defined $_[0] or ref $_[0] eq "CODE"
l!l&&r!l&&!r
010
MC/DC0%0/3
@_ == 1 and !defined($_[0]) || ref $_[0] eq "CODE"
@_ == 1 not defined $_[0] ref $_[0] eq "CODE"
Truth table25%1/4
A and not(B) or C
A= @_ == 1B= not defined $_[0]C= ref $_[0] eq "CODE"
ABCresult
0XX0
1000
11X1
1011
162 or croak "Expected coderef or undef";
163 1 $self->{writer}= $_[0];
164 1 $self->_reset_cached_adapters;
165 }
166 }
167  
168  
169 # Yes, I should use Moo, but in the spirit of Log::Any having no non-core deps,
170 # I decided to do the same.
171 31 ▶sub new {
Subroutine new: called CC=4 SCAR=17.3
Pod documented
172 31 my $class= shift;
173 31 my $self= bless {
174 log_level_num => INFO,
175 log_level_min_num => EMERGENCY-1,
176 log_level_max_num => TRACE,
177 }, $class;
178
179 # Convert hashref to plain key/value list
180 0 ▶ unshift @_, %{ shift @_ }
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 31 31
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
3100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
181 if @_ == 1 && ref $_[0] eq 'HASH';
182
183 # Iterate key/value pairs and call the accessor method for each
184 31 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 31 31
185 0 my ($k, $v)= (shift, shift);
186 0 $self->$k($v);
187 }
188 31 $self;
189 }
190  
191  
192 29 our (%env_profile, %argv_profile);
193 29 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
194 29 $env_profile{1}= { debug => 'DEBUG' };
195 29 $argv_profile{1}= {
196 bundle => 1,
197 verbose => qr/^(--verbose|-v)$/,
198 quiet => qr/^(--quiet|-q)$/,
199 stop => '--'
200 };
201 $argv_profile{consume}= {
202 29 bundle => 1,
203 verbose => qr/^(--verbose|-v)$/,
204 quiet => qr/^(--quiet|-q)$/,
205 stop => '--',
206 remove => 1,
207 };
208 }
209  
210 29 my %_init_args= map { $_ => 1 } qw(
211 level log_level min level_min log_level_min max level_max log_level_max
212 env argv signals handle_signals install_signal_handlers format out output writer
213 );
214 21 ▶sub init {
Subroutine init: called CC=27 SCAR=38.5
Pod documented
215 21 my $self= shift;
216 21 ▶ my $cfg= (@_ == 1 and ref $_[0] eq 'HASH')? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 21 0 21
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
0021
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
217 # Warn on unknown arguments
218 21 my @unknown= grep { !$_init_args{$_} } keys %$cfg;
219 21 ▶ carp "Invalid arguments: ".join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 20 21
220
221 defined $cfg->{$_} and $self->log_level($cfg->{$_})
222 21 ▶ for qw: level log_level :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and $self->log_level($cfg->{$_}) 12 30 42
223
224 defined $cfg->{$_} and $self->log_level_min($cfg->{$_})
225 21 ▶ for qw: min level_min log_level_min :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and $self->log_level_min($cfg->{$_}) 0 63 63
226
227 defined $cfg->{$_} and $self->log_level_max($cfg->{$_})
228 21 ▶ for qw: max level_max log_level_max :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and $self->log_level_max($cfg->{$_}) 0 63 63
229  
230 # Optional ENV processing
231 defined $cfg->{$_} and do {
232 0 my $v= $cfg->{$_};
233 $self->process_env( %{
234 0 ▶ ref $v eq 'HASH'? $v : $env_profile{$v} || croak "Unknown \"$_\" value $v"
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 0 0 0
Condition0%0/2
$Log::Any::Adapter::Daemontools::Config::env_profile{$v} or croak(qq[Unknown "$_" value $v])
l!l
00
MC/DC0%0/1
$Log::Any::Adapter::Daemontools::Config::env_profile{$v} or croak(qq[Unknown "$_" value $v])
$Log::Any::Adapter::Daemontools::Config::env_profile{$v}
Truth table0%0/2
A
A= $Log::Any::Adapter::Daemontools::Config::env_profile{$v}
Aresult
11
00
235 } );
236 21 ▶ } for qw: env process_env :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and do { my $v = $cfg->{$_}; $self->process_env(%{ref $v eq "HASH" ? $v : $Log::Any::Adapter::Daemontools::Config::env_profile{$v} || croak(qq[Unknown "$_" value $v]);}) } 0 42 42
237
238 # Optional ARGV parsing
239 defined $cfg->{$_} and do {
240 6 my $v= $cfg->{$_};
241 $self->process_argv( %{
242 6 ▶ ref $v eq 'HASH'? $v : $argv_profile{$v} || croak "Unknown \"$_\" value $v"
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 1 5 6
Condition50%1/2
$Log::Any::Adapter::Daemontools::Config::argv_profile{$v} or croak(qq[Unknown "$_" value $v])
l!l
50
MC/DC0%0/1
$Log::Any::Adapter::Daemontools::Config::argv_profile{$v} or croak(qq[Unknown "$_" value $v])
$Log::Any::Adapter::Daemontools::Config::argv_profile{$v}
Truth table50%1/2
A
A= $Log::Any::Adapter::Daemontools::Config::argv_profile{$v}
Aresult
11
00
243 } );
244 21 ▶ } for qw: argv process_argv :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and do { my $v = $cfg->{$_}; $self->process_argv(%{ref $v eq "HASH" ? $v : $Log::Any::Adapter::Daemontools::Config::argv_profile{$v} || croak(qq[Unknown "$_" value $v]);}) } 6 36 42
245
246 # Optional installation of signal handlers
247 defined $cfg->{$_} and do {
248 1 ▶ my $rt= ref($cfg->{$_}) || '';
Condition50%1/2
ref $cfg->{$_} || ''
l!l
10
MC/DC0%0/1
ref $cfg->{$_} || ''
ref $cfg->{$_}
Truth table50%1/2
A
A= ref $cfg->{$_}
Aresult
11
00
249 $self->install_signal_handlers( %{
250 1 $rt eq 'HASH'? $cfg->{$_}
251 1 ▶ : $rt eq 'ARRAY'? { verbose => $cfg->{$_}[0], quiet => $cfg->{$_}[1] }
Branch: 2 branches
BranchTrueFalseTotal
$rt eq 'HASH' ? : 0 1 1
$rt eq 'ARRAY' ? : 1 0 1
252 : croak "Unknown \"$_\" value $cfg->{$_}"
253 } );
254 21 ▶ } for qw: signals handle_signals install_signal_handlers :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and do { my $rt = ref $cfg->{$_} || ""; $self->install_signal_handlers(%{$rt eq "HASH" ? $$cfg{$_} : ($rt eq "ARRAY" ? {"verbose", $cfg->{$_}[0], "quiet", $cfg->{$_}[1]} : croak(qq[Unknown "$_" value $cfg->{$_}]));}) } 1 62 63
255
256 $self->format($cfg->{format})
257 21 ▶ if defined $cfg->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg->{"format"} 0 21 21
258
259 defined $cfg->{$_} and $self->output($cfg->{$_})
260 21 ▶ for qw: out output :;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$_} and $self->output($cfg->{$_}) 3 39 42
261
262 $self->writer($cfg->{writer})
263 21 ▶ if defined $cfg->{writer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg->{"writer"} 0 21 21
264
265 21 $self;
266 }
267  
268  
269 # We lied. This is the actual attribute in the implementation
270 73 ▶sub log_level_num { shift->{log_level_num} }
Subroutine log_level_num: called CC=1 SCAR=0.0
Pod documented
271 0 ▶sub log_level_min_num { shift->{log_level_min_num} }
Subroutine log_level_min_num: not called CC=1 SCAR=6.9
Pod documented
272 0 ▶sub log_level_max_num { shift->{log_level_max_num} }
Subroutine log_level_max_num: not called CC=1 SCAR=6.9
Pod documented
273  
274  
275 12 ▶sub log_level_adjust {
Subroutine log_level_adjust: called CC=4 SCAR=17.1
Pod documented
276 12 my ($self, $offset)= @_;
277 12 ▶ defined $offset && ($offset =~ /^-?\d+$/) && @_ == 2
Branch: 1 branch
BranchTrueFalseTotal
defined $offset and $offset =~ /^-?\d+$/ and @_ == 2 or die "Expected offset integer" 12 0 12
Condition33%2/6
defined $offset and $offset =~ /^-?\d+$/ and @_ == 2
!ll&&!rl&&r
0012
defined $offset and $offset =~ /^-?\d+$/
!ll&&!rl&&r
0012
MC/DC0%0/3
defined $offset and $offset =~ /^-?\d+$/ and @_ == 2
defined $offset $offset =~ /^-?\d+$/ @_ == 2
Truth table25%1/4
A and B and C
A= defined $offsetB= $offset =~ /^-?\d+$/C= @_ == 2
ABCresult
0XX0
10X0
1100
1111
278 or die "Expected offset integer";
279 12 $self->log_level( $self->log_level_num + $offset );
280 }
281  
282  
283 29 my %_process_env_args= ( debug => 1, log_level => 1 );
284 11 ▶sub process_env {
Subroutine process_env: called CC=6 SCAR=18.1
Pod documented
285 11 my ($self, %spec)= @_;
286 # Warn on unknown arguments
287 11 my @unknown= grep { !$_process_env_args{$_} } keys %spec;
288 11 ▶ carp "Invalid arguments: ".join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 11 11
289
290 11 ▶ if (defined $spec{log_level} && defined $ENV{$spec{log_level}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec{"log_level"} and defined $ENV{$spec{"log_level"}}) 4 7 11
Condition66%2/3
defined $spec{"log_level"} and defined $ENV{$spec{"log_level"}}
!ll&&!rl&&r
704
MC/DC50%1/2
defined $spec{"log_level"} and defined $ENV{$spec{"log_level"}}
defined $spec{"log_level"} defined $ENV{$spec{"log_level"}}
Truth table66%2/3
A and B
A= defined $spec{"log_level"}B= defined $ENV{$spec{"log_level"}}
ABresult
0X0
100
111
291 4 $self->log_level($ENV{$spec{log_level}});
292 }
293 11 ▶ if (defined $spec{debug} && defined $ENV{$spec{debug}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec{"debug"} and defined $ENV{$spec{"debug"}}) 7 4 11
Condition66%2/3
defined $spec{"debug"} and defined $ENV{$spec{"debug"}}
!ll&&!rl&&r
407
MC/DC50%1/2
defined $spec{"debug"} and defined $ENV{$spec{"debug"}}
defined $spec{"debug"} defined $ENV{$spec{"debug"}}
Truth table66%2/3
A and B
A= defined $spec{"debug"}B= defined $ENV{$spec{"debug"}}
ABresult
0X0
100
111
294 7 $self->log_level( $self->debug_level_to_log_level($ENV{$spec{debug}}) );
295 }
296 }
297  
298 7 ▶sub debug_level_to_log_level {
Subroutine debug_level_to_log_level: called CC=2 SCAR=6.9
Pod undocumented
299 7 my ($class, $level)= @_;
300 7 ▶ $level+= 6 if $level =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^-?\d+$/ 4 3 7
301 7 $level;
302 }
303  
304  
305 29 my %_process_argv_args= ( bundle => 1, verbose => 1, quiet => 1, stop => 1, array => 1, remove => 1 );
306 12 ▶sub process_argv {
Subroutine process_argv: called CC=2 SCAR=6.9
Pod documented
307 12 my $self= shift;
308 12 my $ofs= $self->parse_log_level_opts(array => \@ARGV, @_);
309 12 ▶ $self->log_level_adjust($ofs)
Branch: 1 branch
BranchTrueFalseTotal
if $ofs 9 3 12
310 if $ofs;
311 12 1;
312 }
313  
314  
315 218 ▶sub _make_regex_list {
Subroutine _make_regex_list: called CC=5 SCAR=16.2
316 218 ▶ return () unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 6 212 218
317 212 ▶ return qr/^\Q$_[0]\E$/ unless ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 144 68 212
318 68 ▶ return map { _make_regex_list($_) } @{ $_[0] } if ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" 58 10 68
319 10 ▶ return $_[0] if ref $_[0] eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "Regexp" 10 0 10
320 0 croak "Not a regular expression, string, or array: $_[0]"
321 }
322 102 ▶sub _combine_regex {
Subroutine _combine_regex: called CC=3 SCAR=11.0
323 102 my @list= _make_regex_list(@_);
324 102 ▶ return @list == 0? qr/\0^/ # a regex that doesn't match anything
Branch: 2 branches
BranchTrueFalseTotal
@list == 0 ? : 6 96 102
@list == 1 ? : 38 58 96
325 : @list == 1? $list[0]
326 58 : qr/@{[ join '|', @list ]}/;
327 }
328 34 ▶sub parse_log_level_opts {
Subroutine parse_log_level_opts: called CC=4 SCAR=14.2
Pod documented
329 34 my ($class, %spec)= @_;
330 # Warn on unknown arguments
331 34 my @unknown= grep !$_process_argv_args{$_}, keys %spec;
332 34 ▶ carp "Invalid arguments: ".join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 34 34
333
334 34 ▶ defined $spec{array} or croak "Parameter 'array' is required";
Branch: 1 branch
BranchTrueFalseTotal
defined $spec{"array"} or croak("Parameter 'array' is required") 34 0 34
335 34 $spec{$_}= _combine_regex($spec{$_}) for qw( stop verbose quiet );
336 34 return _parse_log_level_opts( $spec{array}, \%spec );
337 }
338 44 ▶sub _parse_log_level_opts {
Subroutine _parse_log_level_opts: called CC=12 SCAR=24.9
339 44 my ($array, $spec)= @_;
340 44 my $level_ofs= 0;
341 44 ▶ for (my $i= 0; $i < @$array; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$array) 92 41 133
342 92 ▶ last if $array->[$i] =~ $spec->{stop};
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] =~ /$spec->{"stop"}/ 3 89 92
343 89 ▶ if ($array->[$i] =~ /^-[^-=][^-=]+$/ and $spec->{bundle}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($array->[$i] =~ /^-[^-=][^-=]+$/ and $spec->{'bundle'}) { } 10 79 89
elsif ($array->[$i] =~ /$spec->{'verbose'}/) { } 34 45 79
elsif ($array->[$i] =~ /$spec->{'quiet'}/) { } 22 23 45
Condition100%3/3
$array->[$i] =~ /^-[^-=][^-=]+$/ and $spec->{"bundle"}
!ll&&!rl&&r
76310
MC/DC100%2/2
$array->[$i] =~ /^-[^-=][^-=]+$/ and $spec->{"bundle"}
$array->[$i] =~ /^-[^-=][^-=]+$/ $spec->{"bundle"}
Truth table100%3/3
A and B
A= $array->[$i] =~ /^-[^-=][^-=]+$/B= $spec->{"bundle"}
ABresult
0X0
100
111
344 # Un-bundle the arguments
345 10 my @un_bundled= map "-$_", split //, substr($array->[$i], 1);
346 10 my $len= @un_bundled;
347 # Then filter them as usual
348 10 $level_ofs += _parse_log_level_opts(\@un_bundled, $spec);
349 # Then re-bundle them, if altered
350 10 ▶ if ($spec->{remove} && $len != @un_bundled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"remove"} and $len != @un_bundled) 5 5 10
Condition66%2/3
$spec->{"remove"} and $len != @un_bundled
!ll&&!rl&&r
505
MC/DC50%1/2
$spec->{"remove"} and $len != @un_bundled
$spec->{"remove"} $len != @un_bundled
Truth table66%2/3
A and B
A= $spec->{"remove"}B= $len != @un_bundled
ABresult
0X0
100
111
351 5 ▶ if (@un_bundled) {
Branch: 1 branch
BranchTrueFalseTotal
if (@un_bundled) { } 1 4 5
352 1 $array->[$i]= '-' . join('', map substr($_,1), @un_bundled);
353 } else {
354 4 splice( @$array, $i--, 1 );
355 }
356 }
357 }
358 elsif ($array->[$i] =~ $spec->{verbose}) {
359 34 $level_ofs++;
360 34 ▶ splice( @$array, $i--, 1 ) if $spec->{remove};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"remove"} 16 18 34
361 }
362 elsif ($array->[$i] =~ $spec->{quiet}) {
363 22 $level_ofs--;
364 22 ▶ splice( @$array, $i--, 1 ) if $spec->{remove};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"remove"} 10 12 22
365 }
366 }
367 44 return $level_ofs;
368 }
369  
370  
371 29 my %_handle_signal_args= ( verbose => 1, quiet => 1 );
372 1 ▶sub install_signal_handlers {
Subroutine install_signal_handlers: called CC=4 SCAR=14.2
Pod documented
373 1 my ($self, %spec)= @_;
374 # Warn on unknown arguments
375 1 my @unknown= grep { !$_handle_signal_args{$_} } keys %spec;
376 1 ▶ carp "Invalid arguments: ".join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 1 1
377
378 1 ▶ $SIG{ $spec{verbose} }= sub { $self->log_level_adjust(1); }
Subroutine __ANON__: called CC=1 SCAR=0.0
379 1 ▶ if $spec{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $spec{"verbose"} 1 0 1
380
381 2 ▶ $SIG{ $spec{quiet} }= sub { $self->log_level_adjust(-1); }
Subroutine __ANON__: called CC=1 SCAR=0.0
382 1 ▶ if $spec{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $spec{"quiet"} 1 0 1
383 }
384  
385  
386 44 ▶sub compiled_writer {
Subroutine compiled_writer: called CC=3 SCAR=11.1
Pod documented
387 44 my $self= shift;
388 44 ▶ $self->{writer} || ($self->{_writer_cache} ||= $self->_build_writer_cache)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"writer"} or $self->{"_writer_cache"} ||= $self->_build_writer_cache 1 43 44
Condition66%2/3
$self->{"_writer_cache"} ||= $self->_build_writer_cache
l!l&&r!l&&!r
3400
MC/DC0%0/2
$self->{"_writer_cache"} ||= $self->_build_writer_cache
$self->{"_writer_cache"} $self->_build_writer_cache
Truth table66%2/3
A ||= B
A= $self->{"_writer_cache"}B= $self->_build_writer_cache
ABresult
1X1
011
000
389 }
390  
391 # This method combines the output and format settings into a writer.
392 # It also ensures the output is using autoflush
393 40 ▶sub _build_writer_cache {
Subroutine _build_writer_cache: called CC=2 SCAR=7.0
394 40 my $self= shift;
395 40 my $code= "sub { \n" . $self->_build_writer_code . "\n}";
396 40 my $err;
397 40 ▶ my $writer= $self->_build_writer_eval_in_clean_scope( $code, \$err )
Branch: 1 branch
BranchTrueFalseTotal
my $writer = $self->_build_writer_eval_in_clean_scope($code, \$err) or croak("Compilation of log writer failed: $err\nSource code is: $code") 40 0 40
398 or croak "Compilation of log writer failed: $err\nSource code is: $code";
399 40 $self->_enable_autoflush($self->output);
400 40 return $writer;
401 }
402  
403 # separate from _build_writer_cache so that test cases (and maybe subclasses)
404 # can inspect the generated code.
405 40 ▶sub _build_writer_code {
Subroutine _build_writer_code: called CC=9 SCAR=22.0
406 40 my $self= shift;
407 40 my $format= $self->format;
408 40 my $code= ' my ($adapter, $level, $message)= @_;'."\n"
409 . ' $message =~ s/\n+$//;'."\n";
410
411 40 ▶ if ($format =~ /\$\{?category(\W|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /\$\{?category(\W|$)/) 1 39 40
412 1 $code .= ' my $category= $adapter->category;'."\n";
413 }
414 40 ▶ if ($format =~ /\$\{?(package|file|line|file_brief)(\W|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /\$\{?(package|file|line|file_brief)(\W|$)/) 6 34 40
415 6 $code .= ' my ($package,$file,$line);'."\n"
416 .' { my $i= 0; do { ($package, $file, $line)= caller(++$i) } while $package =~ /^Log::Any/; };'."\n";
417
418 6 ▶ $code .= ' my $file_brief= $file;'."\n"
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /\$\{?file_brief(\W|$)/ 3 3 6
419 .' $file_brief =~ s|.*[\\/]lib[\\/]||;'."\n"
420 if $format =~ /\$\{?file_brief(\W|$)/;
421 }
422 40 ▶ if ($format =~ /\$\{?level_prefix(\W|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /\$\{?level_prefix(\W|$)/) 27 13 40
423 27 $code .= ' my $level_prefix= ($level eq "info")? "" : "$level: ";'."\n";
424 }
425 40 my $output= $self->output;
426 40 ▶ if (ref $output eq 'GLOB') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $output eq 'GLOB') { } 36 4 40
elsif (ref($output)->can('print')) { } 2 2 4
elsif (ref $output eq 'CODE') { } 2 0 2
427 36 $code .= ' print $output (';
428 } elsif (ref($output)->can('print')) {
429 2 $code .= ' $output->print(';
430 } elsif (ref $output eq 'CODE') {
431 2 $code .= ' $output->(';
432 } else {
433 0 croak "Unhandled type of output: $output";
434 }
435
436 40 ▶ if (ref $format eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format eq 'CODE') { } 1 39 40
437 # Closure over '$format', rather than deparsing the coderef
438 1 $code .= ' map {; $format->($adapter, $level) } split /\n/, $message)';
439 } else {
440 39 $code .= ' map {; '.$format.' } split /\n/, $message)';
441 }
442  
443 40 return $code;
444 }
445  
446 40 ▶sub _enable_autoflush {
Subroutine _enable_autoflush: called CC=3 SCAR=11.0
447 40 my ($self, $thing)= @_;
448 # This module tries to be very backward-compatible, and not force people to use IO::Handle
449 # if they didn't intend to...
450 40 ▶ if (ref $thing eq 'GLOB') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $thing eq 'GLOB') { } 36 4 40
elsif (ref($thing)->can('autoflush')) { } 1 3 4
451 36 my $prev= select($thing);
452 36 $|= 1;
453 36 select($prev);
454 36 1;
455 }
456 elsif (ref($thing)->can('autoflush')) {
457 1 $thing->autoflush(1);
458 1 1;
459 }
460 else {
461 3 0;
462 }
463 }
464  
465 # "Cached Adapters" is conceptually a field of the config object, but then
466 # it shows a giant mess if/when you Dump() the object, so I'm using this trick
467 # to keep the list attached to the package instead of the object.
468 # Object destructor cleans up the list.
469 29 our %_cached_adapters;
470  
471 # Holds a list of weak references to Adapter instances which have cached values from this config
472 102 ▶sub _cached_adapters {
Subroutine _cached_adapters: called CC=2 SCAR=6.9
473 # Use refaddr in case someone subclasses and gets creative with re-blessing objects
474 102 ▶ $_cached_adapters{refaddr $_[0]} ||= [];
Condition100%2/2
$Log::Any::Adapter::Daemontools::Config::_cached_adapters{builtin::refaddr($_[0])} ||= []
l!l
7329
MC/DC100%1/1
$Log::Any::Adapter::Daemontools::Config::_cached_adapters{builtin::refaddr($_[0])} ||= []
$Log::Any::Adapter::Daemontools::Config::_cached_adapters{builtin::refaddr($_[0])}
Truth table100%2/2
A
A= $Log::Any::Adapter::Daemontools::Config::_cached_adapters{builtin::refaddr($_[0])}
Aresult
11
00
475 }
476 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
477 2 delete $_cached_adapters{refaddr $_[0]};
478 }
479  
480 # Called by an adapter after it caches things from this config to ask that it
481 # be notified about any changes.
482 44 ▶sub _register_cached_adapter {
Subroutine _register_cached_adapter: called CC=1 SCAR=0.0
483 44 my ($self, $adapter)= @_;
484 44 my $cache= $self->_cached_adapters;
485 44 push @$cache, $adapter;
486 44 weaken( $cache->[-1] );
487 }
488  
489 # Inform all the Adapters who have cached our settings that the cache is invalid.
490 58 ▶sub _reset_cached_adapters {
Subroutine _reset_cached_adapters: called CC=2 SCAR=6.9
491 58 my $self= shift;
492 58 my $cache= $self->_cached_adapters;
493 58 $_->_uncache_config for grep { defined } @$cache;
494 58 @$cache= ();
495 }
496  
497 29 1;
498  
499 __END__
↑ Summary Daemontools.pm »