LOGGER: enhancements

This commit is contained in:
XargonWan 2024-01-05 10:59:18 +01:00
parent 878e3e41ca
commit 90cf770298
2 changed files with 31 additions and 3 deletions

View file

@ -156,6 +156,7 @@ if [[ ! -f "$rd_conf" ]]; then
log i "RetroDECK config file initialized. Contents:\n" log i "RetroDECK config file initialized. Contents:\n"
log i "$(cat "$rd_conf")" log i "$(cat "$rd_conf")"
conf_read # Load new variables into memory conf_read # Load new variables into memory
tmplog_merger
# If the config file is existing i just read the variables # If the config file is existing i just read the variables
else else
@ -169,6 +170,7 @@ else
fi fi
conf_read conf_read
tmplog_merger
# Verify rdhome is where it is supposed to be. # Verify rdhome is where it is supposed to be.
if [[ ! -d $rdhome ]]; then if [[ ! -d $rdhome ]]; then
@ -177,6 +179,7 @@ else
new_home_path=$(directory_browse "RetroDECK folder location") new_home_path=$(directory_browse "RetroDECK folder location")
set_setting_value $rd_conf "rdhome" "$new_home_path" retrodeck "paths" set_setting_value $rd_conf "rdhome" "$new_home_path" retrodeck "paths"
conf_read conf_read
tmplog_merger
prepare_emulator "retrodeck" "postmove" prepare_emulator "retrodeck" "postmove"
prepare_emulator "all" "postmove" prepare_emulator "all" "postmove"
conf_write conf_write

View file

@ -7,12 +7,20 @@
# log e "bar" -> logs an error with message bar in the default log file retrodeck/logs/retrodeck.log # log e "bar" -> logs an error with message bar in the default log file retrodeck/logs/retrodeck.log
# log i "par" rekku.log -> logs an information with message in the specified log file inside the logs folder retrodeck/logs/rekku.log # log i "par" rekku.log -> logs an information with message in the specified log file inside the logs folder retrodeck/logs/rekku.log
exec > >(tee -a "$logs_folder/retrodeck.log") 2>&1 if [ "${log_init:-false}" = false ]; then
logs_folder=${logs_folder:-"/tmp"}
touch "$logs_folder/retrodeck.log"
# exec > >(tee "$logs_folder/retrodeck.log") 2>&1 # this is broken, creates strange artifacts and corrupts the log file
log_init=true
fi
log() { log() {
# exec > >(tee "$logs_folder/retrodeck.log") 2>&1 # this is broken, creates strange artifacts and corrupts the log file
local level="$1" local level="$1"
local message="$2" local message="$2"
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S])" local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])"
local colorize_terminal local colorize_terminal
# Use specified logfile or default to retrodeck.log # Use specified logfile or default to retrodeck.log
@ -89,4 +97,21 @@ log() {
# Write the log message to the log file # Write the log message to the log file
echo "$log_message" >> "$logfile" echo "$log_message" >> "$logfile"
} }
# This function is merging the temporary log file into the actual one
tmplog_merger() {
# Check if /tmp/retrodeck.log exists
if [ -e "/tmp/retrodeck.log" ]; then
# Sort both temporary and existing log files by timestamp
sort -k1,1n -k2,2M -k3,3n -k4,4n -k5,5n "/tmp/retrodeck.log" "$logs_folder/retrodeck.log" > "$logs_folder/merged_logs.tmp"
# Move the merged logs to replace the original log file
mv "$logs_folder/merged_logs.tmp" "$logs_folder/retrodeck.log"
# Remove the temporary file
rm "/tmp/retrodeck.log"
fi
}