blib/lib/Log/Stack.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 86.3% 19 / 22 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.8% 13 / 14 Podpod 83.3% 5 / 6 totaltotal 87.8% 116 / 132 CC 19 SCAR 14.2
CC
19
Cov
92%
throw
8.2
log
5.0
hook
4.1
CRAP 4.2
↑ Summary
1 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Log::Stack;
4  
5 # ABSTRACT: Cache log messages and throw them later
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.001'; # VERSION
12  
13  
14 has _stack => (
15 is => 'lazy',
16 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 clearer => 1,
18 );
19  
20 1 has _target => (
21 is => 'ro',
22 );
23  
24 has _defaults => (
25 is => 'ro',
26 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 1 );
28  
29 has _hooks => (
30 is => 'ro',
31 7 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 );
33  
34 1 has _initialized => (
35 is => 'rw',
36 default => 0,
37 );
38  
39 28 ▶sub _exechook {
Subroutine _exechook: called CC=3 SCAR=11.0
40 28 my ($self, $hook, @args) = @_;
41 28 ▶ return unless $self->_hooks->{$hook};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_hooks->{$hook} 22 6 28
42 6 foreach my $code (@{ $self->_hooks->{$hook} }) {
43 6 $code->($self, @args);
44 }
45 }
46  
47  
48 7 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
49 7 my $class = shift;
50 7 my ($target, %defaults);
51 7 ▶ $target = shift if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 5 2 7
52 7 %defaults = @_;
53 return {
54 7 _target => $target,
55 _defaults => \%defaults,
56 }
57 }
58  
59  
60 2 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
61 2 my ($self, %vals) = @_;
62 2 foreach my $key (keys %vals) {
63 2 $self->_defaults->{$key} = delete $vals{$key};
64 }
65 }
66  
67  
68 17 ▶sub log {
Subroutine log: called CC=5 SCAR=16.1
Pod documented
69 17 my ($self, $level, $msg, %rest) = @_;
70 17 ▶ unless ($self->_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_initialized) 10 7 17
71 10 $self->_exechook('init');
72 10 $self->_initialized(1);
73 }
74 17 foreach my $key (keys %{ $self->_defaults }) {
75 3 ▶ next if exists $rest{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rest{$key} 0 3 3
76 3 my $val = $self->_defaults->{$key};
77 3 ▶ if (ref $val eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'CODE') { } 1 2 3
78 1 $rest{$key} = scalar($val->($level, $msg));
79 } else {
80 2 $rest{$key} = $val;
81 }
82 }
83 17 push @{ $self->_stack } => [ $level, $msg, %rest ];
84 }
85  
86  
87 6 ▶sub throw {
Subroutine throw: called CC=8 SCAR=21.0
Pod documented
88 6 my ($self, $target) = @_;
89 6 ▶ return unless @{ $self->_stack };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_stack;} 1 5 6
90 5 ▶ $target //= $self->_target;
Condition66%2/3
$target //= $self->_target
l!l&&r!l&&!r
230
MC/DC0%0/2
$target //= $self->_target
$target $self->_target
Truth table66%2/3
A //= B
A= $targetB= $self->_target
ABresult
1X1
011
000
91 5 ▶ if (ref $target eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $target eq 'CODE') { } 4 1 5
elsif (builtin::blessed($target) and $target->can('log')) { } 1 0 1
92 4 $self->_exechook('before');
93 4 ▶ while (my $msg = shift @{ $self->_stack }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = shift @{$self->_stack;}) 6 4 10
94 6 $target->(@$msg);
95 }
96 4 $self->_exechook('after');
97 ▶ } elsif (blessed $target and $target->can('log')) {
Condition33%1/3
builtin::blessed($target) and $target->can("log")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($target) and $target->can("log")
builtin::blessed($target) $target->can("log")
Truth table33%1/3
A and B
A= builtin::blessed($target)B= $target->can("log")
ABresult
0X0
100
111
98 1 $self->_exechook('before');
99 1 ▶ while (my $msg = shift @{ $self->_stack }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = shift @{$self->_stack;}) 2 1 3
100 2 $target->log(@$msg);
101 }
102 1 $self->_exechook('after');
103 } else {
104 0 croak "Logging target must be a blessed object with a log method or a CodeRref";
105 }
106 5 $self->_exechook('cleanup');
107 5 $self->_initialized(0);
108 }
109  
110  
111 3 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
112 3 my $self = shift;
113 3 $self->_clear_stack;
114 3 $self->_exechook('cleanup');
115 3 $self->_initialized(0);
116 }
117  
118  
119 4 ▶sub hook {
Subroutine hook: called CC=4 SCAR=14.1
Pod documented
120 4 my ($self, %def) = @_;
121 4 foreach my $name (keys %def) {
122 4 my $code = $def{$name};
123 4 ▶ croak "Hook for $name must be a CodeRef" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 4 4
124 4 ▶ $self->_hooks->{$name} //= [];
Condition50%1/2
$self->_hooks->{$name} //= []
l!l
04
MC/DC0%0/1
$self->_hooks->{$name} //= []
$self->_hooks->{$name}
Truth table50%1/2
A
A= $self->_hooks->{$name}
Aresult
11
00
125 4 push @{ $self->_hooks->{$name} } => $code;
126 }
127 }
128  
129  
130  
131 1 1;
132  
133 __END__
↑ Summary