From b5f50dcb17471bff7af343cb2555c47147d0783e Mon Sep 17 00:00:00 2001 From: Will Budic Date: Thu, 13 Nov 2025 10:40:18 +1100 Subject: [PATCH] Loggin timezone file name resolve bettered. --- .gitignore | 3 +- system/modules/CNF.pm | 68 +++++++++++++++++++-------------- system/modules/CNFDateTime.pm | 4 +- tests/testCNFNodeShortiefs.pl | 7 +++- tests/testCNFParserLogging.pl | 15 +++++--- tests/test_CursesProgressBar.pl | 11 ++++++ 6 files changed, 69 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 72bd400..89d2050 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,4 @@ rss_* tree_* vscode_local_extensions/extensions.json sudo_apt.pl.cnf.dat -zzz_temp.log -apps/sql.log +*.log diff --git a/system/modules/CNF.pm b/system/modules/CNF.pm index 34ecc24..451ded7 100644 --- a/system/modules/CNF.pm +++ b/system/modules/CNF.pm @@ -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}); diff --git a/system/modules/CNFDateTime.pm b/system/modules/CNFDateTime.pm index 3b32232..7640038 100644 --- a/system/modules/CNFDateTime.pm +++ b/system/modules/CNFDateTime.pm @@ -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})){ diff --git a/tests/testCNFNodeShortiefs.pl b/tests/testCNFNodeShortiefs.pl index 76631e2..a5809a6 100644 --- a/tests/testCNFNodeShortiefs.pl +++ b/tests/testCNFNodeShortiefs.pl @@ -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; diff --git a/tests/testCNFParserLogging.pl b/tests/testCNFParserLogging.pl index 834a0f8..a4a5936 100644 --- a/tests/testCNFParserLogging.pl +++ b/tests/testCNFParserLogging.pl @@ -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 diff --git a/tests/test_CursesProgressBar.pl b/tests/test_CursesProgressBar.pl index fbaeeb7..ad24b87 100644 --- a/tests/test_CursesProgressBar.pl +++ b/tests/test_CursesProgressBar.pl @@ -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="" +>>> <<> }); +$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( -- 2.34.1