Dispatch/File.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 77.5% 31 / 40 Conditioncond 66.6% 8 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 3 totaltotal 85.3% 122 / 143 CC 24 SCAR 18.2
CC
24
Cov
88%
_open_file
9.4
log_message
8.1
new
7.1
CRAP 6.2
« Dispatch/File/Locked.pm ↑ Summary Dispatch/Output.pm »
1 package Log::Dispatch::File;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 our $VERSION = '2.71';
7  
8 27 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Log::Dispatch::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Scalar::Util qw( openhandle );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 27 ▶use base qw( Log::Dispatch::Output );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Prevents death later on if IO::File can't export this constant.
16 27 ▶*O_APPEND = \&APPEND unless defined &O_APPEND;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Log::Dispatch::File::O_APPEND 27 0 27
17  
18 0 ▶sub APPEND {0}
Subroutine APPEND: not called CC=1 SCAR=6.9
Pod undocumented
19  
20 {
21 27 my $validator = validation_for(
22 params => {
23 filename => { type => t('NonEmptyStr') },
24 mode => {
25 type => t('Value'),
26 default => '>',
27 },
28 binmode => {
29 type => t('Str'),
30 optional => 1,
31 },
32 autoflush => {
33 type => t('Bool'),
34 default => 1,
35 },
36 close_after_write => {
37 type => t('Bool'),
38 default => 0,
39 },
40 lazy_open => {
41 type => t('Bool'),
42 default => 0,
43 },
44 permissions => {
45 type => t('PositiveOrZeroInt'),
46 optional => 1,
47 },
48 syswrite => {
49 type => t('Bool'),
50 default => 0,
51 },
52 },
53 slurpy => 1,
54 );
55  
56 # We stick these in $self as-is without looking at them in new().
57 27 my @self_p = qw(
58 autoflush
59 binmode
60 close_after_write
61 filename
62 lazy_open
63 permissions
64 syswrite
65 );
66  
67 68 ▶ sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod undocumented
68 68 my $class = shift;
69 68 my %p = $validator->(@_);
70  
71 68 my $self = bless { map { $_ => delete $p{$_} } @self_p }, $class;
72  
73 68 ▶ if ( $self->{close_after_write} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'close_after_write'}) { } 23 45 68
elsif ($p{'mode'} =~ /^(?:>>|append)$/ or $p{'mode'} =~ /^\d+$/ and $p{'mode'} == O_APPEND()) { } 31 14 45
74 23 $self->{mode} = '>>';
75 }
76 ▶ elsif (
Condition50%3/6
$p{"mode"} =~ /^(?:>>|append)$/ or $p{"mode"} =~ /^\d+$/ and $p{"mode"} == O_APPEND()
l!l&&r!l&&!r
31014
$p{"mode"} =~ /^\d+$/ and $p{"mode"} == O_APPEND()
!ll&&!rl&&r
1400
MC/DC33%1/3
$p{"mode"} =~ /^(?:>>|append)$/ or $p{"mode"} =~ /^\d+$/ and $p{"mode"} == O_APPEND()
$p{"mode"} =~ /^(?:>>|append)$/ $p{"mode"} =~ /^\d+$/ $p{"mode"} == O_APPEND()
Truth table50%2/4
A or B and C
A= $p{"mode"} =~ /^(?:>>|append)$/B= $p{"mode"} =~ /^\d+$/C= $p{"mode"} == O_APPEND()
ABCresult
1XX1
0111
00X0
0100
77 $p{mode} =~ /^(?:>>|append)$/
78 || ( $p{mode} =~ /^\d+$/
79 && $p{mode} == O_APPEND() )
80 ) {
81 31 $self->{mode} = '>>';
82 }
83 else {
84 14 $self->{mode} = '>';
85 }
86 68 delete $p{mode};
87  
88 68 $self->_basic_init(%p);
89 $self->_open_file()
90 68 ▶ unless $self->{close_after_write} || $self->{lazy_open};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"close_after_write"} or $self->{"lazy_open"} 44 24 68
Condition100%3/3
$self->{"close_after_write"} or $self->{"lazy_open"}
l!l&&r!l&&!r
23144
MC/DC100%2/2
$self->{"close_after_write"} or $self->{"lazy_open"}
$self->{"close_after_write"} $self->{"lazy_open"}
Truth table100%3/3
A or B
A= $self->{"close_after_write"}B= $self->{"lazy_open"}
ABresult
1X1
011
000
91  
92 68 return $self;
93 }
94 }
95  
96 27 ▶sub _open_file {
Subroutine _open_file: called CC=9 SCAR=22.4
97 78 my $self = shift;
98  
99 ## no critic (InputOutput::RequireBriefOpen)
100 open my $fh, $self->{mode}, $self->{filename}
101 78 ▶ or die "Cannot write to '$self->{filename}': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $self->{"mode"}, $self->{"filename"} or die qq[Cannot write to '$self->{"filename"}': $!] 78 0 78
102  
103 78 ▶ if ( $self->{autoflush} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autoflush"}) 78 0 78
104 78 $fh->autoflush(1);
105 }
106  
107 78 ▶ if ( $self->{permissions}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"permissions"} and not $self->{"chmodded"}) 2 76 78
Condition66%2/3
$self->{"permissions"} and not $self->{"chmodded"}
!ll&&!rl&&r
7602
MC/DC50%1/2
$self->{"permissions"} and not $self->{"chmodded"}
$self->{"permissions"} not $self->{"chmodded"}
Truth table66%2/3
A and not(B)
A= $self->{"permissions"}B= not $self->{"chmodded"}
ABresult
0X0
100
111
108 && !$self->{chmodded} ) {
109 ## no critic (ValuesAndExpressions::ProhibitLeadingZeros)
110 2 my $current_mode = ( stat $self->{filename} )[2] & 07777;
111 2 ▶ if ( $current_mode ne $self->{permissions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_mode ne $self->{"permissions"}) 1 1 2
112 chmod $self->{permissions}, $self->{filename}
113 or die sprintf(
114 'Cannot chmod %s to %04o: %s',
115 1 ▶ $self->{filename}, $self->{permissions} & 07777, $!
Branch: 1 branch
BranchTrueFalseTotal
chmod $self->{"permissions"}, $self->{"filename"} or die sprintf("Cannot chmod %s to %04o: %s", $self->{"filename"}, $self->{"permissions"} & 4095, $!) 1 0 1
116 );
117 }
118  
119 2 $self->{chmodded} = 1;
120 }
121  
122 78 ▶ if ( $self->{binmode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"binmode"}) 2 76 78
123 binmode $fh, $self->{binmode}
124 2 ▶ or die "Cannot set binmode on filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $self->{"binmode"} or die "Cannot set binmode on filehandle: $!" 2 0 2
125 }
126  
127 78 $self->{fh} = $fh;
128 }
129  
130 28 ▶sub log_message {
Subroutine log_message: called CC=8 SCAR=20.9
Pod undocumented
131 28 my $self = shift;
132 28 my %p = @_;
133  
134 28 ▶ if ( $self->{close_after_write} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'close_after_write'}) { } 3 25 28
elsif ($self->{'lazy_open'}) { } 1 24 25
135 3 $self->_open_file;
136 }
137 elsif ( $self->{lazy_open} ) {
138 1 $self->_open_file;
139 1 $self->{lazy_open} = 0;
140 }
141  
142 28 my $fh = $self->{fh};
143  
144 28 ▶ if ( $self->{syswrite} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'syswrite'}) { } 2 26 28
145 defined syswrite( $fh, $p{message} )
146 2 ▶ or die "Cannot write to '$self->{filename}': $!";
Branch: 1 branch
BranchTrueFalseTotal
defined syswrite($fh, $p{"message"}) or die qq[Cannot write to '$self->{"filename"}': $!] 2 0 2
147 }
148 else {
149 print $fh $p{message}
150 26 ▶ or die "Cannot write to '$self->{filename}': $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $p{"message"} or die qq[Cannot write to '$self->{"filename"}': $!] 26 0 26
151 }
152  
153 28 ▶ if ( $self->{close_after_write} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"close_after_write"}) 3 25 28
154 3 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die qq[Cannot close '$self->{"filename"}': $!] 3 0 3
155 or die "Cannot close '$self->{filename}': $!";
156 3 delete $self->{fh};
157 }
158 }
159  
160 68 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
161 68 my $self = shift;
162  
163 68 ▶ if ( $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"}) 45 23 68
164 45 my $fh = $self->{fh};
165 ## no critic (InputOutput::RequireCheckedSyscalls)
166 45 ▶ close $fh if openhandle($fh);
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle($fh) 45 0 45
167 }
168 }
169  
170 27 1;
171  
172 # ABSTRACT: Object for logging to files
173  
174 __END__
« Dispatch/File/Locked.pm ↑ Summary Dispatch/Output.pm »