blib/lib/No/Worries/Log.pm

filter: Statementstmt 85.8% 316 / 368 Branchbran 67.3% 62 / 92 Conditioncond 61.5% 24 / 39 MC/DCmcdc 53.8% 14 / 26 Subroutinesub 85.5% 65 / 76 Podpod 100% 14 / 14 totaltotal 80.4% 495 / 615 CC 65 SCAR 19.2
CC
65
Cov
81%
log_configure
20.0
log_error
20.0
log_warning
20.0
CRAP 6.8
« blib/lib/No/Worries/Export.pm ↑ Summary blib/lib/No/Worries/DN.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/Log.pm #
4 # #
5 # Description: logging without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::Log;
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 our $VERSION = "1.7";
17 3 our $REVISION = sprintf("%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 3 ▶use IO::Handle qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use No::Worries qw($HostName $ProgramName);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use No::Worries::Date qw(date_stamp);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use No::Worries::File qw(file_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #
31 # constants
32 #
33  
34 3 ▶use constant _LEVEL_ERROR => "error";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant _LEVEL_WARNING => "warning";
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant _LEVEL_INFO => "info";
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use constant _LEVEL_DEBUG => "debug";
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant _LEVEL_TRACE => "trace";
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 #
41 # global variables
42 #
43  
44 3 our($Handler);
45  
46 our(
47 3 %_KnownLevel, # hash with known levels
48 %_InterestingLevel, # hash with interesting levels
49 %_Level2Char, # hash mapping levels to chars for the output
50 $_MaybeInterestingInfo, # filtering sub (partial)
51 $_InterestingInfo, # filtering sub (complete)
52 $_ConfigTag, # dev:ino:mtime of the last configuration file used
53 );
54  
55 #+++############################################################################
56 # #
57 # configuring #
58 # #
59 #---############################################################################
60  
61 #
62 # configure the module from the given file (if needed)
63 #
64  
65 0 ▶sub log_configure ($) {
Subroutine log_configure: not called CC=4 SCAR=30.0
Pod documented
66 0 my($path) = @_;
67 0 my(@stat, $tag);
68  
69 0 ▶ @stat = stat($path) or dief("cannot stat(%s): %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
@stat = stat $path or &dief("cannot stat(%s): %s", $path, $!) 0 0 0
70 0 $tag = join(":", $stat[0], $stat[1], $stat[9]);
71 0 ▶ return(0) if $_ConfigTag and $_ConfigTag eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $No::Worries::Log::_ConfigTag and $No::Worries::Log::_ConfigTag eq $tag 0 0 0
Condition0%0/3
$No::Worries::Log::_ConfigTag and $No::Worries::Log::_ConfigTag eq $tag
!ll&&!rl&&r
000
MC/DC0%0/2
$No::Worries::Log::_ConfigTag and $No::Worries::Log::_ConfigTag eq $tag
$No::Worries::Log::_ConfigTag $No::Worries::Log::_ConfigTag eq $tag
Truth table0%0/3
A and B
A= $No::Worries::Log::_ConfigTagB= $No::Worries::Log::_ConfigTag eq $tag
ABresult
0X0
100
111
72 0 log_filter(file_read($path));
73 0 $_ConfigTag = $tag;
74 0 return(1);
75 }
76  
77 #+++############################################################################
78 # #
79 # filtering #
80 # #
81 #---############################################################################
82  
83 #
84 # return the Perl code to use for a given filtering expression
85 #
86  
87 46 ▶sub _expr_code ($@) {
Subroutine _expr_code: called CC=4 SCAR=13.9
88 46 my($partial, $attr, $op, $value) = @_;
89  
90 # for partial filtering, we do not care about the message
91 46 ▶ return("1") if $attr eq "message" and $partial;
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "message" and $partial 2 44 46
Condition100%3/3
$attr eq "message" and $partial
!ll&&!rl&&r
4222
MC/DC100%2/2
$attr eq "message" and $partial
$attr eq "message" $partial
Truth table100%3/3
A and B
A= $attr eq "message"B= $partial
ABresult
0X0
100
111
92 # for the attributes we know about, it is easy
93 44 ▶ return("\$info->{$attr} $op $value")
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^(level|time|program|host|file|line|sub|message)$/ 30 14 44
94 if $attr =~ /^(level|time|program|host|file|line|sub|message)$/;
95 # for the other attributes, the test always fails if not defined
96 14 return("(defined(\$info->{$attr}) and \$info->{$attr} $op $value)");
97 }
98  
99 #
100 # compile the given filter
101 #
102  
103 20 ▶sub _compile_filter ($@) {
Subroutine _compile_filter: called CC=4 SCAR=13.9
104 20 my($partial, @filter) = @_;
105 20 my(@code, $code, $sub);
106  
107 20 @code = (
108 "package No::Worries::Log::Filter;",
109 "use strict;",
110 "use warnings;",
111 "\$sub = sub {",
112 " my(\$info) = \@_;",
113 " return(1) if",
114 );
115 20 foreach my $expr (@filter) {
116 72 ▶ if (ref($expr) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expr eq 'ARRAY') { } 46 26 72
117 46 push(@code, " " . _expr_code($partial, @{ $expr }));
118 } else {
119 26 push(@code, " $expr");
120 }
121 }
122 20 $code[-1] .= ";";
123 20 push(@code,
124 " return(0);",
125 "}",
126 );
127 20 $code = join("\n", @code);
128 1 ▶ eval($code); ## no critic 'BuiltinFunctions::ProhibitStringyEval'
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
129 20 ▶ dief("invalid code built: %s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
130 20 return($sub);
131 }
132  
133 #
134 # parse a single filtering expression
135 #
136  
137 27 ▶sub _parse_expr ($$$) {
Subroutine _parse_expr: called CC=15 SCAR=27.1
138 27 my($line, $level, $expr) = @_;
139 27 my($attr, $op, $value);
140  
141 # we first parse the (attr, op, value) triplet
142 27 ▶ if ($_KnownLevel{$expr}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($No::Worries::Log::_KnownLevel{$expr}) { } 14 13 27
elsif ($expr =~ /^(\w+)(==|!=)$/ and $1 ne 'level') { } 1 12 13
elsif ($expr =~ /^(\w+)(==|!=|=~|!~|>=?|<=?)(\S+)$/ and $1 ne 'level') { } 10 2 12
143 # there can be only one level per filter line and we keep track of it
144 14 ▶ dief("invalid filter line: %s", $line) if ${ $level };
Branch: 1 branch
BranchTrueFalseTotal
if ${$level;} 1 13 14
145 13 ${ $level } = $expr;
146 13 ($attr, $op, $value) = ("level", "==", $expr);
147 ▶ } elsif ($expr =~ /^(\w+)(==|!=)$/ and $1 ne "level") {
Condition66%2/3
$expr =~ /^(\w+)(==|!=)$/ and $1 ne "level"
!ll&&!rl&&r
1201
MC/DC50%1/2
$expr =~ /^(\w+)(==|!=)$/ and $1 ne "level"
$expr =~ /^(\w+)(==|!=)$/ $1 ne "level"
Truth table66%2/3
A and B
A= $expr =~ /^(\w+)(==|!=)$/B= $1 ne "level"
ABresult
0X0
100
111
148 # special case for comparison with empty string
149 1 ($attr, $op, $value) = ($1, $2, "");
150 ▶ } elsif ($expr =~ /^(\w+)(==|!=|=~|!~|>=?|<=?)(\S+)$/ and $1 ne "level") {
Condition100%3/3
$expr =~ /^(\w+)(==|!=|=~|!~|>=?|<=?)(\S+)$/ and $1 ne "level"
!ll&&!rl&&r
1110
MC/DC100%2/2
$expr =~ /^(\w+)(==|!=|=~|!~|>=?|<=?)(\S+)$/ and $1 ne "level"
$expr =~ /^(\w+)(==|!=|=~|!~|>=?|<=?)(\S+)$/ $1 ne "level"
Truth table100%3/3
A and B
A= $expr =~ /^(\w+)(==|!=|=~|!~|>=?|<=?)(\S+)$/B= $1 ne "level"
ABresult
0X0
100
111
151 # normal case
152 10 ($attr, $op, $value) = ($1, $2, $3);
153 } else {
154 2 dief("invalid filter expression: %s", $expr);
155 }
156 # we then check the value
157 24 ▶ if ($op eq "=~" or $op eq "!~") {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '=~' or $op eq '!~') { } 4 20 24
elsif ($op eq '==' or $op eq '!=') { } 17 3 20
Condition100%3/3
$op eq "=~" or $op eq "!~"
l!l&&r!l&&!r
2220
MC/DC100%2/2
$op eq "=~" or $op eq "!~"
$op eq "=~" $op eq "!~"
Truth table100%3/3
A or B
A= $op eq "=~"B= $op eq "!~"
ABresult
1X1
011
000
158 # match: check that the value is a valid regular expression
159 4 eval { $expr =~ /$value/ };
160 4 ▶ dief("invalid regexp: %s", $value) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
161 4 $value = "m\0$value\0";
162 ▶ } elsif ($op eq "==" or $op eq "!=") {
Condition100%3/3
$op eq "==" or $op eq "!="
l!l&&r!l&&!r
1613
MC/DC100%2/2
$op eq "==" or $op eq "!="
$op eq "==" $op eq "!="
Truth table100%3/3
A or B
A= $op eq "=="B= $op eq "!="
ABresult
1X1
011
000
163 # equality: adjust according to type
164 17 ▶ unless ($value =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^-?\d+$/) 15 2 17
165 15 ▶ $op = $op eq "==" ? "eq" : "ne";
Branch: 1 branch
BranchTrueFalseTotal
$op eq '==' ? : 15 0 15
166 15 $value = "qq\0$value\0";
167 }
168 } else {
169 # numerical: check that the value is a valid integer
170 3 ▶ dief("invalid integer: %s", $value) unless $value =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^-?\d+$/ 0 3 3
171 }
172 # so far, so good
173 24 return([ $attr, $op, $value ]);
174 }
175  
176 #
177 # parse and compile the filter to use
178 #
179  
180 13 ▶sub log_filter ($) {
Subroutine log_filter: called CC=12 SCAR=25.0
Pod documented
181 13 my($filter) = @_;
182 13 my($and_re, $or_re, $level, @list, @filter, %il, $ii, $mii);
183  
184 # strip comments and empty lines and extra spaces
185 13 @list = ();
186 13 foreach my $line (split(/\n/, $filter)) {
187 19 $line =~ s/^\s+//;
188 19 $line =~ s/\s+$//;
189 19 $line =~ s/\s+/ /g;
190 19 ▶ next if $line eq "" or $line =~ /^\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" or $line =~ /^\#/ 4 15 19
Condition100%3/3
$line eq "" or $line =~ /^\#/
l!l&&r!l&&!r
3115
MC/DC100%2/2
$line eq "" or $line =~ /^\#/
$line eq "" $line =~ /^\#/
Truth table100%3/3
A or B
A= $line eq ""B= $line =~ /^\#/
ABresult
1X1
011
000
191 15 push(@list, $line);
192 }
193 13 $filter = join("\n", @list);
194 # find out how to split lines and expressions
195 13 ▶ if ($filter =~ /\s(and|or)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter =~ /\s(and|or)\s/) { } 2 11 13
196 # with syntactical sugar
197 2 $and_re = qr/\s+and\s+/;
198 2 $or_re = qr/\s+or\s+/;
199 } else {
200 # without syntactical sugar
201 11 $and_re = qr/ /;
202 11 $or_re = qr/\n/;
203 }
204 # parse line by line
205 13 foreach my $line (split($or_re, $filter)) {
206 16 $line =~ s/^\s+//;
207 16 $line =~ s/\s+$//;
208 16 ▶ next if $line eq "" or $line =~ /^\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" or $line =~ /^\#/ 0 16 16
Condition33%1/3
$line eq "" or $line =~ /^\#/
l!l&&r!l&&!r
0016
MC/DC0%0/2
$line eq "" or $line =~ /^\#/
$line eq "" $line =~ /^\#/
Truth table33%1/3
A or B
A= $line eq ""B= $line =~ /^\#/
ABresult
1X1
011
000
209 16 $level = "";
210 16 @list = ();
211 16 foreach my $expr (split($and_re, $line)) {
212 27 $expr = _parse_expr($line, \$level, $expr);
213 # each expression within a line is AND'ed
214 24 push(@list, $expr, "and");
215 }
216 13 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) { } 12 1 13
217 # one level specified
218 12 $il{$level}++;
219 } else {
220 # no level specified => all are potentially interesting
221 1 foreach my $kl (keys(%_KnownLevel)) {
222 5 $il{$kl}++;
223 }
224 }
225 # remove the last "and"
226 13 pop(@list);
227 # each line within a filter is OR'ed
228 13 push(@filter, @list, "or");
229 }
230 10 ▶ if (@filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (@filter) { } 10 0 10
231 # non-empty filter => remove the last "or"
232 10 pop(@filter);
233 } else {
234 # empty filter => default behavior
235 0 %il = (_LEVEL_INFO() => 1);
236 0 @filter = ("1");
237 }
238 10 $ii = _compile_filter(0, @filter);
239 10 $mii = _compile_filter(1, @filter);
240 # so far, so good...
241 10 %_InterestingLevel = %il;
242 10 $_InterestingInfo = $ii;
243 10 $_MaybeInterestingInfo = $mii;
244 }
245  
246 #+++############################################################################
247 # #
248 # outputting #
249 # #
250 #---############################################################################
251  
252 #
253 # default handler: print compact yet user friendly output to STDOUT or STDERR
254 #
255  
256 0 ▶sub log2std ($) {
Subroutine log2std: not called CC=3 SCAR=24.8
Pod documented
257 0 my($info) = @_;
258 0 my($id, $string, $fh);
259  
260 0 ▶ $id = $INC{"threads.pm"} ? "$info->{pid}.$info->{tid}": $info->{pid};
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 0 0
261 $string = sprintf("%s %s %s[%s]: %s\n",
262 $_Level2Char{$info->{level}}, date_stamp($info->{time}),
263 0 $info->{program}, $id, $info->{message});
264 0 ▶ $fh = $info->{level} eq _LEVEL_INFO ? *STDOUT : *STDERR;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'level'} eq 'info' ? : 0 0 0
265 0 $fh->print($string);
266 0 $fh->flush();
267 0 return(1);
268 }
269  
270 #
271 # dump handler: print all attributes to STDERR
272 #
273  
274 0 ▶sub log2dump ($) {
Subroutine log2dump: not called CC=3 SCAR=24.8
Pod documented
275 0 my($info) = @_;
276 0 my(@list);
277  
278 0 foreach my $attr (sort(keys(%{ $info }))) {
279 0 ▶ if ($info->{$attr} =~ /^[\w\.\-\/]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{$attr} =~ m[^[\w\.\-/]*$]) { } 0 0 0
280 0 push(@list, "$attr=$info->{$attr}");
281 } else {
282 0 push(@list, "$attr=\'$info->{$attr}\'");
283 }
284 }
285 0 STDERR->print("% @list\n");
286 0 STDERR->flush();
287 0 return(1);
288 }
289  
290 #+++############################################################################
291 # #
292 # formatting #
293 # #
294 #---############################################################################
295  
296 #
297 # format the message
298 #
299  
300 18 ▶sub _message ($$) {
Subroutine _message: called CC=10 SCAR=23.0
301 18 my($message, $info) = @_;
302 18 my(@list, $format, $pos);
303  
304 18 @list = @{ $message };
305 18 ▶ unless (@list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@list) 1 17 18
306 # no message given => empty string
307 1 return("");
308 }
309 17 $format = shift(@list);
310 17 ▶ if (ref($format) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format eq "CODE") 3 14 17
311 # code message => result of the call
312 3 return($format->(@list));
313 }
314 14 ▶ if (ref($format)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format) 1 13 14
315 # unexpected first argument
316 1 dief("unexpected argument: %s", $format);
317 }
318 13 ▶ unless (@list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@list) 6 7 13
319 # plain message
320 6 return($format);
321 }
322 # sprintf message => format it
323 7 $pos = 0;
324 7 foreach my $arg (@list) {
325 7 ▶ if (ref($arg) eq "SCALAR") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'SCALAR') { } 2 5 7
elsif (not ref $arg) { } 3 2 5
326 # attribute argument
327 1 dief("unknown attribute: %s", ${ $arg })
328 2 ▶ unless defined($info->{${ $arg }});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$info{${$arg;}} 1 1 2
329 1 $arg = $info->{${ $arg }};
330 } elsif (not ref($arg)) {
331 # plain argument
332 3 ▶ dief("undefined argument at position %d", $pos)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 1 2 3
333 unless defined($arg);
334 } else {
335 2 dief("unexpected argument: %s", $arg);
336 }
337 3 $pos++;
338 }
339 3 return(sprintf($format, @list));
340 }
341  
342 #+++############################################################################
343 # #
344 # handling #
345 # #
346 #---############################################################################
347  
348 #
349 # handle information
350 #
351  
352 23 ▶sub _handle ($$) {
Subroutine _handle: called CC=5 SCAR=16.1
353 23 my($message, $info) = @_;
354 23 my(@list);
355  
356 # build the info to log with minimal (= cheap to get) information
357 23 $info->{time} = time();
358 23 $info->{program} = $ProgramName;
359 23 $info->{host} = $HostName;
360 23 $info->{pid} = $$;
361 23 ▶ $info->{tid} = threads->tid() if $INC{"threads.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 23 23
362 23 @list = caller(1);
363 23 $info->{file} = $list[1];
364 23 $info->{line} = $list[2];
365 23 @list = caller(2);
366 23 ▶ $info->{caller} = defined($list[3]) ? $list[3] : "main";
Branch: 1 branch
BranchTrueFalseTotal
defined $list[3] ? : 9 14 23
367 # check if we may care about this info
368 23 ▶ return(0) unless $_MaybeInterestingInfo->($info);
Branch: 1 branch
BranchTrueFalseTotal
unless &$No::Worries::Log::_MaybeInterestingInfo($info) 5 18 23
369 # format the message
370 18 $info->{message} = _message($message, $info);
371 # we always strip trailing spaces
372 13 $info->{message} =~ s/\s+$//;
373 # check if we really care about this info
374 13 ▶ return(0) unless $_InterestingInfo->($info);
Branch: 1 branch
BranchTrueFalseTotal
unless &$No::Worries::Log::_InterestingInfo($info) 0 13 13
375 # now send it to the right final handler
376 13 return($Handler->($info));
377 }
378  
379 #+++############################################################################
380 # #
381 # public API #
382 # #
383 #---############################################################################
384  
385 #
386 # check whether a level is "active"
387 #
388  
389 0 ▶sub log_wants_error () { return($_InterestingLevel{_LEVEL_ERROR()}) }
Subroutine log_wants_error: not called CC=1 SCAR=6.9
Pod documented
390 0 ▶sub log_wants_warning () { return($_InterestingLevel{_LEVEL_WARNING()}) }
Subroutine log_wants_warning: not called CC=1 SCAR=6.9
Pod documented
391 4 ▶sub log_wants_info () { return($_InterestingLevel{_LEVEL_INFO()}) }
Subroutine log_wants_info: called CC=1 SCAR=0.0
Pod documented
392 4 ▶sub log_wants_debug () { return($_InterestingLevel{_LEVEL_DEBUG()}) }
Subroutine log_wants_debug: called CC=1 SCAR=0.0
Pod documented
393 4 ▶sub log_wants_trace () { return($_InterestingLevel{_LEVEL_TRACE()}) }
Subroutine log_wants_trace: called CC=1 SCAR=0.0
Pod documented
394  
395 #
396 # log error information
397 #
398  
399 0 ▶sub log_error (@) {
Subroutine log_error: not called CC=4 SCAR=30.0
Pod documented
400 0 my(@args) = @_;
401 0 my($attrs);
402  
403 0 ▶ return(0) unless $_InterestingLevel{_LEVEL_ERROR()};
Branch: 1 branch
BranchTrueFalseTotal
unless $No::Worries::Log::_InterestingLevel{"error"} 0 0 0
404 0 ▶ if (@args and ref($args[-1]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[-1] eq 'HASH') { } 0 0 0
Condition0%0/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
405 0 $attrs = pop(@args);
406 } else {
407 0 $attrs = {};
408 }
409 0 return(_handle(\@args, { %{ $attrs }, level => _LEVEL_ERROR }));
410 }
411  
412 #
413 # log warning information
414 #
415  
416 0 ▶sub log_warning (@) {
Subroutine log_warning: not called CC=4 SCAR=30.0
Pod documented
417 0 my(@args) = @_;
418 0 my($attrs);
419  
420 0 ▶ return(0) unless $_InterestingLevel{_LEVEL_WARNING()};
Branch: 1 branch
BranchTrueFalseTotal
unless $No::Worries::Log::_InterestingLevel{"warning"} 0 0 0
421 0 ▶ if (@args and ref($args[-1]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[-1] eq 'HASH') { } 0 0 0
Condition0%0/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
422 0 $attrs = pop(@args);
423 } else {
424 0 $attrs = {};
425 }
426 0 return(_handle(\@args, { %{ $attrs }, level => _LEVEL_WARNING }));
427 }
428  
429 #
430 # log informational information ;-)
431 #
432  
433 14 ▶sub log_info (@) {
Subroutine log_info: called CC=4 SCAR=13.9
Pod documented
434 14 my(@args) = @_;
435 14 my($attrs);
436  
437 14 ▶ return(0) unless $_InterestingLevel{_LEVEL_INFO()};
Branch: 1 branch
BranchTrueFalseTotal
unless $No::Worries::Log::_InterestingLevel{"info"} 1 13 14
438 13 ▶ if (@args and ref($args[-1]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[-1] eq 'HASH') { } 1 12 13
Condition100%3/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
1111
MC/DC100%2/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table100%3/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
439 1 $attrs = pop(@args);
440 } else {
441 12 $attrs = {};
442 }
443 13 return(_handle(\@args, { %{ $attrs }, level => _LEVEL_INFO }));
444 }
445  
446 #
447 # log debugging information
448 #
449  
450 10 ▶sub log_debug (@) {
Subroutine log_debug: called CC=4 SCAR=14.0
Pod documented
451 10 my(@args) = @_;
452 10 my($attrs);
453  
454 10 ▶ return(0) unless $_InterestingLevel{_LEVEL_DEBUG()};
Branch: 1 branch
BranchTrueFalseTotal
unless $No::Worries::Log::_InterestingLevel{"debug"} 0 10 10
455 10 ▶ if (@args and ref($args[-1]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[-1] eq 'HASH') { } 2 8 10
Condition66%2/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
082
MC/DC50%1/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
456 2 $attrs = pop(@args);
457 } else {
458 8 $attrs = {};
459 }
460 10 return(_handle(\@args, { %{ $attrs }, level => _LEVEL_DEBUG }));
461 }
462  
463 #
464 # log tracing information (fixed message)
465 #
466  
467 0 ▶sub log_trace () {
Subroutine log_trace: not called CC=2 SCAR=17.9
Pod documented
468 0 ▶ return(0) unless $_InterestingLevel{_LEVEL_TRACE()};
Branch: 1 branch
BranchTrueFalseTotal
unless $No::Worries::Log::_InterestingLevel{"trace"} 0 0 0
469 0 return(_handle(
470 [ "in %s at %s line %s", \ "caller", \ "file", \ "line" ],
471 { level => _LEVEL_TRACE },
472 ));
473 }
474  
475 #+++############################################################################
476 # #
477 # module initialization #
478 # #
479 #---############################################################################
480  
481 # we select the relevant handler to use
482 3 ▶if ($ENV{NO_WORRIES} and $ENV{NO_WORRIES} =~ /\b(log2dump)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'NO_WORRIES'} and $ENV{'NO_WORRIES'} =~ /\b(log2dump)\b/) { } 0 3 3
Condition33%1/3
$ENV{"NO_WORRIES"} and $ENV{"NO_WORRIES"} =~ /\b(log2dump)\b/
!ll&&!rl&&r
300
MC/DC0%0/2
$ENV{"NO_WORRIES"} and $ENV{"NO_WORRIES"} =~ /\b(log2dump)\b/
$ENV{"NO_WORRIES"} $ENV{"NO_WORRIES"} =~ /\b(log2dump)\b/
Truth table33%1/3
A and B
A= $ENV{"NO_WORRIES"}B= $ENV{"NO_WORRIES"} =~ /\b(log2dump)\b/
ABresult
0X0
100
111
483 0 $Handler = \&log2dump;
484 } else {
485 3 $Handler = \&log2std;
486 };
487  
488 # here are all the known levels
489 3 %_Level2Char = (
490 error => "!",
491 warning => "?",
492 info => ":",
493 debug => "#",
494 trace => "=",
495 );
496 3 foreach my $level (keys(%_Level2Char)) {
497 15 $_KnownLevel{$level}++;
498 }
499  
500 # by default we only care about informational level or higher
501 3 %_InterestingLevel = %_KnownLevel;
502 3 delete($_InterestingLevel{_LEVEL_DEBUG()});
503 3 delete($_InterestingLevel{_LEVEL_TRACE()});
504  
505 # by default we do not filter anything out
506 0 ▶$_MaybeInterestingInfo = $_InterestingInfo = sub { return(1) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
507  
508 #
509 # export control
510 #
511  
512 1 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.2
513 1 my($pkg, %exported);
514  
515 1 $pkg = shift(@_);
516 1 grep($exported{$_}++,
517 map("log_$_", qw(configure filter)),
518 map("log_$_", qw(error warning info debug trace)),
519 map("log_wants_$_", qw(error warning info debug trace)),
520 );
521 0 ▶ $exported{"log2std"} = sub { $Handler = \&log2std };
Subroutine __ANON__: not called CC=1 SCAR=1.2
522 0 ▶ $exported{"log2dump"} = sub { $Handler = \&log2dump };
Subroutine __ANON__: not called CC=1 SCAR=1.2
523 1 export_control(scalar(caller()), $pkg, \%exported, @_);
524 }
525  
526 3 1;
527  
528 __DATA__
« blib/lib/No/Worries/Export.pm ↑ Summary blib/lib/No/Worries/DN.pm »