| 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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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)
MC/DC0%0/1
delete $self->{'handle'} || (return)
delete $self->{'handle'}
Truth table50%1/2
A
A= delete $self->{'handle'}
| |||||||||||||||||||||
| 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
MC/DC0%0/2
$self->{'handle'} || $self->acquire
$self->{'handle'}
$self->acquire
Truth table66%2/3
A || B
A= $self->{'handle'}B= $self->acquire
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||