blib/lib/Dancer/Logger/Hourlyfile.pm

filter: Statementstmt 42.0% 29 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 9 / 12 Podpod 100% 1 / 1 totaltotal 34.2% 39 / 114 CC 13 SCAR 36.4
CC
13
Cov
31%
_setfh
90.0
init
20.0
_log
6.0
CRAP 38.0
↑ Summary
1 package Dancer::Logger::Hourlyfile;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base 'Dancer::Logger::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Dancer::FileUtils qw(open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.06';
15  
16 sub init {
17 0 ▶ my $self = shift;
Subroutine init: not called CC=4 SCAR=30.0
Pod documented
18 0 $self->SUPER::init(@_);
19  
20 # Grab Settings
21 0 $self->{log_path} = setting('log_path') . '/';
22 0 ▶ $self->{log_fname} = setting('log_file') . '.log' || setting('environment') . '.log';
Condition0%0/2
setting('log_file') . '.log' || setting('environment') . '.log'
l!l
00
MC/DC0%0/1
setting('log_file') . '.log' || setting('environment') . '.log'
setting('log_file') . '.log'
Truth table0%0/2
A
A= setting('log_file') . '.log'
Aresult
11
00
23 0 $self->{log_file} = $self->{log_fname};
24 0 ▶ $self->{log_hourly} = setting('log_hourly') || "filename";
Condition0%0/2
setting('log_hourly') || 'filename'
l!l
00
MC/DC0%0/1
setting('log_hourly') || 'filename'
setting('log_hourly')
Truth table0%0/2
A
A= setting('log_hourly')
Aresult
11
00
25  
26 0 my $log_time = setting('log_time');
27 0 ▶ if ( $log_time eq 'gmtime' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_time eq 'gmtime') { } 0 0 0
28 0 $self->{log_time} = 'gmtime';
29 } else {
30 0 $self->{log_time} = 'localtime';
31 }
32  
33 }
34  
35  
36 sub _log {
37 0 ▶ my ( $self, $level, $message ) = @_;
Subroutine _log: not called CC=2 SCAR=17.9
38  
39 # Check or set fh
40 0 $self->_setfh;
41  
42 0 my $fh = $self->{fh};
43  
44 0 ▶ $fh->print( $self->format_message( $level => $message ) )
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($self->format_message($level, $message)) or carp(qq[writing logs to file $self->{"logfile"} failed: $!]) 0 0 0
45 or carp "writing logs to file $self->{logfile} failed: $!";
46 }
47  
48  
49 sub _setfh {
50 0 ▶ my $self = shift;
Subroutine _setfh: not called CC=9 SCAR=45.0
51
52 0 my $test_fh = $self->{fh};
53 0 my $current_time = undef;
54 0 my $path_time = undef;
55 0 my $file_time = undef;
56  
57  
58 0 ▶ if ( $self->{log_time} eq 'gmt' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_time'} eq 'gmt') { } 0 0 0
59 0 $current_time = POSIX::strftime( "%Y-%m-%d-%H", gmtime ) ;
60 0 $path_time = POSIX::strftime( "%Y/%m/%d/", gmtime ) ;
61 0 $file_time = POSIX::strftime( "%Y%m%d%H_", gmtime ) ;
62 } else {
63 0 $current_time = POSIX::strftime( "%Y-%m-%d-%H", localtime ) ;
64 0 $path_time = POSIX::strftime( "%Y/%m/%d/", localtime ) ;
65 0 $file_time = POSIX::strftime( "%Y%m%d%H_", localtime ) ;
66 }
67  
68 # Return if fh is good
69 0 ▶ if ( ref $test_fh && $test_fh->opened ) { # fh exists and is open
Branch: 1 branch
BranchTrueFalseTotal
if (ref $test_fh and $test_fh->opened) 0 0 0
Condition0%0/3
ref $test_fh and $test_fh->opened
!ll&&!rl&&r
000
MC/DC0%0/2
ref $test_fh and $test_fh->opened
ref $test_fh $test_fh->opened
Truth table0%0/3
A and B
A= ref $test_fhB= $test_fh->opened
ABresult
0X0
100
111
70 0 return ;
71 }
72  
73 # Date hasn't changed
74 0 ▶ if ( exists $self->{log_current} && $self->{log_current} eq $current_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"log_current"} and $self->{"log_current"} eq $current_time) 0 0 0
Condition0%0/3
exists $self->{"log_current"} and $self->{"log_current"} eq $current_time
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"log_current"} and $self->{"log_current"} eq $current_time
exists $self->{"log_current"} $self->{"log_current"} eq $current_time
Truth table0%0/3
A and B
A= exists $self->{"log_current"}B= $self->{"log_current"} eq $current_time
ABresult
0X0
100
111
75 0 return ;
76 }
77  
78 # if here, then fh is no good. Clean it up
79 0 ▶ close($test_fh) if ( ref $test_fh );;
Branch: 1 branch
BranchTrueFalseTotal
if ref $test_fh 0 0 0
80  
81 0 $self->{log_current} = $current_time; # reset tracking of time change
82 0 $self->{log_file} = $self->{log_path} ;
83  
84 # YYYY/MM/DD to path if extended
85 0 ▶ if ( $self->{log_hourly} eq 'extended' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log_hourly"} eq "extended") 0 0 0
86 0 $self->{log_path} .= $path_time ;
87 }
88  
89 # Make sure path exists
90 0 make_path( $self->{log_path} );
91  
92 # finish building filename
93 0 $self->{log_file} = $self->{log_path} . $file_time . $self->{log_fname};
94  
95 # get a new fh
96 open( my $fh, '>>', $self->{log_file} )
97 0 ▶ || croak "Unable to open $self->{log_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $self->{"log_file"} or croak(qq[Unable to open $self->{"log_file"}: $!]) 0 0 0
98  
99 0 $fh->autoflush(1);
100  
101 # store fh for later use
102 0 $self->{fh} = $fh;
103  
104 }
105  
106 1 1;
107 __END__
↑ Summary