blib/lib/Log/Dispatch/File/Alerts.pm

filter: Statementstmt 67.8% 59 / 87 Branchbran 42.8% 6 / 14 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 78.5% 11 / 14 Podpod 100% 2 / 2 totaltotal 60.3% 79 / 131 CC 11 SCAR 24.9
CC
11
Cov
60%
log_message
42.0
new
4.0
_current_time
2.3
CRAP 12.0
↑ Summary
1 ## no critic
2 package Log::Dispatch::File::Alerts;
3
4 7 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 7 ▶use Log::Dispatch::File '2.37';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Log::Log4perl::DateFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Fcntl ':flock'; # import LOCK_* constants
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 7 our @ISA = qw(Log::Dispatch::File);
13
14 7 our $VERSION = '1.04';
15
16 7 our $TIME_HIRES_AVAILABLE = undef;
17
18 7 ▶BEGIN { # borrowed from Log::Log4perl::Layout::PatternLayout, Thanks!
Subroutine BEGIN: called CC=2 SCAR=7.4
19 # Check if we've got Time::HiRes. If not, don't make a big fuss,
20 # just set a flag so we know later on that we can't have fine-grained
21 # time stamps
22
23 7 eval { require Time::HiRes; };
24 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 7 7
25 0 $TIME_HIRES_AVAILABLE = 0;
26 } else {
27 7 $TIME_HIRES_AVAILABLE = 1;
28 }
29 }
30
31 # Preloaded methods go here.
32
33 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
34 4 my $proto = shift;
35 4 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
36
37 4 my %p = @_;
38
39 4 my $self = bless {}, $class;
40
41 # only append mode is supported
42 4 $p{mode} = 'append';
43 # 'close' mode is always used
44 4 $p{close_after_write} = 1;
45
46 # base class initialization
47 4 $self->_basic_init(%p);
48
49 # split pathname into path, basename, extension
50 4 ▶ if ($p{filename} =~ /^(.*)\%d\{([^\}]*)\}(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p{'filename'} =~ /^(.*)\%d\{([^\}]*)\}(.*)$/) { } 2 2 4
elsif ($p{'filename'} =~ /^(.*)(\.[^\.]+)$/) { } 1 1 2
51 2 $self->{rolling_filename_prefix} = $1;
52 2 $self->{rolling_filename_postfix} = $3;
53 2 $self->{rolling_filename_format} = Log::Log4perl::DateFormat->new($2);
54 2 $self->{filename} = $self->_createFilename(0);
55 } elsif ($p{filename} =~ /^(.*)(\.[^\.]+)$/) {
56 1 $self->{rolling_filename_prefix} = $1;
57 1 $self->{rolling_filename_postfix} = $2;
58 1 $self->{rolling_filename_format} = Log::Log4perl::DateFormat->new('-yyyy-MM-dd-$!');
59 1 $self->{filename} = $self->_createFilename(0);
60 } else {
61 1 $self->{rolling_filename_prefix} = $p{filename};
62 1 $self->{rolling_filename_postfix} = '';
63 1 $self->{rolling_filename_format} = Log::Log4perl::DateFormat->new('.yyyy-MM-dd-$!');
64 1 $self->{filename} = $self->_createFilename(0);
65 }
66
67 4 $self->_make_handle();
68
69 0 return $self;
70 }
71
72 0 ▶sub log_message { # parts borrowed from Log::Dispatch::FileRotate, Thanks!
Subroutine log_message: not called CC=6 SCAR=37.4
Pod documented
73 0 my $self = shift;
74 0 my %p = @_;
75 0 my $try = 1;
76 0 my $firstfilename = $self->_createFilename(0); # if this is generated, we are done
77
78 0 ▶ while (defined $try) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $try) 0 0 0
79 0 $self->{filename} = $self->_createFilename($try);
80
81 0 ▶ if (($try > 1 and $firstfilename eq $self->{filename}) or $try < 1) { # later checks for integer overflow
Branch: 1 branch
BranchTrueFalseTotal
if ($try > 1 and $firstfilename eq $self->{"filename"} or $try < 1) 0 0 0
Condition0%0/6
$try > 1 and $firstfilename eq $self->{"filename"} or $try < 1
l!l&&r!l&&!r
000
$try > 1 and $firstfilename eq $self->{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/3
$try > 1 and $firstfilename eq $self->{"filename"} or $try < 1
$try > 1 $firstfilename eq $self->{"filename"} $try < 1
Truth table0%0/5
A and B or C
A= $try > 1B= $firstfilename eq $self->{"filename"}C= $try < 1
ABCresult
11X1
0X11
1011
0X00
1000
82 die 'could not find an unused file for filename "'
83 . $self->{filename}
84 0 . '". Did you use "!"?';
85 }
86
87 0 $self->_open_file;
88 0 $self->_lock();
89 0 my $fh = $self->{fh};
90 0 ▶ if (not -s $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -s $fh) { } 0 0 0
91 # if the file is zero-sized, it is fresh.
92 # else someone else already used it.
93 0 print $fh $p{message};
94 0 $try = undef;
95 } else {
96 0 $try++;
97 }
98 0 $self->_unlock();
99 0 close($fh);
100 0 $self->{fh} = undef;
101 }
102 }
103
104 0 ▶sub _lock { # borrowed from Log::Dispatch::FileRotate, Thanks!
Subroutine _lock: not called CC=1 SCAR=6.9
105 0 my $self = shift;
106 0 flock($self->{fh},LOCK_EX);
107 # Make sure we are at the EOF
108 0 seek($self->{fh}, 0, 2);
109 0 return 1;
110 }
111
112 0 ▶sub _unlock { # borrowed from Log::Dispatch::FileRotate, Thanks!
Subroutine _unlock: not called CC=1 SCAR=6.9
113 0 my $self = shift;
114 0 flock($self->{fh},LOCK_UN);
115 0 return 1;
116 }
117
118 4 ▶sub _current_time { # borrowed from Log::Log4perl::Layout::PatternLayout, Thanks!
Subroutine _current_time: called CC=2 SCAR=8.1
119 # Return secs and optionally msecs if we have Time::HiRes
120 4 ▶ if($TIME_HIRES_AVAILABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Dispatch::File::Alerts::TIME_HIRES_AVAILABLE) { } 0 4 4
121 0 return (Time::HiRes::gettimeofday());
122 } else {
123 4 return (time(), 0);
124 }
125 }
126
127 4 ▶sub _createFilename {
Subroutine _createFilename: called CC=1 SCAR=0.0
128 4 my $self = shift;
129 4 my $try = shift;
130 return $self->{rolling_filename_prefix}
131 . $self->_format($try)
132 4 . $self->{rolling_filename_postfix};
133 }
134
135 4 ▶sub _format {
Subroutine _format: called CC=1 SCAR=0.0
136 4 my $self = shift;
137 4 my $try = shift;
138 4 my $result = $self->{rolling_filename_format}->format($self->_current_time());
139 4 $result =~ s/(\$+)/sprintf('%0'.length($1).'.'.length($1).'u', $$)/eg;
140 4 $result =~ s/(\!+)/sprintf('%0'.length($1).'.'.length($1).'u', substr($try, -length($1)))/eg;
141 4 return $result;
142 }
143
144 7 1;
145 __END__
↑ Summary