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

filter: Statementstmt 89.1% 74 / 83 Branchbran 56.8% 25 / 44 Conditioncond 57.1% 8 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 15 / 15 Podpod 75.0% 3 / 4 totaltotal 76.1% 128 / 168 SCAR 37.1
CC
29
Cov
76%
_write
11.5
new
10.7
_do_reopen
5.2
CRAP 40.8
« blib/lib/Bot/Cobalt/Frontend/Utils.pm ↑ Summary blib/lib/Bot/Cobalt/Core/Loader.pm »
1 package Bot::Cobalt::Logger::Output::File;
2 1 $Bot::Cobalt::Logger::Output::File::VERSION = '0.021002';
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶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 sub new {
23 2 ▶ my $class = shift;
Subroutine new: called CC=9 SCAR=23.7
Pod undocumented
24  
25 2 my $self = [
26 '', ## PATH
27 undef, ## HANDLE
28 undef, ## MODE
29 undef, ## PERMS
30 undef, ## INODE
31 0, ## RUNNING_IN_HELL
32 ];
33  
34 2 bless $self, $class;
35
36 2 my %args = @_;
37 2 $args{lc $_} = delete $args{$_} for keys %args;
38  
39 confess "new() requires a 'file' argument"
40 2 ▶ unless defined $args{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file"} 1 1 2
41  
42 1 $self->file( $args{file} );
43  
44 $self->mode( $args{mode} )
45 1 ▶ if defined $args{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"mode"} 0 1 1
46  
47 $self->perms( $args{perms} )
48 1 ▶ if defined $args{perms};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"perms"} 0 1 1
49  
50 1 ▶ if ($^O eq 'MSWin32' or $^O eq 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "VMS") 0 1 1
Condition33%1/3
$^O eq "MSWin32" or $^O eq "VMS"
l!l&&r!l&&!r
001
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 1 ▶ $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"}) 0 1 1
56 1 ▶ $self->_close if $self->[RUNNING_IN_HELL];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 0 1 1
57  
58 1 $self
59 }
60  
61 sub file {
62 11 ▶ my ($self, $file) = @_;
Subroutine file: called CC=4 SCAR=14.0
Pod documented
63  
64 11 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 1 10 11
65 1 ▶ $self->_close if $self->_is_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_open 0 1 1
66
67 # stringify a Path::Tiny ->
68 1 $self->[PATH] = $file . '';
69
70 1 ▶ $self->_open unless $self->[RUNNING_IN_HELL];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[5] 1 0 1
71 }
72  
73 11 $self->[PATH]
74 }
75  
76 sub mode {
77 3 ▶ my ($self, $mode) = @_;
Subroutine mode: called CC=3 SCAR=11.1
Pod documented
78
79 3 ▶ return $self->[MODE] = $mode if defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 3 3
80
81 3 ▶ $self->[MODE] //= O_WRONLY | O_APPEND | O_CREAT
Condition100%2/2
$self->[2] //= 1089
l!l
21
MC/DC100%1/1
$self->[2] //= 1089
$self->[2]
Truth table100%2/2
A
A= $self->[2]
Aresult
11
00
82 }
83  
84 sub perms {
85 4 ▶ my ($self, $perms) = @_;
Subroutine perms: called CC=3 SCAR=11.1
Pod documented
86
87 4 ▶ return $self->[PERMS] = $perms if defined $perms;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perms 0 4 4
88
89 4 ▶ $self->[PERMS] //= 0666
Condition100%2/2
$self->[3] //= 438
l!l
31
MC/DC100%1/1
$self->[3] //= 438
$self->[3]
Truth table100%2/2
A
A= $self->[3]
Aresult
11
00
90 }
91  
92 sub _open {
93 3 ▶ my ($self) = @_;
Subroutine _open: called CC=3 SCAR=11.8
94  
95 3 my $fh;
96 3 ▶ unless (sysopen($fh, $self->file, $self->mode, $self->perms) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $self->file, $self->mode, $self->perms) 0 3 3
97 0 warn(
98 "Log file could not be opened: ",
99 join ' ', $self->file, $!
100 );
101 return
102 0 }
103  
104 3 binmode $fh, ':utf8';
105 3 $fh->autoflush;
106  
107 3 ▶ $self->[INODE] = ( stat $self->file )[1]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[5] 3 0 3
108 unless $self->[RUNNING_IN_HELL];
109  
110 3 $self->[HANDLE] = $fh
111 }
112  
113 sub _close {
114 1 ▶ my ($self) = @_;
Subroutine _close: called CC=2 SCAR=7.0
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 sub _is_open {
125 4 ▶ my ($self) = @_;
Subroutine _is_open: called CC=1 SCAR=0.0
126 4 $self->[HANDLE]
127 }
128  
129 sub _do_reopen {
130 2 ▶ my ($self) = @_;
Subroutine _do_reopen: called CC=5 SCAR=16.6
131  
132 ## Are we on a stupid system or dealing with a not-open file?
133 2 ▶ return 1 unless $self->_is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_open 0 2 2
134  
135 2 ▶ unless ( $self->[RUNNING_IN_HELL] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[5]) 2 0 2
136 ## Do the inodes match?
137 2 ▶ return if -e $self->file
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->file and $self->[4] == (stat $self->file)[1] 1 1 2
Condition66%2/3
-e $self->file and $self->[4] == (stat $self->file)[1]
!ll&&!rl&&r
101
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 sub _write {
145 2 ▶ my ($self, $str) = @_;
Subroutine _write: called CC=7 SCAR=24.4
146  
147 2 ▶ if ($self->_do_reopen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_reopen) 1 1 2
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 0 1 1
Condition25%1/4
$self->_open || warn('_open failure') && (return)
l!l
10
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 2 my $timer = 0;
154 2 ▶ until ( flock($self->[HANDLE], LOCK_EX | LOCK_NB) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self->[1], 6) 0 2 2
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 2 print { $self->[HANDLE] } $str;
164  
165 2 flock($self->[HANDLE], LOCK_UN);
166
167 2 ▶ $self->_close if $self->[RUNNING_IN_HELL];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 0 2 2
168  
169 2 1
170 }
171  
172  
173 1 1;
174 __END__
« blib/lib/Bot/Cobalt/Frontend/Utils.pm ↑ Summary blib/lib/Bot/Cobalt/Core/Loader.pm »