Cleanup logging functionality

This commit is contained in:
icenine451 2024-03-22 17:10:26 -04:00
parent ff448e03b3
commit 590b7bcfa9
3 changed files with 88 additions and 140 deletions

View file

@ -17,6 +17,7 @@ source /app/libexec/presets.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
emuconfigs="/app/retrodeck/emu-configs" # folder with all the default emulator configs
rd_defaults="$emuconfigs/defaults/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
@ -65,6 +66,7 @@ citraconf="/var/config/citra-emu/qt-config.ini"
export ESDE_APPDATA_DIR="/var/config/ES-DE"
es_settings="/var/config/ES-DE/settings/es_settings.xml"
es_source_logs="/var/config/ES-DE/logs"
# RetroArch config files
@ -121,6 +123,14 @@ mameconf="/var/config/mame/ini/mame.ini"
mameuiconf="/var/config/mame/ini/ui.ini"
mamedefconf="/var/config/mame/cfg/default.cfg"
# Initialize logging location if it doesn't exist, before anything else happens
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
# We moved the lockfile in /var/config/retrodeck in order to solve issue #53 - Remove in a few versions
if [[ -f "$HOME/retrodeck/.lock" ]]; then
mv "$HOME/retrodeck/.lock" $lockfile

View file

@ -16,150 +16,87 @@
# 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
# This function is merging the temporary log file into the actual one
# tmplog_merger() {
# log d "Starting log merger function"
# create_dir "$rd_logs_folder"
# # Check if /tmp/rdlogs/retrodeck.log exists
# if [ -e "/tmp/rdlogs/retrodeck.log" ] && [ -e "$rd_logs_folder/retrodeck.log" ]; then
# # Sort both temporary and existing log files by timestamp
# #sort -k1,1n -k2,2M -k3,3n -k4,4n -k5,5n "/tmp/rdlogs/retrodeck.log" "$rd_logs_folder/retrodeck.log" > "$rd_logs_folder/merged_logs.tmp"
# # Move the merged logs to replace the original log file
# #mv "$rd_logs_folder/merged_logs.tmp" "$rd_logs_folder/retrodeck.log"
# mv "/tmp/rdlogs/retrodeck.log" "$rd_logs_folder/retrodeck.log"
# # Remove the temporary folder
# rm -rf "/tmp/rdlogs"
# fi
# local ESDE_source_logs="/var/config/ES-DE/logs"
# # Check if the source file exists
# if [ -e "$ESDE_source_logs" ]; then
# # Create the symlink in the logs folder
# ln -sf "$ESDE_source_logs" "$rd_logs_folder/ES-DE"
# log i "ES-DE log folder linked to \"$rd_logs_folder/ES-DE\""
# fi
# }
log() {
# exec > >(tee "$logs_folder/retrodeck.log") 2>&1 # this is broken, creates strange artifacts and corrupts the log file
local level="$1"
local message="$2"
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])"
local colorize_terminal
local level="$1"
local message="$2"
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])"
local colorize_terminal
# Use specified logfile or default to retrodeck.log
local logfile
if [ -n "$3" ]; then
logfile="$3"
else
logfile="$rd_logs_folder/retrodeck.log"
fi
if [[ ! -f "$logfile" ]]; then
touch "$logfile"
echo "$timestamp [WARN] \"$logfile\" not found, creating."
fi
# Use specified logfile or default to retrodeck.log
local logfile
if [ -n "$3" ]; then
logfile="$3"
else
if [ -z $rd_logs_folder ]; then
# echo "Logger: case 1, rd_logs_folder not found, rd_logs_folder=$rd_logs_folder" # TODO: Debug, delete me
rd_logs_folder="/tmp/rdlogs"
create_dir "$rd_logs_folder"
fi
if [ ! -z $rdhome ]; then
# echo "Logger: case 2, rdhome is found, rdhome=$rdhome" # TODO: Debug, delete me
rd_logs_folder="$(get_setting_value "$rd_conf" "logs_folder" "retrodeck" "paths")"
mkdir -p "$rd_logs_folder"
# echo "Logger: case 2, rdhome is found, rd_logs_folder=$rd_logs_folder" # TODO: Debug, delete me
logfile="$rd_logs_folder/retrodeck.log"
touch "$logfile"
local ESDE_source_logs="/var/config/ES-DE/logs"
# Check if the source file exists
if [ -e "$ESDE_source_logs" ] && [ ! -d "$rd_logs_folder/ES-DE" ]; then
# Create the symlink in the logs folder
# echo "Logger: case 2, symlinking \"$ESDE_source_logs\" in \"$rd_logs_folder/ES-DE\"" # TODO: Debug, delete me
ln -sf "$ESDE_source_logs" "$rd_logs_folder/ES-DE"
ln -sf "$HOME/.var/app/net.retrodeck.retrodeck/config/ES-DE/logs" "$rd_logs_folder/ES-DE-outflatpak" # TODO: think a smarter way
fi
else
# echo "Logger: case 3" # TODO: Debug, delete me
logfile="/tmp/rdlogs/retrodeck.log"
echo "$timestamp [WARN] retrodeck folder not found, temporary writing logs in \"$logfile\""
fi
# Check if the shell is sh (not bash or zsh) to avoid colorization
if [ "${SHELL##*/}" = "sh" ]; then
colorize_terminal=false
else
colorize_terminal=true
fi
if [ -z $rdhome ] && [ -d "/tmp/rdlogs" ]; then
# echo "Logger: case 4, rdhome is found, rdhome=$rdhome, and /tmp/rdlogs is found as well" # TODO: Debug, delete me
# echo "Logger: case 4, creating the acutal log dir in $rd_logs_folder" # TODO: Debug, delete me
mkdir -p "$rd_logs_folder"
# echo "Logger: case 4, moving \"/tmp/rdlogs/retrodeck.log\" in \"$rd_logs_folder/retrodeck.log\"" # TODO: Debug, delete me
mv "/tmp/rdlogs/retrodeck.log" "$rd_logs_folder/retrodeck.log"
rm -rf "/tmp/rdlogs"
# echo "Logger: deleting /tmp/rdlogs" # TODO: Debug, delete me
fi
fi
case "$level" in
w)
if [ "$colorize_terminal" = true ]; then
# Warning (yellow) for terminal
colored_message="\e[33m[WARN] $message\e[0m"
else
# Warning (no color for sh) for terminal
colored_message="$timestamp [WARN] $message"
fi
# Write to log file without colorization
log_message="$timestamp [WARN] $message"
;;
e)
if [ "$colorize_terminal" = true ]; then
# Error (red) for terminal
colored_message="\e[31m[ERROR] $message\e[0m"
else
# Error (no color for sh) for terminal
colored_message="$timestamp [ERROR] $message"
fi
# Write to log file without colorization
log_message="$timestamp [ERROR] $message"
;;
i)
# Write to log file without colorization for info message
log_message="$timestamp [INFO] $message"
colored_message=$log_message
;;
d)
if [ "$colorize_terminal" = true ]; then
# Debug (green) for terminal
colored_message="\e[32m[DEBUG] $message\e[0m"
else
# Debug (no color for sh) for terminal
colored_message="$timestamp [DEBUG] $message"
fi
# Write to log file without colorization
log_message="$timestamp [DEBUG] $message"
;;
*)
# Default (no color for other shells) for terminal
colored_message="$timestamp $message"
# Write to log file without colorization
log_message="$timestamp $message"
;;
esac
# Check if the shell is sh (not bash or zsh) to avoid colorization
if [ "${SHELL##*/}" = "sh" ]; then
colorize_terminal=false
else
colorize_terminal=true
fi
# Display the message in the terminal
echo -e "$colored_message"
case "$level" in
w)
if [ "$colorize_terminal" = true ]; then
# Warning (yellow) for terminal
colored_message="\e[33m[WARN] $message\e[0m"
else
# Warning (no color for sh) for terminal
colored_message="$timestamp [WARN] $message"
fi
# Write to log file without colorization
log_message="$timestamp [WARN] $message"
;;
e)
if [ "$colorize_terminal" = true ]; then
# Error (red) for terminal
colored_message="\e[31m[ERROR] $message\e[0m"
else
# Error (no color for sh) for terminal
colored_message="$timestamp [ERROR] $message"
fi
# Write to log file without colorization
log_message="$timestamp [ERROR] $message"
;;
i)
# Write to log file without colorization for info message
log_message="$timestamp [INFO] $message"
colored_message=$log_message
;;
d)
if [ "$colorize_terminal" = true ]; then
# Debug (green) for terminal
colored_message="\e[32m[DEBUG] $message\e[0m"
else
# Debug (no color for sh) for terminal
colored_message="$timestamp [DEBUG] $message"
fi
# Write to log file without colorization
log_message="$timestamp [DEBUG] $message"
;;
*)
# Default (no color for other shells) for terminal
colored_message="$timestamp $message"
# Write to log file without colorization
log_message="$timestamp $message"
;;
esac
# Display the message in the terminal
echo -e "$colored_message"
# Write the log message to the log file
if [ ! -f "$logfile" ]; then
echo "$timestamp [WARN] Log file not found in \"$logfile\", creating it"
touch "$logfile"
fi
echo "$log_message" >> "$logfile"
# Write the log message to the log file
if [ ! -f "$logfile" ]; then
echo "$timestamp [WARN] Log file not found in \"$logfile\", creating it"
touch "$logfile"
fi
echo "$log_message" >> "$logfile"
}

View file

@ -26,7 +26,7 @@ prepare_component() {
fi
done < <(grep -v '^\s*$' $rd_conf | awk '/^\[paths\]/{f=1;next} /^\[/{f=0} f')
create_dir "/var/config/retrodeck/godot"
dir_prep "$rd_logs_folder" "$logs_folder"
dir_prep "$logs_folder" "$rd_logs_folder"
fi
if [[ "$action" == "postmove" ]]; then # Update the paths of any folders that came with the retrodeck folder during a move
while read -r config_line; do
@ -38,7 +38,7 @@ prepare_component() {
fi
fi
done < <(grep -v '^\s*$' $rd_conf | awk '/^\[paths\]/{f=1;next} /^\[/{f=0} f')
dir_prep "$rd_logs_folder" "$logs_folder"
dir_prep "$logs_folder" "$rd_logs_folder"
fi
fi
@ -55,6 +55,7 @@ prepare_component() {
set_setting_value "$es_settings" "MediaDirectory" "$media_folder" "es_settings"
set_setting_value "$es_settings" "UserThemeDirectory" "$themes_folder" "es_settings"
dir_prep "$rdhome/gamelists" "/var/config/ES-DE/gamelists"
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