From 7fb1337bd91393eac060f438dff8164dd427adbd Mon Sep 17 00:00:00 2001 From: MrCee <10969457+MrCee@users.noreply.github.com> Date: Mon, 30 Dec 2024 02:19:03 +1100 Subject: [PATCH] add log file --- .gitignore | 1 + functions.sh | 51 ++++++++++++++++++++++++++++-------- install-synology-homebrew.sh | 6 +++++ 3 files changed, 47 insertions(+), 11 deletions(-) mode change 100644 => 100755 functions.sh diff --git a/.gitignore b/.gitignore index ff1f08b..e84334e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store test.sh +*.log diff --git a/functions.sh b/functions.sh old mode 100644 new mode 100755 index 5862f92..e91c8ad --- a/functions.sh +++ b/functions.sh @@ -120,9 +120,32 @@ EOF export SUDOERS_SETUP_DONE=1 } + + +# ----------------------------------------------- +# Function: func_setup_logging +# Description: Sets up logging by redirecting stdout and stderr to a log file +# Arguments: +# $1 - Path to the log file +# ----------------------------------------------- +func_setup_logging() { + local log_file="$1" + + # Save original stdout and stderr + exec 3>&1 4>&2 + + # Redirect all output to both terminal and log file (overwrite each run) + exec > >(tee "$log_file") 2>&1 + + # Log the start time + echo "=== Script Started at $(date) ===" +} + # ----------------------------------------------- # Function: func_cleanup_exit -# Description: Cleans up the sudoers file upon script exit or interruption. +# Description: Cleans up the environment upon script exit or interruption. +# Restores stdout and stderr, closes duplicated file descriptors, +# and performs other necessary cleanup tasks. # Arguments: # $1 - Exit code (default: 0) # ----------------------------------------------- @@ -131,33 +154,39 @@ func_cleanup_exit() { [[ $DEBUG == 1 ]] && echo "šŸ”„ Debug: func_cleanup_exit called with exit code $exit_code." - # Restore original stty settings + # Restore original stdout and stderr + exec 1>&3 2>&4 + + # Close the duplicated file descriptors + exec 3>&- 4>&- + + # Restore stty settings if they were saved if [[ -n "${orig_stty:-}" ]]; then stty "$orig_stty" fi if [[ $exit_code -eq 0 ]]; then - echo "šŸŽ‰ Script completed successfully." + printf "šŸŽ‰ Script completed successfully." else - echo "āš ļø Script exited with code $exit_code." + printf "\nāš ļø Script exited with code $exit_code." fi - # Perform cleanup if the sudoers file exists or if the flag is set + # Perform existing sudoers cleanup if [[ -n "${SUDOERS_FILE:-}" ]]; then if [[ -f "$SUDOERS_FILE" ]]; then - echo "šŸ—‘ļø Removing sudoers file at '$SUDOERS_FILE'..." - sudo rm -f "$SUDOERS_FILE" 2>/dev/null && echo "šŸ—‘ļø Sudoers file removed." + printf "\nšŸ—‘ļø Removing sudoers file at '$SUDOERS_FILE'..." + sudo rm -f "$sudoers_FILE" 2>/dev/null && printf "\nšŸ—‘ļø Sudoers file removed." else - echo "ā„¹ļø Sudoers file '$SUDOERS_FILE' does not exist. No removal needed." + printf "\nā„¹ļø Sudoers file '$SUDOERS_FILE' does not exist. No removal needed." fi - echo "šŸ”’ Revoking sudo access..." - sudo -k && echo "šŸ”’ Sudo access revoked." + printf "\nšŸ”’ Revoking sudo access..." + sudo -k && printf "\nšŸ”’ Sudo access revoked." # Reset the SUDOERS_SETUP_DONE flag export SUDOERS_SETUP_DONE=0 else - echo "šŸ” Debug: SUDOERS_FILE is not set." + printf "\nšŸ” Debug: SUDOERS_FILE is not set." fi # Unset the EXIT trap to prevent recursion diff --git a/install-synology-homebrew.sh b/install-synology-homebrew.sh index 23b7e34..72feab4 100755 --- a/install-synology-homebrew.sh +++ b/install-synology-homebrew.sh @@ -22,6 +22,12 @@ echo "Working directory: $(pwd)" # Source the functions file source "./functions.sh" +# Set the path to your log file +LOG_FILE="./logfile.log" + +# Initialize logging +func_setup_logging "$LOG_FILE" + # Initialize environment variables func_initialize_env_vars