| 1 | package Log::Facile; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use vars qw($VERSION $TEMPLATE); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | our $TEMPLATE = 'template'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # log template config | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | my @tmpl_accessor = ('TEMPLATE', 'DATE', 'LEVEL', 'MESSAGE',); | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # available accessor list | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | my @accessor = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 'log_file', 'level_debug', 'level_info', 'level_error', | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 'level_warn', 'level_fatal', 'swap_dir', 'date_format', | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 'debug_flag', $TEMPLATE, | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # constructor | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | my ($class, $log_file, $swap_dir) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | bless { | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | log_file => $log_file, | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | swap_dir => $swap_dir, | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | date_format => 'yyyy/mm/dd hh:mi:ss', | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | $TEMPLATE => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 'TEMPLATE' => 'DATE [LEVEL] MESSAGE', | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 'DATE' => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 'LEVEL' => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 'MESSAGE' => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # getter | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | sub get | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 493 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 493 | my ($self, $key, $tmpl_key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 493 | ▶ | if (_is_tmpl_accessor($key, $tmpl_key) == 1) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # log template value | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 13 | return $self->{$TEMPLATE}->{$tmpl_key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | elsif (_is_tmpl_accessor($key, $tmpl_key) == 2) | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # new template value | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 274 | return $self->{$TEMPLATE}; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | elsif (_is_valid_accessor($key)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # get field | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 203 | return $self->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # error | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # setter | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | sub set | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 29 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 29 | my ($self, $key, $value_or_key, $tmpl_value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 29 | ▶ | if (_is_tmpl_accessor($key, $value_or_key) == 1) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | $self->{$TEMPLATE}->{$value_or_key} = $tmpl_value; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | elsif (_is_tmpl_accessor($key, $value_or_key) == 2) | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | push @tmpl_accessor, $value_or_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | $self->{$TEMPLATE}->{$value_or_key} = $tmpl_value; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | elsif (_is_tmpl_accessor($key, $value_or_key) == 255) | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | elsif (_is_valid_accessor($key)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 20 | $self->{$key} = $value_or_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 26 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # tmpl accessor check | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | sub _is_tmpl_accessor | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1217 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_tmpl_accessor: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 1217 | my ($tmpl_key, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1217 | my $enable = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1217 | ▶ | if (defined $tmpl_key && $tmpl_key eq $TEMPLATE) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $tmpl_key and $tmpl_key eq $Log::Facile::TEMPLATE
MC/DC50%1/2
defined $tmpl_key and $tmpl_key eq $Log::Facile::TEMPLATE
defined $tmpl_key
$tmpl_key eq $Log::Facile::TEMPLATE
Truth table66%2/3
A and B
A= defined $tmpl_keyB= $tmpl_key eq $Log::Facile::TEMPLATE
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 746 | $enable = 2; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 746 | for my $each (@tmpl_accessor) | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 2872 | ▶ | if (defined $key && $each eq $key) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $key and $each eq $key
MC/DC100%2/2
defined $key and $each eq $key
defined $key
$each eq $key
Truth table100%3/3
A and B
A= defined $keyB= $each eq $key
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 190 | $enable = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | 190 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ▶ | elsif (defined $key | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $key and $each =~ /$key/ || $key =~ /$each/
$each =~ /$key/ or $key =~ /$each/
MC/DC100%3/3
defined $key and $each =~ /$key/ || $key =~ /$each/
defined $key
$each =~ /$key/
$key =~ /$each/
Truth table100%4/4
A and B or C
A= defined $keyB= $each =~ /$key/C= $key =~ /$each/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | && ($each =~ m/$key/ || $key =~ m/$each/)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | croak "Can't use '" | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | . $key | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | . "' to template " | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | . "because '" | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | . $each | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | . "' has already used."; | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $enable = 255; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1213 | return $enable; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # accessor check | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | sub _is_valid_accessor | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 225 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_valid_accessor: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 225 | my $key = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 225 | my $enable = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 225 | for my $each (@accessor) | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1331 | ▶ | if (defined $key && $key eq $each) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and $key eq $each
MC/DC50%1/2
defined $key and $key eq $each
defined $key
$key eq $each
Truth table66%2/3
A and B
A= defined $keyB= $key eq $each
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 223 | $enable = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 223 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 225 | ▶ | croak 'invalid field name :-P - ' . $key if !$enable; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 223 | return $enable; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # replace log item | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | sub _replace_log_item | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 173 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_replace_log_item: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 173 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # get defined object | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 173 | ▶ | if ( defined $key | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
defined $key and _is_tmpl_accessor($Log::Facile::TEMPLATE, $key) == 1 and defined $self->get($Log::Facile::TEMPLATE)->{$key}
defined $key and _is_tmpl_accessor($Log::Facile::TEMPLATE, $key) == 1
MC/DC33%1/3
defined $key and _is_tmpl_accessor($Log::Facile::TEMPLATE, $key) == 1 and defined $self->get($Log::Facile::TEMPLATE)->{$key}
defined $key
_is_tmpl_accessor($Log::Facile::TEMPLATE, $key) == 1
defined $self->get($Log::Facile::TEMPLATE)->{$key}
Truth table50%2/4
A and B and C
A= defined $keyB= _is_tmpl_accessor($Log::Facile::TEMPLATE, $key) == 1C= defined $self->get($Log::Facile::TEMPLATE)->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | && _is_tmpl_accessor($TEMPLATE, $key) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | && defined $self->get($TEMPLATE)->{$key}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 29 | return $self->get($TEMPLATE)->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ▶ | elsif ( defined $key | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
defined $key and $key eq "DATE" and not defined $self->get($Log::Facile::TEMPLATE)->{"DATE"}
defined $key and $key eq "DATE"
MC/DC33%1/3
defined $key and $key eq "DATE" and not defined $self->get($Log::Facile::TEMPLATE)->{"DATE"}
defined $key
$key eq "DATE"
not defined $self->get($Log::Facile::TEMPLATE)->{"DATE"}
Truth table50%2/4
A and B and not(C)
A= defined $keyB= $key eq "DATE"C= not defined $self->get($Log::Facile::TEMPLATE)->{"DATE"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | && $key eq 'DATE' | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | && !defined $self->get($TEMPLATE)->{'DATE'}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # get date default sub | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 48 | return $self->_current_date(); | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # return accepted value | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 96 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # get log output string | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | sub _get_log_str | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 24 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_log_str: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 24 | my ($self, $date, $level, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # template hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 24 | my $t_hash = $self->get($TEMPLATE); | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # log template string | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 24 | my $log_str = $t_hash->{'TEMPLATE'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # default values | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 24 | $log_str =~ s/DATE/$date/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 24 | $log_str =~ s/LEVEL/$level/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | 24 | $log_str =~ s/MESSAGE/$message/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # user defined values | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 24 | for my $key (@tmpl_accessor) | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 101 | my $replace = $self->_replace_log_item($key); | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 101 | $log_str =~ s/$key/$replace/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 24 | return $log_str; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # log writer | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | sub _write | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 24 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 24 | my ($self, $p_level, $p_message) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # default values | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 24 | my $date = $self->_replace_log_item('DATE'); | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 24 | my $level = $self->_replace_log_item('LEVEL', $p_level); | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | 24 | my $message = $self->_replace_log_item('MESSAGE', $p_message); | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # log string | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 24 | my $log_str = $self->_get_log_str($date, $level, $message) . $/; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # execute writing log file | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 24 | ▶ | open my $log, ">> " . $self->get('log_file') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | or croak 'log file open error - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 24 | print $log $log_str; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | 24 | ▶ | close $log | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | or croak 'log file close error - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | sub debug | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
debug: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 6 | my ($self, $message_str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 222 | 6 | ▶ | if ($self->get('debug_flag')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | ▶ | my $level = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | defined $self->get('level_debug') | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | ? $self->get('level_debug') | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | : 'DEBUG'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | return $self->_write($level, $message_str); | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | sub info | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
info: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 8 | my ($self, $message_str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | ▶ | my $level = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | defined $self->get('level_info') | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | ? $self->get('level_info') | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | : 'INFO'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 8 | return $self->_write($level, $message_str); | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | sub error | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 4 | my ($self, $message_str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | my $level = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | defined $self->get('level_error') | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | ? $self->get('level_error') | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | : 'ERROR'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 4 | return $self->_write($level, $message_str); | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | sub warn | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
warn: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 4 | my ($self, $message_str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | 4 | ▶ | my $level = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | defined $self->get('level_warn') | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | ? $self->get('level_warn') | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | : 'WARN'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 4 | return $self->_write($level, $message_str); | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | sub fatal | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fatal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4 | my ($self, $message_str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | my $level = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | defined $self->get('level_fatal') | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | ? $self->get('level_fatal') | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | : 'FATAL'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 4 | return $self->_write($level, $message_str); | |||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | sub swap | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
swap: called CC=13 SCAR=26.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 6 | my ($self, $swap_dir) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # set swap dir | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 6 | ▶ | if (defined $swap_dir) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | $self->set('swap_dir', $swap_dir); | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | elsif (!defined $self->get('swap_dir')) | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my $log_dir = $self->get('log_file'); | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | $log_dir =~ s/(.+\/).+$/$1/; | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $self->set('swap_dir', $log_dir); | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # get log filename prefix | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 6 | my $file_pref = $self->get('log_file'); | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | 6 | $file_pref =~ s/.+\/(.+?)$/$1/; | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # move current log file | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 6 | ▶ | if (!-d $self->get('swap_dir')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | ▶ | mkdir $self->get('swap_dir') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | or croak 'create swap dir error - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 5 | ▶ | if (-f $self->get('log_file')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 4 | ▶ | rename $self->get('log_file'), $self->get('swap_dir') . '/' . $file_pref | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | or croak 'current file move error - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # rename files | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 3 | ▶ | opendir my $s_dir, $self->get('swap_dir') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | or croak 'dir open error - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 3 | for my $each (grep /$file_pref/, reverse sort readdir $s_dir) | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | $each = $self->get('swap_dir') . '/' . $each; | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | 4 | my $rename_pref = $self->get('swap_dir') . '/' . $file_pref . '.'; | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | 4 | ▶ | if ($each =~ /\.(\d)$/) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | ▶ | rename $each, $rename_pref . ($1 + 1) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 323 | or croak 'rename error (' . $rename_pref . ($1 + 1) . ') - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 3 | ▶ | rename $each, $rename_pref . '1' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | or croak 'rename error (' . $rename_pref . '.1) - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 3 | ▶ | closedir $s_dir | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | or croak 'dir close error - ' . $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # get current datetime | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | sub _current_date | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 48 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_current_date: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 48 | my ($self, $pat) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # datetime values | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 48 | my @da = localtime(time); | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | 48 | my $year4 = sprintf("%04d", $da[5] + 1900); | |||||||||||||||||||||||||||||||||||||||||||||
| 343 | 48 | my $year2 = sprintf("%02d", $da[5] + 1900 - 2000); | |||||||||||||||||||||||||||||||||||||||||||||
| 344 | 48 | my $month = sprintf("%02d", $da[4] + 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 345 | 48 | my $day = sprintf("%02d", $da[3]); | |||||||||||||||||||||||||||||||||||||||||||||
| 346 | 48 | my $hour = sprintf("%02d", $da[2]); | |||||||||||||||||||||||||||||||||||||||||||||
| 347 | 48 | my $min = sprintf("%02d", $da[1]); | |||||||||||||||||||||||||||||||||||||||||||||
| 348 | 48 | my $sec = sprintf("%02d", $da[0]); | |||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # date format | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 48 | ▶ | my $date_str = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | (defined $self->get('date_format')) | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | ? $self->get('date_format') | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | : 'yyyy/mm/dd hh:mi:ss'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # replace format values | ||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 48 | $date_str =~ s/yyyy/$year4/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | 48 | $date_str =~ s/yy/$year2/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 359 | 48 | $date_str =~ s/mm/$month/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | 48 | $date_str =~ s/dd/$day/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | 48 | $date_str =~ s/hh/$hour/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 362 | 48 | $date_str =~ s/mi/$min/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | 48 | $date_str =~ s/ss/$sec/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 48 | return $date_str; | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 369 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||