| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 74 | 23 | $self->{mode} = '>>'; | |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||
| 76 | ▶ | elsif ( | |||||||||||||||||||||||||||||||||
|
Condition50%3/6
$p{"mode"} =~ /^(?:>>|append)$/ or $p{"mode"} =~ /^\d+$/ and $p{"mode"} == O_APPEND()
$p{"mode"} =~ /^\d+$/ and $p{"mode"} == O_APPEND()
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()
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->{"close_after_write"} or $self->{"lazy_open"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 78 | ▶ | if ( $self->{autoflush} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 78 | $fh->autoflush(1); | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | 78 | ▶ | if ( $self->{permissions} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"permissions"} and not $self->{"chmodded"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 2 | $self->{chmodded} = 1; | |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | 78 | ▶ | if ( $self->{binmode} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 123 | binmode $fh, $self->{binmode} | ||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | or die "Cannot set binmode on filehandle: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 145 | defined syswrite( $fh, $p{message} ) | ||||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | or die "Cannot write to '$self->{filename}': $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||||||||
| 149 | print $fh $p{message} | ||||||||||||||||||||||||||||||||||
| 150 | 26 | ▶ | or die "Cannot write to '$self->{filename}': $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | 28 | ▶ | if ( $self->{close_after_write} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 154 | 3 | ▶ | close $fh | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 164 | 45 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 165 | ## no critic (InputOutput::RequireCheckedSyscalls) | ||||||||||||||||||||||||||||||||||
| 166 | 45 | ▶ | close $fh if openhandle($fh); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | 27 | 1; | |||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | # ABSTRACT: Object for logging to files | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||||||||