2024-03-22 09:12:12 +00:00
# SORRY, I WILL CLEAN UP THIS
# -Xargon
2024-01-04 08:26:42 +00:00
# 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
2024-03-21 21:01:17 +00:00
# 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
# log i - normal informational message
# log w - waring: something is not expected but it's not a big deal
# log e - error: something broke
2024-01-04 08:26:42 +00:00
# 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( ) {
2024-10-18 13:27:41 +00:00
if [ [ ! $logging_level = = "none" ] ] ; then
2024-01-05 09:59:18 +00:00
2024-10-18 13:29:25 +00:00
local level = " $1 "
local message = " $2 "
local timestamp = " $( date +[ %Y-%m-%d\ %H:%M:%S.%3N] ) "
local colorize_terminal
2024-03-22 21:10:26 +00:00
2024-10-18 13:29:25 +00:00
# Use specified logfile or default to retrodeck.log
local logfile
if [ -n " $3 " ] ; then
logfile = " $3 "
else
logfile = " $rd_logs_folder /retrodeck.log "
fi
2024-03-22 21:10:26 +00:00
2024-10-18 13:29:25 +00:00
# Check if the shell is sh (not bash or zsh) to avoid colorization
if [ " ${ SHELL ##*/ } " = "sh" ] ; then
colorize_terminal = false
else
colorize_terminal = true
fi
2024-03-22 21:10:26 +00:00
2024-10-18 13:29:25 +00:00
case " $level " in
2024-10-18 13:40:28 +00:00
d)
if [ [ $logging_level = = "debug" ] ] ; then
if [ " $colorize_terminal " = true ] ; then
# 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 "
2024-10-18 13:29:25 +00:00
fi
; ;
e)
2024-10-18 13:40:28 +00:00
if [ [ $logging_level = = "debug" || $logging_level = = "error" ] ] ; then
if [ " $colorize_terminal " = true ] ; then
# 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 "
2024-10-18 13:29:25 +00:00
fi
; ;
2024-10-18 13:40:28 +00:00
w)
if [ [ $logging_level = = "debug" || $logging_level = = "error" || $logging_level = = "warn" ] ] ; then
if [ " $colorize_terminal " = true ] ; then
# 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 "
2024-10-18 13:29:25 +00:00
fi
; ;
2024-10-18 13:40:28 +00:00
i)
if [ [ $logging_level = = "debug" || $logging_level = = "error" || $logging_level = = "warn" || $logging_level = = "info" ] ] ; then
# Write to log file without colorization for info message
log_message = " $timestamp [INFO] $message "
colored_message = $log_message
fi
2024-10-18 13:29:25 +00:00
; ;
2024-10-18 13:40:28 +00:00
*)
2024-10-18 13:29:25 +00:00
# Default (no color for other shells) for terminal
colored_message = " $timestamp $message "
# Write to log file without colorization
log_message = " $timestamp $message "
; ;
esac
2024-03-22 21:10:26 +00:00
2024-10-18 13:25:54 +00:00
# Display the message in the terminal
echo -e " $colored_message " >& 2
2024-03-22 21:10:26 +00:00
2024-10-18 13:25:54 +00:00
# Write the log message to the log file
if [ ! -f " $logfile " ] ; then
echo " $timestamp [WARN] Log file not found in \" $logfile \", creating it " >& 2
touch " $logfile "
fi
echo " $log_message " >> " $logfile "
2024-03-22 21:10:26 +00:00
fi
2024-01-05 09:59:18 +00:00
}