blib/lib/Dancer/Logger/File.pm

filter: Statementstmt 85.9% 55 / 64 Branchbran 60.7% 17 / 28 Conditioncond 47.8% 11 / 23 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 69.5% 98 / 141 SCAR 35.4
CC
23
Cov
72%
logdir
30.2
_log
9.1
init
5.1
CRAP 34.4
« blib/lib/Dancer/Error.pm ↑ Summary blib/lib/Dancer/Handler.pm »
1 package Dancer::Logger::File;
2 13 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: file-based logging engine for Dancer
4 13 $Dancer::Logger::File::VERSION = '1.3520';
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use base 'Dancer::Logger::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Dancer::FileUtils qw(open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Fcntl qw(:flock SEEK_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub logdir {
16 19 ▶ if ( my $altpath = setting('log_path') ) {
Subroutine logdir: called CC=13 SCAR=34.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (my $altpath = setting("log_path")) 3 16 19
17 3 return $altpath;
18 }
19  
20 16 my $logroot = setting('appdir');
21  
22 16 ▶ if ( $logroot and ! -d $logroot and ! mkdir $logroot ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logroot and not -d $logroot and not mkdir($logroot)) 0 16 16
Condition50%3/6
$logroot and not -d $logroot and not mkdir($logroot)
!ll&&!rl&&r
1600
$logroot and not -d $logroot
!ll&&!rl&&r
1150
MC/DC0%0/3
$logroot and not -d $logroot and not mkdir($logroot)
$logroot not -d $logroot not mkdir($logroot)
Truth table50%2/4
A and not(B) and not(C)
A= $logrootB= not -d $logrootC= not mkdir($logroot)
ABCresult
0XX0
10X0
1100
1111
23 0 carp "app directory '$logroot' doesn't exist, am unable to create it";
24 0 return;
25 }
26  
27 16 ▶ my $expected_path = $logroot ?
Branch: 1 branch
BranchTrueFalseTotal
$logroot ? : 15 1 16
28 Dancer::FileUtils::path($logroot, 'logs') :
29 Dancer::FileUtils::path('logs');
30  
31 16 ▶ return $expected_path if -d $expected_path && -x _ && -w _;
Branch: 1 branch
BranchTrueFalseTotal
if -d $expected_path and -x _ and -w _ 7 9 16
Condition66%4/6
-d $expected_path and -x _ and -w _
!ll&&!rl&&r
907
-d $expected_path and -x _
!ll&&!rl&&r
907
MC/DC33%1/3
-d $expected_path and -x _ and -w _
-d $expected_path -x _ -w _
Truth table50%2/4
A and B and C
A= -d $expected_pathB= -x _C= -w _
ABCresult
0XX0
10X0
1100
1111
32  
33 9 ▶ unless (-w $logroot and -x _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $logroot and -x _) 0 9 9
Condition33%1/3
-w $logroot and -x _
!ll&&!rl&&r
009
MC/DC0%0/2
-w $logroot and -x _
-w $logroot -x _
Truth table33%1/3
A and B
A= -w $logrootB= -x _
ABresult
0X0
100
111
34 0 my $perm = (stat $logroot)[2] & 07777;
35 0 chmod($perm | 0700, $logroot);
36 0 ▶ unless (-w $logroot and -x _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $logroot and -x _) 0 0 0
Condition0%0/3
-w $logroot and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-w $logroot and -x _
-w $logroot -x _
Truth table0%0/3
A and B
A= -w $logrootB= -x _
ABresult
0X0
100
111
37 0 carp "app directory '$logroot' isn't writable/executable and can't chmod it";
38 0 return;
39 }
40 }
41 9 return $expected_path;
42 }
43  
44 sub init {
45 18 ▶ my $self = shift;
Subroutine init: called CC=5 SCAR=16.4
Pod documented
46 18 $self->SUPER::init(@_);
47  
48 18 my $logdir = logdir();
49 18 ▶ return unless ($logdir);
Branch: 1 branch
BranchTrueFalseTotal
unless $logdir 0 18 18
50  
51 18 ▶ my $logfile = setting('log_file') || setting('environment').".log";
Condition100%2/2
setting('log_file') || setting('environment') . '.log'
l!l
117
MC/DC100%1/1
setting('log_file') || setting('environment') . '.log'
setting('log_file')
Truth table100%2/2
A
A= setting('log_file')
Aresult
11
00
52  
53 18 ▶ mkdir($logdir) unless(-d $logdir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $logdir 9 9 18
54 18 $logfile = File::Spec->catfile($logdir, $logfile);
55  
56 18 my $fh;
57 18 ▶ unless($fh = open_file('>>', $logfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh = open_file(">>", $logfile)) 0 18 18
58 0 carp "unable to create or append to $logfile";
59 0 return;
60 }
61  
62 # looks like older perls don't auto-convert to IO::File
63 # and can't autoflush
64 # see https://github.com/PerlDancer/Dancer/issues/954
65 18 eval { $fh->autoflush };
66  
67 18 $self->{logfile} = $logfile;
68 18 $self->{fh} = $fh;
69 }
70  
71 sub _log {
72 20 ▶ my ($self, $level, $message) = @_;
Subroutine _log: called CC=7 SCAR=22.1
73 20 my $fh = $self->{fh};
74  
75 20 ▶ return unless(ref $fh && $fh->opened);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh and $fh->opened 0 20 20
Condition33%1/3
ref $fh and $fh->opened
!ll&&!rl&&r
0020
MC/DC0%0/2
ref $fh and $fh->opened
ref $fh $fh->opened
Truth table33%1/3
A and B
A= ref $fhB= $fh->opened
ABresult
0X0
100
111
76  
77 20 ▶ flock($fh, LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or carp(qq[locking logfile $self->{"logfile"} failed]) 0 20 20
78 or carp "locking logfile $self->{logfile} failed";
79 20 ▶ seek($fh, 0, SEEK_END)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 2 or carp(qq[seeking to logfile $self->{"logfile"} end failed]) 0 20 20
80 or carp "seeking to logfile $self->{logfile} end failed";
81 20 ▶ $fh->print($self->format_message($level => $message))
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($self->format_message($level, $message)) or carp(qq[writing to logfile $self->{"logfile"} failed]) 0 20 20
82 or carp "writing to logfile $self->{logfile} failed";
83 20 ▶ flock($fh, LOCK_UN)
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 8 or carp(qq[unlocking logfile $self->{"logfile"} failed]) 0 20 20
84 or carp "unlocking logfile $self->{logfile} failed";
85  
86 }
87  
88 13 1;
89  
90 __END__
« blib/lib/Dancer/Error.pm ↑ Summary blib/lib/Dancer/Handler.pm »