From: Will Budic Date: Mon, 10 Nov 2025 06:50:54 +0000 (+1100) Subject: CNF base package now made, logging revisited. X-Git-Url: https://lifelog.hopto.org/gitweb/?a=commitdiff_plain;h=023221399b96f83542004b90f6cf397d92435dfd;p=PerlCNF.git CNF base package now made, logging revisited. --- diff --git a/system/modules/CNF.pm b/system/modules/CNF.pm index 72214ee..34ecc24 100644 --- a/system/modules/CNF.pm +++ b/system/modules/CNF.pm @@ -26,6 +26,14 @@ sub _isTrue{ return 0 if(not $value); return ($value =~ /1|true|yes|on|da/i) ? 1:0 } +## +# Return actual cnf file stats and state. +## +sub _fetchScriptStat{ + my $cnf_file = shift; + my @stat = stat($cnf_file); + return \@stat; +} my $LOG_TRIM_SUB; my $LOG_TAIL_COUNT = 0; @@ -65,54 +73,55 @@ sub log { if(%log && _isTrue($log{enabled}) && $message){ if(!$LOG_FILE){ my $dir = $log{directory}; $dir = '.' if not $dir; $dir .= '/' if $dir !~ /\/$/; - my $log = $log{file}; - $LOG_TAIL_COUNT = $log{tail}; $LOG_TAIL_COUNT = 0 if not $LOG_TAIL_COUNT; - $LOG_FILE = $dir.$log; + my $log = $log{file}; if(not $log){ warn "Missing log file name in %LOG settings."; return $time . " " .$message } + $LOG_TAIL_COUNT = $log{tail}; $LOG_TAIL_COUNT = 0 if not $LOG_TAIL_COUNT; + $LOG_FILE = $dir.$log; mkdir $dir if not ( -e $dir and -d $dir); } - open (my $fh, ">>", $LOG_FILE) or die $!; - print $fh $time . " - " . $message ."\n"; - close $fh; - - if($LOG_TAIL_COUNT>0){ + if($LOG_FILE){ # now available do it + open (my $fh, ">>", $LOG_FILE) or die $!; + print $fh $time . " - " . $message ."\n"; + close $fh; + if($LOG_TAIL_COUNT>0){ #2025-11-10 Mayor rewrite since v.3.3.6 Previous version was buffering and sorting unnecessary, slowing performance. - if($LOG_CURRENT_LINE_CNT == 0 && !$LOG_TRIM_SUB){ - #This section scope is only called once. - $fh = File::ReadBackwards->new($LOG_FILE) or die $!; - ++ $LOG_CURRENT_LINE_CNT while($fh->readline); - ### - $LOG_TRIM_SUB = sub { - ## - my $tmpFile = "/tmp/".$log{file}; - my $fh = File::ReadBackwards->new($LOG_FILE) or die $!; - my $cut = $LOG_CURRENT_LINE_CNT - $LOG_TAIL_COUNT; - open (my $fhTemp, ">", $tmpFile) or die $!; - for (1..$cut){ - print $fhTemp $fh->readline() - } - close $fhTemp; - - open (my $fhLog, ">", $LOG_FILE) or die $!; - $fh = File::ReadBackwards->new($tmpFile) or die $!; - for (1..$cut){ - my $line = $fh->readline(); - print $fhLog $line; - } - close $fhLog; - $LOG_CURRENT_LINE_CNT = $cut; - unlink $tmpFile - } - }else{ - $LOG_CURRENT_LINE_CNT++; - } - if($LOG_CURRENT_LINE_CNT > $LOG_TAIL_COUNT+1){ - $LOG_TRIM_SUB->(); + if($LOG_CURRENT_LINE_CNT == 0 && !$LOG_TRIM_SUB){ + #This section scope is only called once. + $fh = File::ReadBackwards->new($LOG_FILE) or die $!; + ++ $LOG_CURRENT_LINE_CNT while($fh->readline); + ### + $LOG_TRIM_SUB = sub { + ## + my $tmpFile = "/tmp/".$log{file}; + my $fh = File::ReadBackwards->new($LOG_FILE) or die $!; + my $cut = $LOG_CURRENT_LINE_CNT - $LOG_TAIL_COUNT; + open (my $fhTemp, ">", $tmpFile) or die $!; + for (1..$cut){ + print $fhTemp $fh->readline() + } + close $fhTemp; + + open (my $fhLog, ">", $LOG_FILE) or die $!; + $fh = File::ReadBackwards->new($tmpFile) or die $!; + for (1..$cut){ + my $line = $fh->readline(); + print $fhLog $line; + } + close $fhLog; + $LOG_CURRENT_LINE_CNT = $cut; + unlink $tmpFile } + }else{ + $LOG_CURRENT_LINE_CNT++; } + if($LOG_CURRENT_LINE_CNT > $LOG_TAIL_COUNT+1){ + $LOG_TRIM_SUB->(); + } + } + } } return $time . " " .$message; }