Rearrange logging checks based on verbosity

This commit is contained in:
icenine451 2024-10-18 09:29:25 -04:00
parent c8fcabec02
commit ef9b5024fd

View file

@ -19,74 +19,73 @@
log() { log() {
if [[ ! $logging_level == "none" ]]; then if [[ ! $logging_level == "none" ]]; then
local level="$1" local level="$1"
local message="$2" local message="$2"
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])" local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])"
local colorize_terminal local colorize_terminal
# Use specified logfile or default to retrodeck.log # Use specified logfile or default to retrodeck.log
local logfile local logfile
if [ -n "$3" ]; then if [ -n "$3" ]; then
logfile="$3" logfile="$3"
else else
logfile="$rd_logs_folder/retrodeck.log" logfile="$rd_logs_folder/retrodeck.log"
fi fi
# Check if the shell is sh (not bash or zsh) to avoid colorization # Check if the shell is sh (not bash or zsh) to avoid colorization
if [ "${SHELL##*/}" = "sh" ]; then if [ "${SHELL##*/}" = "sh" ]; then
colorize_terminal=false colorize_terminal=false
else else
colorize_terminal=true colorize_terminal=true
fi fi
case "$level" in case "$level" in
w) d)
if [ "$colorize_terminal" = true ]; then if [ "$colorize_terminal" = true ]; then
# Warning (yellow) for terminal # Debug (green) for terminal
colored_message="\e[33m[WARN] $message\e[0m" colored_message="\e[32m[DEBUG] $message\e[0m"
else else
# Warning (no color for sh) for terminal # Debug (no color for sh) for terminal
colored_message="$timestamp [WARN] $message" colored_message="$timestamp [DEBUG] $message"
fi fi
# Write to log file without colorization # Write to log file without colorization
log_message="$timestamp [WARN] $message" log_message="$timestamp [DEBUG] $message"
;; ;;
e) e)
if [ "$colorize_terminal" = true ]; then if [ "$colorize_terminal" = true ]; then
# Error (red) for terminal # Error (red) for terminal
colored_message="\e[31m[ERROR] $message\e[0m" colored_message="\e[31m[ERROR] $message\e[0m"
else else
# Error (no color for sh) for terminal # Error (no color for sh) for terminal
colored_message="$timestamp [ERROR] $message" colored_message="$timestamp [ERROR] $message"
fi fi
# Write to log file without colorization # Write to log file without colorization
log_message="$timestamp [ERROR] $message" log_message="$timestamp [ERROR] $message"
;; ;;
i) w)
# Write to log file without colorization for info message if [ "$colorize_terminal" = true ]; then
log_message="$timestamp [INFO] $message" # Warning (yellow) for terminal
colored_message=$log_message colored_message="\e[33m[WARN] $message\e[0m"
;; else
d) # Warning (no color for sh) for terminal
if [ "$colorize_terminal" = true ]; then colored_message="$timestamp [WARN] $message"
# Debug (green) for terminal fi
colored_message="\e[32m[DEBUG] $message\e[0m" # Write to log file without colorization
else log_message="$timestamp [WARN] $message"
# Debug (no color for sh) for terminal ;;
colored_message="$timestamp [DEBUG] $message" i)
fi # Write to log file without colorization for info message
# Write to log file without colorization log_message="$timestamp [INFO] $message"
log_message="$timestamp [DEBUG] $message" colored_message=$log_message
;; ;;
*) *)
# Default (no color for other shells) for terminal # Default (no color for other shells) for terminal
colored_message="$timestamp $message" colored_message="$timestamp $message"
# Write to log file without colorization # Write to log file without colorization
log_message="$timestamp $message" log_message="$timestamp $message"
;; ;;
esac esac
# Display the message in the terminal # Display the message in the terminal
echo -e "$colored_message" >&2 echo -e "$colored_message" >&2