CLI: don't show logs on informational messages

This commit is contained in:
XargonWan 2025-02-20 10:27:26 +09:00
parent d2de02b922
commit 00a8552788
3 changed files with 167 additions and 154 deletions

View file

@ -22,30 +22,30 @@
# The function auto-detects if the shell is sh and avoids colorizing the output in that case. # The function auto-detects if the shell is sh and avoids colorizing the output in that case.
log() { log() {
# Exit early if logging_level is "none" # Exit immediately if logging_level is "none"
if [[ $logging_level == "none" ]]; then if [[ $logging_level == "none" ]]; then
return return
fi fi
local level="$1" # Logging level of the current message local level="$1" # Current message level
local message="$2" # Message to log local message="$2" # Message to log
local logfile="${3:-$rd_logs_folder/retrodeck.log}" # Log file, default to retrodeck.log local logfile="${3:-$rd_logs_folder/retrodeck.log}" # Default log file
local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])" # Timestamp for the log entry local timestamp="$(date +[%Y-%m-%d\ %H:%M:%S.%3N])" # Timestamp
local colorize_terminal=true local colorize_terminal=true
# Determine the calling function or use [FWORK] # Determine the calling function, or use [FWORK]
local caller="${FUNCNAME[1]:-FWORK}" local caller="${FUNCNAME[1]:-FWORK}"
caller="${caller^^}" # Convert to uppercase caller="${caller^^}" # Convert to uppercase
# Check if the shell is sh (not bash or zsh) to avoid colorization # Check if the shell is sh to avoid colorization
if [ "${SHELL##*/}" = "sh" ]; then if [ "${SHELL##*/}" = "sh" ]; then
colorize_terminal=false colorize_terminal=false
fi fi
# Function to check if the current message level should be logged # Internal function to check if the message should be logged
should_log() { should_log() {
case "$logging_level" in case "$logging_level" in
debug) return 0 ;; # Always log everything debug) return 0 ;; # Log everything
info) [[ "$level" == "i" || "$level" == "e" ]] && return 0 ;; info) [[ "$level" == "i" || "$level" == "e" ]] && return 0 ;;
warn) [[ "$level" != "d" ]] && return 0 ;; warn) [[ "$level" != "d" ]] && return 0 ;;
error) [[ "$level" == "e" ]] && return 0 ;; error) [[ "$level" == "e" ]] && return 0 ;;
@ -54,7 +54,7 @@ log() {
} }
if should_log; then if should_log; then
# Define message colors based on level # Define colors based on the message level
case "$level" in case "$level" in
d) d)
color="\e[32m[DEBUG]" color="\e[32m[DEBUG]"
@ -78,7 +78,7 @@ log() {
;; ;;
esac esac
# Construct the log message # Build the message to display
if [ "$colorize_terminal" = true ]; then if [ "$colorize_terminal" = true ]; then
colored_message="$color [$caller] $message\e[0m" colored_message="$color [$caller] $message\e[0m"
else else
@ -86,17 +86,20 @@ log() {
fi fi
log_message="$timestamp $prefix [$caller] $message" log_message="$timestamp $prefix [$caller] $message"
# Display the message in the terminal # If silent mode is not active, print the message to the terminal
if [[ "$LOG_SILENT" != "true" ]]; then
echo -e "$colored_message" >&2 echo -e "$colored_message" >&2
fi
# Write the log message to the log file # Ensure the log file exists
if [ ! -f "$logfile" ]; then if [ ! -f "$logfile" ]; then
#echo "$timestamp [WARN] Log file not found in \"$logfile\", creating it" >&2 # Disabled it as it's always appearing because of log rotation
if [[ ! -d "$(dirname "$logfile")" ]]; then if [[ ! -d "$(dirname "$logfile")" ]]; then
mkdir -p "$(dirname "$logfile")" mkdir -p "$(dirname "$logfile")"
fi fi
touch "$logfile" touch "$logfile"
fi fi
# Write the log to the file
echo "$log_message" >> "$logfile" echo "$log_message" >> "$logfile"
fi fi
} }

View file

@ -6,6 +6,8 @@ run_game() {
system="" system=""
manual_mode=false manual_mode=false
usage="Usage: flatpak run net.retrodeck.retrodeck [-e emulator] [-s system] [-m] game"
# Parse options for system, emulator, and manual mode # Parse options for system, emulator, and manual mode
while getopts ":e:s:m" opt; do while getopts ":e:s:m" opt; do
case ${opt} in case ${opt} in
@ -20,7 +22,7 @@ run_game() {
log i "Run game: manual mode enabled" log i "Run game: manual mode enabled"
;; ;;
\?) \?)
echo "Usage: $0 [-e emulator] [-s system] [-m] game" echo "$usage"
exit 1 exit 1
;; ;;
esac esac
@ -30,7 +32,7 @@ run_game() {
# Check for game argument # Check for game argument
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
log e "Game path is required." log e "Game path is required."
log i "Usage: $0 [-e emulator] [-s system] [-m] game" log i "$usage"
exit 1 exit 1
fi fi

View file

@ -1,7 +1,6 @@
#!/bin/bash #!/bin/bash
source /app/libexec/global.sh # Function to display CLI help
show_cli_help() { show_cli_help() {
echo -e " echo -e "
Usage: Usage:
@ -10,7 +9,7 @@ flatpak run [FLATPAK-RUN-OPTION] net.retrodeck.retrodeck [ARGUMENTS]
Arguments: Arguments:
-h, --help \t Print this help -h, --help \t Print this help
-v, --version \t Print RetroDECK version -v, --version \t Print RetroDECK version
--info-msg \t Print paths and config informations --show-config \t Print information about the RetroDECK configuration file and its contents
--debug \t Enable debug logging for this run of RetroDECK --debug \t Enable debug logging for this run of RetroDECK
--configurator \t Starts the RetroDECK Configurator --configurator \t Starts the RetroDECK Configurator
--compress-one <file> \t Compresses target file to a compatible format --compress-one <file> \t Compresses target file to a compatible format
@ -35,55 +34,71 @@ https://retrodeck.net
" "
} }
# Arguments section # Check if is an infromational message
# If so, set LOG_SILENT to true, source the global.sh script,
for i in "$@"; do # show the needed information and quit
case $i in case "$1" in
-h*|--help*) -h|--help)
if [[ "$version" == *"cooker"* ]]; then LOG_SILENT=true
echo "RetroDECK $version" source /app/libexec/global.sh
else if [[ "$version" =~ ^[0-9] ]]; then
echo "RetroDECK v$version" echo "RetroDECK v$version"
else
echo "RetroDECK $version"
fi fi
show_cli_help show_cli_help
exit exit 0
;; ;;
--version*|-v*) -v|--version)
LOG_SILENT=true
source /app/libexec/global.sh
if [[ "$version" =~ ^[0-9] ]]; then
echo "RetroDECK v$version" echo "RetroDECK v$version"
exit else
echo "RetroDECK $version"
fi
exit 0
;; ;;
--info-msg*) --show-config)
echo "RetroDECK v$version" LOG_SILENT=true
source /app/libexec/global.sh
echo "RetroDECK config file is in: $rd_conf" echo "RetroDECK config file is in: $rd_conf"
echo "Contents:" echo "Contents:"
cat $rd_conf cat "$rd_conf"
exit exit 0
;; ;;
--debug*) esac
source /app/libexec/global.sh
# Process command-line arguments
while [[ $# -gt 0 ]]; do
# If the first argument is -e, -s, -m, or a valid file, attempt to launch the game
if [ -f "$1" ] || [[ "$1" == "-e" || "$1" == "-s" || "$1" == "-m" ]]; then
echo "$LOG_BUFFER"
log i "Game start option detected: $1"
run_game "$@"
exit 0
fi
case "$1" in
--debug)
logging_level="debug" logging_level="debug"
shift shift
;; ;;
--compress-one*) --compress-one)
cli_compress_single_game "$2" cli_compress_single_game "$2"
exit
;;
--compress-all*)
cli_compress_all_games "$2"
;;
--configurator*)
sh /app/tools/configurator.sh
#sh /app/bin/godot-configurator.sh
exit 0 exit 0
;; ;;
# --legacy-configurator*) --compress-all)
# sh /app/tools/configurator.sh cli_compress_all_games "$2"
# if [[ $(configurator_generic_question_dialog "RetroDECK Configurator" "Would you like to launch RetroDECK after closing the Configurator?") == "false" ]]; then shift 2
# exit ;;
# else --configurator)
# shift sh /app/tools/configurator.sh
# fi exit 0
# ;; ;;
--reset*) --reset)
component="${@:2}" component="${@:2}"
if [ -z "$component" ]; then if [ -z "$component" ]; then
echo "You are about to reset one or more RetroDECK components or emulators." echo "You are about to reset one or more RetroDECK components or emulators."
@ -93,18 +108,19 @@ for i in "$@"; do
fi fi
log d "Resetting component: $component" log d "Resetting component: $component"
prepare_component "reset" "$component" prepare_component "reset" "$component"
exit exit 0
;; ;;
--factory-reset*) --factory-reset)
prepare_component --factory-reset prepare_component --factory-reset
exit 0
;; ;;
--test-upgrade*) --test-upgrade)
if [[ "$2" =~ ^.+ ]]; then if [[ "$2" =~ ^.+ ]]; then
echo "You are about to test upgrading RetroDECK from version $2 to $hard_version" echo "You are about to test upgrading RetroDECK from version $2 to $hard_version"
read -p "Enter 'y' to continue, 'n' to start RetroDECK normally: " response read -p "Enter 'y' to continue, 'n' to start RetroDECK normally: " response
if [[ $response == [yY] ]]; then if [[ $response == [yY] ]]; then
version="$2" version="$2"
logging_level="debug" # Temporarily enable debug logging_level="debug" # Temporarily enable debug logging
shift 2 shift 2
else else
shift shift
@ -114,7 +130,7 @@ for i in "$@"; do
exit 1 exit 1
fi fi
;; ;;
--set*) --set)
preset="$2" preset="$2"
if [ "$preset" == "cheevos" ]; then if [ "$preset" == "cheevos" ]; then
echo "Error: The 'cheevos' preset is not yet supported via CLI. Please use the RetroDECK Configurator." echo "Error: The 'cheevos' preset is not yet supported via CLI. Please use the RetroDECK Configurator."
@ -122,7 +138,7 @@ for i in "$@"; do
fi fi
value="$3" value="$3"
if [ -z "$preset" ]; then if [ -z "$preset" ]; then
echo "Error: No preset specified. Usage: --set <preset> [value], --set help for more help" echo "Error: No preset specified. Usage: --set <preset> [value] (use --set help for more information)"
exit 1 exit 1
fi fi
if [ "$preset" == "help" ]; then if [ "$preset" == "help" ]; then
@ -130,11 +146,11 @@ for i in "$@"; do
fetch_all_presets fetch_all_presets
echo "Usage: --set <preset> [value]" echo "Usage: --set <preset> [value]"
echo "Examples:" echo "Examples:"
echo "Force borders to be true for gba:" echo " Force borders to be true for GBA:"
echo " make_preset_changes borders gba true" echo " make_preset_changes borders gba true"
echo " Force borders to be true for all supported systems:" echo " Force borders to be true for all supported systems:"
echo " make_preset_changes borders all true" echo " make_preset_changes borders all true"
echo "Toggle gba in preset borders, this will disable the enabled and vice versa:" echo " Toggle GBA in preset borders (this will disable if enabled and vice versa):"
echo " make_preset_changes borders gba true" echo " make_preset_changes borders gba true"
echo " Toggle all in preset borders:" echo " Toggle all in preset borders:"
echo " make_preset_changes borders all" echo " make_preset_changes borders all"
@ -143,28 +159,20 @@ for i in "$@"; do
make_preset_changes "$preset" "$value" make_preset_changes "$preset" "$value"
exit 0 exit 0
;; ;;
--open*) --open)
open_component "${@:2}" open_component "${@:2}"
exit 0 exit 0
;; ;;
*) -*)
# Assume unknown arguments are game start arguments # Catch-all for unrecognized options starting with a dash
if [ -f "$i" ]; then log e "Error: Unknown option '$1'"
log i "Attempting to start the game: $i" echo "Error: Unrecognized option '$1'. Use -h or --help for usage information."
run_game "$@" exit 1
exit
elif [[ "$i" == "-e" || "$i" == "-s" || "$i" == "-m" ]]; then
log i "Game start option detected: $i"
run_game "$@"
exit
else
log i "Command or File '$i' not found. Ignoring argument and continuing..."
break # Continue with the main program
fi
;; ;;
-*|--*) *)
log i "Unknown option $i" # If it reaches here and is an unrecognized argument, report the error
show_cli_help log e "Error: Command or file '$1' not recognized."
echo "Error: Command or file '$1' not recognized. Use -h or --help for usage information."
exit 1 exit 1
;; ;;
esac esac