| 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.02, | |||||||||||||||||||||||||||
| 17 | base => 'Badger::Log', | ||||||||||||||||||||||||||||
| 18 | filesystem => 'FS', | ||||||||||||||||||||||||||||
| 19 | utils => 'Now', | ||||||||||||||||||||||||||||
| 20 | config => [ | ||||||||||||||||||||||||||||
| 21 | 'filesystem|method:FS', | ||||||||||||||||||||||||||||
| 22 | 'filename|class:FILENAME', | ||||||||||||||||||||||||||||
| 23 | 'filename_format|class:FILENAME_FORMAT', | ||||||||||||||||||||||||||||
| 24 | 'keep_open|class:KEEP_OPEN=0', | ||||||||||||||||||||||||||||
| 25 | ], | ||||||||||||||||||||||||||||
| 26 | messages => { | ||||||||||||||||||||||||||||
| 27 | no_filename => 'No filename or filename_format specified for log file', | ||||||||||||||||||||||||||||
| 28 | started => 'Started logging', | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | our $FORMATS = { | |||||||||||||||||||||||||||
| 32 | DATE => '%Y-%m-%d', | ||||||||||||||||||||||||||||
| 33 | TIME => '%H-%M-%S', | ||||||||||||||||||||||||||||
| 34 | YEAR => '%Y', | ||||||||||||||||||||||||||||
| 35 | MONTH => '%m', | ||||||||||||||||||||||||||||
| 36 | DAY => '%d', | ||||||||||||||||||||||||||||
| 37 | HOURS => '%H', | ||||||||||||||||||||||||||||
| 38 | MINUTES => '%M', | ||||||||||||||||||||||||||||
| 39 | SECONDS => '%S', | ||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub init { | ||||||||||||||||||||||||||||
| 43 | 2 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 2 | $self->init_log($config); | |||||||||||||||||||||||||||
| 45 | 2 | $self->init_file($config); | |||||||||||||||||||||||||||
| 46 | 2 | return $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub init_file { | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init_file: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # init_log() has already called configured() which will copy the filename | ||||||||||||||||||||||||||||
| 53 | # parameter into $self. We don't make the filename parameter mandatory | ||||||||||||||||||||||||||||
| 54 | # in the config schema because that will report missing parameters | ||||||||||||||||||||||||||||
| 55 | # using error_msg() which has been redefined in Badger::Log. So we | ||||||||||||||||||||||||||||
| 56 | # check for it here and use our "backdoor" _error_msg() method. | ||||||||||||||||||||||||||||
| 57 | return $self->_error_msg('no_filename') | ||||||||||||||||||||||||||||
| 58 | unless defined $self->{ filename } | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | || defined $self->{ filename_format }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"filename"} or defined $self->{"filename_format"}
MC/DC0%0/2
defined $self->{"filename"} or defined $self->{"filename_format"}
defined $self->{"filename"}
defined $self->{"filename_format"}
Truth table66%2/3
A or B
A= defined $self->{"filename"}B= defined $self->{"filename_format"}
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # if filename is set but not filename_format then this is a no-op | ||||||||||||||||||||||||||||
| 62 | # as expand_filename() will return the filename | ||||||||||||||||||||||||||||
| 63 | 2 | $self->{ filename } = $self->expand_filename; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | $self->info_msg('started'); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub expand_filename { | ||||||||||||||||||||||||||||
| 69 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
expand_filename: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 10 | ▶ | my $format = $self->{ filename_format } || return $self->{ filename }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'filename_format'} || (return $self->{'filename'})
MC/DC100%1/1
$self->{'filename_format'} || (return $self->{'filename'})
$self->{'filename_format'}
Truth table100%2/2
A
A= $self->{'filename_format'}
| |||||||||||||||||||||||||||||
| 71 | 5 | my $strftime = $format; | |||||||||||||||||||||||||||
| 72 | # expand any <XXXX> markers to convert to strftime format | ||||||||||||||||||||||||||||
| 73 | 5 | ▶ | $strftime =~ s/<(\w+)>/$FORMATS->{ $1 } || $1/ge; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | # now format using strftime | ||||||||||||||||||||||||||||
| 75 | 5 | return Now->format($strftime); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub filename_changed { | ||||||||||||||||||||||||||||
| 79 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
filename_changed: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 8 | my $filename = $self->expand_filename; | |||||||||||||||||||||||||||
| 81 | return $filename ne $self->{ filename } | ||||||||||||||||||||||||||||
| 82 | 8 | ▶ | ? ($self->{ filename } = $filename) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | : undef; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub file { | ||||||||||||||||||||||||||||
| 87 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 3 | my $filename = $self->{ filename }; | |||||||||||||||||||||||||||
| 89 | 3 | return ($self->{ file } = $self->{ filesystem }->file($filename)); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub filehandle { | ||||||||||||||||||||||||||||
| 93 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
filehandle: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if ($self->filename_changed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | $self->release; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 8 | return $self->acquire; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub acquire { | ||||||||||||||||||||||||||||
| 101 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
acquire: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Badger::Filesystem::File::append() method returns file handle open for append | ||||||||||||||||||||||||||||
| 104 | return $self->{ handle } | ||||||||||||||||||||||||||||
| 105 | 8 | ▶ | ||= $self->file->append; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"handle"} ||= $self->file->append
MC/DC0%0/2
$self->{"handle"} ||= $self->file->append
$self->{"handle"}
$self->file->append
Truth table66%2/3
A ||= B
A= $self->{"handle"}B= $self->file->append
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub release { | ||||||||||||||||||||||||||||
| 109 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
release: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | 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'}
| |||||||||||||||||||||||||||||
| 111 | 3 | $fh->close; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub log { | ||||||||||||||||||||||||||||
| 116 | 8 | ▶ | my ($self, $level, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 8 | my $handle = $self->filehandle; | |||||||||||||||||||||||||||
| 118 | 8 | my $output = sprintf($self->format($level, $message)); | |||||||||||||||||||||||||||
| 119 | 8 | $output =~ s/\n+$//; | |||||||||||||||||||||||||||
| 120 | 8 | $handle->printflush($output, "\n"); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | ▶ | $self->release unless $self->{ keep_open }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub DESTROY { | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | shift->release; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||