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;
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
}
}
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.
##
}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});
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{
###
$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();
#
$test -> doneFailed();
}
+`rm $logfile` if -f $logfile;
+
#
# TESTING ANY POSSIBLE SUBS ARE FOLLOWING FROM HERE #
#
\ No newline at end of file
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
.config/Code/logs/*
>>
});
+$cnf->log("$0 Activated.");
if( not $cnf -> constantsRegistryCheck(qw(
TZ target
postfix
$cui->setprogress($cnt++);
$action->text($file->{path});
+ $cnf->log( $file->{path} );
usleep(1000);
if($stop){
$yes = $cui->dialog(