# This script renames log files if they are big enough.
# Compresses or removes older log files.
#
-# Copyright (C) 2015, 2016, 2023 Balthasar Szczepański
+# Copyright (C) 2015, 2016, 2023, 2024 Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# TODO: use a real log rotate instead of this tool?
+
+use strict;
+use utf8;
+use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
+###PERL_LIB: use lib /botm/lib/bsta
+use botm_common (
+ 'join_path',
+ 'system_encoded',
+ 'opendir_encoded', 'readdir_decoded',
+ '_x_encoded', 'stat_encoded',
+ 'unlink_encoded', 'rename_encoded'
+);
+
+###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
###PERL_GZIP: GZIP = gzip
###PERL_LOG_PATH: LOG_PATH = /botm/log/bsta/
###PERL_LOGS_UNCOMPRESSED: LOGS_UNCOMPRESSED = 2
###PERL_LOGS_TOTAL: LOGS_TOTAL = 10
+binmode STDIN, ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+decode_argv();
-if ($ARGV[0] ne '') {
- $log_path = $ARGV[0];
-}
-else {
- $log_path = LOG_PATH;
-}
-if ($ARGV[1] =~ /^([0-9]+)$/) {
- $log_size_limit = $1;
-}
-else {
- $log_size_limit = LOG_SIZE_LIMIT;
-}
-if ($ARGV[2] =~ /^([0-9]+)$/) {
- $logs_total = $1;
-}
-else {
- $logs_total = LOGS_TOTAL;
-}
-if ($ARGV[3] =~ /^([0-9]+)$/) {
- $logs_uncompressed = $1;
-}
-else {
- $logs_uncompressed = LOGS_UNCOMPRESSED;
-}
+my $log_path = ($ARGV[0] ne '' ) ? $ARGV[0]: LOG_PATH();
+my $log_size_limit = ($ARGV[1] =~ /^[0-9]+$/) ? int($&) : LOG_SIZE_LIMIT();
+my $logs_total = ($ARGV[2] =~ /^[0-9]+$/) ? int($&) : LOGS_TOTAL();
+my $logs_uncompressed = ($ARGV[3] =~ /^[0-9]+$/) ? int($&) : LOGS_UNCOMPRESSED();
-if ( opendir ($dir, $log_path)) {
- while ($subpath = readdir $dir) {
- if ($subpath !~ /\.log$/) {
+if (opendir_encoded(my $dir, $log_path)) {
+ while (defined (my $file_name = readdir_decoded($dir))) {
+ if ($file_name !~ /\.log$/) {
next;
}
- $fullpath=$log_path.$subpath;
- unless (-f $fullpath) {
+ my $full_path = join_path(PATH_SEPARATOR(), $log_path, $file_name);
+ unless (_x_encoded('-f', $full_path)) {
next;
}
- unless (@stat = stat($fullpath)) {
+ my @stat;
+ unless (@stat = stat_encoded($full_path)) {
next;
}
if ($stat[7] > $log_size_limit) {
- movelog($fullpath,0,0);
+ move_log($full_path, 0, 0);
}
-
}
closedir($dir);
}
+else {
+ print "fail ";
+ print $log_path;
+ print "\n";
+}
-sub movelog {
+sub move_log {
(my $path, my $number, my $gz) = @_;
- my $nextgz = 0;
- my $thispath;
- my $nextpath;
- my $nextnumber=$number+1;
- my @gzip_arg = (GZIP, '-q', '-9','-f');
+ my $next_gz = 0;
+ my $this_path;
+ my $next_path;
+ my $next_number = $number + 1;
+ my @gzip_arg = (GZIP(), '-q', '-9','-f');
- $thispath = $path.(($number != 0)?'.'.$number.($gz?'.gz':''):'');
+ $this_path = $path.(
+ ($number != 0) ?
+ '.'.$number.($gz ? '.gz' : '') :
+ ''
+ );
if ($number == $logs_total) {
- if (unlink $thispath) {
+ if (unlink_encoded($this_path)) {
return 1;
}
else {
}
}
if ($number == $logs_uncompressed) {
- $nextgz=1;
- $nextpath = $path.'.'.$nextnumber.'.gz';
+ $next_gz = 1;
+ $next_path = $path.'.'.$next_number.'.gz';
}
else {
- $nextpath = $path.'.'.$nextnumber.($gz?'.gz':'');
+ $next_path = $path.'.'.$next_number.($gz ? '.gz' : '');
}
- if (-e $nextpath) {
- unless (movelog($path,$nextnumber,($nextgz or $gz)?1:0)) {
+ # TODO: consider loop instead of recursion?
+ if (_x_encoded('-e', $next_path)) {
+ unless (move_log($path, $next_number, ($next_gz or $gz) ? 1 : 0)) {
return 0;
}
}
- if ($nextgz) {
- push @gzip_arg, $thispath;
- unless (! system (@gzip_arg)) {
+ if ($next_gz) {
+ push @gzip_arg, $this_path;
+ unless (! system_encoded(GZIP(), @gzip_arg)) {
return 0;
}
- $thispath .= '.gz';
+ $this_path .= '.gz';
}
- unless (rename ($thispath, $nextpath)) {
+ unless (rename_encoded($this_path, $next_path)) {
return 0;
}
return 1;