From 28550e33acf8ffba4fcca3d1d2f60b2dc6ef2546 Mon Sep 17 00:00:00 2001 From: XargonWan Date: Thu, 4 Jan 2024 09:26:42 +0100 Subject: [PATCH] LOGGER: added a first logging script and sourced it in all the needed scripts --- automation_tools/appdata_management.sh | 1 + functions/050_save_migration.sh | 2 ++ functions/checks.sh | 2 ++ functions/compression.sh | 2 ++ functions/dialogs.sh | 1 + functions/functions.sh | 2 ++ functions/global.sh | 1 + functions/logger.sh | 42 ++++++++++++++++++++++++++ functions/multi_user.sh | 2 ++ functions/patching.sh | 2 ++ functions/prepare_emulator.sh | 2 ++ functions/presets.sh | 2 ++ retrodeck.sh | 1 + tools/configurator.sh | 1 + tools/retrodeck_function_wrapper.sh | 1 + 15 files changed, 64 insertions(+) create mode 100644 functions/logger.sh diff --git a/automation_tools/appdata_management.sh b/automation_tools/appdata_management.sh index f6cdfb19..2a0e35f5 100755 --- a/automation_tools/appdata_management.sh +++ b/automation_tools/appdata_management.sh @@ -4,6 +4,7 @@ # This script is getting the latest release notes from the wiki and add them to the appdata source automation_tools/version_extractor.sh +source /app/libexec/logger.sh # Fetch appdata version appdata_version=$(fetch_appdata_version) diff --git a/functions/050_save_migration.sh b/functions/050_save_migration.sh index 0f53c65c..480e22a4 100644 --- a/functions/050_save_migration.sh +++ b/functions/050_save_migration.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + save_migration() { # Finding existing ROMs folder if [ -d "$default_sd/retrodeck" ] diff --git a/functions/checks.sh b/functions/checks.sh index bb93b322..3646b907 100644 --- a/functions/checks.sh +++ b/functions/checks.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + check_network_connectivity() { # This function will do a basic check for network availability and return "true" if it is working. # USAGE: if [[ $(check_network_connectivity) == "true" ]]; then diff --git a/functions/compression.sh b/functions/compression.sh index 72876292..ee440cf7 100644 --- a/functions/compression.sh +++ b/functions/compression.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + compress_game() { # Function for compressing one or more files to .chd format # USAGE: compress_game $format $full_path_to_input_file diff --git a/functions/dialogs.sh b/functions/dialogs.sh index 2571a35b..6b8217e4 100644 --- a/functions/dialogs.sh +++ b/functions/dialogs.sh @@ -1,6 +1,7 @@ #!/bin/bash source functions/functions.sh +source /app/libexec/logger.sh debug_dialog() { # This function is for displaying commands run by the Configurator without actually running them diff --git a/functions/functions.sh b/functions/functions.sh index 3b3719fb..3bada305 100644 --- a/functions/functions.sh +++ b/functions/functions.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + directory_browse() { # This function browses for a directory and returns the path chosen # USAGE: path_to_be_browsed_for=$(directory_browse $action_text) diff --git a/functions/global.sh b/functions/global.sh index 954acdda..0e0dcce5 100644 --- a/functions/global.sh +++ b/functions/global.sh @@ -12,6 +12,7 @@ source /app/libexec/patching.sh source /app/libexec/post_update.sh source /app/libexec/prepare_emulator.sh source /app/libexec/presets.sh +source /app/libexec/logger.sh # Static variables rd_conf="/var/config/retrodeck/retrodeck.cfg" # RetroDECK config file path diff --git a/functions/logger.sh b/functions/logger.sh new file mode 100644 index 00000000..52effe18 --- /dev/null +++ b/functions/logger.sh @@ -0,0 +1,42 @@ +# 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 + +# Example usage: +# log w "foo" -> logs a warning with message foo 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() { + local level="$1" + local message="$2" + local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S])" + local logfile="${3:-$logs_folder/retrodeck.log}" # Use specified logfile or default to retrodeck.log + + case "$level" in + w) + # Warning (yellow) for terminal, no color for log file + colored_message="\e[33m[WARN]\e[0m $message" + echo "$timestamp $colored_message" | tee -a >(sed $'s,\e\\[[0-9;]*[a-zA-Z],,g' >> "$logfile") + ;; + e) + # Error (red) for terminal, no color for log file + colored_message="\e[31m[ERROR]\e[0m $message" + echo "$timestamp $colored_message" | tee -a >(sed $'s,\e\\[[0-9;]*[a-zA-Z],,g' >> "$logfile") + ;; + i) + # Info (green) for terminal, no color for log file + colored_message="\e[32m[INFO]\e[0m $message" + echo "$timestamp $colored_message" | tee -a >(sed $'s,\e\\[[0-9;]*[a-zA-Z],,g' >> "$logfile") + ;; + d) + # Debug (green) for both terminal and log file + colored_message="\e[32m[DEBUG]\e[0m $message" + echo "$timestamp $colored_message" | tee -a "$logfile" + ;; + *) + # Default (no color) + echo "$timestamp $message" | tee -a "$logfile" + ;; + esac +} \ No newline at end of file diff --git a/functions/multi_user.sh b/functions/multi_user.sh index 08d9bf4d..c1056c42 100644 --- a/functions/multi_user.sh +++ b/functions/multi_user.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + multi_user_set_default_dialog() { chosen_user="$1" choice=$(zenity --icon-name=net.retrodeck.retrodeck --info --no-wrap --ok-label="Yes" --extra-button="No" --extra-button="No and don't ask again" \ diff --git a/functions/patching.sh b/functions/patching.sh index c732163c..db5dc4b7 100644 --- a/functions/patching.sh +++ b/functions/patching.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + set_setting_value() { # Function for editing settings # USAGE: set_setting_value "$setting_file" "$setting_name" "$new_setting_value" "$system" "$section_name(optional)" diff --git a/functions/prepare_emulator.sh b/functions/prepare_emulator.sh index 1ce4cd8d..f2e65593 100644 --- a/functions/prepare_emulator.sh +++ b/functions/prepare_emulator.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + prepare_emulator() { # This function will perform one of several actions on one or more emulators # The actions currently include "reset" and "postmove" diff --git a/functions/presets.sh b/functions/presets.sh index 664b7893..0bf5a2f5 100644 --- a/functions/presets.sh +++ b/functions/presets.sh @@ -1,5 +1,7 @@ #!/bin/bash +source /app/libexec/logger.sh + change_preset_dialog() { # This function will build a list of all systems compatible with a given preset, their current enable/disabled state and allow the user to change one or more # USAGE: change_preset_dialog "$preset" diff --git a/retrodeck.sh b/retrodeck.sh index daeb3cc8..b8104b26 100644 --- a/retrodeck.sh +++ b/retrodeck.sh @@ -1,6 +1,7 @@ #!/bin/bash source /app/libexec/global.sh +source /app/libexec/logger.sh # Arguments section diff --git a/tools/configurator.sh b/tools/configurator.sh index 659ac0f4..fd78e9c6 100644 --- a/tools/configurator.sh +++ b/tools/configurator.sh @@ -3,6 +3,7 @@ # VARIABLES SECTION source /app/libexec/global.sh +source /app/libexec/logger.sh # DIALOG SECTION diff --git a/tools/retrodeck_function_wrapper.sh b/tools/retrodeck_function_wrapper.sh index 98148ab8..94fca5b2 100644 --- a/tools/retrodeck_function_wrapper.sh +++ b/tools/retrodeck_function_wrapper.sh @@ -4,6 +4,7 @@ # USAGE: /bin/bash retrodeck_function_wrapper.sh ... source /app/libexec/global.sh +source /app/libexec/logger.sh # Check if a function was specified if [[ $# -lt 1 ]]; then