| 1 | package File::RotateLogs; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use POSIX qw//; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Fcntl qw/:DEFAULT/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Proc::Daemon; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | subtype 'File::RotateLogs::Path' | ||||||||||||||||||||||||||||||||||||||||||
| 15 | => as 'Str' | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | => message { "This argument must be Str or Object that has a stringify method" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | coerce 'File::RotateLogs::Path' | ||||||||||||||||||||||||||||||||||||||||||
| 18 | => from 'Object' => via { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | my $logfile = $_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | if ( my $stringify = overload::Method( $logfile, '""' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | return $stringify->($logfile); | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $logfile; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | no Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | has 'logfile' => ( | |||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => 'File::RotateLogs::Path', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | coerce => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | has 'linkname' => ( | |||||||||||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | isa => 'File::RotateLogs::Path', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | coerce => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | has 'rotationtime' => ( | |||||||||||||||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | default => 86400 | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | has 'maxage' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | default => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | warn "[INFO] File::RotateLogs: 'maxage' was not configured. RotateLogs doesn't remove any log files\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | has 'sleep_before_remove' => ( | |||||||||||||||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | default => 3, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | has 'offset' => ( | |||||||||||||||||||||||||||||||||||||||||
| 64 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 65 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 66 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | sub _gen_filename { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 45 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gen_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 45 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 45 | my $time = $now - (($now + $self->offset) % $self->rotationtime); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 45 | return POSIX::strftime($self->logfile, localtime($time)); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | sub print { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 45 | ▶ | my ($self,$log) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
print: called CC=13 SCAR=27.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 45 | my $fname = $self->_gen_filename; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 45 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 45 | ▶ | if ( $self->{fh} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 30 | ▶ | if ( $fname eq $self->{fname} && $self->{pid} == $$ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fname eq $self->{"fname"} and $self->{"pid"} == $$
MC/DC50%1/2
$fname eq $self->{"fname"} and $self->{"pid"} == $$
$fname eq $self->{"fname"}
$self->{"pid"} == $$
Truth table66%2/3
A and B
A= $fname eq $self->{"fname"}B= $self->{"pid"} == $$
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 16 | $fh = delete $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 14 | $fh = delete $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 14 | ▶ | close $fh if $fh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 14 | undef $fh; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 45 | ▶ | unless ($fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 29 | ▶ | my $is_new = ( ! -f $fname || ( $self->linkname && ! -l $self->linkname ) ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
not -f $fname or $self->linkname and not -l $self->linkname
$self->linkname and not -l $self->linkname
MC/DC0%0/3
not -f $fname or $self->linkname and not -l $self->linkname
not -f $fname
$self->linkname
not -l $self->linkname
Truth table25%1/4
not(A) or B and not(C)
A= not -f $fnameB= $self->linknameC= not -l $self->linkname
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 29 | ▶ | open $fh, '>>:utf8:unix', $fname or die "Cannot open file($fname): $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 29 | ▶ | if ( $is_new ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 29 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 98 | 29 | $self->rotation($fname); | |||||||||||||||||||||||||||||||||||||||||
| 99 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 29 | ▶ | warn "failed rotation or symlink: $@" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 45 | ▶ | $fh->print($log) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | or die "Cannot write to $fname: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 45 | $self->{fh} = $fh; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 45 | $self->{fname} = $fname; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 45 | $self->{pid} = $$; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | sub rotation { | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 29 | ▶ | my ($self, $fname) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rotation: called CC=9 SCAR=22.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 29 | my $lock = $fname .'_lock'; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 29 | ▶ | sysopen(my $lockfh, $lock, O_CREAT|O_EXCL) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 29 | close($lockfh); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 29 | ▶ | if ( $self->linkname ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 29 | my $symlink = $fname .'_symlink'; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 29 | ▶ | symlink($fname, $symlink) or die $!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 29 | ▶ | rename($symlink, $self->linkname) or die $!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 29 | ▶ | if ( ! $self->maxage ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 26 | unlink $lock; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 26 | return; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | my $time = time; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | my @to_unlink = grep { $time - [stat($_)]->[9] > $self->maxage } | |||||||||||||||||||||||||||||||||||||||||
| 131 | glob($self->logfile_pattern); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | if ( ! @to_unlink ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | unlink $lock; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | if ( $self->sleep_before_remove ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | $self->unlink_background(@to_unlink,$lock); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | unlink $_ for @to_unlink; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | unlink $lock; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | sub logfile_pattern { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logfile_pattern: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 3 | my $logfile = $self->logfile; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | $logfile =~ s!%[%+A-Za-z]!*!g; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 3 | $logfile =~ s!\*+!*!g; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | $logfile; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | sub unlink_background { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | ▶ | my ($self, @files) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unlink_background: called CC=3 SCAR=13.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | my $daemon = Proc::Daemon->new(); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | @files = map { File::Spec->rel2abs($_) } @files; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | ▶ | if ( ! $daemon->Init ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $0 = "$0 rotatelogs unlink worker"; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | sleep $self->sleep_before_remove; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | unlink $_ for @files; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||||||||||||||||