blib/lib/Log/Any/App.pm

filter: Statementstmt 81.1% 499 / 615 Branchbran 64.1% 172 / 268 Conditioncond 55.3% 26 / 47 MC/DCmcdc 28.5% 8 / 28 Subroutinesub 88.0% 44 / 50 Podpod 100% 1 / 1 totaltotal 74.3% 750 / 1009 CC 182 SCAR 32.2
CC
182
Cov
75%
_gen_appender_config
74.5
_set_level
43.5
_gen_l4p_config
41.1
CRAP 24.9
↑ Summary
1 package Log::Any::App;
2  
3 3 our $DATE = '2019-01-09'; # DATE
4 3 our $VERSION = '0.540'; # VERSION
5  
6 # i need this to run on centos 5.x. otherwise all my other servers are debian
7 # 5.x and 6.x+ (perl 5.010).
8 3 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Log::Any::IfLOG;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use vars qw($dbg_ctx);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our %PATTERN_STYLES = (
20 plain => '%m',
21 plain_nl => '%m%n',
22 script_short => '[%r] %m%n',
23 script_long => '[%d] %m%n',
24 daemon => '[pid %P] [%d] %m%n',
25 syslog => '[pid %p] %m',
26 );
27 3 for (keys %PATTERN_STYLES) {
28 18 $PATTERN_STYLES{"cat_$_"} = "[cat %c]$PATTERN_STYLES{$_}";
29 18 $PATTERN_STYLES{"loc_$_"} = "[loc %l]$PATTERN_STYLES{$_}";
30 }
31  
32 3 my $init_args;
33 3 our $init_called;
34 3 my $is_daemon;
35  
36 # poor man's version of 5.10's //
37 3482 ▶sub _ifdef {
Subroutine _ifdef: called CC=3 SCAR=11.0
38 3482 my $def = pop @_;
39 3482 for (@_) {
40 3482 ▶ return $_ if defined($_);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 104 3378 3482
41 }
42 3378 $def;
43 }
44  
45 # j=as json (except the last default)
46 144 ▶sub _ifdefj {
Subroutine _ifdefj: called CC=3 SCAR=11.1
47 144 require JSON::MaybeXS;
48  
49 144 my $def = pop @_;
50 144 for (@_) {
51 239 ▶ return JSON::MaybeXS::decode_json($_) if defined($_);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 0 239 239
52 }
53 144 $def;
54 }
55  
56 49 ▶sub init {
Subroutine init: called CC=6 SCAR=18.9
Pod documented
57 49 ▶ return if $init_called++;
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Any::App::init_called++ 0 49 49
58  
59 49 $is_daemon = undef;
60  
61 49 my ($args, $caller) = @_;
62 49 ▶ $caller ||= caller();
Condition66%2/3
$caller ||= caller()
l!l&&r!l&&!r
2470
MC/DC0%0/2
$caller ||= caller()
$caller caller()
Truth table66%2/3
A ||= B
A= $callerB= caller()
ABresult
1X1
011
000
63  
64 49 my $spec = _parse_opts($args, $caller);
65 49 ▶ if ($spec->{log} && $spec->{init}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"log"} and $spec->{"init"}) 1 48 49
Condition66%2/3
$spec->{"log"} and $spec->{"init"}
!ll&&!rl&&r
0481
MC/DC50%1/2
$spec->{"log"} and $spec->{"init"}
$spec->{"log"} $spec->{"init"}
Truth table66%2/3
A and B
A= $spec->{"log"}B= $spec->{"init"}
ABresult
0X0
100
111
66 1 _init_log4perl($spec);
67 1 ▶ if ($ENV{LOG_ENV}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LOG_ENV"}) 0 1 1
68 0 my $log_main = Log::Any->get_logger(category => 'main');
69 0 $log_main->tracef("Environment variables: %s", \%ENV);
70 }
71 }
72 49 $spec;
73 }
74  
75 6 ▶sub _gen_appender_config {
Subroutine _gen_appender_config: called CC=15 SCAR=43.1
76 6 my ($ospec, $apd_name, $filter) = @_;
77  
78 6 my $name = $ospec->{name};
79 6 my $class;
80 6 my $params = {};
81 6 ▶ if ($name =~ /^dir/i) {
Branch: 6 branches
BranchTrueFalseTotal
if ($name =~ /^dir/i) { } 0 6 6
elsif ($name =~ /^file/i) { } 6 0 6
elsif ($name =~ /^screen/i) { } 0 0 0
elsif ($name =~ /^syslog/i) { } 0 0 0
elsif ($name =~ /^unixsock/i) { } 0 0 0
elsif ($name =~ /^array/i) { } 0 0 0
82 0 $class = "Log::Dispatch::Dir";
83 0 $params->{dirname} = $ospec->{path};
84 0 $params->{filename_pattern} = $ospec->{filename_pattern};
85 0 ▶ $params->{max_size} = $ospec->{max_size} if $ospec->{max_size};
Branch: 1 branch
BranchTrueFalseTotal
if $ospec->{"max_size"} 0 0 0
86 0 ▶ $params->{max_files} = $ospec->{histories}+1 if $ospec->{histories};
Branch: 1 branch
BranchTrueFalseTotal
if $ospec->{"histories"} 0 0 0
87 0 ▶ $params->{max_age} = $ospec->{max_age} if $ospec->{max_age};
Branch: 1 branch
BranchTrueFalseTotal
if $ospec->{"max_age"} 0 0 0
88 } elsif ($name =~ /^file/i) {
89 6 $class = "Log::Dispatch::FileWriteRotate";
90 6 my ($dir, $prefix) = $ospec->{path} =~ m!(.+)/(.+)!;
91 6 ▶ $dir ||= "."; $prefix ||= $ospec->{path};
Condition40%2/5
$dir ||= "."
l!l
60
$prefix ||= $ospec->{"path"}
l!l&&r!l&&!r
600
MC/DC0%0/1
$dir ||= "."
$dir
MC/DC0%0/2
$prefix ||= $ospec->{"path"}
$prefix $ospec->{"path"}
Truth table50%1/2
A
A= $dir
Aresult
11
00
Truth table33%1/3
A ||= B
A= $prefixB= $ospec->{"path"}
ABresult
1X1
011
000
92 6 $params->{dir} = $dir;
93 6 $params->{prefix} = $prefix;
94 6 $params->{suffix} = $ospec->{suffix};
95 6 $params->{size} = $ospec->{max_size};
96 6 $params->{period} = $ospec->{period};
97 6 $params->{histories} = $ospec->{histories};
98 6 $params->{buffer_size} = $ospec->{buffer_size};
99 } elsif ($name =~ /^screen/i) {
100 $class = "Log::Log4perl::Appender::" .
101 0 ▶ ($ospec->{color} ? "ScreenColoredLevels" : "Screen");
Branch: 1 branch
BranchTrueFalseTotal
$ospec->{'color'} ? : 0 0 0
102 0 ▶ $params->{stderr} = $ospec->{stderr} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$ospec->{'stderr'} ? : 0 0 0
103 0 $params->{"color.WARN"} = "bold blue"; # blue on black is so unreadable
104 } elsif ($name =~ /^syslog/i) {
105 0 $class = "Log::Dispatch::Syslog";
106 0 $params->{mode} = 'append';
107 0 $params->{ident} = $ospec->{ident};
108 0 $params->{facility} = $ospec->{facility};
109 } elsif ($name =~ /^unixsock/i) {
110 0 $class = "Log::Log4perl::Appender::Socket::UNIX";
111 0 $params->{Socket} = $ospec->{path};
112 } elsif ($name =~ /^array/i) {
113 0 $class = "Log::Dispatch::ArrayWithLimits";
114 0 $params->{array} = $ospec->{array};
115 0 $params->{max_elems} = $ospec->{max_elems};
116 } else {
117 0 die "BUG: Unknown appender type: $name";
118 }
119  
120 join(
121 "",
122 "log4perl.appender.$apd_name = $class\n",
123 12 (map { "log4perl.appender.$apd_name.$_ = $params->{$_}\n" }
124 6 ▶ grep {defined $params->{$_}} keys %$params),
Branch: 1 branch
BranchTrueFalseTotal
$filter ? : 6 0 6
125 "log4perl.appender.$apd_name.layout = PatternLayout\n",
126 "log4perl.appender.$apd_name.layout.ConversionPattern = $ospec->{pattern}\n",
127 ($filter ? "log4perl.appender.$apd_name.Filter = $filter\n" : ""),
128 );
129 }
130  
131 0 ▶sub _lit {
Subroutine _lit: not called CC=1 SCAR=6.9
132 0 require Data::Dump;
133 0 Data::Dump::dump(shift);
134 }
135  
136 1 ▶sub _gen_l4p_config {
Subroutine _gen_l4p_config: called CC=34 SCAR=37.2
137 1 my ($spec) = @_;
138  
139 1 my @otypes = qw(file dir screen syslog unixsock array);
140  
141 # we use a custom perl code to implement filter_* specs.
142 1 my @fccode;
143 1 push @fccode, 'my %p = @_';
144 1 push @fccode, 'my $str';
145 1 for my $ospec (map { @{ $spec->{$_} } } @otypes) {
146 2 ▶ if (defined $ospec->{filter_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ospec->{"filter_text"}) 0 2 2
147 0 push @fccode, '$str = '._lit($ospec->{filter_text});
148 0 push @fccode, 'return 0 if $p{name} eq '._lit($ospec->{name}).
149 ' && index($_, $str) == -1';
150 }
151 2 ▶ if (defined $ospec->{filter_no_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ospec->{"filter_no_text"}) 0 2 2
152 0 push @fccode, '$str = '._lit($ospec->{filter_no_text});
153 0 push @fccode, 'return 0 if $p{name} eq '._lit($ospec->{name}).
154 ' && index($_, $str) > -1';
155 }
156 2 ▶ if (defined $ospec->{filter_citext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ospec->{"filter_citext"}) 0 2 2
157 0 push @fccode, '$str = '._lit($ospec->{filter_citext});
158 0 push @fccode, 'return 0 if $p{name} eq '._lit($ospec->{name}).
159 ' && !/\Q$str/io';
160 }
161 2 ▶ if (defined $ospec->{filter_no_citext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ospec->{"filter_no_citext"}) 0 2 2
162 0 push @fccode, '$str = '._lit($ospec->{filter_no_citext});
163 0 push @fccode, 'return 0 if $p{name} eq '._lit($ospec->{name}).
164 ' && /\Q$str/io';
165 }
166 2 ▶ if (defined $ospec->{filter_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ospec->{"filter_re"}) 0 2 2
167 0 push @fccode, '$str = '._lit($ospec->{filter_re});
168 push @fccode, 'return 0 if $p{name} eq '._lit($ospec->{name}).
169 0 ▶ ' && $_ !~ ' . (ref($ospec->{filter_re}) eq 'Regexp' ? '$str' : 'qr/$str/o');
Branch: 1 branch
BranchTrueFalseTotal
ref $ospec->{'filter_re'} eq 'Regexp' ? : 0 0 0
170 }
171 2 ▶ if (defined $ospec->{filter_no_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ospec->{"filter_no_re"}) 0 2 2
172 0 push @fccode, '$str = '._lit($ospec->{filter_no_re});
173 push @fccode, 'return 0 if $p{name} eq '._lit($ospec->{name}).
174 0 ▶ ' && $_ =~ ' . (ref($ospec->{filter_re}) eq 'Regexp' ? '$str' : 'qr/$str/o');
Branch: 1 branch
BranchTrueFalseTotal
ref $ospec->{'filter_re'} eq 'Regexp' ? : 0 0 0
175 }
176 }
177 1 push @fccode, "1";
178 1 my $fccode = join "; ", @fccode;
179  
180 my $filters_str = join(
181 "",
182 "log4perl.filter.FilterCustom = sub { $fccode }\n",
183 "\n",
184 "log4perl.filter.FilterOFF0 = Log::Log4perl::Filter::LevelRange\n",
185 "log4perl.filter.FilterOFF0.LevelMin = TRACE\n",
186 "log4perl.filter.FilterOFF0.LevelMax = FATAL\n",
187 "log4perl.filter.FilterOFF0.AcceptOnMatch = false\n",
188 "\n",
189 "log4perl.filter.FilterOFF = Log::Log4perl::Filter::Boolean\n",
190 "log4perl.filter.FilterOFF.logic = FilterOFF0 && FilterCustom\n",
191 1 map {join(
192 "",
193 "log4perl.filter.Filter${_}0 = Log::Log4perl::Filter::LevelRange\n",
194 "log4perl.filter.Filter${_}0.LevelMin = $_\n",
195 "log4perl.filter.Filter${_}0.LevelMax = FATAL\n",
196 "log4perl.filter.Filter${_}0.AcceptOnMatch = true\n",
197 "\n",
198 "log4perl.filter.Filter$_ = Log::Log4perl::Filter::Boolean\n",
199 "log4perl.filter.Filter$_.logic = Filter${_}0 && FilterCustom\n",
200 "\n",
201 )} qw(FATAL ERROR WARN INFO DEBUG), # TRACE
202 );
203  
204 1 my %levels; # key = output name; value = { cat => level, ... }
205 my %cats; # list of categories
206 1 my %ospecs; # key = oname; this is just a shortcut to get ospec
207  
208 # 1. list all levels for each category and output
209 1 for my $ospec (map { @{ $spec->{$_} } } @otypes) {
210 2 my $oname = $ospec->{name};
211 2 $ospecs{$oname} = $ospec;
212 2 $levels{$oname} = {};
213 2 my %seen_cats;
214 2 ▶ if ($ospec->{category_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ospec->{"category_level"}) 2 0 2
215 2 ▶ while (my ($cat0, $level) = each %{ $ospec->{category_level} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$ospec{"category_level"};}) 4 2 6
216 4 my @cat = _extract_category($ospec, $cat0);
217 4 for my $cat (@cat) {
218 4 ▶ next if $seen_cats{$cat}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_cats{$cat}++ 0 4 4
219 4 $cats{$cat}++;
220 4 $levels{$oname}{$cat} = $level;
221 }
222 }
223 }
224 2 ▶ if ($spec->{category_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"category_level"}) 2 0 2
225 2 ▶ while (my ($cat0, $level) = each %{ $spec->{category_level} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$spec{"category_level"};}) 2 2 4
226 2 my @cat = _extract_category($ospec, $cat0);
227 2 for my $cat (@cat) {
228 4 ▶ next if $seen_cats{$cat}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_cats{$cat}++ 0 4 4
229 4 $cats{$cat}++;
230 4 $levels{$oname}{$cat} = $level;
231 }
232 }
233 }
234 2 my @cat = _extract_category($ospec);
235 2 for my $cat (@cat) {
236 2 ▶ next if $seen_cats{$cat}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_cats{$cat}++ 0 2 2
237 2 $cats{$cat}++;
238 2 $levels{$oname}{$cat} = $ospec->{level};
239 }
240 }
241 #print Dumper \%levels; exit;
242  
243 40 ▶ my $find_olevel = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
244 40 my ($oname, $cat) = @_;
245 40 my $olevel = $levels{$oname}{''};
246 40 my @c = split /\./, $cat;
247 40 ▶ for (my $i=0; $i<@c; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @c) 64 40 104
248 64 my $c = join(".", @c[0..$i]);
249 64 ▶ if ($levels{$oname}{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($levels{$oname}{$c}) 42 22 64
250 42 $olevel = $levels{$oname}{$c};
251 }
252 }
253 40 $olevel;
254 1 };
255  
256 # 2. determine level for each category (which is the minimum level of all
257 # appenders for that category)
258 1 my %cat_configs; # key = cat, value = [catlevel, apdname, ...]
259 1 my $add_str = '';
260 1 my $apd_str = '';
261 1 for my $cat0 (sort {$a cmp $b} keys %cats) {
262 7 ▶ $add_str .= "log4perl.additivity.$cat0 = 0\n" unless $cat0 eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $cat0 eq "" 6 1 7
263 7 my @cats = ($cat0);
264 # since we don't use additivity, we need to add supercategories ourselves
265 7 ▶ while ($cat0 =~ s/\.[^.]+$//) { push @cats, $cat0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cat0 =~ s/\.[^.]+$//) 6 7 13
266 7 for my $cat (@cats) {
267 13 my $cat_level;
268 13 for my $oname (keys %levels) {
269 26 my $olevel = $find_olevel->($oname, $cat);
270 26 ▶ next unless $olevel;
Branch: 1 branch
BranchTrueFalseTotal
unless $olevel 0 26 26
271 26 $cat_level = _ifdef($cat_level, $olevel);
272 26 $cat_level = _min_level($cat_level, $olevel);
273 }
274 13 $cat_configs{$cat} = [uc($cat_level)];
275 #next if $cat_level eq 'off';
276 }
277 }
278 #print Dumper \%cat_configs; exit;
279  
280 # 3. add appenders for each category
281 1 my %generated_appenders; # key = apdname, just a memory hash
282 1 for my $cat (keys %cat_configs) {
283 7 my $cat_level = $cat_configs{$cat}[0];
284 7 for my $oname (keys %levels) {
285 14 my $ospec = $ospecs{$oname};
286 14 my $olevel = $find_olevel->($oname, $cat);
287 #print "D:oname=$oname, cat=$cat, olevel=$olevel, cat_level=$cat_level\n";
288 14 my $apd_name;
289 my $filter;
290 14 ▶ if ($olevel ne $cat_level &&
Branch: 1 branch
BranchTrueFalseTotal
if ($olevel ne $cat_level and _min_level($olevel, $cat_level) eq $cat_level) { } 5 9 14
Condition66%2/3
$olevel ne $cat_level and _min_level($olevel, $cat_level) eq $cat_level
!ll&&!rl&&r
095
MC/DC50%1/2
$olevel ne $cat_level and _min_level($olevel, $cat_level) eq $cat_level
$olevel ne $cat_level _min_level($olevel, $cat_level) eq $cat_level
Truth table66%2/3
A and B
A= $olevel ne $cat_levelB= _min_level($olevel, $cat_level) eq $cat_level
ABresult
0X0
100
111
291 _min_level($olevel, $cat_level) eq $cat_level) {
292 # we need to filter the appender, since the category level is
293 # lower than the output level
294 5 $apd_name = $oname . "_" . uc($olevel);
295 5 $filter = "Filter".uc($olevel);
296 } else {
297 9 $apd_name = $oname;
298 9 $filter = "FilterCustom";
299 }
300 14 ▶ unless ($generated_appenders{$apd_name}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($generated_appenders{$apd_name}++) 6 8 14
301 6 $apd_str .= _gen_appender_config($ospec, $apd_name, $filter).
302 "\n";
303 }
304 14 push @{ $cat_configs{$cat} }, $apd_name;
305 }
306 }
307 #print Dumper \%cat_configs; exit;
308  
309 # 4. write out log4perl category line
310 1 my $cat_str = '';
311 1 for my $cat (sort {$a cmp $b} keys %cat_configs) {
312 7 ▶ my $l = $cat eq '' ? '' : ".$cat";
Branch: 1 branch
BranchTrueFalseTotal
$cat eq '' ? : 1 6 7
313 7 $cat_str .= "log4perl.logger$l = ".join(", ", @{ $cat_configs{$cat} })."\n";
314 }
315  
316 join(
317 1 "",
318 "# filters\n", $filters_str,
319 "# categories\n", $cat_str, $add_str, "\n",
320 "# appenders\n", $apd_str,
321 );
322 }
323  
324 1 ▶sub _init_log4perl {
Subroutine _init_log4perl: called CC=8 SCAR=27.2
325 1 require Log::Log4perl;
326  
327 1 my ($spec) = @_;
328  
329 # create intermediate directories for dir
330 1 for (@{ $spec->{dir} }) {
331 0 my $dir = _dirname($_->{path});
332 0 ▶ make_path($dir) if length($dir) && !(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
if length $dir and not -d $dir 0 0 0
Condition0%0/3
length $dir and not -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
length $dir and not -d $dir
length $dir not -d $dir
Truth table0%0/3
A and not(B)
A= length $dirB= not -d $dir
ABresult
0X0
100
111
333 }
334  
335 # create intermediate directories for file
336 1 for (@{ $spec->{file} }) {
337 2 my $dir = _dirname($_->{path});
338 2 ▶ make_path($dir) if length($dir) && !(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
if length $dir and not -d $dir 0 2 2
Condition33%1/3
length $dir and not -d $dir
!ll&&!rl&&r
020
MC/DC0%0/2
length $dir and not -d $dir
length $dir not -d $dir
Truth table33%1/3
A and not(B)
A= length $dirB= not -d $dir
ABresult
0X0
100
111
339 }
340  
341 1 my $config_str = _gen_l4p_config($spec);
342 1 ▶ if ($spec->{dump}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"dump"}) 0 1 1
343 0 require Data::Dump;
344 0 print "Log::Any::App configuration:\n",
345 Data::Dump::dump($spec);
346 0 print "Log4perl configuration: <<EOC\n", $config_str, "EOC\n";
347 }
348  
349 1 Log::Log4perl->init(\$config_str);
350 1 Log::Any::Adapter->set('Log4perl');
351 }
352  
353 49 ▶sub _basename {
Subroutine _basename: called CC=1 SCAR=0.0
354 49 my $path = shift;
355 49 my ($vol, $dir, $file) = File::Spec->splitpath($path);
356 49 $file;
357 }
358  
359 2 ▶sub _dirname {
Subroutine _dirname: called CC=1 SCAR=0.0
360 2 my $path = shift;
361 2 my ($vol, $dir, $file) = File::Spec->splitpath($path);
362 2 $dir;
363 }
364  
365 # we separate args and opts, because we need to export logger early
366 # (BEGIN), but configure logger in INIT (to be able to detect
367 # existence of other modules).
368  
369 2 ▶sub _parse_args {
Subroutine _parse_args: called CC=4 SCAR=13.9
370 2 my ($args, $caller) = @_;
371 2 $args = _ifdef($args, []); # if we don't import(), we never get args
372  
373 2 my $i = 0;
374 2 ▶ while ($i < @$args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$args) 10 2 12
375 10 my $arg = $args->[$i];
376 10 ▶ do { $i+=2; next } if $arg =~ /^-(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^-(\w+)$/) 9 1 10
377 1 ▶ if ($arg eq '$log') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq '$log') { } 1 0 1
378 1 _export_logger($caller);
379 } else {
380 0 die "Unknown arg '$arg', valid arg is '\$log' or -OPTS";
381 }
382 1 $i++;
383 }
384 }
385  
386 49 ▶sub _parse_opts {
Subroutine _parse_opts: called CC=27 SCAR=35.3
387 49 require File::HomeDir;
388  
389 49 my ($args, $caller) = @_;
390 49 $args = _ifdef($args, []); # if we don't import(), we never get args
391 49 _debug("parse_opts: args = [".join(", ", @$args)."]");
392  
393 49 my $i = 0;
394 49 my %opts;
395 49 ▶ while ($i < @$args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$args) 73 49 122
396 73 my $arg = $args->[$i];
397 73 ▶ do { $i++; next } unless $arg =~ /^-(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^-(\w+)$/) 1 72 73
398 72 my $opt = $1;
399 72 ▶ die "Missing argument for option $opt" unless $i++ < @$args-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $i++ < @$args - 1 0 72 72
400 72 $arg = $args->[$i];
401 72 $opts{$opt} = $arg;
402 72 $i++;
403 }
404  
405 49 my $spec = {};
406  
407 49 $spec->{log} = _ifdef($ENV{LOG}, 1);
408 49 ▶ if (defined $opts{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"log"}) 0 49 49
409 0 $spec->{log} = $opts{log};
410 0 delete $opts{log};
411 }
412 # exit as early as possible if we are not doing any logging
413 49 ▶ goto END_PARSE_OPTS unless $spec->{log};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"log"} 0 49 49
414  
415 49 $spec->{name} = _basename($0);
416 49 ▶ if (defined $opts{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 6 43 49
417 6 $spec->{name} = $opts{name};
418 6 delete $opts{name};
419 }
420  
421 49 $spec->{level_flag_paths} = [File::HomeDir->my_home, "/etc"];
422 49 ▶ if (defined $opts{level_flag_paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"level_flag_paths"}) 4 45 49
423 4 $spec->{level_flag_paths} = $opts{level_flag_paths};
424 4 delete $opts{level_flag_paths};
425 }
426  
427 49 $spec->{level} = _set_level("", "", $spec);
428 49 ▶ if (!$spec->{level} && defined($opts{level})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $spec->{'level'} and defined $opts{'level'}) { } 0 49 49
elsif (not $spec->{'level'}) { } 31 18 49
Condition66%2/3
not $spec->{"level"} and defined $opts{"level"}
!ll&&!rl&&r
18310
MC/DC0%0/2
not $spec->{"level"} and defined $opts{"level"}
not $spec->{"level"} defined $opts{"level"}
Truth table66%2/3
not(A) and B
A= not $spec->{"level"}B= defined $opts{"level"}
ABresult
0X0
100
111
429 0 $spec->{level} = _check_level($opts{level}, "-level");
430 0 _debug("Set general level to $spec->{level} (from -level)");
431 } elsif (!$spec->{level}) {
432 31 $spec->{level} = "warn";
433 31 _debug("Set general level to $spec->{level} (default)");
434 }
435 49 delete $opts{level};
436  
437 49 $spec->{category_alias} = _ifdefj($ENV{LOG_CATEGORY_ALIAS}, {});
438 49 ▶ if (defined $opts{category_alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"category_alias"}) 1 48 49
439 die "category_alias must be a hashref"
440 1 ▶ unless ref($opts{category_alias}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"category_alias"} eq "HASH" 0 1 1
441 1 $spec->{category_alias} = $opts{category_alias};
442 1 delete $opts{category_alias};
443 }
444  
445 49 ▶ if (defined $opts{category_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"category_level"}) 1 48 49
446 die "category_level must be a hashref"
447 1 ▶ unless ref($opts{category_level}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"category_level"} eq "HASH" 0 1 1
448 1 $spec->{category_level} = {};
449 1 for (keys %{ $opts{category_level} }) {
450 $spec->{category_level}{$_} =
451 1 _check_level($opts{category_level}{$_}, "-category_level{$_}");
452 }
453 1 delete $opts{category_level};
454 }
455  
456 49 $spec->{init} = 1;
457 49 ▶ if (defined $opts{init}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"init"}) 48 1 49
458 48 $spec->{init} = $opts{init};
459 48 delete $opts{init};
460 }
461  
462 49 $spec->{daemon} = 0;
463 49 ▶ if (defined $opts{daemon}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"daemon"}) 2 47 49
464 2 $spec->{daemon} = $opts{daemon};
465 2 _debug("setting is_daemon=$opts{daemon} (from daemon option)");
466 2 $is_daemon = $opts{daemon};
467 2 delete $opts{daemon};
468 }
469  
470 49 $spec->{dump} = $ENV{LOGANYAPP_DEBUG};
471 49 ▶ if (defined $opts{dump}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"dump"}) 0 49 49
472 0 $spec->{dump} = 1;
473 0 delete $opts{dump};
474 }
475  
476 49 $spec->{filter_text} = $ENV{LOG_FILTER_TEXT};
477 49 ▶ if (defined $opts{filter_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter_text"}) 0 49 49
478 0 $spec->{filter_text} = $opts{filter_text};
479 0 delete $opts{filter_text};
480 }
481 49 $spec->{filter_no_text} = $ENV{LOG_FILTER_NO_TEXT};
482 49 ▶ if (defined $opts{filter_no_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter_no_text"}) 0 49 49
483 0 $spec->{filter_no_text} = $opts{filter_no_text};
484 0 delete $opts{filter_no_text};
485 }
486 49 $spec->{filter_citext} = $ENV{LOG_FILTER_CITEXT};
487 49 ▶ if (defined $opts{filter_citext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter_citext"}) 0 49 49
488 0 $spec->{filter_citext} = $opts{filter_citext};
489 0 delete $opts{filter_citext};
490 }
491 49 $spec->{filter_no_citext} = $ENV{LOG_FILTER_NO_CITEXT};
492 49 ▶ if (defined $opts{filter_no_citext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter_no_citext"}) 0 49 49
493 0 $spec->{filter_no_citext} = $opts{filter_no_citext};
494 0 delete $opts{filter_no_citext};
495 }
496 49 $spec->{filter_re} = $ENV{LOG_FILTER_RE};
497 49 ▶ if (defined $opts{filter_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter_re"}) 0 49 49
498 0 $spec->{filter_re} = $opts{filter_re};
499 0 delete $opts{filter_re};
500 }
501 49 $spec->{filter_no_re} = $ENV{LOG_FILTER_NO_RE};
502 49 ▶ if (defined $opts{filter_no_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter_no_re"}) 0 49 49
503 0 $spec->{filter_no_re} = $opts{filter_no_re};
504 0 delete $opts{filter_no_re};
505 }
506  
507 49 $spec->{file} = [];
508 49 ▶ _parse_opt_file($spec, _ifdef($opts{file}, ($0 ne '-e' ? 1:0)));
Branch: 1 branch
BranchTrueFalseTotal
$0 ne '-e' ? : 48 1 49
509 49 delete $opts{file};
510  
511 49 $spec->{dir} = [];
512 49 _parse_opt_dir($spec, _ifdef($opts{dir}, 0));
513 49 delete $opts{dir};
514  
515 49 $spec->{screen} = [];
516 49 _parse_opt_screen($spec, _ifdef($opts{screen}, !_is_daemon()));
517 49 delete $opts{screen};
518  
519 49 $spec->{syslog} = [];
520 49 _parse_opt_syslog($spec, _ifdef($opts{syslog}, _is_daemon()));
521 49 delete $opts{syslog};
522  
523 49 $spec->{unixsock} = [];
524 49 _parse_opt_unixsock($spec, _ifdef($opts{unixsock}, 0));
525 49 delete $opts{unixsock};
526  
527 49 $spec->{array} = [];
528 49 _parse_opt_array($spec, _ifdef($opts{array}, 0));
529 49 delete $opts{array};
530  
531 49 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 0 49 49
532 0 die "Unknown option(s) ".join(", ", keys %opts)." Known opts are: ".
533 "log, name, level, category_level, category_alias, dump, init, ".
534 "filter_{,no_}{text,citext,re}, file, dir, screen, syslog, ".
535 "unixsock, array";
536 }
537  
538 END_PARSE_OPTS:
539 #use Data::Dump; dd $spec;
540 49 $spec;
541 }
542  
543 98 ▶sub _is_daemon {
Subroutine _is_daemon: called CC=5 SCAR=16.1
544 98 ▶ if (defined $is_daemon) { return $is_daemon }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $is_daemon) 51 47 98
545 47 ▶ if (defined $main::IS_DAEMON) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::IS_DAEMON) 1 46 47
546 1 $is_daemon = $main::IS_DAEMON;
547 1 _debug("Setting is_daemon=$main::IS_DAEMON (from \$main::IS_DAEMON)");
548 1 return $main::IS_DAEMON;
549 }
550  
551 46 for (
552 "App/Daemon.pm",
553 "Daemon/Easy.pm",
554 "Daemon/Daemonize.pm",
555 "Daemon/Generic.pm",
556 "Daemonise.pm",
557 "Daemon/Simple.pm",
558 "HTTP/Daemon.pm",
559 "IO/Socket/INET/Daemon.pm",
560 #"Mojo/Server/Daemon.pm", # simply loading Mojo::UserAgent will load this too
561 "MooseX/Daemonize.pm",
562 "Net/Daemon.pm",
563 "Net/Server.pm",
564 "Proc/Daemon.pm",
565 "Proc/PID/File.pm",
566 "Win32/Daemon/Simple.pm") {
567 636 ▶ if ($INC{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{$_}) 2 634 636
568 2 _debug("setting is_daemon=1 (from existence of module $_)");
569 2 $is_daemon = 1;
570 2 return 1;
571 }
572 }
573 44 _debug("setting is_daemon=0 (no indication that we are a daemon)");
574 44 $is_daemon = 0;
575 44 0;
576 }
577  
578 296 ▶sub _parse_opt_OUTPUT {
Subroutine _parse_opt_OUTPUT: called CC=13 SCAR=25.7
579 296 my (%args) = @_;
580 296 my $kind = $args{kind};
581 296 my $default_sub = $args{default_sub};
582 296 my $postprocess = $args{postprocess};
583 296 my $spec = $args{spec};
584 296 my $arg = $args{arg};
585  
586 296 ▶ return unless $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 200 96 296
587  
588 96 ▶ if (!ref($arg) || ref($arg) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $arg or ref $arg eq 'HASH') { } 95 1 96
elsif (ref $arg eq 'ARRAY') { } 1 0 1
Condition100%3/3
not ref $arg or ref $arg eq "HASH"
l!l&&r!l&&!r
8961
MC/DC100%2/2
not ref $arg or ref $arg eq "HASH"
not ref $arg ref $arg eq "HASH"
Truth table100%3/3
not(A) or B
A= not ref $argB= ref $arg eq "HASH"
ABresult
1X1
011
000
589 95 my $name = uc($kind).(@{ $spec->{$kind} }+0);
590 95 local $dbg_ctx = $name;
591 95 push @{ $spec->{$kind} }, $default_sub->($spec);
592 95 $spec->{$kind}[-1]{name} = $name;
593 95 ▶ if (!ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $arg) { } 89 6 95
594 # leave every output parameter as is
595 } else {
596 6 for my $k (keys %$arg) {
597 12 for ($spec->{$kind}[-1]) {
598 12 ▶ exists($_->{$k}) or die "Invalid $kind argument: $k, please".
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{$k} or die "Invalid $kind argument: $k, please" . ' only specify one of: ' . join(", ", sort(keys %$_)) 12 0 12
599 " only specify one of: " . join(", ", sort keys %$_);
600 $_->{$k} = $k eq 'level' ?
601 12 ▶ _check_level($arg->{$k}, "-$kind") : $arg->{$k};
Branch: 1 branch
BranchTrueFalseTotal
$k eq 'level' ? : 2 10 12
602 12 ▶ _debug("Set level of $kind to $_->{$k} (spec)")
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "level" 2 10 12
603 if $k eq 'level';
604 }
605 }
606 }
607 95 $spec->{$kind}[-1]{main_spec} = $spec;
608 95 _set_pattern($spec->{$kind}[-1], $kind);
609 95 ▶ $postprocess->(spec => $spec, ospec => $spec->{$kind}[-1])
Branch: 1 branch
BranchTrueFalseTotal
if $postprocess 48 47 95
610 if $postprocess;
611 } elsif (ref($arg) eq 'ARRAY') {
612 1 for (@$arg) {
613 2 _parse_opt_OUTPUT(%args, arg => $_);
614 }
615 } else {
616 0 die "Invalid argument for -$kind, ".
617 "must be a boolean or hashref or arrayref";
618 }
619 }
620  
621 95 ▶sub _set_pattern_style {
Subroutine _set_pattern_style: called CC=3 SCAR=12.0
622 95 my ($x) = @_;
623 ($ENV{LOG_SHOW_LOCATION} ? 'loc_':
624 95 ▶ $ENV{LOG_SHOW_CATEGORY} ? 'cat_':'') . $x;
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'LOG_SHOW_LOCATION'} ? : 0 95 95
$ENV{'LOG_SHOW_CATEGORY'} ? : 0 95 95
625 }
626  
627 48 ▶sub _default_file {
Subroutine _default_file: called CC=3 SCAR=11.0
628 48 require File::HomeDir;
629  
630 48 my ($spec) = @_;
631 48 my $level = _set_level("file", "file", $spec);
632 48 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 39 9 48
633 39 $level = $spec->{level};
634 39 _debug("Set level of file to $level (general level)");
635 }
636 return {
637 level => $level,
638 category_level => _ifdefj($ENV{FILE_LOG_CATEGORY_LEVEL},
639 $ENV{LOG_CATEGORY_LEVEL},
640 $spec->{category_level}),
641 path => $> ? File::Spec->catfile(File::HomeDir->my_home, "$spec->{name}.log") :
642 "/var/log/$spec->{name}.log", # XXX and on Windows?
643 max_size => undef,
644 histories => undef,
645 period => undef,
646 buffer_size => undef,
647 category => '',
648 pattern_style => _set_pattern_style('daemon'),
649 pattern => undef,
650  
651 filter_text => _ifdef($ENV{FILE_LOG_FILTER_TEXT}, $spec->{filter_text}),
652 filter_no_text => _ifdef($ENV{FILE_LOG_FILTER_NO_TEXT}, $spec->{filter_no_text}),
653 filter_citext => _ifdef($ENV{FILE_LOG_FILTER_CITEXT}, $spec->{filter_citext}),
654 filter_no_citext => _ifdef($ENV{FILE_LOG_FILTER_NO_CITEXT}, $spec->{filter_no_citext}),
655 filter_re => _ifdef($ENV{FILE_LOG_FILTER_RE}, $spec->{filter_re}),
656 48 ▶ filter_no_re => _ifdef($ENV{FILE_LOG_FILTER_NO_RE}, $spec->{filter_no_re}),
Branch: 1 branch
BranchTrueFalseTotal
$> ? : 48 0 48
657 };
658 }
659  
660 49 ▶sub _parse_opt_file {
Subroutine _parse_opt_file: called CC=4 SCAR=13.9
661 49 my ($spec, $arg) = @_;
662  
663 49 ▶ if (!ref($arg) && $arg && $arg !~ /^(1|yes|true)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i) 2 47 49
Condition100%6/6
not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i
!ll&&!rl&&r
5422
not ref $arg and $arg
!ll&&!rl&&r
3244
MC/DC100%3/3
not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i
not ref $arg $arg not $arg =~ /^(1|yes|true)$/i
Truth table100%4/4
not(A) and B and not(C)
A= not ref $argB= $argC= not $arg =~ /^(1|yes|true)$/i
ABCresult
0XX0
10X0
1100
1111
664 2 $arg = {path => $arg};
665 }
666  
667 _parse_opt_OUTPUT(
668 kind => 'file', default_sub => \&_default_file,
669 spec => $spec, arg => $arg,
670 48 ▶ postprocess => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
671 48 my (%args) = @_;
672 48 my $spec = $args{spec};
673 48 my $ospec = $args{ospec};
674 48 ▶ if ($ospec->{path} =~ m!/$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ospec->{"path"} =~ m[/$]) 2 46 48
675 2 my $p = $ospec->{path};
676 2 $p .= "$spec->{name}.log";
677 2 _debug("File path ends with /, assumed to be dir, ".
678 "final path becomes $p");
679 2 $ospec->{path} = $p;
680 }
681 },
682 49 );
683 }
684  
685 0 ▶sub _default_dir {
Subroutine _default_dir: not called CC=3 SCAR=24.8
686 0 require File::HomeDir;
687  
688 0 my ($spec) = @_;
689 0 my $level = _set_level("dir", "dir", $spec);
690 0 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 0 0 0
691 0 $level = $spec->{level};
692 0 _debug("Set level of dir to $level (general level)");
693 }
694 return {
695 level => $level,
696 category_level => _ifdefj($ENV{DIR_LOG_CATEGORY_LEVEL},
697 $ENV{LOG_CATEGORY_LEVEL},
698 $spec->{category_level}),
699 path => $> ? File::Spec->catfile(File::HomeDir->my_home, "log", $spec->{name}) :
700 "/var/log/$spec->{name}", # XXX and on Windows?
701 max_size => undef,
702 max_age => undef,
703 histories => undef,
704 category => '',
705 pattern_style => _set_pattern_style('plain'),
706 pattern => undef,
707 filename_pattern => undef,
708  
709 filter_text => _ifdef($ENV{DIR_LOG_FILTER_TEXT}, $spec->{filter_text}),
710 filter_no_text => _ifdef($ENV{DIR_LOG_FILTER_NO_TEXT}, $spec->{filter_no_text}),
711 filter_citext => _ifdef($ENV{DIR_LOG_FILTER_CITEXT}, $spec->{filter_citext}),
712 filter_no_citext => _ifdef($ENV{DIR_LOG_FILTER_NO_CITEXT}, $spec->{filter_no_citext}),
713 filter_re => _ifdef($ENV{DIR_LOG_FILTER_RE}, $spec->{filter_re}),
714 0 ▶ filter_no_re => _ifdef($ENV{DIR_LOG_FILTER_NO_RE}, $spec->{filter_no_re}),
Branch: 1 branch
BranchTrueFalseTotal
$> ? : 0 0 0
715 };
716 }
717  
718 49 ▶sub _parse_opt_dir {
Subroutine _parse_opt_dir: called CC=4 SCAR=17.9
719 49 my ($spec, $arg) = @_;
720  
721 49 ▶ if (!ref($arg) && $arg && $arg !~ /^(1|yes|true)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i) 0 49 49
Condition33%2/6
not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i
!ll&&!rl&&r
4900
not ref $arg and $arg
!ll&&!rl&&r
0490
MC/DC0%0/3
not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i
not ref $arg $arg not $arg =~ /^(1|yes|true)$/i
Truth table25%1/4
not(A) and B and not(C)
A= not ref $argB= $argC= not $arg =~ /^(1|yes|true)$/i
ABCresult
0XX0
10X0
1100
1111
722 0 $arg = {path => $arg};
723 }
724  
725 _parse_opt_OUTPUT(
726 49 kind => 'dir', default_sub => \&_default_dir,
727 spec => $spec, arg => $arg,
728 );
729 }
730  
731 43 ▶sub _default_screen {
Subroutine _default_screen: called CC=3 SCAR=11.0
732 43 my ($spec) = @_;
733 43 my $level = _set_level("screen", "screen", $spec);
734 43 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 34 9 43
735 34 $level = $spec->{level};
736 34 _debug("Set level of screen to $level (general level)");
737 }
738 return {
739 color => _ifdef($ENV{COLOR}, (-t STDOUT)),
740 stderr => 1,
741 level => $level,
742 category_level => _ifdefj($ENV{SCREEN_LOG_CATEGORY_LEVEL},
743 $ENV{LOG_CATEGORY_LEVEL},
744 $spec->{category_level}),
745 category => '',
746 pattern_style => _set_pattern_style(
747 $ENV{LOG_ELAPSED_TIME_IN_SCREEN} ? 'script_short' : 'plain_nl'),
748 pattern => undef,
749  
750 filter_text => _ifdef($ENV{SCREEN_LOG_FILTER_TEXT}, $spec->{filter_text}),
751 filter_no_text => _ifdef($ENV{SCREEN_FILTER_NO_TEXT}, $spec->{filter_no_text}),
752 filter_citext => _ifdef($ENV{SCREEN_FILTER_CITEXT}, $spec->{filter_citext}),
753 filter_no_citext => _ifdef($ENV{SCREEN_FILTER_NO_CITEXT}, $spec->{filter_no_citext}),
754 filter_re => _ifdef($ENV{SCREEN_FILTER_RE}, $spec->{filter_re}),
755 43 ▶ filter_no_re => _ifdef($ENV{SCREEN_FILTER_NO_RE}, $spec->{filter_no_re}),
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'LOG_ELAPSED_TIME_IN_SCREEN'} ? : 0 43 43
756 };
757 }
758  
759 49 ▶sub _parse_opt_screen {
Subroutine _parse_opt_screen: called CC=1 SCAR=0.0
760 49 my ($spec, $arg) = @_;
761 49 _parse_opt_OUTPUT(
762 kind => 'screen', default_sub => \&_default_screen,
763 spec => $spec, arg => $arg,
764 );
765 }
766  
767 4 ▶sub _default_syslog {
Subroutine _default_syslog: called CC=2 SCAR=7.0
768 4 my ($spec) = @_;
769 4 my $level = _set_level("syslog", "syslog", $spec);
770 4 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 4 0 4
771 4 $level = $spec->{level};
772 4 _debug("Set level of syslog to $level (general level)");
773 }
774 return {
775 level => $level,
776 category_level => _ifdefj($ENV{SYSLOG_LOG_CATEGORY_LEVEL},
777 $ENV{LOG_CATEGORY_LEVEL},
778 $spec->{category_level}),
779 ident => $spec->{name},
780 facility => 'daemon',
781 pattern_style => _set_pattern_style('syslog'),
782 pattern => undef,
783 category => '',
784  
785 filter_text => _ifdef($ENV{SYSLOG_LOG_FILTER_TEXT}, $spec->{filter_text}),
786 filter_no_text => _ifdef($ENV{SYSLOG_FILTER_NO_TEXT}, $spec->{filter_no_text}),
787 filter_citext => _ifdef($ENV{SYSLOG_FILTER_CITEXT}, $spec->{filter_citext}),
788 filter_no_citext => _ifdef($ENV{SYSLOG_FILTER_NO_CITEXT}, $spec->{filter_no_citext}),
789 filter_re => _ifdef($ENV{SYSLOG_FILTER_RE}, $spec->{filter_re}),
790 4 filter_no_re => _ifdef($ENV{SYSLOG_FILTER_NO_RE}, $spec->{filter_no_re}),
791 };
792 }
793  
794 49 ▶sub _parse_opt_syslog {
Subroutine _parse_opt_syslog: called CC=1 SCAR=0.0
795 49 my ($spec, $arg) = @_;
796 49 _parse_opt_OUTPUT(
797 kind => 'syslog', default_sub => \&_default_syslog,
798 spec => $spec, arg => $arg,
799 );
800 }
801  
802 0 ▶sub _default_unixsock {
Subroutine _default_unixsock: not called CC=3 SCAR=24.8
803 0 require File::HomeDir;
804  
805 0 my ($spec) = @_;
806 0 my $level = _set_level("unixsock", "unixsock", $spec);
807 0 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 0 0 0
808 0 $level = $spec->{level};
809 0 _debug("Set level of unixsock to $level (general level)");
810 }
811 return {
812 level => $level,
813 category_level => _ifdefj($ENV{UNIXSOCK_LOG_CATEGORY_LEVEL},
814 $ENV{LOG_CATEGORY_LEVEL},
815 $spec->{category_level}),
816 path => $> ? File::Spec->catfile(File::HomeDir->my_home, "$spec->{name}-log.sock") :
817 "/var/run/$spec->{name}-log.sock", # XXX and on Windows?
818 category => '',
819 pattern_style => _set_pattern_style('daemon'),
820 pattern => undef,
821  
822 filter_text => _ifdef($ENV{UNIXSOCK_LOG_FILTER_TEXT}, $spec->{filter_text}),
823 filter_no_text => _ifdef($ENV{UNIXSOCK_LOG_FILTER_NO_TEXT}, $spec->{filter_no_text}),
824 filter_citext => _ifdef($ENV{UNIXSOCK_LOG_FILTER_CITEXT}, $spec->{filter_citext}),
825 filter_no_citext => _ifdef($ENV{UNIXSOCK_LOG_FILTER_NO_CITEXT}, $spec->{filter_no_citext}),
826 filter_re => _ifdef($ENV{UNIXSOCK_LOG_FILTER_RE}, $spec->{filter_re}),
827 0 ▶ filter_no_re => _ifdef($ENV{UNIXSOCK_LOG_FILTER_NO_RE}, $spec->{filter_no_re}),
Branch: 1 branch
BranchTrueFalseTotal
$> ? : 0 0 0
828 };
829 }
830  
831 49 ▶sub _parse_opt_unixsock {
Subroutine _parse_opt_unixsock: called CC=4 SCAR=23.2
832 49 my ($spec, $arg) = @_;
833  
834 49 ▶ if (!ref($arg) && $arg && $arg !~ /^(1|yes|true)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i) 0 49 49
Condition33%2/6
not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i
!ll&&!rl&&r
4900
not ref $arg and $arg
!ll&&!rl&&r
0490
MC/DC0%0/3
not ref $arg and $arg and not $arg =~ /^(1|yes|true)$/i
not ref $arg $arg not $arg =~ /^(1|yes|true)$/i
Truth table25%1/4
not(A) and B and not(C)
A= not ref $argB= $argC= not $arg =~ /^(1|yes|true)$/i
ABCresult
0XX0
10X0
1100
1111
835 0 $arg = {path => $arg};
836 }
837  
838 _parse_opt_OUTPUT(
839 kind => 'unixsock', default_sub => \&_default_unixsock,
840 spec => $spec, arg => $arg,
841 0 ▶ postprocess => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
842 0 my (%args) = @_;
843 0 my $spec = $args{spec};
844 0 my $ospec = $args{ospec};
845 0 ▶ if ($ospec->{path} =~ m!/$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ospec->{"path"} =~ m[/$]) 0 0 0
846 0 my $p = $ospec->{path};
847 0 $p .= "$spec->{name}-log.sock";
848 0 _debug("Unix socket path ends with /, assumed to be dir, ".
849 "final path becomes $p");
850 0 $ospec->{path} = $p;
851 }
852  
853 # currently Log::Log4perl::Appender::Socket::UNIX *connects to an
854 # existing and listening* Unix socket and prints log to it. we are
855 # *not* creating a listening unix socket where clients can connect
856 # and see logs. to do that, we'll need a separate thread/process
857 # that listens to unix socket and stores (some) log entries and
858 # display it to users when they connect and request them.
859 #
860 #if ($ospec->{create} && !(-e $ospec->{path})) {
861 # _debug("Creating Unix socket $ospec->{path} ...");
862 # require IO::Socket::UNIX::Util;
863 # IO::Socket::UNIX::Util::create_unix_socket(
864 # $ospec->{path});
865 #}
866 },
867 49 );
868 }
869  
870 0 ▶sub _default_array {
Subroutine _default_array: not called CC=2 SCAR=17.9
871 0 my ($spec) = @_;
872 0 my $level = _set_level("array", "array", $spec);
873 0 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 0 0 0
874 0 $level = $spec->{level};
875 0 _debug("Set level of array to $level (general level)");
876 }
877 return {
878 level => $level,
879 category_level => _ifdefj($ENV{ARRAY_LOG_CATEGORY_LEVEL},
880 $ENV{LOG_CATEGORY_LEVEL},
881 $spec->{category_level}),
882 array => [],
883 max_elems => undef,
884 category => '',
885 pattern_style => _set_pattern_style('script_long'),
886 pattern => undef,
887  
888 filter_text => _ifdef($ENV{ARRAY_LOG_FILTER_TEXT}, $spec->{filter_text}),
889 filter_no_text => _ifdef($ENV{ARRAY_LOG_FILTER_NO_TEXT}, $spec->{filter_no_text}),
890 filter_citext => _ifdef($ENV{ARRAY_LOG_FILTER_CITEXT}, $spec->{filter_citext}),
891 filter_no_citext => _ifdef($ENV{ARRAY_LOG_FILTER_NO_CITEXT}, $spec->{filter_no_citext}),
892 filter_re => _ifdef($ENV{ARRAY_LOG_FILTER_RE}, $spec->{filter_re}),
893 0 filter_no_re => _ifdef($ENV{ARRAY_LOG_FILTER_NO_RE}, $spec->{filter_no_re}),
894 };
895 }
896  
897 49 ▶sub _parse_opt_array {
Subroutine _parse_opt_array: called CC=1 SCAR=0.0
898 49 my ($spec, $arg) = @_;
899  
900 49 _parse_opt_OUTPUT(
901 kind => 'array', default_sub => \&_default_array,
902 spec => $spec, arg => $arg,
903 );
904 }
905  
906 95 ▶sub _set_pattern {
Subroutine _set_pattern: called CC=4 SCAR=14.3
907 95 my ($s, $name) = @_;
908 95 _debug("Setting $name pattern ...");
909 95 ▶ unless (defined($s->{pattern})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s->{"pattern"}) 95 0 95
910 die "BUG: neither pattern nor pattern_style is defined ($name)"
911 95 ▶ unless defined($s->{pattern_style});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->{"pattern_style"} 0 95 95
912 die "Unknown pattern style for $name `$s->{pattern_style}`, ".
913 "use one of: ".join(", ", keys %PATTERN_STYLES)
914 95 ▶ unless defined($PATTERN_STYLES{ $s->{pattern_style} });
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Log::Any::App::PATTERN_STYLES{$s->{"pattern_style"}} 0 95 95
915 95 $s->{pattern} = $PATTERN_STYLES{ $s->{pattern_style} };
916 95 _debug("Set $name pattern to `$s->{pattern}` ".
917 "(from style `$s->{pattern_style}`)");
918 }
919 }
920  
921 8 ▶sub _extract_category {
Subroutine _extract_category: called CC=6 SCAR=18.2
922 8 my ($ospec, $c) = @_;
923 8 my $c0 = _ifdef($c, $ospec->{category});
924 8 my @res;
925 0 ▶ if (ref($c0) eq 'ARRAY') { @res = @$c0 } else { @res = ($c0) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c0 eq 'ARRAY') { } 0 8 8
926 # replace alias with real value
927 8 ▶ for (my $i=0; $i<@res; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @res) 8 8 16
928 8 my $c1 = $res[$i];
929 8 my $a = $ospec->{main_spec}{category_alias}{$c1};
930 8 ▶ next unless defined($a);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 6 2 8
931 2 ▶ if (ref($a) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'ARRAY') { } 2 0 2
932 2 splice @res, $i, 1, @$a;
933 2 $i += (@$a-1);
934 } else {
935 0 $res[$i] = $a;
936 }
937 }
938 8 for (@res) {
939 10 s/::/./g;
940 # $_ = lc; # XXX do we need this?
941 }
942 8 @res;
943 }
944  
945 0 ▶sub _cat2apd {
Subroutine _cat2apd: not called CC=1 SCAR=6.9
946 0 my $cat = shift;
947 0 $cat =~ s/[^A-Za-z0-9_]+/_/g;
948 0 $cat;
949 }
950  
951 25 ▶sub _check_level {
Subroutine _check_level: called CC=2 SCAR=7.1
952 25 my ($level, $from) = @_;
953 25 ▶ $level =~ /^(off|fatal|error|warn|info|debug|trace)$/i
Branch: 1 branch
BranchTrueFalseTotal
$level =~ /^(off|fatal|error|warn|info|debug|trace)$/i or die "Unknown level (from $from): $level" 25 0 25
954 or die "Unknown level (from $from): $level";
955 25 lc($1);
956 }
957  
958 144 ▶sub _set_level {
Subroutine _set_level: called CC=37 SCAR=37.7
959 144 my ($prefix, $which, $spec) = @_;
960 #use Data::Dump; dd $spec;
961 144 ▶ my $p_ = $prefix ? "${prefix}_" : "";
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 95 49 144
962 144 ▶ my $P_ = $prefix ? uc("${prefix}_") : "";
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 95 49 144
963 144 ▶ my $F_ = $prefix ? ucfirst("${prefix}_") : "";
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 95 49 144
964 144 ▶ my $pd = $prefix ? "${prefix}-" : "";
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 95 49 144
965 144 ▶ my $pr = $prefix ? qr/$prefix(_|-)/ : qr//;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 95 49 144
966 144 my ($level, $from);
967  
968 144 my @label2level =([trace=>"trace"], [debug=>"debug"],
969 [verbose=>"info"], [quiet=>"error"]);
970  
971 144 ▶ _debug("Setting ", ($which ? "level of $which" : "general level"), " ...");
Branch: 1 branch
BranchTrueFalseTotal
$which ? : 95 49 144
972 SET:
973 {
974 144 ▶ if ($INC{"App/Options.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"App/Options.pm"}) 0 144 144
975 0 my $key;
976 0 for (qw/log_level loglevel/) {
977 0 $key = $p_ . $_;
978 0 _debug("Checking \$App::options{$key}: ", _ifdef($App::options{$key}, "(undef)"));
979 0 ▶ if ($App::options{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::options{$key}) 0 0 0
980 0 $level = _check_level($App::options{$key}, "\$App::options{$key}");
981 0 $from = "\$App::options{$key}";
982 0 last SET;
983 }
984 }
985 0 for (@label2level) {
986 0 $key = $p_ . $_->[0];
987 0 _debug("Checking \$App::options{$key}: ", _ifdef($App::options{$key}, "(undef)"));
988 0 ▶ if ($App::options{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::options{$key}) 0 0 0
989 0 $level = $_->[1];
990 0 $from = "\$App::options{$key}";
991 0 last SET;
992 }
993 }
994 }
995  
996 144 my $i = 0;
997 144 _debug("Checking \@ARGV ...");
998 144 ▶ while ($i < @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ARGV) 36 136 172
999 36 my $arg = $ARGV[$i];
1000 36 $from = "cmdline arg $arg";
1001 36 ▶ if ($arg =~ /^--${pr}log[_-]?level=(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--${pr}log[_-]?level=(.+)/) 0 36 36
1002 0 _debug("\$ARGV[$i] looks like an option to specify level: $arg");
1003 0 $level = _check_level($1, "ARGV $arg");
1004 0 last SET;
1005 }
1006 36 ▶ if ($arg =~ /^--${pr}log[_-]?level$/ and $i < @ARGV-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--${pr}log[_-]?level$/ and $i < @ARGV - 1) 6 30 36
Condition66%2/3
$arg =~ /^--${pr}log[_-]?level$/ and $i < @ARGV - 1
!ll&&!rl&&r
3006
MC/DC50%1/2
$arg =~ /^--${pr}log[_-]?level$/ and $i < @ARGV - 1
$arg =~ /^--${pr}log[_-]?level$/ $i < @ARGV - 1
Truth table66%2/3
A and B
A= $arg =~ /^--${pr}log[_-]?level$/B= $i < @ARGV - 1
ABresult
0X0
100
111
1007 6 _debug("\$ARGV[$i] and \$ARGV[${\($i+1)}] looks like an option to specify level: $arg ", $ARGV[$i+1]);
1008 6 $level = _check_level($ARGV[$i+1], "ARGV $arg ".$ARGV[$i+1]);
1009 6 last SET;
1010 }
1011 30 for (@label2level) {
1012 120 ▶ if ($arg =~ /^--${pr}$_->[0](=(1|yes|true))?$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--$pr$_->[0](=(1|yes|true))?$/i) 2 118 120
1013 2 _debug("\$ARGV[$i] looks like an option to specify level: $arg");
1014 2 $level = $_->[1];
1015 2 last SET;
1016 }
1017 }
1018 28 $i++;
1019 }
1020  
1021 136 for (qw/LOG_LEVEL LOGLEVEL/) {
1022 270 my $key = $P_ . $_;
1023 270 _debug("Checking environment variable $key: ", _ifdef($ENV{$key}, "(undef)"));
1024 270 ▶ if ($ENV{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$key}) 2 268 270
1025 2 $level = _check_level($ENV{$key}, "ENV $key");
1026 2 $from = "\$ENV{$key}";
1027 2 last SET;
1028 }
1029 }
1030 134 for (@label2level) {
1031 533 my $key = $P_ . uc($_->[0]);
1032 533 _debug("Checking environment variable $key: ", _ifdef($ENV{$key}, "(undef)"));
1033 533 ▶ if ($ENV{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$key}) 2 531 533
1034 2 $level = $_->[1];
1035 2 $from = "\$ENV{$key}";
1036 2 last SET;
1037 }
1038 }
1039  
1040 132 for my $dir (@{$spec->{level_flag_paths}}) {
1041 252 for (@label2level) {
1042 999 my $filename = "$dir/$spec->{name}." . $P_ . "log_level";
1043 999 my $exists = -f $filename;
1044 999 my $content;
1045 999 ▶ if ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) 2 997 999
1046 2 open my($f), $filename;
1047 2 $content = <$f>;
1048 2 ▶ chomp($content) if defined($content);
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 2 0 2
1049 2 close $f;
1050 }
1051 999 ▶ _debug("Checking level flag file content $filename: ",
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 2 997 999
1052 (defined($content) ? $content : "(undef)"));
1053 999 ▶ if (defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 2 997 999
1054 2 $level = _check_level($content,
1055 "level flag file $filename");
1056 2 $from = $filename;
1057 2 last SET;
1058 }
1059  
1060 997 $filename = "$dir/$spec->{name}." . $P_ . uc($_->[0]);
1061 997 $exists = -e $filename;
1062 997 ▶ _debug("Checking level flag file $filename: ",
Branch: 1 branch
BranchTrueFalseTotal
$exists ? : 2 995 997
1063 ($exists ? "EXISTS" : 0));
1064 997 ▶ if ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) 2 995 997
1065 2 $level = $_->[1];
1066 2 $from = $filename;
1067 2 last SET;
1068 }
1069 }
1070 }
1071  
1072 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1073 128 for ("${F_}Log_Level", "${P_}LOG_LEVEL", "${p_}log_level",
1074 "${F_}LogLevel", "${P_}LOGLEVEL", "${p_}loglevel") {
1075 738 my $varname = "main::$_";
1076 738 _debug("Checking variable \$$varname: ", _ifdef($$varname, "(undef)"));
1077 738 ▶ if ($$varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$varname) 12 726 738
1078 12 $from = "\$$varname";
1079 12 $level = _check_level($$varname, "\$$varname");
1080 12 last SET;
1081 }
1082 }
1083 116 for (@label2level) {
1084 452 for my $varname (
1085 "main::$F_" . ucfirst($_->[0]),
1086 "main::$P_" . uc($_->[0])) {
1087 900 _debug("Checking variable \$$varname: ", _ifdef($$varname, "(undef)"));
1088 900 ▶ if ($$varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$varname) 8 892 900
1089 8 $from = "\$$varname";
1090 8 $level = $_->[1];
1091 8 last SET;
1092 }
1093 }
1094 }
1095 }
1096  
1097 144 ▶ _debug("Set ", ($which ? "level of $which" : "general level"), " to $level (from $from)") if $level;
Branch: 2 branches
BranchTrueFalseTotal
if $level 36 108 144
$which ? : 18 18 36
1098 144 return $level;
1099 }
1100  
1101 # return the lower level (e.g. _min_level("debug", "INFO") -> INFO
1102 40 ▶sub _min_level {
Subroutine _min_level: called CC=2 SCAR=6.9
1103 40 my ($l1, $l2) = @_;
1104 40 my %vals = (OFF=>99,
1105 FATAL=>6, ERROR=>5, WARN=>4, INFO=>3, DEBUG=>2, TRACE=>1);
1106 40 ▶ $vals{uc($l1)} > $vals{uc($l2)} ? $l2 : $l1;
Branch: 1 branch
BranchTrueFalseTotal
$vals{uc $l1} > $vals{uc $l2} ? : 7 33 40
1107 }
1108  
1109 1 ▶sub _export_logger {
Subroutine _export_logger: called CC=1 SCAR=0.0
1110 1 my ($caller) = @_;
1111 1 my $log_for_caller = Log::Any->get_logger(category => $caller);
1112 1 my $varname = "$caller\::log";
1113 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1114 1 *$varname = \$log_for_caller;
1115 }
1116  
1117 5169 ▶sub _debug {
Subroutine _debug: called CC=3 SCAR=18.4
1118 5169 ▶ return unless $ENV{LOGANYAPP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"LOGANYAPP_DEBUG"} 5169 0 5169
1119 0 ▶ print $dbg_ctx, ": " if $dbg_ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Any::App::dbg_ctx 0 0 0
1120 0 print @_, "\n";
1121 }
1122  
1123 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
1124 2 my ($self, @args) = @_;
1125 2 my $caller = caller();
1126 2 _parse_args(\@args, $caller);
1127 2 $init_args = \@args;
1128 }
1129  
1130 {
1131 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1132 # if we are loaded at run-time, it's too late to run INIT blocks, so user
1133 # must call init() manually. but sometimes this is what the user wants. so
1134 # shut up perl warning.
1135 2 ▶ INIT {
Subroutine INIT: called CC=1 SCAR=0.0
1136 2 my $caller = caller();
1137 2 init($init_args, $caller);
1138 }
1139 }
1140  
1141 3 1;
1142 # ABSTRACT: An easy way to use Log::Any in applications
1143  
1144 __END__
↑ Summary