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
echo -e "$colored_message" >&2 if [[ "$LOG_SILENT" != "true" ]]; then
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
} }
@ -130,4 +133,4 @@ rotate_logs() {
# Compress without directory structure and suppress tar output # Compress without directory structure and suppress tar output
tar -czf "${logfile}.1.tar.gz" -C "$(dirname "$logfile")" "$(basename "$logfile")" --remove-files &>/dev/null tar -czf "${logfile}.1.tar.gz" -C "$(dirname "$logfile")" "$(basename "$logfile")" --remove-files &>/dev/null
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,16 +1,15 @@
#!/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:
flatpak run [FLATPAK-RUN-OPTION] net.retrodeck.retrodeck [ARGUMENTS] 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,139 +34,148 @@ https://retrodeck.net
" "
} }
# Arguments section # Check if is an infromational message
# If so, set LOG_SILENT to true, source the global.sh script,
# show the needed information and quit
case "$1" in
-h|--help)
LOG_SILENT=true
source /app/libexec/global.sh
if [[ "$version" =~ ^[0-9] ]]; then
echo "RetroDECK v$version"
else
echo "RetroDECK $version"
fi
show_cli_help
exit 0
;;
-v|--version)
LOG_SILENT=true
source /app/libexec/global.sh
if [[ "$version" =~ ^[0-9] ]]; then
echo "RetroDECK v$version"
else
echo "RetroDECK $version"
fi
exit 0
;;
--show-config)
LOG_SILENT=true
source /app/libexec/global.sh
echo "RetroDECK config file is in: $rd_conf"
echo "Contents:"
cat "$rd_conf"
exit 0
;;
esac
for i in "$@"; do source /app/libexec/global.sh
case $i in
-h*|--help*) # Process command-line arguments
if [[ "$version" == *"cooker"* ]]; then while [[ $# -gt 0 ]]; do
echo "RetroDECK $version" # If the first argument is -e, -s, -m, or a valid file, attempt to launch the game
else if [ -f "$1" ] || [[ "$1" == "-e" || "$1" == "-s" || "$1" == "-m" ]]; then
echo "RetroDECK v$version" echo "$LOG_BUFFER"
fi log i "Game start option detected: $1"
show_cli_help run_game "$@"
exit
;;
--version*|-v*)
echo "RetroDECK v$version"
exit
;;
--info-msg*)
echo "RetroDECK v$version"
echo "RetroDECK config file is in: $rd_conf"
echo "Contents:"
cat $rd_conf
exit
;;
--debug*)
logging_level="debug"
shift
;;
--compress-one*)
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
;;
# --legacy-configurator*)
# sh /app/tools/configurator.sh
# if [[ $(configurator_generic_question_dialog "RetroDECK Configurator" "Would you like to launch RetroDECK after closing the Configurator?") == "false" ]]; then
# exit
# else
# shift
# fi
# ;;
--reset*)
component="${@:2}"
if [ -z "$component" ]; then
echo "You are about to reset one or more RetroDECK components or emulators."
echo -e "Available options are:\nall, $(prepare_component --list | tr ' ' ',' | sed 's/,/, /g')"
read -p "Please enter the component you would like to reset: " component
component=$(echo "$component" | tr '[:upper:]' '[:lower:]')
fi
log d "Resetting component: $component"
prepare_component "reset" "$component"
exit
;;
--factory-reset*)
prepare_component --factory-reset
;;
--test-upgrade*)
if [[ "$2" =~ ^.+ ]]; then
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
if [[ $response == [yY] ]]; then
version="$2"
logging_level="debug" # Temporarily enable debug
shift 2
else
shift
fi
else
echo "Error: Invalid format. Usage: --test-upgrade <version>"
exit 1
fi
;;
--set*)
preset="$2"
if [ "$preset" == "cheevos" ]; then
echo "Error: The 'cheevos' preset is not yet supported via CLI. Please use the RetroDECK Configurator."
exit 1
fi
value="$3"
if [ -z "$preset" ]; then
echo "Error: No preset specified. Usage: --set <preset> [value], --set help for more help"
exit 1
fi
if [ "$preset" == "help" ]; then
echo "Used to toggle or set a preset. Available presets are:"
fetch_all_presets
echo "Usage: --set <preset> [value]"
echo "Examples:"
echo "Force borders to be true for gba:"
echo " make_preset_changes borders gba true"
echo "Force borders to be true for all supported systems:"
echo " make_preset_changes borders all true"
echo "Toggle gba in preset borders, this will disable the enabled and vice versa:"
echo " make_preset_changes borders gba true"
echo "Toggle all in preset borders:"
echo " make_preset_changes borders all"
exit 0 exit 0
fi fi
make_preset_changes "$preset" "$value"
exit 0 case "$1" in
;; --debug)
--open*) logging_level="debug"
open_component "${@:2}" shift
exit 0 ;;
;; --compress-one)
*) cli_compress_single_game "$2"
# Assume unknown arguments are game start arguments exit 0
if [ -f "$i" ]; then ;;
log i "Attempting to start the game: $i" --compress-all)
run_game "$@" cli_compress_all_games "$2"
exit shift 2
elif [[ "$i" == "-e" || "$i" == "-s" || "$i" == "-m" ]]; then ;;
log i "Game start option detected: $i" --configurator)
run_game "$@" sh /app/tools/configurator.sh
exit exit 0
else ;;
log i "Command or File '$i' not found. Ignoring argument and continuing..." --reset)
break # Continue with the main program component="${@:2}"
fi if [ -z "$component" ]; then
;; echo "You are about to reset one or more RetroDECK components or emulators."
-*|--*) echo -e "Available options are:\nall, $(prepare_component --list | tr ' ' ',' | sed 's/,/, /g')"
log i "Unknown option $i" read -p "Please enter the component you would like to reset: " component
show_cli_help component=$(echo "$component" | tr '[:upper:]' '[:lower:]')
exit 1 fi
;; log d "Resetting component: $component"
esac prepare_component "reset" "$component"
exit 0
;;
--factory-reset)
prepare_component --factory-reset
exit 0
;;
--test-upgrade)
if [[ "$2" =~ ^.+ ]]; then
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
if [[ $response == [yY] ]]; then
version="$2"
logging_level="debug" # Temporarily enable debug logging
shift 2
else
shift
fi
else
echo "Error: Invalid format. Usage: --test-upgrade <version>"
exit 1
fi
;;
--set)
preset="$2"
if [ "$preset" == "cheevos" ]; then
echo "Error: The 'cheevos' preset is not yet supported via CLI. Please use the RetroDECK Configurator."
exit 1
fi
value="$3"
if [ -z "$preset" ]; then
echo "Error: No preset specified. Usage: --set <preset> [value] (use --set help for more information)"
exit 1
fi
if [ "$preset" == "help" ]; then
echo "Used to toggle or set a preset. Available presets are:"
fetch_all_presets
echo "Usage: --set <preset> [value]"
echo "Examples:"
echo " Force borders to be true for GBA:"
echo " make_preset_changes borders gba true"
echo " Force borders to be true for all supported systems:"
echo " make_preset_changes borders all true"
echo " Toggle GBA in preset borders (this will disable if enabled and vice versa):"
echo " make_preset_changes borders gba true"
echo " Toggle all in preset borders:"
echo " make_preset_changes borders all"
exit 0
fi
make_preset_changes "$preset" "$value"
exit 0
;;
--open)
open_component "${@:2}"
exit 0
;;
-*)
# Catch-all for unrecognized options starting with a dash
log e "Error: Unknown option '$1'"
echo "Error: Unrecognized option '$1'. Use -h or --help for usage information."
exit 1
;;
*)
# If it reaches here and is an unrecognized argument, report the error
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
;;
esac
done done
# if lockfile exists # if lockfile exists