| 1 | package ScriptUtil; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Benchmark; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Carp qw(carp confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | extends 'Moose::Object'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ################################################################################ | ||||||||||||||||||||||||||||
| 21 | # Object Attributes: | ||||||||||||||||||||||||||||
| 22 | ################################################################################ | ||||||||||||||||||||||||||||
| 23 | 1 | has 'verbose' => ( isa => 'Bool', is => 'rw', required => 0, default => 0 ); | |||||||||||||||||||||||||||
| 24 | 1 | has 'nolog' => ( isa => 'Bool', is => 'rw', required => 0, default => 0 ); | |||||||||||||||||||||||||||
| 25 | 1 | has 'nolock' => ( isa => 'Bool', is => 'rw', required => 0, default => 0 ); | |||||||||||||||||||||||||||
| 26 | 1 | has '_parent' => ( isa => 'Str', is => 'rw', required => 0, default => undef ); | |||||||||||||||||||||||||||
| 27 | 1 | has '_bench_start' => ( isa => 'Benchmark', is => 'rw', required => 0 ); | |||||||||||||||||||||||||||
| 28 | 1 | has '_bench_end' => ( isa => 'Benchmark', is => 'rw', required => 0 ); | |||||||||||||||||||||||||||
| 29 | 1 | has '_self_fh' => ( isa => 'Any', is => 'rw', required => 0, default => undef ); | |||||||||||||||||||||||||||
| 30 | 1 | has 'log_debug' => ( isa => 'Int', is => 'ro', required => 0, default => 5 ); | |||||||||||||||||||||||||||
| 31 | 1 | has 'log_path' => ( isa => 'Str', is => 'rw', required => 0, default => '.' ); | |||||||||||||||||||||||||||
| 32 | 1 | has 'log_filename' => ( isa => 'Str', is => 'rw', required => 0, default => undef ); | |||||||||||||||||||||||||||
| 33 | 1 | has 'log_mode' => ( isa => 'Str', is => 'ro', required => 0, default => '>>' ); | |||||||||||||||||||||||||||
| 34 | 1 | has 'log_stderrredirect' => ( isa => 'Bool', is => 'ro', required => 0, default => 0 ); | |||||||||||||||||||||||||||
| 35 | 1 | has 'log_storeexptext' => ( isa => 'Bool', is => 'ro', required => 0, default => 1 ); | |||||||||||||||||||||||||||
| 36 | 1 | has 'log_datetimestamp' => ( isa => 'Bool', is => 'ro', required => 0, default => 1 ); | |||||||||||||||||||||||||||
| 37 | 1 | has 'log_logfiledatetime' => ( isa => 'Bool', is => 'rw', required => 0, default => 1 ); | |||||||||||||||||||||||||||
| 38 | 1 | has 'logger' => ( isa => 'File::Log', is => 'rw', required => 0, default => undef ); | |||||||||||||||||||||||||||
| 39 | 1 | has 'log_rotation' => ( isa => 'Bool', is => 'ro', required => 0, default => 0 ); | |||||||||||||||||||||||||||
| 40 | 1 | has 'log_zip_after_days' => ( isa => 'Num', is => 'ro', required => 0, default => 2 ); | |||||||||||||||||||||||||||
| 41 | 1 | has 'log_rm_after_days' => ( isa => 'Num', is => 'ro', required => 0, default => 31 ); | |||||||||||||||||||||||||||
| 42 | 1 | has '_log_pattern' => ( isa => 'Str', is => 'ro', required => 0, default => '.log' ); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | ################################################################################ | ||||||||||||||||||||||||||||
| 45 | # Methods | ||||||||||||||||||||||||||||
| 46 | ################################################################################ | ||||||||||||||||||||||||||||
| 47 | # Constructor: | ||||||||||||||||||||||||||||
| 48 | ################################################################################ | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=45.8
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 51 | 1 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Begin benchmark timing: | ||||||||||||||||||||||||||||
| 54 | 0 | $self->_bench_start(new Benchmark); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Get the details of the script using this module: | ||||||||||||||||||||||||||||
| 57 | 0 | my ($package, $parent, $line) = caller; | |||||||||||||||||||||||||||
| 58 | 0 | $self->_parent($parent); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Lock ourself, so we only run one process at a time: | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ($self->nolock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->locker(); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Set up logging if required: | ||||||||||||||||||||||||||||
| 66 | 0 | my $logger = undef; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless ($self->nolog) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # If log rotation is on, we must use date/time file names for log files, | ||||||||||||||||||||||||||||
| 70 | # Otherwise the current log file we are using will never be rotated: | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (!$self->log_logfiledatetime && $self->log_rotation) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->log_logfiledatetime and $self->log_rotation
MC/DC0%0/2
not $self->log_logfiledatetime and $self->log_rotation
not $self->log_logfiledatetime
$self->log_rotation
Truth table0%0/3
not(A) and B
A= not $self->log_logfiledatetimeB= $self->log_rotation
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($self->verbose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | carp("NOTICE: Log file date/time file name format will be used because log rotation has been enabled\n"); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | $self->log_logfiledatetime(1); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless (defined($self->log_filename)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | my $log_filename = $parent; | |||||||||||||||||||||||||||
| 80 | 0 | $log_filename =~ s/.pl$//; | |||||||||||||||||||||||||||
| 81 | 0 | $log_filename .= '.log'; | |||||||||||||||||||||||||||
| 82 | 0 | $self->log_filename($log_filename); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (defined($self->log_path)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->log_filename(File::Spec->catfile($self->log_path, $self->log_filename)); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $self->{'logger'} = File::Log->new( | ||||||||||||||||||||||||||
|
Condition0%0/2
'File::Log'->new({'debug', $self->log_debug, 'logFileName', $self->log_filename, 'logFileMode', $self->log_mode, 'stderrredirect', $self->log_stderrredirect, 'StoreExpText', $self->log_storeexptext, 'DateTimeStamp', $self->log_datetimestamp, 'LogFileDateTime', $self->log_logfiledatetime}) || confess(q(- Error: can't create log file: [) . $self->log_filename . "]\n- Cause: $!\n")
MC/DC0%0/1
'File::Log'->new({'debug', $self->log_debug, 'logFileName', $self->log_filename, 'logFileMode', $self->log_mode, 'stderrredirect', $self->log_stderrredirect, 'StoreExpText', $self->log_storeexptext, 'DateTimeStamp', $self->log_datetimestamp, 'LogFileDateTime', $self->log_logfiledatetime}) || confess(q(- Error: can't create log file: [) . $self->log_filename . "]\n- Cause: $!\n")
'File::Log'->new({'debug', $self->log_debug, 'logFileName', $self->log_filename, 'logFileMode', $self->log_mode, 'stderrredirect', $self->log_stderrredirect, 'StoreExpText', $self->log_storeexptext, 'DateTimeStamp', $self->log_datetimestamp, 'LogFileDateTime', $self->log_logfiledatetime})
Truth table0%0/2
A
A= 'File::Log'->new({'debug', $self->log_debug, 'logFileName', $self->log_filename, 'logFileMode', $self->log_mode, 'stderrredirect', $self->log_stderrredirect, 'StoreExpText', $self->log_storeexptext, 'DateTimeStamp', $self->log_datetimestamp, 'LogFileDateTime', $self->log_logfiledatetime})
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | debug => $self->log_debug, | ||||||||||||||||||||||||||||
| 92 | logFileName => $self->log_filename, | ||||||||||||||||||||||||||||
| 93 | logFileMode => $self->log_mode, | ||||||||||||||||||||||||||||
| 94 | stderrredirect => $self->log_stderrredirect, | ||||||||||||||||||||||||||||
| 95 | StoreExpText => $self->log_storeexptext, | ||||||||||||||||||||||||||||
| 96 | DateTimeStamp => $self->log_datetimestamp, | ||||||||||||||||||||||||||||
| 97 | LogFileDateTime => $self->log_logfiledatetime, | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | ) || confess("- Error: can't create log file: [" . $self->log_filename . "]\n- Cause: $!\n"); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $self->echo("Starting up script: [" . $self->_parent . "] whirr click"); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Move into the scripts working directory: | ||||||||||||||||||||||||||||
| 105 | 0 | my $dirname = dirname($parent); | |||||||||||||||||||||||||||
| 106 | 0 | $self->echo("Moving into path: [" . $dirname . "] (" . getcwd() . ")"); | |||||||||||||||||||||||||||
| 107 | 0 | chdir($dirname); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Rotate logs if required: | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($self->log_rotation) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $self->log_rotate(); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | return $class->meta->new_object( | |||||||||||||||||||||||||||
| 115 | __INSTANCE__ => $self, | ||||||||||||||||||||||||||||
| 116 | @_, | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | ################################################################################ | ||||||||||||||||||||||||||||
| 120 | # Method to lock the calling script so only one instance can run | ||||||||||||||||||||||||||||
| 121 | ################################################################################ | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub locker { | ||||||||||||||||||||||||||
|
Subroutine
locker: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | unless ( defined($self->_self_fh) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | open(SELF_FH,">>",$self->_parent) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | || confess("\n- Error: Cannot open [" . $self->_parent . "]\n- Cause: $!\n"); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | unless ( flock(SELF_FH, LOCK_EX|LOCK_NB) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | confess("\n- Error: [" . $self->_parent . "] is already running\n- Cause: $!\n"); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | $self->_self_fh(*SELF_FH); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | return $self; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | ################################################################################ | ||||||||||||||||||||||||||||
| 139 | # Method to unlock the calling script so other instances can run | ||||||||||||||||||||||||||||
| 140 | ################################################################################ | ||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sub unlocker { | ||||||||||||||||||||||||||
|
Subroutine
unlocker: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | if ( defined($self->_self_fh) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | close($self->_self_fh); | |||||||||||||||||||||||||||
| 146 | 0 | $self->_self_fh(undef); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | return $self; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | ################################################################################ | ||||||||||||||||||||||||||||
| 152 | # Method to print & log message depending on the demands of the script | ||||||||||||||||||||||||||||
| 153 | ################################################################################ | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub echo { | ||||||||||||||||||||||||||
|
Subroutine
echo: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 0 | my ($self, $message, $debug) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | unless (defined($debug)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | $debug = $self->log_debug; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | unless ($self->nolog) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $self->logger->msg($debug, "+ " . $message . "\n"); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ($self->verbose && $debug <= $self->log_debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->verbose and $debug <= $self->log_debug
MC/DC0%0/2
$self->verbose and $debug <= $self->log_debug
$self->verbose
$debug <= $self->log_debug
Truth table0%0/3
A and B
A= $self->verboseB= $debug <= $self->log_debug
| |||||||||||||||||||||||||||||
| 166 | 0 | print "+ " . $message . "\n"; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | return $self; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | ################################################################################ | ||||||||||||||||||||||||||||
| 172 | # Method to clean white space out of strings | ||||||||||||||||||||||||||||
| 173 | ################################################################################ | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub trim { | ||||||||||||||||||||||||||
|
Subroutine
trim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 0 | my ($self, $string) = @_; | |||||||||||||||||||||||||||
| 176 | # Trim leading & Trailing Spaces: | ||||||||||||||||||||||||||||
| 177 | 0 | $string =~ s/^\s+//; | |||||||||||||||||||||||||||
| 178 | 0 | $string =~ s/\s+$//; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # Trim leading & Trailing Tabs: | ||||||||||||||||||||||||||||
| 181 | 0 | $string =~ s/^\t+//; | |||||||||||||||||||||||||||
| 182 | 0 | $string =~ s/\t+$//; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | return $string; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | ################################################################################ | ||||||||||||||||||||||||||||
| 187 | # Method to rotate logs | ||||||||||||||||||||||||||||
| 188 | ################################################################################ | ||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub log_rotate { | ||||||||||||||||||||||||||
|
Subroutine
log_rotate: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | my $logs_compressed = 0; | |||||||||||||||||||||||||||
| 193 | 0 | my $logs_deleted = 0; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | $self->echo("Looking for logs to rotate in path: [" . $self->log_path . "]"); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | opendir(LOG_DIR, $self->log_path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | || confess("\n- Error, can't open log directory for reading: [" . $self->log_path . "]\n- Cause: $!\n"); | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | while (my $log_file = readdir(LOG_DIR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Only looking for files that match log pattern: | ||||||||||||||||||||||||||||
| 203 | 0 | my $_log_pattern_match = $self->_log_pattern . "\$"; | |||||||||||||||||||||||||||
| 204 | 0 | my $zip_pattern_match = $self->_log_pattern . ".zip\$"; | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | next if ($log_file !~ /$_log_pattern_match/i && $log_file !~ /$zip_pattern_match/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$log_file =~ /$_log_pattern_match/i or $log_file =~ /$zip_pattern_match/i
MC/DC0%0/2
$log_file =~ /$_log_pattern_match/i or $log_file =~ /$zip_pattern_match/i
$log_file =~ /$_log_pattern_match/i
$log_file =~ /$zip_pattern_match/i
Truth table0%0/3
A or B
A= $log_file =~ /$_log_pattern_match/iB= $log_file =~ /$zip_pattern_match/i
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | my $fullpath_log_file; | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | if ($self->log_path eq '.') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | $fullpath_log_file = $log_file; | |||||||||||||||||||||||||||
| 210 | } else { | ||||||||||||||||||||||||||||
| 211 | 0 | $fullpath_log_file = File::Spec->catfile($self->log_path, $log_file); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # And definately skip the current log file we are using: | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if(File::Spec->canonpath($self->logger->getLogFileName()) eq $fullpath_log_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | $self->echo("Skipping current log file (in use): [" . $fullpath_log_file . "]", 10); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # Get stat info about the log file: | ||||||||||||||||||||||||||||
| 220 | 0 | my @file_info = stat($fullpath_log_file); | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # Current epoch time: | ||||||||||||||||||||||||||||
| 223 | 0 | my $now_epoch = time(); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | my $log_file_age = (($now_epoch - $file_info[10]) / 86400); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # If log_zip_after_days is 0 don't do zipping: | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | unless ($self->log_zip_after_days == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ($log_file_age >= $self->log_zip_after_days && $log_file !~ /$zip_pattern_match/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$log_file_age >= $self->log_zip_after_days and not $log_file =~ /$zip_pattern_match/i
MC/DC0%0/2
$log_file_age >= $self->log_zip_after_days and not $log_file =~ /$zip_pattern_match/i
$log_file_age >= $self->log_zip_after_days
not $log_file =~ /$zip_pattern_match/i
Truth table0%0/3
A and not(B)
A= $log_file_age >= $self->log_zip_after_daysB= not $log_file =~ /$zip_pattern_match/i
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | $self->echo("Found log file to compress: [" . $fullpath_log_file . "] Age: [" . $log_file_age . "] days old"); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | my $zip_file = $fullpath_log_file . '.zip'; | |||||||||||||||||||||||||||
| 234 | 0 | $self->echo("Compressing: [" . $fullpath_log_file . "] As: [" . $zip_file . "]"); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # Create a zip file object: | ||||||||||||||||||||||||||||
| 237 | 0 | my $zip = Archive::Zip->new(); | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Add the log file from disk: | ||||||||||||||||||||||||||||
| 240 | 0 | my $file_member = $zip->addFile($fullpath_log_file); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # Save the zip file: | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | unless ($zip->writeToFileNamed($zip_file) == AZ_OK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 0 | confess("\n- Error, can't write zip file: [" . $zip_file . "]\n- Cause: $!\n"); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | $logs_compressed++; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Reomove the log file that has been compressed: | ||||||||||||||||||||||||||||
| 250 | 0 | unlink($fullpath_log_file); | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 0 | $fullpath_log_file = $zip_file; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # If log_rm_after_days is 0 don't do deletions: | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | unless ($self->log_rm_after_days == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | if ($log_file_age >= $self->log_rm_after_days) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | $self->echo("Found log file to remove: [" . $fullpath_log_file . "] Age: [" . $log_file_age . "] days old"); | |||||||||||||||||||||||||||
| 262 | 0 | $self->echo("Deleting: [" . $fullpath_log_file . "]"); | |||||||||||||||||||||||||||
| 263 | 0 | unlink($fullpath_log_file); | |||||||||||||||||||||||||||
| 264 | 0 | $logs_deleted++; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | $self->echo("Total logs compressed: [" . $logs_compressed . "]"); | |||||||||||||||||||||||||||
| 272 | 0 | $self->echo("Total logs deleted: [" . $logs_deleted . "]"); | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | return $self; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | ################################################################################ | ||||||||||||||||||||||||||||
| 278 | # Desstructor: | ||||||||||||||||||||||||||||
| 279 | ################################################################################ | ||||||||||||||||||||||||||||
| 280 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=10.5
| |||||||||||||||||||||||||||||
| 281 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 1 | $self->unlocker(); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # End benchmark timing: | ||||||||||||||||||||||||||||
| 286 | 1 | $self->_bench_end(new Benchmark); | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # Calculate execution time: | ||||||||||||||||||||||||||||
| 289 | 1 | my $diff = timediff($self->_bench_end, $self->_bench_start); | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # Benchmark report: | ||||||||||||||||||||||||||||
| 292 | 0 | $self->echo("Benchmark report: " . timestr($diff, 'all')); | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | $self->echo("Shutting down script: [" . $self->_parent . "] grind clunk"); | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Close log file if required: | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | unless ($self->nolog) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $self->logger->close(); | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | ################################################################################ | ||||||||||||||||||||||||||||
| 302 | 1 | 1; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | __END__ | ||||||||||||||||||||||||||||