Updated logging function and added LOG_TOPIC environment variable

This commit is contained in:
2026-02-27 19:57:20 -06:00
parent cfed476096
commit 382a096c1b
4 changed files with 47 additions and 26 deletions

View File

@@ -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
if [ "$level" = "ERROR" ]; then
printf "%s\n" "$log_line" >> "$ERROR_LOG"
fi
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"
}
# --- Exit function ---

View File

@@ -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

View File

@@ -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"

View File

@@ -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
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "SaveLog='${log_line}'"
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local log_line="[$timestamp] [$level] $msg"
# printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null
[ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$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
# 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"
}