blib/lib/File/RotateLogs.pm

filter: Statementstmt 92.3% 96 / 104 Branchbran 63.8% 23 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 4 totaltotal 79.4% 139 / 175 SCAR 33.1
CC
24
Cov
82%
print
15.8
rotation
9.6
unlink_background
3.8
CRAP 27.4
↑ Summary Build »
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
BranchTrueFalseTotal
if (my $stringify = overload::Method($logfile, "\"\"")) 2 0 2
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
BranchTrueFalseTotal
if ($self->{"fh"}) 30 15 45
83 30 ▶ if ( $fname eq $self->{fname} && $self->{pid} == $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq $self->{'fname'} and $self->{'pid'} == $$) { } 16 14 30
Condition66%2/3
$fname eq $self->{"fname"} and $self->{"pid"} == $$
!ll&&!rl&&r
14016
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"} == $$
ABresult
0X0
100
111
84 16 $fh = delete $self->{fh};
85 }
86 else {
87 14 $fh = delete $self->{fh};
88 14 ▶ close $fh if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 14 0 14
89 14 undef $fh;
90 }
91 }
92  
93 45 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 29 16 45
94 29 ▶ my $is_new = ( ! -f $fname || ( $self->linkname && ! -l $self->linkname ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!(-f $fname) || $self->linkname && !(-l $self->linkname) ? : 29 0 29
Condition16%1/6
not -f $fname or $self->linkname and not -l $self->linkname
l!l&&r!l&&!r
2900
$self->linkname and not -l $self->linkname
!ll&&!rl&&r
000
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
ABCresult
1XX1
0111
00X0
0100
95 29 ▶ open $fh, '>>:utf8:unix', $fname or die "Cannot open file($fname): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>:utf8:unix", $fname or die "Cannot open file($fname): $!" 0 29 29
96 29 ▶ if ( $is_new ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new) 29 0 29
97 29 eval {
98 29 $self->rotation($fname);
99 };
100 29 ▶ warn "failed rotation or symlink: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 29 29
101 }
102 }
103  
104 45 ▶ $fh->print($log)
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($log) or die "Cannot write to ${fname}: $!" 0 45 45
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
BranchTrueFalseTotal
sysopen my $lockfh, $lock, 192 or return 0 29 29
117 29 close($lockfh);
118 29 ▶ if ( $self->linkname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->linkname) 29 0 29
119 29 my $symlink = $fname .'_symlink';
120 29 ▶ symlink($fname, $symlink) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
symlink $fname, $symlink or die $! 0 29 29
121 29 ▶ rename($symlink, $self->linkname) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
rename $symlink, $self->linkname or die $! 0 29 29
122 }
123  
124 29 ▶ if ( ! $self->maxage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->maxage) 26 3 29
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
BranchTrueFalseTotal
unless (@to_unlink) 2 1 3
133 2 unlink $lock;
134 2 return;
135 }
136  
137 1 ▶ if ( $self->sleep_before_remove ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sleep_before_remove) { } 1 0 1
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
BranchTrueFalseTotal
if (not $daemon->Init) 0 1 1
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__
↑ Summary Build »