Log4perl/Appender/File.pm

filter: Statementstmt 73.3% 124 / 169 Branchbran 52.7% 58 / 110 Conditioncond 57.5% 19 / 33 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 90.0% 18 / 20 Podpod 22.2% 2 / 9 totaltotal 63.2% 229 / 362 CC 69 SCAR 36.9
CC
69
Cov
64%
perms_fix
132.0
file_open
54.4
log
11.2
CRAP 40.0
« Log4perl/JavaMap/FileAppender.pm ↑ Summary Log4perl.pm »
1 ##################################################
2 package Log::Log4perl::Appender::File;
3 ##################################################
4  
5 13 our @ISA = qw(Log::Log4perl::Appender);
6  
7 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Log::Log4perl::Config::Watch;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use File::Spec::Functions qw(splitpath catpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use constant _INTERNAL_DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use constant SYSWRITE_UTF8_OK => ( $] < 5.024 );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ##################################################
17 41 ▶sub new {
Subroutine new: called CC=9 SCAR=23.5
Pod documented
18 ##################################################
19 41 my($class, @options) = @_;
20  
21 41 my $self = {
22 name => "unknown name",
23 umask => undef,
24 owner => undef,
25 group => undef,
26 autoflush => 1,
27 syswrite => 0,
28 mode => "append",
29 binmode => undef,
30 utf8 => 0,
31 recreate => 0,
32 recreate_check_interval => 30,
33 recreate_check_signal => undef,
34 recreate_pid_write => undef,
35 create_at_logtime => 0,
36 header_text => undef,
37 mkpath => 0,
38 mkpath_umask => 0,
39 @options,
40 };
41  
42 41 ▶ if($self->{create_at_logtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"create_at_logtime"}) 2 39 41
43 2 $self->{recreate} = 1;
44 }
45 41 for my $param ('umask', 'mkpath_umask') {
46 82 ▶ if(defined $self->{$param} and $self->{$param} =~ /^0/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$param} and $self->{$param} =~ /^0/) 42 40 82
Condition66%2/3
defined $self->{$param} and $self->{$param} =~ /^0/
!ll&&!rl&&r
40042
MC/DC50%1/2
defined $self->{$param} and $self->{$param} =~ /^0/
defined $self->{$param} $self->{$param} =~ /^0/
Truth table66%2/3
A and B
A= defined $self->{$param}B= $self->{$param} =~ /^0/
ABresult
0X0
100
111
47 # umask value is a string, meant to be an oct value
48 42 $self->{$param} = oct($self->{$param});
49 }
50 }
51  
52 die "Mandatory parameter 'filename' missing" unless
53 41 ▶ exists $self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"filename"} 0 41 41
54  
55 41 bless $self, $class;
56  
57 41 ▶ if($self->{recreate_pid_write}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"recreate_pid_write"}) 0 41 41
58 0 print "Creating pid file",
59 " $self->{recreate_pid_write}\n" if _INTERNAL_DEBUG;
60 0 ▶ open FILE, ">$self->{recreate_pid_write}" or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, qq[>$self->{"recreate_pid_write"}] or die qq[Cannot open $self->{"recreate_pid_write"}] 0 0 0
61 die "Cannot open $self->{recreate_pid_write}";
62 0 print FILE "$$\n";
63 0 close FILE;
64 }
65  
66 41 print "Calling syswrite_encoder\n" if _INTERNAL_DEBUG;
67  
68 41 $self->{syswrite_encoder} = $self->syswrite_encoder();
69  
70 41 print "syswrite_encoder returned\n" if _INTERNAL_DEBUG;
71  
72 # This will die() if it fails
73 41 ▶ $self->file_open() unless $self->{create_at_logtime};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"create_at_logtime"} 39 2 41
74  
75 41 return $self;
76 }
77  
78 ##################################################
79 41 ▶sub syswrite_encoder {
Subroutine syswrite_encoder: called CC=4 SCAR=21.7
Pod undocumented
80 ##################################################
81 41 my($self) = @_;
82  
83 41 ▶ if( !SYSWRITE_UTF8_OK and $self->{syswrite} and $self->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"syswrite"} and $self->{"utf8"}) 0 41 41
Condition66%2/3
$self->{"syswrite"} and $self->{"utf8"}
!ll&&!rl&&r
3470
MC/DC0%0/2
$self->{"syswrite"} and $self->{"utf8"}
$self->{"syswrite"} $self->{"utf8"}
Truth table66%2/3
A and B
A= $self->{"syswrite"}B= $self->{"utf8"}
ABresult
0X0
100
111
84 0 print "Requiring Encode\n" if _INTERNAL_DEBUG;
85 0 eval { require Encode };
86 0 print "Requiring Encode returned: $@\n" if _INTERNAL_DEBUG;
87  
88 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
89 0 die "syswrite and utf8 requires Encode.pm";
90 } else {
91 0 ▶ return sub { Encode::encode_utf8($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 }
93 }
94  
95 41 return undef;
96 }
97  
98 ##################################################
99 1 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
100 ##################################################
101 1 my($self) = @_;
102  
103 1 return $self->{filename};
104 }
105  
106 ##################################################
107 50 ▶sub file_open {
Subroutine file_open: called CC=32 SCAR=40.0
Pod undocumented
108 ##################################################
109 50 my($self) = @_;
110  
111 50 my $arrows = ">";
112 50 my $sysmode = (O_CREAT|O_WRONLY);
113  
114  
115 50 ▶ if($self->{mode} eq "append") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'mode'} eq 'append') { } 37 13 50
elsif ($self->{'mode'} eq 'pipe') { } 0 13 13
116 37 $arrows = ">>";
117 37 $sysmode |= O_APPEND;
118 } elsif ($self->{mode} eq "pipe") {
119 0 $arrows = "|";
120 } else {
121 13 $sysmode |= O_TRUNC;
122 }
123  
124 50 my $fh = do { local *FH; *FH; };
125  
126  
127 50 my $didnt_exist = ! -e $self->{filename};
128 50 ▶ if($didnt_exist && $self->{mkpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($didnt_exist and $self->{"mkpath"}) 2 48 50
Condition100%3/3
$didnt_exist and $self->{"mkpath"}
!ll&&!rl&&r
23252
MC/DC100%2/2
$didnt_exist and $self->{"mkpath"}
$didnt_exist $self->{"mkpath"}
Truth table100%3/3
A and B
A= $didnt_existB= $self->{"mkpath"}
ABresult
0X0
100
111
129 2 my ($volume, $path, $file) = splitpath($self->{filename});
130 2 ▶ if($path ne '' && !-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path ne "" and not -e $path) 2 0 2
Condition33%1/3
$path ne "" and not -e $path
!ll&&!rl&&r
002
MC/DC0%0/2
$path ne "" and not -e $path
$path ne "" not -e $path
Truth table33%1/3
A and not(B)
A= $path ne ""B= not -e $path
ABresult
0X0
100
111
131 2 ▶ my $old_umask = umask($self->{mkpath_umask}) if defined $self->{mkpath_umask};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"mkpath_umask"} 2 0 2
132 2 my $options = {};
133 2 foreach my $param (qw(owner group) ) {
134 4 ▶ $options->{$param} = $self->{$param} if defined $self->{$param};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$param} 0 4 4
135 }
136 2 eval {
137 2 mkpath(catpath($volume, $path, ''),$options);
138 };
139 2 ▶ umask($old_umask) if defined $old_umask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_umask 2 0 2
140 2 ▶ die "Can't create path ${path} ($!)" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
141 }
142 }
143  
144 50 ▶ my $old_umask = umask($self->{umask}) if defined $self->{umask};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"umask"} 1 49 50
145  
146 50 eval {
147 50 ▶ if($self->{syswrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'syswrite'}) { } 9 41 50
148 9 ▶ sysopen $fh, "$self->{filename}", $sysmode or
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, qq[$self->{"filename"}], $sysmode or die qq[Can't sysopen $self->{"filename"} ($!)] 9 0 9
149 die "Can't sysopen $self->{filename} ($!)";
150 } else {
151 41 ▶ open $fh, "$arrows$self->{filename}" or
Branch: 1 branch
BranchTrueFalseTotal
open $fh, qq[$arrows$self->{"filename"}] or die qq[Can't open $self->{"filename"} ($!)] 41 0 41
152 die "Can't open $self->{filename} ($!)";
153 }
154 };
155 50 ▶ umask($old_umask) if defined $old_umask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_umask 1 49 50
156 50 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 50 50
157  
158 50 ▶ if($didnt_exist and
Branch: 1 branch
BranchTrueFalseTotal
if ($didnt_exist and defined $self->{"owner"} || defined $self->{"group"}) 0 50 50
Condition50%3/6
$didnt_exist and defined $self->{"owner"} || defined $self->{"group"}
!ll&&!rl&&r
23270
defined $self->{"owner"} or defined $self->{"group"}
l!l&&r!l&&!r
0027
MC/DC0%0/3
$didnt_exist and defined $self->{"owner"} || defined $self->{"group"}
$didnt_exist defined $self->{"owner"} defined $self->{"group"}
Truth table50%2/4
A and B or C
A= $didnt_existB= defined $self->{"owner"}C= defined $self->{"group"}
ABCresult
0XX0
1000
11X1
1011
159 ( defined $self->{owner} or defined $self->{group} )
160 ) {
161  
162 0 eval { $self->perms_fix() };
163  
164 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
165 # Cleanup and re-throw
166 0 unlink $self->{filename};
167 0 die $@;
168 }
169 }
170  
171 50 ▶ if($self->{recreate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"recreate"}) 19 31 50
172 $self->{watcher} = Log::Log4perl::Config::Watch->new(
173 file => $self->{filename},
174 (defined $self->{recreate_check_interval} ?
175 (check_interval => $self->{recreate_check_interval}) : ()),
176 (defined $self->{recreate_check_signal} ?
177 19 ▶ (signal => $self->{recreate_check_signal}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'recreate_check_interval'} ? : 19 0 19
defined $self->{'recreate_check_signal'} ? : 7 12 19
178 );
179 }
180  
181 50 $self->{fh} = $fh;
182  
183 50 ▶ if ($self->{autoflush} and ! $self->{syswrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autoflush"} and not $self->{"syswrite"}) 41 9 50
Condition66%2/3
$self->{"autoflush"} and not $self->{"syswrite"}
!ll&&!rl&&r
0941
MC/DC50%1/2
$self->{"autoflush"} and not $self->{"syswrite"}
$self->{"autoflush"} not $self->{"syswrite"}
Truth table66%2/3
A and not(B)
A= $self->{"autoflush"}B= not $self->{"syswrite"}
ABresult
0X0
100
111
184 41 my $oldfh = select $self->{fh};
185 41 $| = 1;
186 41 select $oldfh;
187 }
188  
189 50 ▶ if (defined $self->{binmode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"binmode"}) 0 50 50
190 0 binmode $self->{fh}, $self->{binmode};
191 }
192  
193 50 ▶ if ($self->{utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"utf8"}) 0 50 50
194 # older perls can handle syswrite+utf8 just fine
195 0 ▶ if(SYSWRITE_UTF8_OK or !$self->{syswrite}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"syswrite"}) 0 0 0
196 0 binmode $self->{fh}, ":utf8";
197 }
198 }
199  
200 50 ▶ if(defined $self->{header_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"header_text"}) 2 48 50
201 2 ▶ if( $self->{header_text} !~ /\n\Z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"header_text"} =~ /\n\Z/) 2 0 2
202 2 $self->{header_text} .= "\n";
203 }
204  
205 # quick and dirty print/syswrite without the usual
206 # log() recreate magic.
207 2 local $self->{recreate} = 0;
208 2 $self->log( message => $self->{header_text} );
209 }
210 }
211  
212 ##################################################
213 11 ▶sub file_close {
Subroutine file_close: called CC=2 SCAR=6.9
Pod undocumented
214 ##################################################
215 11 my($self) = @_;
216  
217 11 ▶ if(defined $self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fh"}) 9 2 11
218 9 $self->close_with_care( $self->{ fh } );
219 }
220  
221 11 undef $self->{fh};
222 }
223  
224 ##################################################
225 0 ▶sub perms_fix {
Subroutine perms_fix: not called CC=11 SCAR=48.8
Pod undocumented
226 ##################################################
227 0 my($self) = @_;
228  
229 0 my ($uid_org, $gid_org) = (stat $self->{filename})[4,5];
230  
231 0 my ($uid, $gid) = ($uid_org, $gid_org);
232  
233 0 ▶ if(!defined $uid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid) 0 0 0
234 0 die "stat of $self->{filename} failed ($!)";
235 }
236  
237 0 my $needs_fixing = 0;
238  
239 0 ▶ if(defined $self->{owner}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"owner"}) 0 0 0
240 0 $uid = $self->{owner};
241 0 ▶ if($self->{owner} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"owner"} =~ /^\d+$/) 0 0 0
242 0 $uid = (getpwnam($self->{owner}))[2];
243 0 ▶ die "Unknown user: $self->{owner}" unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 0 0 0
244 }
245 }
246  
247 0 ▶ if(defined $self->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"group"}) 0 0 0
248 0 $gid = $self->{group};
249 0 ▶ if($self->{group} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"group"} =~ /^\d+$/) 0 0 0
250 0 $gid = getgrnam($self->{group});
251  
252 0 ▶ die "Unknown group: $self->{group}" unless defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gid 0 0 0
253 }
254 }
255 0 ▶ if($uid != $uid_org or $gid != $gid_org) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid != $uid_org or $gid != $gid_org) 0 0 0
Condition0%0/3
$uid != $uid_org or $gid != $gid_org
l!l&&r!l&&!r
000
MC/DC0%0/2
$uid != $uid_org or $gid != $gid_org
$uid != $uid_org $gid != $gid_org
Truth table0%0/3
A or B
A= $uid != $uid_orgB= $gid != $gid_org
ABresult
1X1
011
000
256 0 ▶ chown($uid, $gid, $self->{filename}) or
Branch: 1 branch
BranchTrueFalseTotal
chown $uid, $gid, $self->{"filename"} or die qq[chown('${uid}', '${gid}') on '$self->{"filename"}' failed: $!] 0 0 0
257 die "chown('$uid', '$gid') on '$self->{filename}' failed: $!";
258 }
259 }
260  
261 ##################################################
262 11 ▶sub file_switch {
Subroutine file_switch: called CC=1 SCAR=0.0
Pod undocumented
263 ##################################################
264 11 my($self, $new_filename) = @_;
265  
266 11 print "Switching file from $self->{filename} to $new_filename\n" if
267 _INTERNAL_DEBUG;
268  
269 11 $self->file_close();
270 11 $self->{filename} = $new_filename;
271 11 $self->file_open();
272 }
273  
274 ##################################################
275 61 ▶sub log {
Subroutine log: called CC=11 SCAR=24.1
Pod undocumented
276 ##################################################
277 61 my($self, %params) = @_;
278  
279 # Warning: this function gets called by file_open() which assumes
280 # it can use it as a simple print/syswrite wrapper by temporary
281 # disabling the 'recreate' entry. Add anything fancy here and
282 # fix up file_open() accordingly.
283  
284 61 ▶ if($self->{recreate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"recreate"}) 20 41 61
285 20 ▶ if($self->{recreate_check_signal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'recreate_check_signal'}) { } 9 11 20
286 9 ▶ if(!$self->{watcher} or
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"watcher"} or $self->{"watcher"}{"signal_caught"}) 4 5 9
Condition100%3/3
not $self->{"watcher"} or $self->{"watcher"}{"signal_caught"}
l!l&&r!l&&!r
135
MC/DC100%2/2
not $self->{"watcher"} or $self->{"watcher"}{"signal_caught"}
not $self->{"watcher"} $self->{"watcher"}{"signal_caught"}
Truth table100%3/3
not(A) or B
A= not $self->{"watcher"}B= $self->{"watcher"}{"signal_caught"}
ABresult
1X1
011
000
287 $self->{watcher}->{signal_caught}) {
288 4 $self->file_switch($self->{filename});
289 4 $self->{watcher}->{signal_caught} = 0;
290 }
291 } else {
292 11 ▶ if(!$self->{watcher} or
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"watcher"} or $self->{"watcher"}->file_has_moved) 6 5 11
Condition100%3/3
not $self->{"watcher"} or $self->{"watcher"}->file_has_moved
l!l&&r!l&&!r
155
MC/DC100%2/2
not $self->{"watcher"} or $self->{"watcher"}->file_has_moved
not $self->{"watcher"} $self->{"watcher"}->file_has_moved
Truth table100%3/3
not(A) or B
A= not $self->{"watcher"}B= $self->{"watcher"}->file_has_moved
ABresult
1X1
011
000
293 $self->{watcher}->file_has_moved()) {
294 6 $self->file_switch($self->{filename});
295 }
296 }
297 }
298  
299 61 my $fh = $self->{fh};
300  
301 61 ▶ if($self->{syswrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'syswrite'}) { } 11 50 61
302 my $rc =
303 syswrite( $fh,
304 $self->{ syswrite_encoder } ?
305 $self->{ syswrite_encoder }->($params{message}) :
306 11 ▶ $params{message} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'syswrite_encoder'} ? : 0 11 11
307  
308 11 ▶ if(!defined $rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc) 0 11 11
309 0 die "Cannot syswrite to '$self->{filename}': $!";
310 }
311 } else {
312 print $fh $params{message} or
313 50 ▶ die "Cannot write to '$self->{filename}': $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $params{"message"} or die qq[Cannot write to '$self->{"filename"}': $!] 50 0 50
314 }
315 }
316  
317 ##################################################
318 41 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
319 ##################################################
320 41 my($self) = @_;
321  
322 41 ▶ if ($self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"}) 41 0 41
323 41 my $fh = $self->{fh};
324 41 $self->close_with_care( $fh );
325 }
326 }
327  
328 ###########################################
329 50 ▶sub close_with_care {
Subroutine close_with_care: called CC=5 SCAR=23.2
Pod undocumented
330 ###########################################
331 50 my( $self, $fh ) = @_;
332  
333 50 my $prev_rc = $?;
334  
335 50 my $rc = close $fh;
336  
337 # [rt #84723] If a sig handler is reaping the child generated
338 # by close() internally before close() gets to it, it'll
339 # result in a weird (but benign) error that we don't want to
340 # expose to the user.
341 50 ▶ if( !$rc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 0 50 50
342 0 ▶ if( $self->{ mode } eq "pipe" and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mode'} eq 'pipe' and $!{'ECHILD'}) { } 0 0 0
Condition0%0/3
$self->{"mode"} eq "pipe" and $!{"ECHILD"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"mode"} eq "pipe" and $!{"ECHILD"}
$self->{"mode"} eq "pipe" $!{"ECHILD"}
Truth table0%0/3
A and B
A= $self->{"mode"} eq "pipe"B= $!{"ECHILD"}
ABresult
0X0
100
111
343 5 ▶ $!{ ECHILD } ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
344 0 ▶ if( $Log::Log4perl::CHATTY_DESTROY_METHODS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Log4perl::CHATTY_DESTROY_METHODS) 0 0 0
345 0 warn "$$: pipe closed with ECHILD error -- guess that's ok";
346 }
347 0 $? = $prev_rc;
348 } else {
349 0 warn "Can't close $self->{filename} ($!)";
350 }
351 }
352  
353 50 return $rc;
354 }
355  
356 13 1;
357  
358 __END__
« Log4perl/JavaMap/FileAppender.pm ↑ Summary Log4perl.pm »