mirror of
https://github.com/RetroDECK/RetroDECK.git
synced 2025-01-19 15:15:38 +00:00
LOGGER: actual implementation of log levels
This commit is contained in:
parent
74fec2e9fa
commit
c35db6b46f
|
@ -1,99 +1,87 @@
|
||||||
# SORRY, I WILL CLEAN UP THIS
|
|
||||||
# -Xargon
|
|
||||||
|
|
||||||
# This script provides a logging function 'log' that can be sourced in other scripts.
|
# 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.
|
# It logs messages to both the terminal and a specified logfile, supporting multiple 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
|
# 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.
|
||||||
|
#
|
||||||
|
# Supported logging levels (controlled by the variable 'logging_level'):
|
||||||
|
# - none: No logs are produced.
|
||||||
|
# - info: Logs informational messages (i) and errors (e).
|
||||||
|
# - warn: Logs warnings (w), informational messages (i), and errors (e).
|
||||||
|
# - debug: Logs all message types (d, w, i, e).
|
||||||
|
#
|
||||||
# Type of log messages:
|
# Type of log messages:
|
||||||
# log d - debug message: maybe in the future we can decide to hide them in main builds or if an option is toggled
|
# - d: Debug message (logged only in debug level).
|
||||||
# log i - normal informational message
|
# - i: Informational message (logged in debug, info, and warn levels).
|
||||||
# log w - waring: something is not expected but it's not a big deal
|
# - w: Warning message (logged in debug and warn levels).
|
||||||
# log e - error: something broke
|
# - e: Error message (logged in all levels except none).
|
||||||
|
#
|
||||||
# Example usage:
|
# Example usage:
|
||||||
# log w "foo" -> logs a warning with message foo in the default log file retrodeck/logs/retrodeck.log
|
# log w "foo" -> Logs a warning with message "foo" to 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 e "bar" -> Logs an error with message "bar" to 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 i "baz" rekku.log -> Logs an informational message "baz" to the specified log file retrodeck/logs/rekku.log.
|
||||||
|
#
|
||||||
|
# The function auto-detects if the shell is sh and avoids colorizing the output in that case.
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
if [[ ! $logging_level == "none" ]]; then
|
# Exit early if logging_level is "none"
|
||||||
|
if [[ $logging_level == "none" ]]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
local level="$1"
|
local level="$1" # Logging level of the current message
|
||||||
local message="$2"
|
local message="$2" # Message to log
|
||||||
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])"
|
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])" # Timestamp for the log entry
|
||||||
local colorize_terminal
|
local logfile="${3:-$rd_logs_folder/retrodeck.log}" # Log file, default to retrodeck.log
|
||||||
|
local colorize_terminal=true
|
||||||
|
|
||||||
# Use specified logfile or default to retrodeck.log
|
# Check if the shell is sh (not bash or zsh) to avoid colorization
|
||||||
local logfile
|
if [ "${SHELL##*/}" = "sh" ]; then
|
||||||
if [ -n "$3" ]; then
|
colorize_terminal=false
|
||||||
logfile="$3"
|
fi
|
||||||
else
|
|
||||||
logfile="$rd_logs_folder/retrodeck.log"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if the shell is sh (not bash or zsh) to avoid colorization
|
# Function to check if the current message level should be logged
|
||||||
if [ "${SHELL##*/}" = "sh" ]; then
|
should_log() {
|
||||||
colorize_terminal=false
|
case "$logging_level" in
|
||||||
else
|
debug) return 0 ;; # Always log everything
|
||||||
colorize_terminal=true
|
info) [[ "$level" == "i" || "$level" == "e" ]] && return 0 ;;
|
||||||
fi
|
warn) [[ "$level" != "d" ]] && return 0 ;;
|
||||||
|
error) [[ "$level" == "e" ]] && return 0 ;;
|
||||||
|
esac
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if should_log; then
|
||||||
|
# Define message colors based on level
|
||||||
case "$level" in
|
case "$level" in
|
||||||
d)
|
d)
|
||||||
if [[ $logging_level == "debug" ]]; then
|
color="\e[32m[DEBUG]"
|
||||||
if [ "$colorize_terminal" = true ]; then
|
prefix="[DEBUG]"
|
||||||
# Debug (green) for terminal
|
|
||||||
colored_message="\e[32m[DEBUG] $message\e[0m"
|
|
||||||
else
|
|
||||||
# Debug (no color for sh) for terminal
|
|
||||||
colored_message="$timestamp [DEBUG] $message"
|
|
||||||
fi
|
|
||||||
# Write to log file without colorization
|
|
||||||
log_message="$timestamp [DEBUG] $message"
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
e)
|
e)
|
||||||
if [[ $logging_level == "debug" || $logging_level == "error" ]]; then
|
color="\e[31m[ERROR]"
|
||||||
if [ "$colorize_terminal" = true ]; then
|
prefix="[ERROR]"
|
||||||
# Error (red) for terminal
|
|
||||||
colored_message="\e[31m[ERROR] $message\e[0m"
|
|
||||||
else
|
|
||||||
# Error (no color for sh) for terminal
|
|
||||||
colored_message="$timestamp [ERROR] $message"
|
|
||||||
fi
|
|
||||||
# Write to log file without colorization
|
|
||||||
log_message="$timestamp [ERROR] $message"
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
w)
|
w)
|
||||||
if [[ $logging_level == "debug" || $logging_level == "error" || $logging_level == "warn" ]]; then
|
color="\e[33m[WARN]"
|
||||||
if [ "$colorize_terminal" = true ]; then
|
prefix="[WARN]"
|
||||||
# Warning (yellow) for terminal
|
|
||||||
colored_message="\e[33m[WARN] $message\e[0m"
|
|
||||||
else
|
|
||||||
# Warning (no color for sh) for terminal
|
|
||||||
colored_message="$timestamp [WARN] $message"
|
|
||||||
fi
|
|
||||||
# Write to log file without colorization
|
|
||||||
log_message="$timestamp [WARN] $message"
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
i)
|
i)
|
||||||
if [[ $logging_level == "debug" || $logging_level == "error" || $logging_level == "warn" || $logging_level == "info" ]]; then
|
color="\e[34m[INFO]"
|
||||||
# Write to log file without colorization for info message
|
prefix="[INFO]"
|
||||||
log_message="$timestamp [INFO] $message"
|
|
||||||
colored_message=$log_message
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# Default (no color for other shells) for terminal
|
color="\e[37m[LOG]"
|
||||||
colored_message="$timestamp $message"
|
prefix="[LOG]"
|
||||||
# Write to log file without colorization
|
|
||||||
log_message="$timestamp $message"
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
# Construct the log message
|
||||||
|
if [ "$colorize_terminal" = true ]; then
|
||||||
|
colored_message="$color $message\e[0m"
|
||||||
|
else
|
||||||
|
colored_message="$timestamp $prefix $message"
|
||||||
|
fi
|
||||||
|
log_message="$timestamp $prefix $message"
|
||||||
|
|
||||||
# Display the message in the terminal
|
# Display the message in the terminal
|
||||||
echo -e "$colored_message" >&2
|
echo -e "$colored_message" >&2
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue