From 90cf770298faad2d633c666c334a7267e17e7eed Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Fri, 5 Jan 2024 10:59:18 +0100
Subject: [PATCH] LOGGER: enhancements

---
 functions/global.sh |  3 +++
 functions/logger.sh | 31 ++++++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/functions/global.sh b/functions/global.sh
index 8608c9d2..945dba0f 100644
--- a/functions/global.sh
+++ b/functions/global.sh
@@ -156,6 +156,7 @@ if [[ ! -f "$rd_conf" ]]; then
   log i "RetroDECK config file initialized. Contents:\n"
   log i "$(cat "$rd_conf")"
   conf_read # Load new variables into memory
+  tmplog_merger
 
 # If the config file is existing i just read the variables
 else
@@ -169,6 +170,7 @@ else
   fi
 
   conf_read
+  tmplog_merger
 
   # Verify rdhome is where it is supposed to be.
   if [[ ! -d $rdhome ]]; then
@@ -177,6 +179,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
     prepare_emulator "retrodeck" "postmove"
     prepare_emulator "all" "postmove"
     conf_write
diff --git a/functions/logger.sh b/functions/logger.sh
index d1b14b51..f8cceb3b 100755
--- a/functions/logger.sh
+++ b/functions/logger.sh
@@ -7,12 +7,20 @@
 # 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
 
-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() {
+
+    # 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])"
+    local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])"
     local colorize_terminal
 
     # Use specified logfile or default to retrodeck.log
@@ -89,4 +97,21 @@ log() {
     # Write the log message to the log file
     echo "$log_message" >> "$logfile"
 
-}
\ No newline at end of file
+}
+
+# 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
+}