Agent/Driver/File.pm

filter: Statementstmt 77.0% 104 / 135 Branchbran 59.6% 37 / 62 Conditioncond 53.3% 8 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 84.0% 21 / 25 Podpod 45.4% 10 / 22 totaltotal 68.4% 184 / 269 CC 39 SCAR 23.5
CC
39
Cov
70%
chanfn
30.0
make
21.7
open_channel
9.3
CRAP 10.5
« Agent.pm ↑ Summary Agent/Prefixer.pm »
1 ###########################################################################
2 #
3 # File.pm
4 #
5 # Copyright (C) 1999 Raphael Manfredi.
6 # Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
7 # all rights reserved.
8 #
9 # See the README file included with the
10 # distribution for license information.
11 #
12 ##########################################################################
13  
14 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 require Log::Agent::Driver;
16  
17 ########################################################################
18 package Log::Agent::Driver::File;
19  
20 8 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 @ISA = qw(Log::Agent::Driver);
23  
24 #
25 # ->make -- defined
26 #
27 # Creation routine.
28 #
29 # Attributes (and switches that set them):
30 #
31 # prefix the application name
32 # duperr whether to duplicate "error" channels to "output"
33 # stampfmt stamping format ("syslog", "date", "own", "none") or closure
34 # showpid whether to show pid after prefix in []
35 # channels where each channel ("error", "output", "debug") goes
36 # chanperm what permissions each channel ("error", "output", "debug") has
37 # magic_open flag to tell whether ">>file" or "|proc" are allowed filenames
38 # rotate default rotating policy for logfiles
39 #
40 # Additional switches:
41 #
42 # file sole channel, implies -duperr = 0 and supersedes -channels
43 # perm file permissions that supersedes all channel permissions
44 #
45 # Other attributes:
46 #
47 # channel_obj opened channel objects
48 #
49 21 ▶sub make {
Subroutine make: called CC=16 SCAR=30.8
Pod documented
50 21 my $self = bless {}, shift;
51 21 my (%args) = @_;
52 21 my $prefix;
53 my $file;
54 21 my $perm;
55  
56 my %set = (
57 -prefix => \$prefix, # Handled by parent via _init
58 -duperr => \$self->{'duperr'},
59 -channels => \$self->{'channels'},
60 -chanperm => \$self->{'chanperm'},
61 -stampfmt => \$self->{'stampfmt'},
62 -showpid => \$self->{'showpid'},
63 -magic_open => \$self->{'magic_open'},
64 -file => \$file,
65 -perm => \$perm,
66 21 -rotate => \$self->{'rotate'},
67 );
68  
69 21 ▶ while (my ($arg, $val) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 52 21 73
70 52 my $vset = $set{lc($arg)};
71 52 ▶ unless (ref $vset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $vset) 0 52 52
72 0 require Carp;
73 0 Carp::croak("Unknown switch $arg");
74 }
75 52 $$vset = $val;
76 }
77  
78 #
79 # If -file was used, it supersedes -duperr and -channels
80 #
81  
82 21 ▶ if (defined $file && length $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and length $file) 6 15 21
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
1506
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
83 6 $self->{'channels'} = {
84 'debug' => $file,
85 'output' => $file,
86 'error' => $file,
87 };
88 6 $self->{'duperr'} = 0;
89 }
90  
91 #
92 # and we do something similar for file permissions
93 #
94  
95 21 ▶ if (defined $perm && length $perm) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $perm and length $perm) 6 15 21
Condition66%2/3
defined $perm and length $perm
!ll&&!rl&&r
1506
MC/DC50%1/2
defined $perm and length $perm
defined $perm length $perm
Truth table66%2/3
A and B
A= defined $permB= length $perm
ABresult
0X0
100
111
96 $self->{chanperm} = {
97 6 debug => $perm,
98 output => $perm,
99 error => $perm
100 };
101 }
102  
103 21 $self->_init($prefix, 0); # 1 is the skip Carp penalty for confess
104  
105 21 ▶ $self->{channels} = {} unless $self->channels; # No defined channels
Branch: 1 branch
BranchTrueFalseTotal
unless $self->channels 1 20 21
106 21 ▶ $self->{chanperm} = {} unless $self->chanperm; # No defined perms
Branch: 1 branch
BranchTrueFalseTotal
unless $self->chanperm 13 8 21
107 21 $self->{channel_obj} = {}; # No opened files
108  
109 #
110 # Check for logfile rotation, which can be specified on a global or
111 # file by file basis. Since Log::Agent::Rotate is a separate extension,
112 # it may not be installed.
113 #
114  
115 21 ▶ my $use_rotate = defined($self->rotate) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->rotate ? : 0 21 21
116 21 ▶ unless ($use_rotate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_rotate) 21 0 21
117 21 foreach my $chan (keys %{$self->channels}) {
118 47 ▶ $use_rotate = 1 if ref $self->channels->{$chan} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->channels->{$chan} eq "ARRAY" 0 47 47
119 47 ▶ last if $use_rotate;
Branch: 1 branch
BranchTrueFalseTotal
if $use_rotate 0 47 47
120 }
121 }
122  
123 21 ▶ if ($use_rotate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_rotate) 0 21 21
124 0 eval {
125 0 require Log::Agent::File::Rotate;
126 };
127 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
128 0 warn $@;
129 0 require Carp;
130 0 Carp::croak("Must install Log::Agent::Rotate to use rotation");
131 }
132 }
133  
134 21 return $self;
135 }
136  
137 #
138 # Attribute access
139 #
140  
141 37 ▶sub duperr { $_[0]->{duperr} }
Subroutine duperr: called CC=1 SCAR=0.0
Pod undocumented
142 128 ▶sub channels { $_[0]->{channels} }
Subroutine channels: called CC=1 SCAR=0.0
Pod undocumented
143 66 ▶sub chanperm { $_[0]->{chanperm} }
Subroutine chanperm: called CC=1 SCAR=0.0
Pod undocumented
144 125 ▶sub channel_obj { $_[0]->{channel_obj} }
Subroutine channel_obj: called CC=1 SCAR=0.0
Pod undocumented
145 35 ▶sub stampfmt { $_[0]->{stampfmt} }
Subroutine stampfmt: called CC=1 SCAR=0.0
Pod undocumented
146 35 ▶sub showpid { $_[0]->{showpid} }
Subroutine showpid: called CC=1 SCAR=0.0
Pod undocumented
147 33 ▶sub magic_open { $_[0]->{magic_open} }
Subroutine magic_open: called CC=1 SCAR=0.0
Pod undocumented
148 56 ▶sub rotate { $_[0]->{rotate} }
Subroutine rotate: called CC=1 SCAR=0.0
Pod undocumented
149  
150 #
151 # ->prefix_msg -- defined
152 #
153 # NOP: channel handles prefixing for us.
154 #
155 70 ▶sub prefix_msg {
Subroutine prefix_msg: called CC=1 SCAR=0.0
Pod documented
156 70 my $self = shift;
157 70 return $_[0];
158 }
159  
160 #
161 # ->chanfn
162 #
163 # Return channel file name.
164 #
165 0 ▶sub chanfn {
Subroutine chanfn: not called CC=5 SCAR=34.0
Pod undocumented
166 0 my $self = shift;
167 0 my ($channel) = @_;
168 0 my $filename = $self->channels->{$channel};
169 0 ▶ if (ref $filename eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq "ARRAY") 0 0 0
170 0 $filename = $filename->[0];
171 }
172 # No channel defined, use 'error'
173 0 ▶ $filename = $self->channels->{'error'} unless
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and length $filename 0 0 0
Condition0%0/3
defined $filename and length $filename
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filename and length $filename
defined $filename length $filename
Truth table0%0/3
A and B
A= defined $filenameB= length $filename
ABresult
0X0
100
111
174 defined $filename && length $filename;
175 0 ▶ $filename = '<STDERR>' unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 0 0
176  
177 0 return $filename;
178 }
179  
180 #
181 # ->channel_eq -- defined
182 #
183 # Compare two channels.
184 #
185 # It's hard to know for certain that two channels are equivalent, so we
186 # compare filenames. This is not correct, of course, but it will do for
187 # what we're trying to achieve here, namely avoid duplicates if possible
188 # when traces are remapped to Carp::Datum.
189 #
190 0 ▶sub channel_eq {
Subroutine channel_eq: not called CC=1 SCAR=6.9
Pod documented
191 0 my $self = shift;
192 0 my ($chan1, $chan2) = @_;
193 0 my $fn1 = $self->chanfn($chan1);
194 0 my $fn2 = $self->chanfn($chan2);
195 0 return $fn1 eq $fn2;
196 }
197  
198 #
199 # ->write -- defined
200 #
201 77 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod documented
202 77 my $self = shift;
203 77 my ($channel, $priority, $logstring) = @_;
204 77 my $chan = $self->channel($channel);
205 77 ▶ return unless $chan;
Branch: 1 branch
BranchTrueFalseTotal
unless $chan 0 77 77
206  
207 77 $chan->write($priority, $logstring);
208 }
209  
210 #
211 # ->channel
212 #
213 # Return channel object (one of the Log::Agent::Channel::* objects)
214 #
215 77 ▶sub channel {
Subroutine channel: called CC=2 SCAR=6.9
Pod undocumented
216 77 my $self = shift;
217 77 my ($name) = @_;
218 77 my $obj = $self->channel_obj->{$name};
219 77 ▶ $obj = $self->open_channel($name) unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 35 42 77
220 77 return $obj;
221 }
222  
223  
224 #
225 # ->open_channel
226 #
227 # Open given channel according to the configured channel description and
228 # return the object file descriptor.
229 #
230 # If no channel of that name was defined, use 'error' or STDERR.
231 #
232 35 ▶sub open_channel {
Subroutine open_channel: called CC=9 SCAR=22.3
Pod undocumented
233 35 my $self = shift;
234 35 my ($name) = @_;
235 35 my $filename = $self->channels->{$name};
236  
237 #
238 # Handle possible logfile rotation, which may be defined globally
239 # or on a file by file basis.
240 #
241  
242 35 my $rotate; # A Log::Agent::Rotate object
243 35 ▶ if (ref $filename eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq 'ARRAY') { } 0 35 35
244 0 ($filename, $rotate) = @$filename;
245 } else {
246 35 $rotate = $self->rotate;
247 }
248  
249 35 my @common_args = (
250 -prefix => $self->prefix,
251 -stampfmt => $self->stampfmt,
252 -showpid => $self->showpid,
253 );
254 35 my @other_args;
255 my $type;
256  
257 #
258 # No channel defined, use 'error', or revert to STDERR
259 #
260  
261 35 ▶ unless (defined $filename && length $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename and length $filename) 4 31 35
Condition66%2/3
defined $filename and length $filename
!ll&&!rl&&r
4031
MC/DC50%1/2
defined $filename and length $filename
defined $filename length $filename
Truth table66%2/3
A and B
A= defined $filenameB= length $filename
ABresult
0X0
100
111
262 4 $filename = $self->channels->{'error'};
263 4 ▶ ($filename, $rotate) = @$filename if ref $filename eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filename eq "ARRAY" 0 4 4
264 }
265  
266 35 ▶ unless (defined $filename && length $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and length $filename) { } 33 2 35
Condition66%2/3
defined $filename and length $filename
!ll&&!rl&&r
2033
MC/DC50%1/2
defined $filename and length $filename
defined $filename length $filename
Truth table66%2/3
A and B
A= defined $filenameB= length $filename
ABresult
0X0
100
111
267 2 require Log::Agent::Channel::Handle;
268 2 select((select(main::STDERR), $| = 1)[0]);
269 2 $type = "Log::Agent::Channel::Handle";
270 2 @other_args = (-handle => \*main::STDERR);
271 } else {
272 33 require Log::Agent::Channel::File;
273 33 $type = "Log::Agent::Channel::File";
274 33 @other_args = (
275 -filename => $filename,
276 -magic_open => $self->magic_open,
277 -share => 1,
278 );
279 push(@other_args, -fileperm => $self->chanperm->{$name})
280 33 ▶ if $self->chanperm->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->chanperm->{$name} 12 21 33
281 33 ▶ push(@other_args, -rotate => $rotate) if ref $rotate;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rotate 0 33 33
282 }
283  
284 35 return $self->channel_obj->{$name} =
285 $type->make(@common_args, @other_args);
286 }
287  
288 #
289 # ->emit_output
290 #
291 # Force error message to the regular 'output' channel with a specified tag.
292 #
293 6 ▶sub emit_output {
Subroutine emit_output: called CC=1 SCAR=0.0
Pod undocumented
294 6 my $self = shift;
295 6 my ($prio, $tag, $str) = @_;
296 6 my $cstr = $str->clone; # We're prepending tag on a copy
297 6 $cstr->prepend("$tag: ");
298 6 $self->write('output', $prio, $cstr);
299 }
300  
301 ###
302 ### Redefined routines to handle duperr
303 ###
304  
305 #
306 # ->logconfess
307 #
308 # When `duperr' is true, emit message on the 'output' channel prefixed
309 # with FATAL.
310 #
311 sub logconfess {
312 my $self = shift;
313 my ($str) = @_;
314 $self->emit_output('critical', "FATAL", $str) if $self->duperr;
315 $self->SUPER::logconfess($str); # Carp strips calls within hierarchy
316 }
317  
318 #
319 # ->logxcroak
320 #
321 # When `duperr' is true, emit message on the 'output' channel prefixed
322 # with FATAL.
323 #
324 0 ▶sub logxcroak {
Subroutine logxcroak: not called CC=2 SCAR=17.9
Pod documented
325 0 my $self = shift;
326 0 my ($offset, $str) = @_;
327 0 my $msg = Log::Agent::Message->make(
328 $self->carpmess($offset, $str, \&Carp::shortmess)
329 );
330 0 ▶ $self->emit_output('critical', "FATAL", $msg) if $self->duperr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->duperr 0 0 0
331  
332 #
333 # Carp strips calls within hierarchy, so that new call should not show,
334 # there's no need to adjust the frame offset.
335 #
336 0 $self->SUPER::logdie($msg);
337 }
338  
339 #
340 # ->logdie
341 #
342 # When `duperr' is true, emit message on the 'output' channel prefixed
343 # with FATAL.
344 #
345 2 ▶sub logdie {
Subroutine logdie: called CC=2 SCAR=6.9
Pod documented
346 2 my $self = shift;
347 2 my ($str) = @_;
348 2 ▶ $self->emit_output('critical', "FATAL", $str) if $self->duperr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->duperr 1 1 2
349 2 $self->SUPER::logdie($str);
350 }
351  
352 #
353 # ->logerr
354 #
355 # When `duperr' is true, emit message on the 'output' channel prefixed
356 # with ERROR.
357 #
358 12 ▶sub logerr {
Subroutine logerr: called CC=2 SCAR=6.9
Pod documented
359 12 my $self = shift;
360 12 my ($str) = @_;
361 12 ▶ $self->emit_output('error', "ERROR", $str) if $self->duperr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->duperr 1 11 12
362 12 $self->SUPER::logerr($str);
363 }
364  
365 #
366 # ->logcluck
367 #
368 # When `duperr' is true, emit message on the 'output' channel prefixed
369 # with WARNING.
370 #
371 0 ▶sub logconfess {
Subroutine logconfess: not called CC=2 SCAR=17.9
Pod documented
372 0 my $self = shift;
373 0 my ($str) = @_;
374 0 ▶ $self->emit_output('warning', "WARNING", $str) if $self->duperr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->duperr 0 0 0
375 0 $self->SUPER::logcluck($str); # Carp strips calls within hierarchy
376 }
377  
378 #
379 # ->logwarn
380 #
381 # When `duperr' is true, emit message on the 'output' channel prefixed
382 # with WARNING.
383 #
384 4 ▶sub logwarn {
Subroutine logwarn: called CC=2 SCAR=6.9
Pod documented
385 4 my $self = shift;
386 4 my ($str) = @_;
387 4 ▶ $self->emit_output('warning', "WARNING", $str) if $self->duperr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->duperr 1 3 4
388 4 $self->SUPER::logwarn($str);
389 }
390  
391 #
392 # ->logxcarp
393 #
394 # When `duperr' is true, emit message on the 'output' channel prefixed
395 # with WARNING.
396 #
397 13 ▶sub logxcarp {
Subroutine logxcarp: called CC=2 SCAR=7.0
Pod documented
398 13 my $self = shift;
399 13 my ($offset, $str) = @_;
400 13 my $msg = Log::Agent::Message->make(
401 $self->carpmess($offset, $str, \&Carp::shortmess)
402 );
403 13 ▶ $self->emit_output('warning', "WARNING", $msg) if $self->duperr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->duperr 0 13 13
404 13 $self->SUPER::logwarn($msg);
405 }
406  
407 #
408 # ->DESTROY
409 #
410 # Close all opened channels, so they may be removed from the common pool.
411 #
412 13 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=14.3
413 13 my $self = shift;
414 13 my $channel_obj = $self->channel_obj;
415 13 ▶ return unless defined $channel_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel_obj 0 13 13
416 13 foreach my $chan (values %$channel_obj) {
417 21 ▶ $chan->close if defined $chan;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chan 21 0 21
418 }
419 }
420  
421 8 1; # for require
422 __END__
« Agent.pm ↑ Summary Agent/Prefixer.pm »