Support automatically removing old log files after a number of days

This commit is contained in:
HummyPkg 2019-01-17 12:13:06 +00:00
parent 06188094bf
commit da865f3e85
4 changed files with 24 additions and 3 deletions

View File

@ -30,6 +30,7 @@ set pkgdev [$settings pkgdev]
set rtschedule [$settings rtschedule]
set logsize [$settings logsize]
set logkeep [$settings logkeep]
set logage [$settings logage]
set cryptokey [system encryptionkey]
if {![catch {set ck_fd [open "/mod/boot/cryptokey"]}]} {
@ -45,4 +46,5 @@ handle_int_update pkgdev $pkgdev "Development Package Display"
handle_int_update rtschedule $rtschedule "Real-time scheduling"
handle_int_update logsize $logsize "Log rotation size"
handle_int_update logkeep $logkeep "Logs to keep"
handle_int_update logage $logage "Rotated log max age"

View File

@ -41,6 +41,9 @@ puts "
setting_number logkeep "How many rotated logs to keep" $logkeep \
1 100
setting_number logage "Delete old logs after X days" $logage \
0 365
if {[system modversion 1] >= 214} {
setting_toggle "Disable telnet menu?" "xtelnet" \
[file exists /mod/boot/xtelnet] 0 1

View File

@ -6,18 +6,19 @@ require settings.class
set s [settings]
set logsize [$s logsize]
set logkeep [$s logkeep]
set logage [$s logage]
puts "Rotating at $logsize, keeping $logkeep"
puts "Rotating at $logsize, keeping $logkeep, max age $logage"
set logdir "/mod/tmp"
set baselogs {}
puts "\n-- Rotate\n"
set tstamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
foreach log [glob -nocomplain -tail -directory $logdir *.log] {
puts "\[$log\]"
set tstamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
# Check for old-style rotated log.
if {[string match {*_old.log} $log]} {
@ -30,6 +31,15 @@ foreach log [glob -nocomplain -tail -directory $logdir *.log] {
# Check for already rotated log.
if {[regexp -- {\.[0-9]{14}\.log$} $log]} {
puts " already rotated."
if {$logage > 0} {
set days $(
([clock seconds] - [file mtime $logdir/$log])
/ 86400)
if {$days > $logage} {
file tdelete "$logdir/$log"
puts " Deleting old log $log (age=$days)"
}
}
continue
}

View File

@ -49,6 +49,7 @@ class settings {
audiomp3 0
logsize 1048576
logkeep 2
logage 0
rtschedule 0
}
@ -414,6 +415,11 @@ settings method logkeep {{num -1}} {
return $val
}
settings method logage {{num -1}} {
set val [$self _nval_setting logage $num]
return $val
}
settings method dedup_template {{pattern -1}} {
set val [$self _tval_setting dedup_template $pattern]
if {$val eq "0"} { set val "s%se%E - " }