]> lifelog.hopto.org Git - PerlCNF.git/commitdiff
Loggin timezone file name resolve bettered.
authorWill Budic <redacted>
Wed, 12 Nov 2025 23:40:18 +0000 (10:40 +1100)
committerWill Budic <redacted>
Wed, 12 Nov 2025 23:40:18 +0000 (10:40 +1100)
.gitignore
system/modules/CNF.pm
system/modules/CNFDateTime.pm
tests/testCNFNodeShortiefs.pl
tests/testCNFParserLogging.pl
tests/test_CursesProgressBar.pl

index 72bd40066af680f6912df467b6e43fb10bddc6b7..89d2050656891a70c9adcc344f11acaf752aceb4 100644 (file)
@@ -15,5 +15,4 @@ rss_*
 tree_*
 vscode_local_extensions/extensions.json
 sudo_apt.pl.cnf.dat
-zzz_temp.log
-apps/sql.log
+*.log
index 34ecc247796e91395eff9305ec6e7ede60fb3c3c..451ded7199f9454b7e60de405fbf8d5b59989002 100644 (file)
@@ -59,7 +59,8 @@ sub log {
     my $isWarning = $type eq 'WARNG';
     my $attach  = join @_; $message .= $attach if $attach;
     my %log = $self -> property('%LOG');
-    my $time =  CNFDateTime -> now(exists($self->{TZ})?{TZ=>$self->{TZ}}:undef) -> toTimestamp();
+    my $TZ =  %log && exists $log{TZ} ? $log{TZ} : exists $self->{TZ} ? $self->{TZ}: undef;
+    my $time =  CNFDateTime -> now( $TZ) -> toTimestamp();
 
     $message = "$type "."\e[33m".$message."\e[0m" if $isWarning;
     $message = "" if not $message;
@@ -73,44 +74,45 @@ 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};            
+            my $log               = $log{file}; $log .= '.log' if $log && $log !~ /\.log$/;
             if(not $log){
                 warn "Missing log file name in %LOG settings.";
-                return $time . " " .$message 
+                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);
+            $LOG_FILE             =  ensureDir($dir.$log);
         }
         if($LOG_FILE){ # now available do it
-        open (my $fh, ">>", $LOG_FILE) or die $!;
+        open (my $fh, ">>", $LOG_FILE) or die $! . '->'. $LOG_FILE;
             print $fh $time . " - " . $message ."\n";
-        close $fh;           
+        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 $!;                    
+                $fh = File::ReadBackwards->new($LOG_FILE) or die $! . '->'. $LOG_FILE;
                 ++ $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 $!;
+                return if not -f $LOG_FILE; # Bail out, something maybe deleted it?
+                    my $log     = $log{file}; $log .= '.log' if $log !~ /\.log$/;
+                    my $tmpFile = ensureDir("/tmp/".$log);
+                    my $fh = File::ReadBackwards->new($LOG_FILE) or die $! . '->'. $LOG_FILE;
                     my $cut =  $LOG_CURRENT_LINE_CNT - $LOG_TAIL_COUNT;
-                    open (my $fhTemp, ">", $tmpFile) or die $!;
+                    open (my $fhTemp, ">", $tmpFile) or die $! . '->'. $tmpFile;
                         for (1..$cut){
                             print $fhTemp $fh->readline()
                         }
-                    close $fhTemp;
-                    
+                    close $fhTemp; $fh->close();
+
                     open (my $fhLog, ">", $LOG_FILE) or die $!;
-                        $fh = File::ReadBackwards->new($tmpFile) or die $!;
+                        $fh = File::ReadBackwards->new($tmpFile) or die $! . '->'. $tmpFile;
                         for (1..$cut){
-                            my $line = $fh->readline();                                
-                            print $fhLog $line;                            
+                            my $line = $fh->readline();
+                            print $fhLog $line;
                         }
-                    close $fhLog;
+                    close $fhLog; $fh->close();
                     $LOG_CURRENT_LINE_CNT = $cut;
                     unlink $tmpFile
                 }
@@ -125,19 +127,24 @@ sub log {
     }
     return $time . " " .$message;
 }
+sub ensureDir { my $file = shift;
+    if($file){
+       my @dirs = split '/', $file;
+       if(pop @dirs){
+         my $path;
+          foreach(@dirs){
+            $path .= $_ . '/';
+            if(not -d $path){
+               mkdir $path
+            }
+          }
+       }
+    }
+    return $file;
+}
 
-# my $LOG_TRIM_SUB = sub {
-#                         my $fh = File::ReadBackwards->new($LOG_FILE) or die $!;
-#                         my @buffer; $buffer[@buffer] = $fh->readline() for (1..$LOG_CURRENT_LINE_CNT);
-#                         open (my $fhTemp, ">", "/tmp/".$log{file}) or die $!;
-#                                 foreach my $line(reverse @buffer){print $fhTemp $line if $line}
-#                         close $fhTemp;
-#                         move("/tmp/".$log{file}, $LOG_FILE);
-#                         $LOG_CURRENT_LINE_CNT = int(scalar($fh->{lines}));
-#                    };
 
 our @files;
-
 ##
 # Load CNF DATA file.
 ##
@@ -239,10 +246,13 @@ sub localProjectConfigFile{
         }else{
             $project .= "/" if $project !~ /\/$/
         }
-   $name  =~ m/.*\/(.*)\..*$/ ; $self->{CNF_SCRIPT_NAME} = $1; #<- protected access.
-   $name = "$1.cnf";
+   $name =  _localScriptFileName($name); $self->{CNF_SCRIPT_NAME} = $name; #<- protected access.
+   $name .= '.cnf';
    return $ENV{HOME}."/.config/$project$name"
 }
+sub _localScriptFileName{
+    my $name = shift; $name  =~ m/.*\/(.*)\..*$/;  return $1;
+}
 
 sub doLoadDataFile { my ($self,$e,$v)=@_;
         my ($path,$cnf_file) = ("",$self->{CNF_CONTENT});
index 3b32232f901ebc9d6bdff93a1c697ca791af1ce5..7640038cfe2069224a67a749e89ceb2a7f298149 100644 (file)
@@ -32,8 +32,8 @@ sub now {
     my %settings;
     $r = ref($presume) if !$r;
     if($r eq 'HASH') { %settings = %$presume }
-    elsif ($r eq '' ){ $settings{TZ} = $presume }
-    elsif( $presume =~ /TZ|tz/){ $settings{TZ} = $r}
+    elsif ($r eq '' ){ $settings{TZ} = DEFAULT_TIME_ZONE }
+    elsif( $presume =~ /TZ/i){ $settings{TZ} = $r}
     $settings{epoch} = time if not exists $settings{epoch};
     $settings{TZ}    = DEFAULT_TIME_ZONE if not defined $settings{TZ};
     if(not DateTime::TimeZone->is_valid_name($settings{TZ})){
index 76631e2712c2c722d67f1a2cd5a107b74c3db701..a5809a6f10149b32dc88ab198a9f9bbd0ee46424 100644 (file)
@@ -1,7 +1,12 @@
 #!/usr/bin/env perl
 use warnings; use strict;
 use feature 'say';
-use lib "system/modules";
+##
+# Disable bellow use lib::relative when debugging ->  "perl.perlInc"
+# if set hard linked to vscode project/workspace finds the right folder.
+##
+ use lib::relative (".","../system/modules");
+##
 
 require TestManager;
 require CNFParser;
index 834a0f8bba05a3de47f2895f70675e46b684d835..a4a5936e1fd7d2ea593631fd17a83a1ab818cc8a 100644 (file)
@@ -2,14 +2,18 @@
 use warnings; use strict;
 use Syntax::Keyword::Try;
 
-use lib::relative ('.','../system/modules');
+##
+# Disable bellow use lib::relative when debugging ->  "perl.perlInc"
+# if set hard linked to vscode project/workspace finds the right folder.
+##
+ use lib::relative (".","../system/modules");
+##
 
 require CNFParser;
 require TestManager;
 my $test = TestManager -> new($0);
 my $cnf;
-my $logfile = 'zzz_temp.log';
-
+my $logfile = 'tests/output/zzz_temp.log';
 
 try{
     ###
@@ -34,8 +38,7 @@ try{
         $cnt++;
     }
     close $fh;
-    $test -> evaluate("Is ten lines tailed?", ($cnt-11), 10);
-    `rm $logfile` if -f $logfile;
+    $test -> evaluate("Is ten lines tailed?", ($cnt-11), 10);    
     #
     $test->done();
     #
@@ -45,6 +48,8 @@ catch{
    $test -> doneFailed();
 }
 
+`rm $logfile` if -f $logfile;
+
 #
 #  TESTING ANY POSSIBLE SUBS ARE FOLLOWING FROM HERE  #
 #
\ No newline at end of file
index fbaeeb708537b18866ce972c2be50fbbb0a045d9..ad24b87b8be8f1b2a288069fa2dc7ccc04366fe9 100644 (file)
@@ -16,9 +16,18 @@ use Time::HiRes qw(usleep);
 use CNFParser; use CNFDateTime;
 use TestManager;
 my $test = TestManager -> new($0)-> unsuited();
+my $LOGFILE = CNF::_localScriptFileName($0) . 'log';
 my $cnf  = CNFParser-> blank() -> parseString(q{
+<<%<%LOG
+        enabled=1
+        file=tests/output/}.$LOGFILE.q{
+        tail=1000
+        # Log timezone here explicitly set, not to use the constant, bellow. Log settings is an property.
+        TZ=""
+>>>
 <<<CONST
     # UTC as global setting makes the file relevant globally to instead of one country.
+    # Its here we made into an constant.
     TZ=UTC
     target  =   /mnt/nuc/backups  #comments here now allowed.
     postfix  = testing
@@ -53,6 +62,7 @@ localperl
 .config/Code/logs/*
 >>
 });
+$cnf->log("$0 Activated.");
 if( not $cnf -> constantsRegistryCheck(qw(
         TZ target
         postfix        
@@ -221,6 +231,7 @@ try{
 
             $cui->setprogress($cnt++);
             $action->text($file->{path});
+            $cnf->log( $file->{path} );
             usleep(1000);
             if($stop){
                $yes = $cui->dialog(