lib/Badger/Log.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 89.2% 25 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.9% 10 / 11 Podpod 83.3% 5 / 6 totaltotal 90.9% 90 / 99 CC 18 SCAR 12.9
CC
18
Cov
92%
log
7.1
__ANON__
3.0
__ANON__
3.0
CRAP 3.6
« lib/Badger/Rainbow.pm ↑ Summary lib/Badger/Prototype.pm »
1 #========================================================================
2 #
3 # Badger::Log
4 #
5 # DESCRIPTION
6 # A simple base class logging module.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Log;
14  
15 use Badger::Class
16 2 version => 0.01,
17 base => 'Badger::Prototype',
18 import => 'class',
19 utils => 'blessed Now',
20 config => 'system|class:SYSTEM format|class:FORMAT strftime|class:STRFTIME',
21 constants => 'ARRAY CODE',
22 constant => {
23 MSG => '_msg', # suffix for message methods, e.g. warn_msg()
24 LOG => 'log', # method a delegate must implement
25 },
26 vars => {
27 SYSTEM => 'Badger',
28 FORMAT => '[<time>] [<system>] [<level>] <message>',
29 STRFTIME => '%a %b %d %T %Y',
30 LEVELS => {
31 debug => 0,
32 info => 0,
33 warn => 1,
34 error => 1,
35 fatal => 1,
36 }
37 },
38 messages => {
39 bad_level => 'Invalid logging level: %s',
40 2 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43  
44 class->methods(
45 # Our init method is called init_log() so that we can use Badger::Log as
46 # a mixin or base class without worrying about the init() method clashing
47 # with init() methods from other base classes or mixins. We create an
48 # alias from init() to init_log() so that it also Just Works[tm] as a
49 # stand-alone object
50 init => \&init_log,
51  
52 # Now we define two methods for each logging level. The first expects
53 # a pre-formatted output message (e.g. debug(), info(), warn(), etc)
54 # the second additionally wraps around the message() method inherited
55 # from Badger::Base (eg. debug_msg(), info_msg(), warn_msg(), etc)
56 map {
57 2 my $level = $_; # lexical variable for closure
58  
59 $level => sub {
60 41 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
61 41 ▶ return $self->{ $level } unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 25 16 41
62 $self->log($level, @_)
63 16 ▶ if $self->{ $level };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$level} 13 3 16
64 },
65  
66 ($level.MSG) => sub {
67 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
68 2 ▶ return $self->{ $level } unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
69 $self->log($level, $self->message(@_))
70 2 ▶ if $self->{ $level };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$level} 1 1 2
71 }
72 10 }
73 keys %$LEVELS
74 );
75  
76  
77 sub init_log {
78 9 ▶ my ($self, $config) = @_;
Subroutine init_log: called CC=3 SCAR=11.0
Pod undocumented
79 9 my $class = $self->class;
80 9 my $levels = $class->hash_vars( LEVELS => $config->{ levels } );
81  
82 # populate $self for each level in $LEVEL using the
83 # value in $config, or the default in $LEVEL
84 9 ▶ while (my ($level, $default) = each %$levels) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$levels) 45 9 54
85 $self->{ $level } =
86 defined $config->{ $level }
87 ? $config->{ $level }
88 45 ▶ : $levels->{ $level };
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{$level} ? : 8 37 45
89 }
90  
91 # call the auto-generated configure() method to update $self from $config
92 9 $self->configure($config);
93  
94 9 return $self;
95 }
96  
97 sub log {
98 11 ▶ my $self = shift;
Subroutine log: called CC=7 SCAR=19.6
Pod documented
99 11 my $level = shift;
100 11 my $action = $self->{ $level };
101 11 my $message = join('', @_);
102 11 my $method;
103  
104 11 ▶ return $self->_fatal_msg( bad_level => $level )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 11 11
105 unless defined $action;
106  
107 # depending on what the $action is set to, we add the message to
108 # an array, call a code reference, delegate to another log object,
109 # print or ignore the mesage
110  
111 11 ▶ if (ref $action eq ARRAY) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $action eq 'ARRAY') { } 3 8 11
elsif (ref $action eq 'CODE') { } 3 5 8
elsif (builtin::blessed($action) and $method = $action->can('log')) { } 1 4 5
elsif ($action) { } 4 0 4
112 3 push(@$action, $message);
113 }
114 elsif (ref $action eq CODE) {
115 3 &$action($level, $message);
116 }
117 ▶ elsif (blessed $action && ($method = $action->can(LOG))) {
Condition66%2/3
builtin::blessed($action) and $method = $action->can("log")
!ll&&!rl&&r
401
MC/DC50%1/2
builtin::blessed($action) and $method = $action->can("log")
builtin::blessed($action) $method = $action->can("log")
Truth table66%2/3
A and B
A= builtin::blessed($action)B= $method = $action->can("log")
ABresult
0X0
100
111
118 1 $method->($action, $level, $message);
119 }
120 elsif ($action) {
121 4 warn $self->format($level, $message), "\n";
122 }
123 }
124  
125 sub format {
126 8 ▶ my $self = shift;
Subroutine format: called CC=2 SCAR=6.9
Pod documented
127 my $args = {
128 time => Now->format($self->{ strftime }),
129 system => $self->{ system },
130 8 level => shift,
131 message => shift,
132 };
133 8 my $format = $self->{ format };
134 8 $format =~
135 s/<(\w+)>/
136 defined $args->{ $1 }
137 20 ▶ ? $args->{ $1 }
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{$1} ? : 19 1 20
138 : "<$1>"
139 /eg;
140 8 return $format;
141 }
142  
143 sub level {
144 9 ▶ my $self = shift;
Subroutine level: called CC=3 SCAR=11.0
Pod documented
145 9 my $level = shift;
146 return $self->_fatal_msg( bad_level => $level )
147 9 ▶ unless exists $LEVELS->{ $level };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Badger::Log::LEVELS->{$level} 1 8 9
148 8 ▶ return @_ ? ($self->{ $level } = shift) : $self->{ $level };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 2 8
149 }
150  
151 sub enable {
152 1 ▶ my $self = shift;
Subroutine enable: called CC=2 SCAR=6.9
Pod documented
153 1 $self->level($_ => 1) for @_;
154 }
155  
156 sub disable {
157 1 ▶ my $self = shift;
Subroutine disable: called CC=2 SCAR=6.9
Pod documented
158 1 $self->level($_ => 0) for @_;
159 }
160  
161 sub _error_msg {
162 0 ▶ my $self = shift;
Subroutine _error_msg: not called CC=1 SCAR=6.9
163 0 $self->Badger::Base::error(
164 $self->Badger::Base::message(@_)
165 );
166 }
167  
168 sub _fatal_msg {
169 1 ▶ my $self = shift;
Subroutine _fatal_msg: called CC=1 SCAR=0.0
170 1 $self->Badger::Base::fatal(
171 $self->Badger::Base::message(@_)
172 );
173 }
174  
175  
176 2 1;
177  
178 __END__
« lib/Badger/Rainbow.pm ↑ Summary lib/Badger/Prototype.pm »