LOGGER: added various check cases, might be optimized tho

This commit is contained in:
XargonWan 2024-03-22 10:12:12 +01:00
parent 5570700867
commit 5e642c5bd5
2 changed files with 70 additions and 48 deletions

View file

@ -6,13 +6,13 @@ 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/functions.sh
source /app/libexec/multi_user.sh
source /app/libexec/patching.sh
source /app/libexec/post_update.sh
source /app/libexec/prepare_component.sh
source /app/libexec/presets.sh
source /app/libexec/logger.sh
# Static variables
rd_conf="/var/config/retrodeck/retrodeck.cfg" # RetroDECK config file path
@ -168,7 +168,7 @@ if [[ ! -f "$rd_conf" ]]; then
chmod +rw $rd_conf
log i "RetroDECK config file initialized. Contents:\n\n$(cat $rd_conf)\n"
conf_read # Load new variables into memory
tmplog_merger
#tmplog_merger # This function is tempry(?) removed
# If the config file is existing i just read the variables
else
@ -182,7 +182,7 @@ else
fi
conf_read
tmplog_merger
#tmplog_merger # This function is tempry(?) removed
# Verify rdhome is where it is supposed to be.
if [[ ! -d "$rdhome" ]]; then
@ -191,7 +191,7 @@ else
new_home_path=$(directory_browse "RetroDECK folder location")
set_setting_value $rd_conf "rdhome" "$new_home_path" retrodeck "paths"
conf_read
tmplog_merger
#tmplog_merger # This function is tempry(?) removed
prepare_component "retrodeck" "postmove"
prepare_component "all" "postmove"
conf_write

View file

@ -1,3 +1,6 @@
# SORRY, I WILL CLEAN UP THIS
# -Xargon
# This script provides a logging function 'log' that can be sourced in other scripts.
# It logs messages to both the terminal and a specified logfile, allowing different log levels.
# The log function takes three parameters: log level, log message, and optionally the logfile. If no logfile is specified, it writes to retrodeck/logs/retrodeck.log
@ -13,23 +16,36 @@
# 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() {
# this is a one off otherwise it would be logging every time this function is called
# log d "Starting log merger function"
# create_dir "$rd_logs_folder"
# if retrodeck folder variable is not yet initialized
if [ -z "${rdhome}" ]; then
tmp_logs_folder="/tmp/rdlogs"
logs_folder="$tmp_logs_folder"
create_dir "$logs_folder"
# if the log folder is not existing
elif [ ! -d "$rd_logs_folder" ]; then
# # Check if /tmp/rdlogs/retrodeck.log exists
# if [ -e "/tmp/rdlogs/retrodeck.log" ] && [ -e "$rd_logs_folder/retrodeck.log" ]; then
rd_logs_folder="$rdhome/logs"
create_dir "$rd_logs_folder"
elif [ ! -z "${rdhome}" ] && [ -d "$tmp_logs_folder" ]; then
cp -f "$tmp_logs_folder/retrodeck.log" "$logs_folder/retrodeck.log"
rm -rf "$tmp_logs_folder"
fi
# # 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() {
@ -45,7 +61,41 @@ log() {
if [ -n "$3" ]; then
logfile="$3"
else
logfile="$rd_logs_folder/retrodeck.log"
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
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
# Check if the shell is sh (not bash or zsh) to avoid colorization
@ -113,31 +163,3 @@ log() {
echo "$log_message" >> "$logfile"
}
# This function is merging the temporary log file into the actual one
tmplog_merger() {
create_dir "$rd_logs_folder"
# Check if /tmp/retrodeck.log exists
if [ -e "/tmp/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/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"
# Remove the temporary file
rm "/tmp/retrodeck.log"
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
}