]> lifelog.hopto.org Git - PerlCNF.git/commitdiff
CNF base package now made, logging revisited.
authorWill Budic <redacted>
Mon, 10 Nov 2025 06:50:54 +0000 (17:50 +1100)
committerWill Budic <redacted>
Mon, 10 Nov 2025 06:50:54 +0000 (17:50 +1100)
system/modules/CNF.pm

index 72214ee87d504dae89a8c9d4f87d6518e5424859..34ecc247796e91395eff9305ec6e7ede60fb3c3c 100644 (file)
@@ -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;
 }