File Coverage

lib/Badger/Log/File.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 4 50.0
condition 4 8 50.0
subroutine 7 7 100.0
pod 5 5 100.0
total 42 48 87.5


line stmt bran cond sub pod time code
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
10             #
11             #========================================================================
12              
13             package Badger::Log::File;
14              
15             use Badger::Class
16 1         6 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     1   385 };
  1         2  
28              
29              
30             sub init {
31 1     1 1 2 my ($self, $config) = @_;
32 1         7 $self->init_log($config);
33 1         4 $self->init_file($config);
34 1         2 return $self;
35             }
36              
37             sub init_file {
38 1     1 1 3 my ($self, $config) = @_;
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         2 my $filename = $self->{ filename };
46              
47 1 50       3 return $self->_error_msg('no_filename')
48             unless defined $filename;
49            
50 1         4 $self->{ file } = $self->{ filesystem }->file($filename);
51              
52 1         5 $self->info_msg('started');
53             }
54              
55             sub acquire {
56 1     1 1 10 my $self = shift;
57              
58             # Badger::Filesystem::File::append() method returns file handle open for append
59             return $self->{ handle }
60 1   33     9 ||= $self->{ file }->append;
61             }
62              
63             sub release {
64 1     1 1 1 my $self = shift;
65 1   50     5 my $fh = delete $self->{ handle } || return;
66 1         7 $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     4 1 8 my ($self, $level, $message) = @_;
73              
74             my $handle = $self->{ handle }
75 4   66     12 || $self->acquire;
76              
77 4         19 my $output = sprintf($self->format($level, $message));
78 4         6 $output =~ s/\n+$//;
79 4         15 $handle->printflush($output, "\n");
80              
81 4 50       307 $self->release unless $self->{ keep_open };
82             }
83              
84              
85             sub DESTROY {
86 1     1   5 shift->release;
87             }
88              
89              
90             1;
91              
92             __END__