Agent/Channel/File.pm

filter: Statementstmt 83.3% 85 / 102 Branchbran 57.1% 32 / 56 Conditioncond 37.5% 6 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 93.7% 15 / 16 Podpod 27.2% 3 / 11 totaltotal 67.6% 142 / 210 CC 35 SCAR 31.1
CC
35
Cov
71%
open
45.1
make
10.0
write
5.1
CRAP 22.4
↑ Summary Agent.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::Channel;
16 8 require Log::Agent::Prefixer;
17  
18 ########################################################################
19 package Log::Agent::Channel::File;
20  
21 8 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 @ISA = qw(Log::Agent::Channel Log::Agent::Prefixer);
24  
25 8 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use Log::Agent::Stamping;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #
30 # ->make -- defined
31 #
32 # Creation routine.
33 #
34 # Attributes (and switches that set them):
35 #
36 # prefix the application name
37 # stampfmt stamping format ("syslog", "date", "own", "none") or closure
38 # showpid whether to show pid after prefix in []
39 # filename file name to open (magical open needs -magic_open)
40 # fileperm permissions to open file with
41 # magic_open flag to tell whether ">>file" or "|proc" are allowed filenames
42 # rotate rotating policy for this file
43 # share true implies that non-magic filenames share the same fd object
44 # no_ucfirst don't capitalize first letter of message when no prefix
45 # no_prefixing don't prefix logs
46 # no_newline never append any newline character at the end of messages
47 #
48 # Other attributes:
49 #
50 # fd records Log::Agent::File::* objects
51 # crlf the new-line marker for this OS ("\n" on UNIX)
52 # warned records calls made to hardwired warn() to only do them once
53 #
54 33 ▶sub make {
Subroutine make: called CC=7 SCAR=23.0
Pod documented
55 33 my $self = bless {}, shift;
56 33 my (%args) = @_;
57  
58 my %set = (
59 -prefix => \$self->{'prefix'},
60 -stampfmt => \$self->{'stampfmt'},
61 -showpid => \$self->{'showpid'},
62 -magic_open => \$self->{'magic_open'},
63 -filename => \$self->{'filename'},
64 -fileperm => \$self->{'fileperm'},
65 -rotate => \$self->{'rotate'},
66 -no_ucfirst => \$self->{'no_ucfirst'},
67 -no_prefixing => \$self->{'no_prefixing'},
68 -no_newline => \$self->{'no_newline'},
69 33 -share => \$self->{'share'},
70 );
71  
72 33 ▶ while (my ($arg, $val) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 210 33 243
73 210 my $vset = $set{lc($arg)};
74 210 ▶ unless (ref $vset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $vset) 0 210 210
75 0 require Carp;
76 0 Carp::croak("Unknown switch $arg");
77 }
78 210 $$vset = $val;
79 }
80  
81 #
82 # Initialize proper time-stamping routine.
83 #
84  
85 33 ▶ $self->{'stampfmt'} = stamping_fn($self->stampfmt)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->stampfmt eq "CODE" 27 6 33
86 unless ref $self->stampfmt eq 'CODE';
87  
88 33 $self->{'fd'} = undef;
89 33 ▶ $self->{'crlf'} = $^O =~ /^dos|win/i ? "\r\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /^dos|win/i ? : 0 33 33
90 33 $self->{'warned'} = {};
91  
92 33 ▶ if ($self->rotate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rotate) 0 33 33
93 0 eval {
94 0 require Log::Agent::File::Rotate;
95 };
96 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
97 0 warn $@;
98 0 require Carp;
99 0 Carp::croak("Must install Log::Agent::Rotate to use rotation");
100 }
101 }
102  
103 33 return $self;
104 }
105  
106 #
107 # Attribute access
108 #
109  
110 2 ▶sub magic_open { $_[0]->{'magic_open'} }
Subroutine magic_open: called CC=1 SCAR=0.0
Pod undocumented
111 64 ▶sub rotate { $_[0]->{'rotate'} }
Subroutine rotate: called CC=1 SCAR=0.0
Pod undocumented
112 52 ▶sub filename { $_[0]->{'filename'} }
Subroutine filename: called CC=1 SCAR=0.0
Pod undocumented
113 44 ▶sub fileperm { $_[0]->{'fileperm'} }
Subroutine fileperm: called CC=1 SCAR=0.0
Pod undocumented
114 19 ▶sub fd { $_[0]->{'fd'} }
Subroutine fd: called CC=1 SCAR=0.0
Pod undocumented
115 81 ▶sub share { $_[0]->{'share'} }
Subroutine share: called CC=1 SCAR=0.0
Pod undocumented
116 0 ▶sub warned { $_[0]->{'warned'} }
Subroutine warned: not called CC=1 SCAR=6.9
Pod undocumented
117  
118 #
119 # ->write -- defined
120 #
121 # Write logstring to the file.
122 # Priority is ignored by this channel.
123 #
124 75 ▶sub write {
Subroutine write: called CC=5 SCAR=16.3
Pod documented
125 75 my $self = shift;
126 75 my ($priority, $logstring) = @_;
127  
128 #
129 # This routine is called often...
130 # Bypass the attribute access routines.
131 #
132  
133 75 my $fd = $self->{fd};
134 75 ▶ $fd = $self->open unless $fd;
Branch: 1 branch
BranchTrueFalseTotal
unless $fd 33 42 75
135 75 ▶ return unless ref $fd;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fd 0 75 75
136  
137 75 my $prefix = '';
138 $prefix = $self->prefixing_string(\$logstring)
139 75 ▶ unless $self->{no_prefixing};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_prefixing"} 75 0 75
140  
141 75 my $crlf = '';
142 75 ▶ $crlf = $self->{crlf} unless $self->{no_newline};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_newline"} 75 0 75
143  
144 #
145 # The innocent-looking ->print statement below is NOT a polymorphic call.
146 #
147 # It can be targetted on various Log::Agent::File::* objects, which
148 # all happen to provide a print() feature with the same signature.
149 # However, those clases have no inheritance relationship because Perl
150 # is not typed, and the ancestor would be a deferred class anyway.
151 #
152  
153 75 $fd->print($prefix, $logstring, $crlf);
154 75 return;
155 }
156  
157 #
158 # ->open
159 #
160 # Open channel, and return the opened file descriptor.
161 # Also record opened file within $self->fd.
162 #
163 33 ▶sub open {
Subroutine open: called CC=22 SCAR=38.1
Pod undocumented
164 33 my $self = shift;
165 33 my $filename = $self->filename;
166  
167 33 require Log::Agent::File::Native;
168  
169 33 my $fobj;
170 my $note;
171  
172 #
173 # They may use ">file" or "|proc" as channel files if -magic_open
174 #
175  
176 33 ▶ if ($filename =~ /^\s*[>|]/ && $self->magic_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /^\s*[>|]/ and $self->magic_open) { } 2 31 33
Condition66%2/3
$filename =~ /^\s*[>|]/ and $self->magic_open
!ll&&!rl&&r
3102
MC/DC50%1/2
$filename =~ /^\s*[>|]/ and $self->magic_open
$filename =~ /^\s*[>|]/ $self->magic_open
Truth table66%2/3
A and B
A= $filename =~ /^\s*[>|]/B= $self->magic_open
ABresult
0X0
100
111
177  
178 # restrict the permissions
179 2 my $mask = umask;
180 2 ▶ umask($mask | 0666 ^ $self->fileperm) if defined $self->fileperm;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->fileperm 0 2 2
181  
182 # open the file
183 2 my $h = gensym;
184 2 ▶ $fobj = Log::Agent::File::Native->make($h) if open($h, $filename);
Branch: 1 branch
BranchTrueFalseTotal
if CORE::open $h, $filename 2 0 2
185  
186 # restore the permissions
187 2 umask $mask;
188  
189 } else {
190 #
191 # If the file is already opened, and the current channel can be
192 # shared, do not re-open it: share the same Log::Agent::File::* object,
193 # along with its rotation policy.
194 #
195  
196 31 my $rotate = $self->rotate; # A Log::Agent::Rotate object
197 31 my $pool;
198  
199 31 ▶ if ($self->share) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->share) 31 0 31
200 31 require Log::Agent::File_Pool;
201 31 $pool = Log::Agent::File_Pool::file_pool();
202 31 my ($eobj, $erot) = $pool->get($filename);
203  
204 31 ▶ if (defined $eobj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eobj) 1 30 31
205 1 $fobj = $eobj; # Reuse same object
206 1 ▶ $note = "rotation for '$filename' may be wrong" .
Branch: 1 branch
BranchTrueFalseTotal
if defined $erot and defined $rotate and not $erot->is_same($rotate) 0 1 1
Condition33%2/6
defined $erot and defined $rotate and not $erot->is_same($rotate)
!ll&&!rl&&r
100
defined $erot and defined $rotate
!ll&&!rl&&r
100
MC/DC0%0/3
defined $erot and defined $rotate and not $erot->is_same($rotate)
defined $erot defined $rotate not $erot->is_same($rotate)
Truth table25%1/4
A and B and not(C)
A= defined $erotB= defined $rotateC= not $erot->is_same($rotate)
ABCresult
0XX0
10X0
1100
1111
207 " (shared with distinct policies)" if
208 defined $erot && defined $rotate &&
209 !$erot->is_same($rotate);
210 }
211 }
212  
213 31 ▶ unless (defined $fobj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fobj) 30 1 31
214 30 ▶ if (defined $rotate) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rotate) { } 0 30 30
215 0 $fobj = Log::Agent::File::Rotate->make($filename, $rotate);
216 } else {
217 30 my $h = gensym;
218 30 ▶ $fobj = Log::Agent::File::Native->make($h)
Branch: 2 branches
BranchTrueFalseTotal
if sysopen $h, $filename, 1089, defined $self->fileperm ? $self->fileperm : 438 30 0 30
defined $self->fileperm ? : 12 18 30
219 if sysopen($h, $filename, O_CREAT|O_APPEND|O_WRONLY,
220 defined $self->fileperm ? $self->fileperm : 0666);
221 }
222 }
223  
224 #
225 # Record object in pool if shared, even if already present.
226 # We maintain a refcount of all the shared items.
227 #
228  
229 31 ▶ $pool->put($filename, $fobj, $rotate)
Branch: 1 branch
BranchTrueFalseTotal
if defined $fobj and $self->share 31 0 31
Condition33%1/3
defined $fobj and $self->share
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $fobj and $self->share
defined $fobj $self->share
Truth table33%1/3
A and B
A= defined $fobjB= $self->share
ABresult
0X0
100
111
230 if defined $fobj && $self->share;
231 }
232  
233 #
234 # If an error occurred, we have no choice but to emit a warning via warn().
235 # Otherwise, the error would disappear, and we know they don't want to
236 # silence us, or they would not try to open a logfile.
237 #
238 # Warn only once per filename though.
239 #
240  
241 33 ▶ unless (defined $fobj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fobj) 0 33 33
242 0 ▶ my $prefix = $self->prefixing_string() || "$0: ";
Condition0%0/2
$self->prefixing_string || "$0: "
l!l
00
MC/DC0%0/1
$self->prefixing_string || "$0: "
$self->prefixing_string
Truth table0%0/2
A
A= $self->prefixing_string
Aresult
11
00
243 warn "${prefix}can't open logfile \"$filename\": $!\n"
244 0 ▶ unless $self->warned->{$filename}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->warned->{$filename}++ 0 0 0
245 0 return undef;
246 }
247  
248 33 ▶ $self->{fd} = $fobj || 1; # Avoid recursion in open if not defined
Condition50%1/2
$fobj || 1
l!l
330
MC/DC0%0/1
$fobj || 1
$fobj
Truth table50%1/2
A
A= $fobj
Aresult
11
00
249  
250 #
251 # Print the note, using ->write() now that $self->fd is recorded.
252 #
253  
254 33 ▶ if (defined $note) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $note) 0 33 33
255 0 ▶ $note .= $self->crlf if $self->no_newline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_newline 0 0 0
256 0 $self->write(undef, $note);
257 }
258  
259 33 return $fobj;
260 }
261  
262 #
263 # ->close -- defined
264 #
265 19 ▶sub close {
Subroutine close: called CC=4 SCAR=15.0
Pod documented
266 19 my $self = shift;
267 19 my $fd = $self->fd;
268 19 ▶ return unless ref $fd;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fd 0 19 19
269  
270 19 $self->{fd} = 1; # Prevents further opening from ->write
271 19 ▶ unless ($self->share) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->share) 0 19 19
272 0 $fd->close;
273 0 return;
274 }
275  
276 #
277 # A shared file is physically closed only when the last reference
278 # to it is removed.
279 #
280  
281 19 my $pool = Log::Agent::File_Pool::file_pool();
282 19 ▶ $fd->close if $pool->remove($self->filename);
Branch: 1 branch
BranchTrueFalseTotal
if $pool->remove($self->filename) 19 0 19
283 19 return;
284 }
285  
286 8 1; # for require
287 __END__
↑ Summary Agent.pm »