blib/lib/Log/Agent/Logger.pm

filter: Statementstmt 81.3% 135 / 166 Branchbran 57.3% 39 / 68 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 72.2% 26 / 36 Podpod 91.6% 11 / 12 totaltotal 72.7% 216 / 297 CC 39 SCAR 13.4
CC
39
Cov
73%
make
10.1
_log_fn
6.6
set_max_prio
6.0
CRAP 3.8
↑ Summary
1 #!./perl
2 ###########################################################################
3 #
4 # Logger.pm
5 #
6 # Copyright (C) 1999-2000 Raphael Manfredi.
7 # Copyright (C) 2015 Mark Rogaski, mrogaski@cpan.org;
8 # all rights reserved.
9 #
10 # See the README file included with the
11 # distribution for license information.
12 #
13 ##########################################################################
14  
15 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ########################################################################
18 package Log::Agent::Logger;
19  
20 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 our $VERSION = '0.201';
23 4 $VERSION = eval $VERSION;
24  
25 4 ▶use Log::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Log::Agent::Formatting qw(tag_format_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Log::Agent::Priorities qw(:LEVELS level_from_prio prio_from_level);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Getargs::Long qw(ignorecase);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 4 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.2
31 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 my %fn;
33 4 ▶ %fn = (
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Log::Agent::Logger::emergency 4 0 4
34 'emerg' => q/['emerg', EMERG]/,
35 'emergency' => q/['emerg', EMERG]/,
36 'alert' => q/['alert', ALERT]/,
37 'crit' => q/['crit', CRIT]/,
38 'critical' => q/['crit', CRIT]/,
39 'err' => q/['err', ERROR]/,
40 'error' => q/['err', ERROR]/,
41 'warning', => q/['warning', WARN]/,
42 'warn', => q/['warning', WARN]/,
43 'notice' => q/['notice', NOTICE]/,
44 'info' => q/['info', INFO]/,
45 'debug' => q/['debug', DEBUG]/,
46 ) unless defined &emergency;
47 4 for my $sub (keys %fn) {
48 48 my $prilvl = $fn{$sub};
49 0 ▶ *$sub = eval qq{
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: not called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine __ANON__: not called CC=2 SCAR=7.9
Subroutine __ANON__: called CC=2 SCAR=7.9
Branch: 12 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 0 1 1
if (ref $_[0] eq 'CODE') { } 0 4 4
if (ref $_[0] eq 'CODE') { } 0 1 1
if (ref $_[0] eq 'CODE') { } 0 2 2
if (ref $_[0] eq 'CODE') { } 0 1 1
if (ref $_[0] eq 'CODE') { } 0 5 5
if (ref $_[0] eq 'CODE') { } 0 0 0
if (ref $_[0] eq 'CODE') { } 0 1 1
if (ref $_[0] eq 'CODE') { } 0 1 1
if (ref $_[0] eq 'CODE') { } 0 1 1
if (ref $_[0] eq 'CODE') { } 0 0 0
if (ref $_[0] eq 'CODE') { } 4 2 6
50 sub {
51 my \$self = shift;
52 if (ref \$_[0] eq 'CODE') {
53 \$self->_log_fn($prilvl, \\\@_);
54 } else {
55 \$self->_log($prilvl, \\\@_);
56 }
57 return;
58 }
59 };
60 }
61 }
62  
63 #
64 # ->make
65 #
66 # Creation routine.
67 #
68 # Attributes (and switches that set them):
69 #
70 # -channel logging channel
71 # -max_prio maximum priority logged (included)
72 # -min_prio minimum priority logged (included)
73 # -caller customizes the caller information to be inserted
74 # -priority customizes the priority information to be inserted
75 # -tags list of user-defined tags to add to messages
76 #
77 4 ▶sub make {
Subroutine make: called CC=9 SCAR=23.1
Pod undocumented
78 4 my $self = bless {}, shift;
79 4 my ($caller, $priority, $tags);
80  
81 (
82 $self->{channel}, $self->{max_prio}, $self->{min_prio},
83 4 $caller, $priority, $tags
84 ) = xgetargs(@_,
85 -channel => 'Log::Agent::Channel',
86 -max_prio => ['s', DEBUG],
87 -min_prio => ['s', EMERG],
88 -caller => ['ARRAY'],
89 -priority => ['ARRAY'],
90 -tags => ['ARRAY'],
91 );
92  
93 #
94 # Always use numeric priorities internally
95 #
96  
97 $self->{max_prio} = level_from_prio($self->{max_prio})
98 4 ▶ if $self->{max_prio} =~ /^\D+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_prio"} =~ /^\D+$/ 4 0 4
99  
100 $self->{min_prio} = level_from_prio($self->{min_prio})
101 4 ▶ if $self->{min_prio} =~ /^\D+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"min_prio"} =~ /^\D+$/ 0 4 4
102  
103 4 ▶ $self->set_priority_info(@$priority) if defined $priority;
Branch: 1 branch
BranchTrueFalseTotal
if defined $priority 1 3 4
104 4 ▶ $self->set_caller_info(@$caller) if defined $caller;
Branch: 1 branch
BranchTrueFalseTotal
if defined $caller 1 3 4
105  
106 #
107 # Handle -tags => [ <list of Log::Agent::Tag objects> ]
108 #
109  
110 4 ▶ if (defined $tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags) 1 3 4
111 1 my $type = "Log::Agent::Tag";
112 1 ▶ if (grep { !ref $_ || !$_->isa($type) } @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not ref $_ && $_->isa($type);} @$tags) 0 1 1
Condition33%1/3
ref $_ && $_->isa($type)
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_ && $_->isa($type)
ref $_ $_->isa($type)
Truth table33%1/3
A && B
A= ref $_B= $_->isa($type)
ABresult
0X0
100
111
113 0 require Carp;
114 0 Carp::croak("Argument -tags must supply list of $type objects");
115 }
116 1 ▶ if (@$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tags) 1 0 1
117 1 require Log::Agent::Tag_List;
118 1 $self->{tags} = Log::Agent::Tag_List->make(@$tags);
119 }
120 }
121  
122 4 return $self;
123 }
124  
125 #
126 # Attribute access
127 #
128  
129 0 ▶sub channel { $_[0]->{channel} }
Subroutine channel: not called CC=1 SCAR=6.9
Pod documented
130 0 ▶sub max_prio { $_[0]->{max_prio} }
Subroutine max_prio: not called CC=1 SCAR=6.9
Pod documented
131 0 ▶sub min_prio { $_[0]->{min_prio} }
Subroutine min_prio: not called CC=1 SCAR=6.9
Pod documented
132 1 ▶sub tags { $_[0]->{tags} || $_[0]->_init_tags }
Subroutine tags: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"tags"} or $_[0]->_init_tags 1 0 1
133  
134 0 ▶sub max_prio_str { prio_from_level $_[0]->{max_prio} }
Subroutine max_prio_str: not called CC=1 SCAR=6.9
Pod documented
135 0 ▶sub min_prio_str { prio_from_level $_[0]->{min_prio} }
Subroutine min_prio_str: not called CC=1 SCAR=6.9
Pod documented
136  
137 sub set_max_prio
138 0 ▶ { $_[0]->{max_prio} = $_[1] =~ /^\D+$/ ? level_from_prio($_[1]) : $_[1] }
Subroutine set_max_prio: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /^\D+$/ ? : 0 0 0
139 sub set_min_prio
140 0 ▶ { $_[0]->{min_prio} = $_[1] =~ /^\D+$/ ? level_from_prio($_[1]) : $_[1] }
Subroutine set_min_prio: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /^\D+$/ ? : 0 0 0
141  
142 #
143 # ->close
144 #
145 # Close underlying channel, and detach from it.
146 #
147 3 ▶sub close {
Subroutine close: called CC=2 SCAR=7.0
Pod documented
148 3 my $self = shift;
149 3 my $channel = $self->{channel};
150 3 ▶ return unless defined $channel; # Already closed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel 0 3 3
151 3 $self->{channel} = undef;
152 3 $channel->close;
153 }
154  
155 #
156 # ->set_caller_info
157 #
158 # Change settings of caller tag information.
159 # Giving an empty list removes caller tagging.
160 #
161 3 ▶sub set_caller_info {
Subroutine set_caller_info: called CC=2 SCAR=6.9
Pod documented
162 3 my $self = shift;
163  
164 3 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 2 3
165 1 delete $self->{caller};
166 1 return;
167 }
168  
169 2 require Log::Agent::Tag::Caller;
170 2 $self->{caller} = Log::Agent::Tag::Caller->make(-offset => 4, @_);
171 2 return;
172 }
173  
174 #
175 # ->set_priority_info
176 #
177 # Change settings of caller tag information.
178 # Giving an empty list removes priority tagging.
179 #
180 3 ▶sub set_priority_info {
Subroutine set_priority_info: called CC=2 SCAR=6.9
Pod documented
181 3 my $self = shift;
182 3 my @info = @_;
183  
184 3 ▶ unless (@info) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@info) 1 2 3
185 1 delete $self->{priority};
186 1 return;
187 }
188  
189 2 $self->{priority} = \@info; # For objects created in _prio_tag()
190  
191 #
192 # When settings are changes, we need to clear the cache of priority
193 # tags generated by _prio_tag().
194 #
195  
196 2 $self->{prio_cache} = {}; # Internal for ->_prio_tag()
197 2 return;
198 }
199  
200  
201 #
202 # ->_log
203 #
204 # Emit log at given priority, if within priority bounds.
205 #
206 19 ▶sub _log {
Subroutine _log: called CC=5 SCAR=16.1
207 19 my ($self, $prilvl) = splice(@_, 0, 2);
208 19 my $channel = $self->{channel};
209 19 ▶ return unless defined $channel; # Closed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel 1 18 19
210  
211 #
212 # Prune call if we're not within bounds.
213 # $prilvl is seomthing like ["error", ERROR].
214 #
215  
216 18 my $lvl = $prilvl->[1];
217 18 ▶ return if $lvl > $self->{max_prio} || $lvl < $self->{min_prio};
Branch: 1 branch
BranchTrueFalseTotal
if $lvl > $self->{"max_prio"} or $lvl < $self->{"min_prio"} 1 17 18
Condition66%2/3
$lvl > $self->{"max_prio"} or $lvl < $self->{"min_prio"}
l!l&&r!l&&!r
1017
MC/DC50%1/2
$lvl > $self->{"max_prio"} or $lvl < $self->{"min_prio"}
$lvl > $self->{"max_prio"} $lvl < $self->{"min_prio"}
Truth table66%2/3
A or B
A= $lvl > $self->{"max_prio"}B= $lvl < $self->{"min_prio"}
ABresult
1X1
011
000
218  
219 #
220 # Issue logging.
221 #
222  
223 17 ▶ my $priority = $self->_prio_tag(@$prilvl) if defined $self->{priority};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"priority"} 4 13 17
224  
225 $channel->write($prilvl->[0],
226 17 tag_format_args($self->{caller}, $priority, $self->{tags}, @_));
227  
228 17 return;
229 }
230  
231 #
232 # ->_log_fn
233 #
234 # Emit log at given priority, if within priority bounds.
235 # The logged string needs to be computed by calling back a routine.
236 #
237 4 ▶sub _log_fn {
Subroutine _log_fn: called CC=6 SCAR=18.8
238 4 my ($self, $prilvl) = splice(@_, 0, 2);
239 4 my $channel = $self->{channel};
240 4 ▶ return unless defined $channel; # Closed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel 0 4 4
241  
242 #
243 # Prune call if we're not within bounds.
244 # $prilvl is seomthing like ["error", ERROR].
245 #
246  
247 4 my $lvl = $prilvl->[1];
248 4 ▶ return if $lvl > $self->{max_prio} || $lvl < $self->{min_prio};
Branch: 1 branch
BranchTrueFalseTotal
if $lvl > $self->{"max_prio"} or $lvl < $self->{"min_prio"} 0 4 4
Condition33%1/3
$lvl > $self->{"max_prio"} or $lvl < $self->{"min_prio"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$lvl > $self->{"max_prio"} or $lvl < $self->{"min_prio"}
$lvl > $self->{"max_prio"} $lvl < $self->{"min_prio"}
Truth table33%1/3
A or B
A= $lvl > $self->{"max_prio"}B= $lvl < $self->{"min_prio"}
ABresult
1X1
011
000
249  
250 #
251 # Issue logging.
252 #
253  
254 4 my $fn = shift @{$_[0]};
255 4 my $msg = &$fn(@{$_[0]});
256 4 ▶ return unless length $msg; # Null messsage, don't log
Branch: 1 branch
BranchTrueFalseTotal
unless length $msg 0 4 4
257  
258 4 ▶ my $priority = $self->_prio_tag(@$prilvl) if defined $self->{priority};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"priority"} 0 4 4
259  
260 $channel->write($prilvl->[0],
261 4 tag_format_args($self->{caller}, $priority, $self->{tags}, [$msg]));
262  
263 4 return;
264 }
265  
266 #
267 # _prio_tag
268 #
269 # Returns Log::Agent::Tag::Priority message that is suitable for tagging
270 # at this priority/level, if configured to log priorities.
271 #
272 # Objects are cached into `prio_cache'.
273 #
274 4 ▶sub _prio_tag {
Subroutine _prio_tag: called CC=2 SCAR=7.0
275 4 my $self = shift;
276 4 my ($prio, $level) = @_;
277 4 my $ptag = $self->{prio_cache}->{$prio, $level};
278 4 ▶ return $ptag if defined $ptag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ptag 0 4 4
279  
280 4 require Log::Agent::Tag::Priority;
281  
282 #
283 # Common attributes (formatting, postfixing, etc...) are held in
284 # the `priorities' attribute. We add the priority/level here.
285 #
286  
287 $ptag = Log::Agent::Tag::Priority->make(
288 -priority => $prio,
289 -level => $level,
290 4 @{$self->{priority}}
291 );
292  
293 4 return $self->{prio_cache}->{$prio, $level} = $ptag;
294 }
295  
296 #
297 # ->_init_tags
298 #
299 # Initialize the `tags' attribute the first time it is requested
300 # Returns its value.
301 #
302 0 ▶sub _init_tags {
Subroutine _init_tags: not called CC=1 SCAR=6.9
303 0 my $self = shift;
304 0 require Log::Agent::Tag_List;
305 0 return $self->{tags} = Log::Agent::Tag_List->make();
306 }
307  
308 4 1; # for require
309 __END__
↑ Summary