lib/Badger/Log/File.pm

filter: Statementstmt 100% 25 / 25 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 5 / 5 totaltotal 79.6% 43 / 54 CC 6 SCAR 7.2
CC
6
Cov
84%
log
3.1
acquire
2.3
release
2.0
CRAP 2.1
« lib/Badger/Codec/Unicode.pm ↑ Summary blib/lib/Badger/Codec.pm »
1 #========================================================================
2 #
3 # Badger::Log::File
4 #
5 # DESCRIPTION
6 # Subclass of Badger::Log for logging messages to a file.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Log::File;
14  
15 use Badger::Class
16 1 version => 0.01,
17 base => 'Badger::Log',
18 filesystem => 'FS',
19 config => [
20 'filesystem|method:FS',
21 'filename|class:FILENAME',
22 'keep_open|class:KEEP_OPEN=0',
23 ],
24 messages => {
25 no_filename => 'No filename specified for log file',
26 started => 'Started logging',
27 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 sub init {
31 1 ▶ my ($self, $config) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
32 1 $self->init_log($config);
33 1 $self->init_file($config);
34 1 return $self;
35 }
36  
37 sub init_file {
38 1 ▶ my ($self, $config) = @_;
Subroutine init_file: called CC=2 SCAR=7.0
Pod documented
39  
40 # init_log() has already called configured() which will copy the filename
41 # parameter into $self. We don't make the filename parameter mandatory
42 # in the config schema because that will report missing parameters
43 # using error_msg() which has been redefined in Badger::Log. So we
44 # check for it here and use our "backdoor" _error_msg() method.
45 1 my $filename = $self->{ filename };
46  
47 1 ▶ return $self->_error_msg('no_filename')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 1 1
48 unless defined $filename;
49
50 1 $self->{ file } = $self->{ filesystem }->file($filename);
51  
52 1 $self->info_msg('started');
53 }
54  
55 sub acquire {
56 1 ▶ my $self = shift;
Subroutine acquire: called CC=2 SCAR=8.1
Pod documented
57  
58 # Badger::Filesystem::File::append() method returns file handle open for append
59 return $self->{ handle }
60 1 ▶ ||= $self->{ file }->append;
Condition33%1/3
$self->{"handle"} ||= $self->{"file"}->append
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"handle"} ||= $self->{"file"}->append
$self->{"handle"} $self->{"file"}->append
Truth table33%1/3
A ||= B
A= $self->{"handle"}B= $self->{"file"}->append
ABresult
1X1
011
000
61 }
62  
63 sub release {
64 1 ▶ my $self = shift;
Subroutine release: called CC=2 SCAR=7.1
Pod documented
65 1 ▶ my $fh = delete $self->{ handle } || return;
Condition50%1/2
delete $self->{'handle'} || (return)
l!l
10
MC/DC0%0/1
delete $self->{'handle'} || (return)
delete $self->{'handle'}
Truth table50%1/2
A
A= delete $self->{'handle'}
Aresult
11
00
66 1 $fh->close()
67 # TODO: does IO::File throw an error on close fail?
68 # || return $self->base_error_msg( bad_close => $self->{ filename }, $! );
69 }
70  
71 sub log {
72 4 ▶ my ($self, $level, $message) = @_;
Subroutine log: called CC=3 SCAR=11.2
Pod documented
73  
74 my $handle = $self->{ handle }
75 4 ▶ || $self->acquire;
Condition66%2/3
$self->{'handle'} || $self->acquire
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{'handle'} || $self->acquire
$self->{'handle'} $self->acquire
Truth table66%2/3
A || B
A= $self->{'handle'}B= $self->acquire
ABresult
1X1
011
000
76  
77 4 my $output = sprintf($self->format($level, $message));
78 4 $output =~ s/\n+$//;
79 4 $handle->printflush($output, "\n");
80  
81 4 ▶ $self->release unless $self->{ keep_open };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keep_open"} 0 4 4
82 }
83  
84  
85 sub DESTROY {
86 1 ▶ shift->release;
Subroutine DESTROY: called CC=1 SCAR=0.0
87 }
88  
89  
90 1 1;
91  
92 __END__
« lib/Badger/Codec/Unicode.pm ↑ Summary blib/lib/Badger/Codec.pm »