From 382a096c1b10288f9558725acde58e7df6324d58 Mon Sep 17 00:00:00 2001 From: wytch Date: Fri, 27 Feb 2026 19:57:20 -0600 Subject: [PATCH] Updated logging function and added LOG_TOPIC environment variable --- pub_event[rungame,endgame](sync).ash | 27 +++++++++++++++------------ savesync-logger[start](permanent).ash | 19 ++++++++++++++----- savesync.conf | 1 + savesync[start](permanent).ash | 26 +++++++++++++++++--------- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/pub_event[rungame,endgame](sync).ash b/pub_event[rungame,endgame](sync).ash index 31b0cf3..cf89cfa 100644 --- a/pub_event[rungame,endgame](sync).ash +++ b/pub_event[rungame,endgame](sync).ash @@ -6,21 +6,24 @@ # --- Logger Function --- log() { - local timestamp - local level="$1" - local msg="$2" - mkdir -p "$(dirname "$ERROR_LOG")" - timestamp=$(date '+%Y-%m-%d %H:%M:%S') - local log_line="[$timestamp] [$level] $msg" + local level="$1" local msg="$2" + local timestamp + + timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local log_line="[$timestamp] [$level] $msg" - if [ "$level" = "ERROR" ]; then - printf "%s\n" "$log_line" >> "$ERROR_LOG" - fi + # Handle local emergency error log immediately + if [ "$level" = "ERROR" ]; then + mkdir -p "$(dirname "$ERROR_LOG")" + printf "%s\n" "$log_line" >> "$ERROR_LOG" + fi - mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveLog='${log_line}'" + # Dispatch to the Central Logging Daemon via MQTT + # We use -q 0 (fire and forget) so the game script doesn't wait + mosquitto_pub -h 127.0.0.1 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null - # printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null - [ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line" + # Local debugging + [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" } # --- Exit function --- diff --git a/savesync-logger[start](permanent).ash b/savesync-logger[start](permanent).ash index 002f924..74997cf 100644 --- a/savesync-logger[start](permanent).ash +++ b/savesync-logger[start](permanent).ash @@ -5,10 +5,19 @@ touch "$LOG_FILE" -mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveLogStart=1" -mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value + +log_to_file() { + printf "%s\n" "$1" >> "$LOG_FILE" +} + +# Subscribe and wait for log entries +mosquitto_sub -h 127.0.0.1 -t "$LOG_TOPIC" | while read -r line do - case "$key" in - "SaveLog") printf "%s\n" "$value" >>"$LOG_FILE" ;; - esac + # The line will look like: SaveLog=[2026-...] [INFO] ... + # We strip the "SaveLog=" prefix + msg_content="${line#SaveLog=}" + + if [ -n "$msg_content" ]; then + log_to_file "$msg_content" + fi done diff --git a/savesync.conf b/savesync.conf index d3441ee..b908c96 100644 --- a/savesync.conf +++ b/savesync.conf @@ -3,3 +3,4 @@ export ERROR_FILE="/recalbox/share/system/logs/savesync_error.log" export DEBUG_MODE=1 export REMOTE_BASE="saves:gamepi-tv" export TOPIC="/Recalbox/EmulationStation/Event" +export LOG_TOPIC="Recalbox/Log/Service" diff --git a/savesync[start](permanent).ash b/savesync[start](permanent).ash index 7b02c26..bb1fdde 100644 --- a/savesync[start](permanent).ash +++ b/savesync[start](permanent).ash @@ -7,17 +7,25 @@ # --- Logger Function --- log() { - local timestamp - local level="$1" - local msg="$2" - mkdir -p "$(dirname "$LOG_FILE")" - timestamp=$(date '+%Y-%m-%d %H:%M:%S') - local log_line="[$timestamp] [$level] $msg" + local level="$1" + local msg="$2" + local timestamp + + timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local log_line="[$timestamp] [$level] $msg" - mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveLog='${log_line}'" + # Handle local emergency error log immediately + if [ "$level" = "ERROR" ]; then + mkdir -p "$(dirname "$ERROR_LOG")" + printf "%s\n" "$log_line" >> "$ERROR_LOG" + fi - # printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null - [ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line" + # Dispatch to the Central Logging Daemon via MQTT + # We use -q 0 (fire and forget) so the game script doesn't wait + mosquitto_pub -h 127.0.0.1 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null + + # Local debugging + [ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line" }