LOGGER: added log rotation

This commit is contained in:
XargonWan 2024-11-30 22:39:30 +09:00
parent f71f61cbce
commit 5ffedc8160
3 changed files with 38 additions and 9 deletions

View file

@ -6,11 +6,16 @@
# now disabled as we are importing everything in /app/lib. In case we are breaking something we need to restore this approach
# export LD_LIBRARY_PATH="/app/retrodeck/lib:/app/retrodeck/lib/debug:/app/retrodeck/lib/pkgconfig:$LD_LIBRARY_PATH"
: "${logging_level:=info}" # Initializing the log level variable if not already valued, this will be actually red later from the config file
rd_logs_folder="/var/config/retrodeck/logs" # Static location to write all RetroDECK-related logs
source /app/libexec/logger.sh
rotate_logs
log i "Initializing RetroDECK"
source /app/libexec/050_save_migration.sh
source /app/libexec/checks.sh
source /app/libexec/compression.sh
source /app/libexec/dialogs.sh
source /app/libexec/logger.sh
source /app/libexec/other_functions.sh
source /app/libexec/multi_user.sh
source /app/libexec/framework.sh
@ -23,7 +28,6 @@ source /app/libexec/run_game.sh
# Static variables
rd_conf="/var/config/retrodeck/retrodeck.cfg" # RetroDECK config file path
rd_conf_backup="/var/config/retrodeck/retrodeck.bak" # Backup of RetroDECK config file from update
rd_logs_folder="/var/config/retrodeck/logs" # Static location to write all RetroDECK-related logs
config="/app/retrodeck/config" # folder with all the default emulator configs
rd_defaults="$config/retrodeck/retrodeck.cfg" # A default RetroDECK config file
rd_update_patch="/var/config/retrodeck/rd_update.patch" # A static location for the temporary patch file used during retrodeck.cfg updates
@ -55,7 +59,6 @@ main_repository_name="RetroDECK"
features="$config/retrodeck/reference_lists/features.json" # A file where all the RetroDECK and component capabilities are kept for querying
es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml" # ES-DE supported system list
es_find_rules="/app/share/es-de/resources/systems/linux/es_find_rules.xml" # ES-DE emulator find rules
logging_level="info" # Initializing this to avoid printing blank newlines, this will be actually red later from the config file
# Godot data transfer temp files
@ -139,9 +142,6 @@ mamedefconf="/var/config/mame/cfg/default.cfg"
if [ ! -d "$rd_logs_folder" ]; then
create_dir "$rd_logs_folder"
fi
if [[ ! -d "$rd_logs_folder/ES-DE" ]]; then
dir_prep "$rd_logs_folder/ES-DE" "$es_source_logs"
fi
# Initialize location of Godot temp data files, if it doesn't exist
if [[ ! -d "/var/config/retrodeck/godot" ]]; then

View file

@ -91,9 +91,40 @@ log() {
# Write the log message to the log file
if [ ! -f "$logfile" ]; then
echo "$timestamp [WARN] Log file not found in \"$logfile\", creating it" >&2
#echo "$timestamp [WARN] Log file not found in \"$logfile\", creating it" >&2 # Disabled it as it's always appearing because of log rotation
touch "$logfile"
fi
echo "$log_message" >> "$logfile"
fi
}
# The rotate_logs function manages log file rotation to limit the number of logs retained.
# It compresses the current log file into a .tar.gz archive, increments the version of
# older log files (e.g., retrodeck.1.tar.gz to retrodeck.2.tar.gz), and deletes the oldest
# archive if it exceeds the maximum limit (default: 3 rotated logs). After rotation,
# the original log file is cleared for continued logging.
rotate_logs() {
local logfile="${1:-$rd_logs_folder/retrodeck.log}" # Default log file
local max_logs=3 # Maximum number of rotated logs to keep
# Rotate existing logs
for ((i=max_logs; i>0; i--)); do
if [[ -f "${logfile}.${i}.tar.gz" ]]; then
if (( i == max_logs )); then
# Remove the oldest log if it exceeds the limit
rm -f "${logfile}.${i}.tar.gz"
else
# Rename log file to the next number
mv "${logfile}.${i}.tar.gz" "${logfile}.$((i+1)).tar.gz"
fi
fi
done
# Compress the current log file if it exists
if [[ -f "$logfile" ]]; then
# Compress without directory structure and suppress tar output
tar -czf "${logfile}.1.tar.gz" -C "$(dirname "$logfile")" "$(basename "$logfile")" --remove-files &>/dev/null
fi
}

View file

@ -67,9 +67,7 @@ prepare_component() {
dir_prep "$rdhome/ES-DE/gamelists" "/var/config/ES-DE/gamelists"
dir_prep "$rdhome/ES-DE/collections" "/var/config/ES-DE/collections"
dir_prep "$rdhome/ES-DE/custom_systems" "/var/config/ES-DE/custom_systems"
#dir_prep "$rd_logs_folder/ES-DE" "$es_source_logs"
log d "Generating roms system folders"
#es-de --home /var/config/ES-DE --create-system-dirs
es-de --create-system-dirs
update_splashscreens
fi