blib/lib/Log/StdLog.pm

filter: Statementstmt 95.2% 60 / 63 Branchbran 81.8% 18 / 22 Conditioncond 54.5% 6 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 92.3% 12 / 13 Podpod n/a totaltotal 82.7% 96 / 116 CC 16 SCAR 15.9
CC
16
Cov
88%
PRINT
8.1
import
7.1
TIEHANDLE
3.7
CRAP 4.9
↑ Summary
1 package Log::StdLog;
2  
3 16 ▶use version; $VERSION = qv('0.0.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 ▶use base 'IO::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 my @levels = qw( all trace debug user info warn error fatal none );
12 16 my %severity; @severity{@levels} = 1..@levels;
13  
14 # Aliases...
15 16 $severity{warning} = $severity{warn};
16  
17  
18 14 ▶sub _make_formatter {
Subroutine _make_formatter: called CC=1 SCAR=0.0
19 14 my ($format) = @_;
20 49 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 49 my ($time, $source, $type, @msg) = @_;
22 49 my $msg = join q{}, @msg;
23 49 return sprintf $format, $time, $source, $type, $msg;
24 }
25 14 }
26  
27 15 ▶sub import {
Subroutine import: called CC=7 SCAR=19.7
28 15 my ($package, $opt_ref) = @_;
29 15 my ($caller, $file) = caller;
30  
31  
32 15 ▶ croak "Usage: use $package { file=>\$filename, level=>\$level, format=>sub{...} }\n "
Branch: 1 branch
BranchTrueFalseTotal
if $opt_ref and not ref $opt_ref eq "HASH" 0 15 15
Condition66%2/3
$opt_ref and not ref $opt_ref eq "HASH"
!ll&&!rl&&r
1140
MC/DC0%0/2
$opt_ref and not ref $opt_ref eq "HASH"
$opt_ref not ref $opt_ref eq "HASH"
Truth table66%2/3
A and not(B)
A= $opt_refB= not ref $opt_ref eq "HASH"
ABresult
0X0
100
111
33 if $opt_ref && not ref $opt_ref eq 'HASH';
34  
35 15 ▶ if (not exists $opt_ref->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt_ref->{"file"}) 2 13 15
36 2 $opt_ref->{file} = "$file.log";
37 }
38  
39 15 ▶ if (not exists $opt_ref->{format}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $opt_ref->{'format'}) { } 14 1 15
elsif (not ref $opt_ref->{'format'}) { } 0 1 1
40 14 $opt_ref->{format} = _make_formatter("[%s] [%s] [%s] %s");
41 }
42 elsif (not ref $opt_ref->{format}) {
43 0 $opt_ref->{format} = _make_formatter($opt_ref->{format});
44 }
45
46 15 ▶ if (not exists $opt_ref->{level}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt_ref->{"level"}) 1 14 15
47 1 $opt_ref->{level} = 'user';
48 }
49  
50 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 15 tie *{$caller.'::STDLOG'}, $package, $opt_ref;
52 }
53  
54 15 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=3 SCAR=13.1
55 15 my ($package, $opt_ref) = @_;
56  
57 return bless { file => $opt_ref->{file},
58 handle => $opt_ref->{handle},
59 formatter => $opt_ref->{format},
60 min_severity_name => $opt_ref->{level} || 'user',
61 min_severity => $severity{$opt_ref->{level}}
62 || $severity{user},
63 15 ▶ };
Condition40%2/5
$opt_ref->{'level'} || 'user'
l!l
150
$severity{$opt_ref->{'level'}} || $severity{'user'}
l!l&&r!l&&!r
1500
MC/DC0%0/1
$opt_ref->{'level'} || 'user'
$opt_ref->{'level'}
MC/DC0%0/2
$severity{$opt_ref->{'level'}} || $severity{'user'}
$severity{$opt_ref->{'level'}} $severity{'user'}
Truth table50%1/2
A
A= $opt_ref->{'level'}
Aresult
11
00
Truth table33%1/3
A || B
A= $severity{$opt_ref->{'level'}}B= $severity{'user'}
ABresult
1X1
011
000
64 }
65  
66 16 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 86 ▶sub PRINT {
Subroutine PRINT: called CC=8 SCAR=20.9
69 my ($self, $level, @msg)
70 = @_ == 1 ? ( $_[0], $_[0]->{min_severity_name}, $_ )
71 86 ▶ : @_ == 2 ? ( $_[0], $_[0]->{min_severity_name}, $_[1] )
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 1 85 86
@_ == 2 ? : 1 84 85
72 : ( @_ )
73 ;
74  
75 # No-op if message isn't important enough...
76 86 ▶ my $severity = $severity{$level} || $severity{user};
Condition66%2/3
$severity{$level} || $severity{'user'}
l!l&&r!l&&!r
8420
MC/DC0%0/2
$severity{$level} || $severity{'user'}
$severity{$level} $severity{'user'}
Truth table66%2/3
A || B
A= $severity{$level}B= $severity{'user'}
ABresult
1X1
011
000
77 86 ▶ return 0 if $self->{min_severity} > $severity;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"min_severity"} > $severity 32 54 86
78  
79 # Format message early to get accurate time-stamp...
80 54 my ($sec,$min,$hour,$day,$mon,$year) = localtime;
81 54 $year+=1900;
82 54 $mon++;
83 54 my $time = sprintf("%04d%02d%02d.%02d%02d%02d",
84 $year, $mon, $day, $hour, $min, $sec);
85 54 ▶ $msg[-1] =~ s/\n\z// if @msg;
Branch: 1 branch
BranchTrueFalseTotal
if @msg 54 0 54
86 54 my $log_msg = $self->{formatter}->($time, $$, $level, @msg);
87  
88 # Create connection to log file, if necessary...
89 54 ▶ if (not $self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"handle"}) 12 42 54
90 open $self->{handle}, '>>', $self->{file}
91 12 ▶ or croak "Unable to open log file '$self->{file}'";
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"handle"}, ">>", $self->{"file"} or croak(qq[Unable to open log file '$self->{"file"}']) 12 0 12
92 }
93  
94 # Synchronize writing to file via advisory locking...
95 54 flock($self->{handle}, LOCK_EX);
96 54 my $result = $self->{handle}->print($log_msg."\n");
97 54 flock($self->{handle}, LOCK_UN);
98  
99 54 return $result;
100 }
101  
102 0 ▶sub CLOSE {
Subroutine CLOSE: not called CC=1 SCAR=6.9
103 0 my ($self) = @_;
104 0 $self->{handle}->close();
105 }
106  
107 16 1; # Magic true value required at end of module
108 __END__
↑ Summary