blib/lib/Bot/Cobalt/Logger/Output/File.pm

filter: Statementstmt 89.1% 74 / 83 Branchbran 56.8% 25 / 44 Conditioncond 53.3% 8 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 15 / 15 Podpod 75.0% 3 / 4 totaltotal 75.7% 128 / 169 CC 29 SCAR 18.1
CC
29
Cov
75%
_write
12.0
new
10.7
_do_reopen
5.2
CRAP 6.1
« blib/lib/Bot/Cobalt/Core/Loader.pm ↑ Summary blib/lib/Bot/Cobalt/Timer.pm »
1 package Bot::Cobalt::Logger::Output::File;
2 2 $Bot::Cobalt::Logger::Output::File::VERSION = '0.021003';
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Fcntl qw/:DEFAULT :flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ... after which we fall back to warning on stderr with message included:
12 sub FLOCK_TIMEOUT () { 0.5 }
13  
14  
15 sub PATH () { 0 }
16 sub HANDLE () { 1 }
17 sub MODE () { 2 }
18 sub PERMS () { 3 }
19 sub INODE () { 4 }
20 sub RUNNING_IN_HELL () { 5 }
21  
22 3 ▶sub new {
Subroutine new: called CC=9 SCAR=23.7
Pod undocumented
23 3 my $class = shift;
24  
25 3 my $self = [
26 '', ## PATH
27 undef, ## HANDLE
28 undef, ## MODE
29 undef, ## PERMS
30 undef, ## INODE
31 0, ## RUNNING_IN_HELL
32 ];
33  
34 3 bless $self, $class;
35
36 3 my %args = @_;
37 3 $args{lc $_} = delete $args{$_} for keys %args;
38  
39 confess "new() requires a 'file' argument"
40 3 ▶ unless defined $args{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file"} 1 2 3
41  
42 2 $self->file( $args{file} );
43  
44 $self->mode( $args{mode} )
45 2 ▶ if defined $args{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"mode"} 0 2 2
46  
47 $self->perms( $args{perms} )
48 2 ▶ if defined $args{perms};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"perms"} 0 2 2
49  
50 2 ▶ if ($^O eq 'MSWin32' or $^O eq 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "VMS") 0 2 2
Condition33%1/3
$^O eq "MSWin32" or $^O eq "VMS"
l!l&&r!l&&!r
002
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "VMS"
$^O eq "MSWin32" $^O eq "VMS"
Truth table33%1/3
A or B
A= $^O eq "MSWin32"B= $^O eq "VMS"
ABresult
1X1
011
000
51 0 $self->[RUNNING_IN_HELL] = 1
52 }
53  
54 ## Try to open/create file when object is constructed
55 2 ▶ $self->_open or croak "Could not open specified file ".$args{file};
Branch: 1 branch
BranchTrueFalseTotal
$self->_open or croak('Could not open specified file ' . $args{"file"}) 2 0 2
56 2 ▶ $self->_close if $self->[RUNNING_IN_HELL];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 0 2 2
57  
58 2 $self
59 }
60  
61 18 ▶sub file {
Subroutine file: called CC=4 SCAR=14.0
Pod documented
62 18 my ($self, $file) = @_;
63  
64 18 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 2 16 18
65 2 ▶ $self->_close if $self->_is_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_open 0 2 2
66
67 # stringify a Path::Tiny ->
68 2 $self->[PATH] = $file . '';
69
70 2 ▶ $self->_open unless $self->[RUNNING_IN_HELL];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[5] 2 0 2
71 }
72  
73 18 $self->[PATH]
74 }
75  
76 5 ▶sub mode {
Subroutine mode: called CC=3 SCAR=11.1
Pod documented
77 5 my ($self, $mode) = @_;
78
79 5 ▶ return $self->[MODE] = $mode if defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 5 5
80
81 5 ▶ $self->[MODE] //= O_WRONLY | O_APPEND | O_CREAT
Condition100%2/2
$self->[2] //= 1089
l!l
32
MC/DC100%1/1
$self->[2] //= 1089
$self->[2]
Truth table100%2/2
A
A= $self->[2]
Aresult
11
00
82 }
83  
84 6 ▶sub perms {
Subroutine perms: called CC=3 SCAR=11.1
Pod documented
85 6 my ($self, $perms) = @_;
86
87 6 ▶ return $self->[PERMS] = $perms if defined $perms;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perms 0 6 6
88
89 6 ▶ $self->[PERMS] //= 0666
Condition100%2/2
$self->[3] //= 438
l!l
42
MC/DC100%1/1
$self->[3] //= 438
$self->[3]
Truth table100%2/2
A
A= $self->[3]
Aresult
11
00
90 }
91  
92 5 ▶sub _open {
Subroutine _open: called CC=3 SCAR=11.8
93 5 my ($self) = @_;
94  
95 5 my $fh;
96 5 ▶ unless (sysopen($fh, $self->file, $self->mode, $self->perms) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $self->file, $self->mode, $self->perms) 0 5 5
97 0 warn(
98 "Log file could not be opened: ",
99 join ' ', $self->file, $!
100 );
101 return
102 0 }
103  
104 5 binmode $fh, ':utf8';
105 5 $fh->autoflush;
106  
107 5 ▶ $self->[INODE] = ( stat $self->file )[1]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[5] 5 0 5
108 unless $self->[RUNNING_IN_HELL];
109  
110 5 $self->[HANDLE] = $fh
111 }
112  
113 1 ▶sub _close {
Subroutine _close: called CC=2 SCAR=7.0
114 1 my ($self) = @_;
115
116 1 ▶ return 1 unless $self->_is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_open 0 1 1
117
118 1 close $self->[HANDLE];
119 1 $self->[HANDLE] = undef;
120  
121 1 1
122 }
123  
124 6 ▶sub _is_open {
Subroutine _is_open: called CC=1 SCAR=0.0
125 6 my ($self) = @_;
126 6 $self->[HANDLE]
127 }
128  
129 3 ▶sub _do_reopen {
Subroutine _do_reopen: called CC=5 SCAR=16.6
130 3 my ($self) = @_;
131  
132 ## Are we on a stupid system or dealing with a not-open file?
133 3 ▶ return 1 unless $self->_is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_open 0 3 3
134  
135 3 ▶ unless ( $self->[RUNNING_IN_HELL] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[5]) 3 0 3
136 ## Do the inodes match?
137 3 ▶ return if -e $self->file
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->file and $self->[4] == (stat $self->file)[1] 2 1 3
Condition66%2/3
-e $self->file and $self->[4] == (stat $self->file)[1]
!ll&&!rl&&r
102
MC/DC50%1/2
-e $self->file and $self->[4] == (stat $self->file)[1]
-e $self->file $self->[4] == (stat $self->file)[1]
Truth table66%2/3
A and B
A= -e $self->fileB= $self->[4] == (stat $self->file)[1]
ABresult
0X0
100
111
138 and $self->[INODE] == ( stat $self->file )[1];
139 }
140
141 1
142 1 }
143  
144 3 ▶sub _write {
Subroutine _write: called CC=7 SCAR=24.9
145 3 my ($self, $str) = @_;
146  
147 3 ▶ if ($self->_do_reopen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_reopen) 1 2 3
148 1 $self->_close;
149 1 ▶ $self->_open or warn "_open failure" and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_open or warn "_open failure" and return 1 0 1
Condition20%1/5
$self->_open || warn('_open failure') && (return)
l!l
100
warn "_open failure" and return
!ll
00
MC/DC0%0/2
$self->_open || warn('_open failure') && (return)
$self->_open warn "_open failure"
Truth table33%1/3
A || B
A= $self->_openB= warn "_open failure"
ABresult
1X1
011
000
150 }
151  
152 ## FIXME if flock fails, buffer and try next _write up to X items ?
153 3 my $timer = 0;
154 3 ▶ until ( flock($self->[HANDLE], LOCK_EX | LOCK_NB) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self->[1], 6) 0 3 3
155 0 ▶ if ($timer > FLOCK_TIMEOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer > "0.5") 0 0 0
156 0 warn "flock failure for '@{[$self->file]}' ('$str')";
157 return
158 0 }
159 0 sleep 0.01;
160 0 $timer += 0.01;
161 }
162  
163 3 print { $self->[HANDLE] } $str;
164  
165 3 flock($self->[HANDLE], LOCK_UN);
166
167 3 ▶ $self->_close if $self->[RUNNING_IN_HELL];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 0 3 3
168  
169 3 1
170 }
171  
172  
173 2 1;
174 __END__
« blib/lib/Bot/Cobalt/Core/Loader.pm ↑ Summary blib/lib/Bot/Cobalt/Timer.pm »