| 1 | package Log::File::Rolling; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.006001; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Fcntl ':flock'; # import LOCK_* constants | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | our $VERSION = '0.101'; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | my %p = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # base class initialization | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | #$self->_basic_init(%p); | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | $self->{timezone} = $p{timezone} || 'gmtime'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$p{'timezone'} || 'gmtime'
MC/DC0%0/1
$p{'timezone'} || 'gmtime'
$p{'timezone'}
Truth table50%1/2
A
A= $p{'timezone'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | die "unsupported timezone: '$self->{timezone}' (currently must be 'localtime' or 'gmtime')" | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | if $self->{timezone} ne 'localtime' && $self->{timezone} ne 'gmtime'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"timezone"} ne "localtime" and $self->{"timezone"} ne "gmtime"
MC/DC0%0/2
$self->{"timezone"} ne "localtime" and $self->{"timezone"} ne "gmtime"
$self->{"timezone"} ne "localtime"
$self->{"timezone"} ne "gmtime"
Truth table33%1/3
A and B
A= $self->{"timezone"} ne "localtime"B= $self->{"timezone"} ne "gmtime"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | $self->{filename_format} = $p{filename}; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | if (exists $p{current_symlink}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | $self->{current_symlink} = $p{current_symlink}; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | $self->{rolling_fh_pid} = $$; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | $self->_createFilename(); | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | $self->_rolling_open_file(); | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | sub log { # parts borrowed from Log::Dispatch::FileRotate, Thanks! | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=9 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | my $message = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | if ($self->_createFilename()) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $self->{rolling_fh_pid} = 'x'; # force reopen | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | if (defined $self->{fh} and ($self->{rolling_fh_pid}||'') eq $$ and defined fileno $self->{fh}) { # flock won't work after a fork() | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
defined $self->{"fh"} and ($self->{"rolling_fh_pid"} || "") eq $$ and defined fileno $self->{"fh"}
defined $self->{"fh"} and ($self->{"rolling_fh_pid"} || "") eq $$
$self->{'rolling_fh_pid'} || ''
MC/DC33%1/3
defined $self->{"fh"} and ($self->{"rolling_fh_pid"} || "") eq $$ and defined fileno $self->{"fh"}
defined $self->{"fh"}
$self->{'rolling_fh_pid'}
defined fileno $self->{"fh"}
Truth table50%2/4
A and B and C
A= defined $self->{"fh"}B= $self->{'rolling_fh_pid'}C= defined fileno $self->{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | my $inode = (stat($self->{fh}))[1]; # get real inode | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | my $finode = (stat($self->{filename}))[1]; # Stat the name for comparision | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | if(!defined($finode) || $inode != $finode) { # Oops someone moved things on us. So just reopen our log | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
not defined $finode or $inode != $finode
MC/DC0%0/2
not defined $finode or $inode != $finode
not defined $finode
$inode != $finode
Truth table33%1/3
not(A) or B
A= not defined $finodeB= $inode != $finode
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | $self->_rolling_open_file; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | } elsif (!$self->{current_symlink_inited}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | $self->_update_current_symlink; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | $self->_lock(); | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | print $fh $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | $self->_unlock(); | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | $self->{rolling_fh_pid} = $$; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | $self->_rolling_open_file; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $self->_lock(); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | print $fh $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | $self->_unlock(); | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub _rolling_open_file { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rolling_open_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | open my $fh, '>>:raw', $self->{filename} | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | ▶ | or die "Cannot write to '$self->{filename}': $!"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | $self->{fh} = $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | $self->_update_current_symlink; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | sub _update_current_symlink { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_current_symlink: called CC=6 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | return if !exists $self->{current_symlink}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | my $current_symlink_value = readlink($self->{current_symlink}); | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | if (!defined $current_symlink_value || $current_symlink_value ne $self->{filename}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $current_symlink_value or $current_symlink_value ne $self->{"filename"}
MC/DC0%0/2
not defined $current_symlink_value or $current_symlink_value ne $self->{"filename"}
not defined $current_symlink_value
$current_symlink_value ne $self->{"filename"}
Truth table33%1/3
not(A) or B
A= not defined $current_symlink_valueB= $current_symlink_value ne $self->{"filename"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | my $temp_symlink_file = "$self->{current_symlink}.temp$$"; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | unlink($temp_symlink_file); | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | symlink($self->{filename}, $temp_symlink_file) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | || die "unable to create symlink '$temp_symlink_file': $!"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | if (!rename($temp_symlink_file, $self->{current_symlink})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | unlink($temp_symlink_file); | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | die "unable to overwrite symlink '$self->{current_symlink}': $!"; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | $self->{current_symlink_inited} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | ▶ | sub _lock { # borrowed from Log::Dispatch::FileRotate, Thanks! | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | flock($self->{fh},LOCK_EX); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | # Make sure we are at the EOF | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | seek($self->{fh}, 0, 2); | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | sub _unlock { # borrowed from Log::Dispatch::FileRotate, Thanks! | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unlock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | flock($self->{fh},LOCK_UN); | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | ## Returns true if the filename changed | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | ▶ | sub _createFilename { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_createFilename: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | my $time = time(); | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 7 | ▶ | return 0 if defined $self->{current_filename_time} && $time == $self->{current_filename_time}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"current_filename_time"} and $time == $self->{"current_filename_time"}
MC/DC50%1/2
defined $self->{"current_filename_time"} and $time == $self->{"current_filename_time"}
defined $self->{"current_filename_time"}
$time == $self->{"current_filename_time"}
Truth table66%2/3
A and B
A= defined $self->{"current_filename_time"}B= $time == $self->{"current_filename_time"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | $self->{filename} = Time::Piece->${\$self->{timezone}}->strftime($self->{filename_format}); | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | $self->{current_filename_time} = $time; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||