blib/lib/EntityModel/Log.pm

filter: Statementstmt 60.8% 132 / 217 Branchbran 42.3% 39 / 92 Conditioncond 25.6% 10 / 39 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 65.9% 29 / 44 Podpod 100% 25 / 25 totaltotal 53.8% 236 / 438 CC 67 SCAR 39.8
CC
67
Cov
52%
_stack_trace
306.0
close_after_fork
20.0
parse_message
17.1
CRAP 53.5
↑ Summary
1 package EntityModel::Log;
2 # ABSTRACT: Logging class used by EntityModel
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent qw{Exporter};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.006';
8  
9 =head1 NAME
10  
11 EntityModel::Log - simple logging support for L<EntityModel>
12  
13 =head1 VERSION
14  
15 version 0.006
16  
17 =head1 SYNOPSIS
18  
19 use EntityModel::Log ':all';
20 # Log everything down to level 0 (debug)
21 EntityModel::Log->instance->min_level(0);
22  
23 # STDERR by default, or Test::More::note if you have it loaded
24 logDebug("Test something");
25 logInfo("Object [%s] found", $obj->name);
26 logError("Fatal problem");
27 logInfo(sub { my $str = heavy_operation(); return 'Failed: %s', $str });
28  
29 logInfo("Stack trace - note that it must have at least one parameter (%s): %S", 'like this');
30 logInfo("No stack trace without parameters despite %S");
31  
32 my $log = EntityModel::Log->instance;
33 $log->debug("OO-style debug");
34 $log->info("OO-style info");
35 $log->warning("OO-style warning");
36 $log->error("OO-style error");
37  
38 =head1 DESCRIPTION
39  
40 Yet another logging class. Provides a procedural and OO interface as usual - intended for use
41 with L<EntityModel> only, if you're looking for a general logging framework try one of the
42 other options in the L</SEE ALSO> section.
43  
44 =cut
45  
46 # Need to be able to switch off logging in UNITCHECK stages, since that segfaults perl5.10.1 and possibly other versions
47 2 our $DISABLE = 0;
48  
49 2 ▶use Time::HiRes qw{time};
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use POSIX qw{strftime};
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use List::Util qw{min max};
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use Scalar::Util qw{blessed};
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use Data::Dump ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use Data::Dump::Filtered ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 2 our %EXPORT_TAGS = ( 'all' => [qw/&logDebug &logInfo &logWarning &logError/] );
60 2 our @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );
61  
62 # Internal singleton instance
63 2 my $instance;
64  
65 =head2 instance
66  
67 Returns a handle to the main instance of L<EntityModel::Log>.
68  
69 =cut
70  
71 4 ▶sub instance { my $class = shift; $instance ||= $class->new }
Subroutine instance: called CC=2 SCAR=7.1
Pod documented
Condition66%2/3
$instance ||= $class->new
l!l&&r!l&&!r
310
MC/DC0%0/2
$instance ||= $class->new
$instance $class->new
Truth table66%2/3
A ||= B
A= $instanceB= $class->new
ABresult
1X1
011
000
72  
73 =head1 PROCEDURAL METHODS
74  
75 =cut
76  
77 2 my @LogType = (
78 'Debug',
79 'Info',
80 'Warning',
81 'Error',
82 'Fatal',
83 );
84  
85 =head2 _raise_error_on_global_instance
86  
87 Raise the given (code, message, ...) log event on the L<EntityModel::Log> global instance.
88  
89 =cut
90  
91 2 ▶sub _raise_error_on_global_instance { __PACKAGE__->instance->raise(@_); }
Subroutine _raise_error_on_global_instance: called CC=1 SCAR=0.0
92  
93 =head2 logDebug
94  
95 Raise a debug message. Expect a high volume of these during normal operation
96 so a production server would typically have these disabled.
97  
98 =cut
99  
100 1 ▶sub logDebug { unshift @_, 0; goto &_raise_error_on_global_instance; }
Subroutine logDebug: called CC=1 SCAR=0.0
Pod documented
101  
102 =head2 logInfo
103  
104 Raise an informational message, which we'd like to track for stats
105 reasons - indicates normal operations rather than an error condition.
106  
107 =cut
108  
109 0 ▶sub logInfo { unshift @_, 1; goto &_raise_error_on_global_instance; }
Subroutine logInfo: not called CC=1 SCAR=6.9
Pod documented
110  
111 =head2 logWarning
112  
113 Raise a warning message, for things like 'requested delete for object that does not exist'.
114 You might expect a few of these in regular operations due to concurrent access and timing issues,
115 so they may not necessarily indicate real system problems.
116  
117 =cut
118  
119 0 ▶sub logWarning { unshift @_, 2; goto &_raise_error_on_global_instance; }
Subroutine logWarning: not called CC=1 SCAR=6.9
Pod documented
120  
121 =head2 logError
122  
123 Raise an error - this is likely to be a genuine system problem.
124  
125 =cut
126  
127 1 ▶sub logError { unshift @_, 3; goto &_raise_error_on_global_instance; }
Subroutine logError: called CC=1 SCAR=0.0
Pod documented
128  
129 =head2 logStack
130  
131 Raise an error with stack - this is likely to be a genuine system problem.
132  
133 =cut
134  
135 0 ▶sub logStack {
Subroutine logStack: not called CC=1 SCAR=6.9
Pod documented
136 0 my $txt = __PACKAGE__->instance->parse_message(@_);
137  
138 $txt .= join("\n", map {
139 0 sprintf("%s:%s %s", $_->{filename}, $_->{line}, $_->{subroutine})
140 0 } _stack_trace());
141 0 _raise_error_on_global_instance(3, $txt);
142 }
143  
144 =head2 _stack_trace
145  
146 Get a stack trace, as an array of hashref entries, skipping the top two levels.
147  
148 =cut
149  
150 0 ▶sub _stack_trace {
Subroutine _stack_trace: not called CC=17 SCAR=57.2
151 0 ▶ my $skip = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
152 0 ▶ my $dump = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
153 0 my $idx = 1;
154 0 my @trace;
155 0 my $pkg = __PACKAGE__;
156 {
157 0 package DB;
158 0 ▶ while($idx < 99 && (my @stack = caller($idx))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 99 and my(@stack) = caller $idx) 0 0 0
Condition0%0/3
$idx < 99 and my(@stack) = caller $idx
!ll&&!rl&&r
000
MC/DC0%0/2
$idx < 99 and my(@stack) = caller $idx
$idx < 99 my(@stack) = caller $idx
Truth table0%0/3
A and B
A= $idx < 99B= my(@stack) = caller $idx
ABresult
0X0
100
111
159 0 ++$idx;
160 0 ▶ next if $skip-- > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skip-- > 0 0 0 0
161  
162 0 my %info;
163 0 ▶ @info{qw/package filename line subroutine hasargs wantarray evaltext is_require hints bitmask hinthash/} = map $_ // '', @stack;
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
164 0 $info{args} = [ @DB::args ];
165  
166 # TODO not happy with this. maybe switch to ->isa?
167 0 ▶ push @trace, \%info unless $info{package} eq $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $info{"package"} eq $pkg 0 0 0
168 }
169 }
170  
171 0 foreach my $info (@trace) {
172 0 $info->{file} = File::Basename::basename($info->{filename});
173 0 $info->{code} = '';
174 0 ▶ if($dump) { # could include source context using something like $info{filename} =~ m{^$basePath/(.*)$} || $info{filename} =~ m{^/perl-module-path/(.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump) 0 0 0
175 # I'm hoping this entire function can be replaced by a module from somewhere
176 0 ▶ if(-r $info->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $info->{"filename"}) 0 0 0
177 # Start from five lines before the required line, but clamp to zero
178 0 ▶ my $start = max(0, ($info->{line} // 0) - 5);
Condition0%0/2
$info->{"line"} // 0
l!l
00
MC/DC0%0/1
$info->{"line"} // 0
$info->{"line"}
Truth table0%0/2
A
A= $info->{"line"}
Aresult
11
00
179  
180 # Probably not a safe thing to do, but most modules seem to be ascii or utf8
181 0 ▶ open my $fh, '<:encoding(utf8)', $info->{filename} or die $! . ' when reading ' . $info->{filename} . ' which we expected to have loaded already';
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, "<:encoding(utf8)", $info->{"filename"} or die $! . ' when reading ' . $info->{"filename"} . ' which we expected to have loaded already' 0 0 0
182  
183 0 ▶ if($start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start) 0 0 0
184 0 <$fh> for 1..$start;
185 }
186 0 my $line = $start;
187 0 ▶ $info->{code} .= sprintf("%5d %s", $line++, scalar(<$fh> // last)) for 0..10;
Condition0%0/2
readline $fh // (last)
l!l
00
MC/DC0%0/1
readline $fh // (last)
readline $fh
Truth table0%0/2
A
A= readline $fh
Aresult
11
00
188 0 close $fh;
189 }
190 }
191 }
192 0 return @trace;
193 }
194  
195 =head2 _level_from_string
196  
197 Returns the level matching the given string.
198  
199 =cut
200  
201 0 ▶sub _level_from_string {
Subroutine _level_from_string: not called CC=3 SCAR=24.8
202 0 my $str = lc(shift);
203 0 my $idx = 0;
204 0 foreach (@LogType) {
205 0 ▶ return $idx if $str eq lc($_);
Branch: 1 branch
BranchTrueFalseTotal
if $str eq lc $_ 0 0 0
206 0 ++$idx;
207 }
208 0 die "Bad log level [$str]";
209 }
210  
211 =head2 _timestamp
212  
213 Generate a string in ISO8601-ish format representing the time of this log event.
214  
215 =cut
216  
217 2 ▶sub _timestamp {
Subroutine _timestamp: called CC=1 SCAR=0.0
218 2 my $now = Time::HiRes::time;
219 2 return strftime("%Y-%m-%d %H:%M:%S", gmtime($now)) . sprintf(".%03d", int($now * 1000.0) % 1000.0);
220 }
221  
222 =head2 OO METHODS
223  
224 =cut
225  
226 =head2 new
227  
228 Constructor - currently doesn't do much.
229  
230 =cut
231  
232 1 ▶sub new { bless { handle => undef, is_open => 1, pid => $$ }, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
233  
234 =head2 debug
235  
236 Display a debug message.
237  
238 =cut
239  
240 0 ▶sub debug { shift->raise(0, @_) }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
241  
242 =head2 info
243  
244 Display an info message.
245  
246 =cut
247  
248 0 ▶sub info { shift->raise(1, @_) }
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
249  
250 =head2 warning
251  
252 Display a warning message.
253  
254 =cut
255  
256 0 ▶sub warning { shift->raise(2, @_) }
Subroutine warning: not called CC=1 SCAR=6.9
Pod documented
257  
258 =head2 error
259  
260 Display an error message.
261  
262 =cut
263  
264 0 ▶sub error { shift->raise(3, @_) }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
265  
266 =head2 path
267  
268 Accessor for path setting, if given a new path will close existing file and direct all new output to the given path.
269  
270 =cut
271  
272 0 ▶sub path {
Subroutine path: not called CC=3 SCAR=24.8
Pod documented
273 0 my $self = shift;
274 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
275 0 ▶ $self->close if $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_open 0 0 0
276 0 $self->{path} = shift;
277 0 $self->open;
278 0 return $self;
279 }
280 0 return $self->{path};
281 }
282  
283 =head2 pid
284  
285 Current PID, used for fork tracking.
286  
287 =cut
288  
289 2 ▶sub pid {
Subroutine pid: called CC=2 SCAR=6.9
Pod documented
290 2 my $self = shift;
291 2 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
292 1 $self->{pid} = shift;
293 1 return $self;
294 }
295 1 return $self->{pid};
296 }
297  
298 =head2 is_open
299  
300 Returns true if our log file is already open.
301  
302 =cut
303  
304 7 ▶sub is_open {
Subroutine is_open: called CC=2 SCAR=6.9
Pod documented
305 7 my $self = shift;
306 7 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 4 7
307 3 $self->{is_open} = shift;
308 3 return $self;
309 }
310 4 return $self->{is_open};
311 }
312  
313 =head2 disabled
314  
315 Returns true if we're running disabled.
316 =cut
317  
318 2 ▶sub disabled {
Subroutine disabled: called CC=2 SCAR=8.4
Pod documented
319 2 my $self = shift;
320 2 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
321 0 $self->{disabled} = shift;
322 0 return $self;
323 }
324 2 return $self->{disabled};
325 }
326  
327 =head2 close
328  
329 Close the log file if it's currently open.
330  
331 =cut
332  
333 2 ▶sub close : method {
Subroutine close: called CC=4 SCAR=14.0
Pod documented
334 2 my $self = shift;
335 2 ▶ return $self unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 2 2
336  
337 2 ▶ if(my $h = delete $self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = delete $self->{"handle"}) 1 1 2
338 1 ▶ $h->close or die "Failed to close log file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$h->close or die "Failed to close log file: $!\n" 1 0 1
339 }
340 2 $self->is_open(0);
341 2 return $self;
342 }
343  
344 =head2 close_after_fork
345  
346 Close any active handle if we've forked. This method just does the closing, not the check for $$.
347  
348 =cut
349  
350 0 ▶sub close_after_fork {
Subroutine close_after_fork: not called CC=4 SCAR=30.0
Pod documented
351 0 my $self = shift;
352 0 ▶ return unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 0 0
353  
354 # Don't close STDOUT/STDERR. Bit of a hack really, we should perhaps just close when we were given a path?
355 0 ▶ return if $self->handle == \*STDERR || $self->handle == \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle == \*STDERR or $self->handle == \*STDOUT 0 0 0
Condition0%0/3
$self->handle == \*STDERR or $self->handle == \*STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->handle == \*STDERR or $self->handle == \*STDOUT
$self->handle == \*STDERR $self->handle == \*STDOUT
Truth table0%0/3
A or B
A= $self->handle == \*STDERRB= $self->handle == \*STDOUT
ABresult
1X1
011
000
356 0 $self->close;
357 0 return $self;
358 }
359  
360 =head2 open
361  
362 Open the logfile.
363  
364 =cut
365  
366 0 ▶sub open : method {
Subroutine open: not called CC=3 SCAR=24.8
Pod documented
367 0 my $self = shift;
368 0 ▶ return $self if $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_open 0 0 0
369 0 ▶ open my $fh, '>>', $self->path or die $! . " for " . $self->path;
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, ">>", $self->path or die $! . ' for ' . $self->path 0 0 0
370 0 binmode $fh, ':encoding(utf-8)';
371 0 $fh->autoflush(1);
372 0 $self->{handle} = $fh;
373 0 $self->is_open(1);
374 0 $self->pid($$);
375 0 return $self;
376 }
377  
378 =head2 reopen
379  
380 Helper method to close and reopen logfile.
381  
382 =cut
383  
384 0 ▶sub reopen {
Subroutine reopen: not called CC=2 SCAR=17.9
Pod documented
385 0 my $self = shift;
386 0 ▶ $self->close if $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_open 0 0 0
387 0 $self->open;
388 0 return $self;
389 }
390  
391 =head2 parse_message
392  
393 Generate appropriate text based on whatever we get passed.
394  
395 Each item in the parameter list is parsed first, then the resulting items are passed through L<sprintf>. If only a single item is in the list then the resulting string is returned directly.
396  
397 Item parsing handles the following types:
398  
399 =over 4
400  
401 =item * Single string is passed through unchanged
402  
403 =item * Arrayref or hashref is expanded via L<Data::Dump>
404  
405 =item * Other references are stringified
406  
407 =item * Undef items are replaced with the text 'undef'
408  
409 =back
410  
411 In addition, if the first parameter is a coderef then it is expanded in place (recursively - a coderef can return another coderef). Note that this only happens for the *first* parameter at each
412 level of recursion.
413  
414 =cut
415  
416 8 ▶sub parse_message {
Subroutine parse_message: called CC=14 SCAR=28.4
Pod documented
417 8 my $self = shift;
418 8 ▶ return '' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
419  
420 8 ▶ unshift @_, $_[0]->() while $_[0] && ref($_[0]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] and ref $_[0] eq "CODE" and do { unshift @_, $_[0]->() } 3 8 11
Condition66%2/3
$_[0] and ref $_[0] eq "CODE"
!ll&&!rl&&r
083
MC/DC50%1/2
$_[0] and ref $_[0] eq "CODE"
$_[0] ref $_[0] eq "CODE"
Truth table66%2/3
A and B
A= $_[0]B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
421  
422 # Decompose parameters into strings
423 8 my @data;
424 ITEM:
425 8 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 21 8 29
426 21 my $entry = shift;
427  
428 # Convert to string if we can
429 21 ▶ if(my $ref = ref $entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $entry) 6 15 21
430 6 ▶ if($ref =~ /^CODE/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref =~ /^CODE/) { } 5 1 6
elsif ($ref eq 'ARRAY' or $ref eq 'HASH') { } 1 0 1
431 5 unshift @_, $entry->();
432 5 next ITEM;
433 ▶ } elsif($ref eq 'ARRAY' or $ref eq 'HASH') {
Condition33%1/3
$ref eq "ARRAY" or $ref eq "HASH"
l!l&&r!l&&!r
100
MC/DC0%0/2
$ref eq "ARRAY" or $ref eq "HASH"
$ref eq "ARRAY" $ref eq "HASH"
Truth table33%1/3
A or B
A= $ref eq "ARRAY"B= $ref eq "HASH"
ABresult
1X1
011
000
434 1 $entry = Data::Dump::dump($entry);
435 } else {
436 0 $entry = "$entry";
437 }
438 }
439 16 ▶ $entry //= 'undef';
Condition50%1/2
$entry //= "undef"
l!l
160
MC/DC0%0/1
$entry //= "undef"
$entry
Truth table50%1/2
A
A= $entry
Aresult
11
00
440 16 push @data, $entry;
441 }
442  
443 # Format appropriately
444 8 ▶ my $fmt = shift(@data) // '';
Condition50%1/2
shift @data // ""
l!l
80
MC/DC0%0/1
shift @data // ""
shift @data
Truth table50%1/2
A
A= shift @data
Aresult
11
00
445 8 ▶ return $fmt unless @data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 3 5 8
446  
447 # Special-case the stack trace feature. A bit too special really :(
448 5 $fmt =~ s/%S/join("\n", '', map {
449 0 _stack_line($_)
450 } _stack_trace(0, 1))/e;
451 5 ▶ die "Format undef" unless defined $fmt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fmt 0 5 5
452 0 ▶ die "Undefined entry in data, others are " . join ', ', map { defined($_) } @data if grep { !defined($_) } @data;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @data 0 5 5
453 5 return sprintf($fmt, @data);
454 }
455  
456 0 ▶sub _stack_line {
Subroutine _stack_line: not called CC=3 SCAR=24.8
457 0 my $info = shift;
458 my $txt = sprintf ' => %-32.32s %s(%s) args %s',
459 $info->{package} . ':' . $info->{line},
460 ($info->{subroutine} =~ m{ ( [^:]+$ ) }x),
461 ($info->{package} eq 'EntityModel::Log')
462 ? ('')
463 0 ▶ : (join ', ', map Data::Dump::Filtered::dump_filtered($info, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
464 0 my ($ctx, $obj) = @_;
465 0 ▶ return undef unless $ctx->is_blessed;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->is_blessed 0 0 0
466 0 return { dump => "$obj" };
467 0 ▶ })), join ' ', map $_ // '<undef>', @{ $info->{args} };
Branch: 1 branch
BranchTrueFalseTotal
$info->{'package'} eq 'EntityModel::Log' ? : 0 0 0
Condition0%0/2
$_ // "<undef>"
l!l
00
MC/DC0%0/1
$_ // "<undef>"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
468 0 $txt =~ s{%}{%%}g;
469 0 return $txt;
470 }
471  
472 =head2 min_level
473  
474 Accessor for the current minimum logging level. Values correspond to:
475  
476 =over 4
477  
478 =item * 0 - Debug
479  
480 =item * 1 - Info
481  
482 =item * 2 - Warning
483  
484 =item * 3 - Error
485  
486 =item * 4 - Fatal
487  
488 =back
489  
490 Returns $self when setting a value, otherwise the current value is returned.
491  
492 =cut
493  
494 1 ▶sub min_level {
Subroutine min_level: called CC=2 SCAR=7.4
Pod documented
495 1 my $self = shift;
496 1 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
497 1 $self->{min_level} = shift;
498 1 return $self;
499 }
500 0 return $self->{min_level};
501 }
502  
503 =head2 raise
504  
505 Raise a log message
506  
507 =over 4
508  
509 =item * $level - numeric log level
510  
511 =item * @data - message data
512  
513 =back
514  
515 =cut
516  
517 2 ▶sub raise {
Subroutine raise: called CC=7 SCAR=21.3
Pod documented
518 2 ▶ return $_[0] if $_[0]->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->disabled 0 2 2
519  
520 2 my $self = shift;
521 2 my $level = shift;
522 2 my ($pkg, $file, $line, $sub) = caller(1);
523  
524 # caller(0) gives us the wrong sub for our purposes - we want whatever raised the logXX line
525 2 (undef, undef, undef, $sub) = caller(2);
526  
527 # Apply minimum log level based on method, then class, then default 'info'
528 my $minLevel = ($sub ? $self->{mask}{$sub}{level} : undef)
529 // $self->{mask}{$pkg}{level}
530 // $self->{min_level}
531 2 ▶ // 1;
Branch: 1 branch
BranchTrueFalseTotal
$sub ? : 0 2 2
Condition37%3/8
((($sub ? $self->{"mask"}{$sub}{"level"} : undef) // $self->{"mask"}{$pkg}{"level"}) // $self->{"min_level"}) // 1
l!l
20
(($sub ? $self->{"mask"}{$sub}{"level"} : undef) // $self->{"mask"}{$pkg}{"level"}) // $self->{"min_level"}
l!l&&r!l&&!r
020
($sub ? $self->{"mask"}{$sub}{"level"} : undef) // $self->{"mask"}{$pkg}{"level"}
l!l&&r!l&&!r
002
MC/DC0%0/3
((($sub ? $self->{"mask"}{$sub}{"level"} : undef) // $self->{"mask"}{$pkg}{"level"}) // $self->{"min_level"}) // 1
($sub ? $self->{"mask"}{$sub}{"level"} : undef) $self->{"mask"}{$pkg}{"level"} $self->{"min_level"}
Truth table25%1/4
A // B // C
A= ($sub ? $self->{"mask"}{$sub}{"level"} : undef)B= $self->{"mask"}{$pkg}{"level"}C= $self->{"min_level"}
ABCresult
1XX1
01X1
0011
0000
532 2 ▶ return $self if $minLevel > $level;
Branch: 1 branch
BranchTrueFalseTotal
if $minLevel > $level 0 2 2
533  
534 2 my $txt = $self->parse_message(@_);
535  
536 # Explicitly get time from Time::HiRes for ms accuracy
537 2 my $ts = _timestamp();
538  
539 2 my $type = sprintf("%-8.8s", $LogType[$level]);
540 2 $self->output("$ts $type $file:$line $txt");
541 2 return $self;
542 }
543  
544 =head2 output
545  
546 Sends output to the current filehandle.
547  
548 =cut
549  
550 2 ▶sub output {
Subroutine output: called CC=2 SCAR=6.9
Pod documented
551 2 my $self = shift;
552 2 my $msg = shift;
553 2 ▶ if(my $handle = $self->get_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->get_handle) 1 1 2
554 1 $handle->print($msg . "\n");
555 1 return $self;
556 }
557  
558 1 Test::More::note($msg);
559 1 return $self;
560 }
561  
562 =head2 get_handle
563  
564 Returns a handle if we have one, and 0 if we should fall back to L<Test::More>::note.
565  
566 =cut
567  
568 2 ▶sub get_handle {
Subroutine get_handle: called CC=6 SCAR=20.3
Pod documented
569 2 my $self = shift;
570 # Fall back to Test::More if available, unless we already have a handle
571 2 ▶ if(!$self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"handle"}) 1 1 2
572 1 ▶ return 0 if $ENV{HARNESS_ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HARNESS_ACTIVE"} 1 0 1
573 # Exists, but undef, means STDERR fallback
574 0 ▶ return \*STDERR if exists $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"handle"} 0 0 0
575 }
576  
577 1 ▶ $self->close_after_fork unless $$ == $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ == $self->pid 0 1 1
578  
579 1 ▶ $self->open unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 1 1
580 1 return $self->handle;
581 }
582  
583 =head2 handle
584  
585 Direct(-ish) accessor for the file handle.
586  
587 =cut
588  
589 2 ▶sub handle {
Subroutine handle: called CC=4 SCAR=14.0
Pod documented
590 2 my $self = shift;
591 2 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
592 1 ▶ $self->close if $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_open 1 0 1
593 1 $self->{handle} = shift;
594 1 $self->is_open(1);
595 1 $self->pid($$);
596 1 return $self;
597 }
598 1 ▶ $self->reopen unless $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} 0 1 1
599 1 return $self->{handle};
600 }
601  
602 1 ▶END { $instance->close if $instance; }
Subroutine END: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $instance 1 0 1
603  
604 2 1;
605  
606 __END__
↑ Summary