logging over mqtt, TODO: Write log handler

This commit is contained in:
Sonja Rivierien
2026-02-27 16:26:27 -06:00
parent 276b84b9aa
commit 3d766f28d8
3 changed files with 65 additions and 24 deletions

View File

@@ -1,12 +1,32 @@
#!/bin/ash
# shellcheck shell=ash
#
#
ERROR_LOG="/recalbox/share/system/logs/savesync-error.log"
# --- 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"
if [ "$level" = "ERROR" ]; then
printf "%s\n" "$log_line" >> "$ERROR_LOG"
fi
mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "SaveLog=${log_line}"
# printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null
[ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line"
}
eventfile="/tmp/es_state.inf"
# 1. Check if the file exists and is not empty
if [ ! -s "$eventfile" ]; then
echo "Error: $eventfile is missing or empty."
log "ERROR" "$eventfile is missing or empty."
exit 1
fi
@@ -14,13 +34,19 @@ fi
PAYLOAD=$(cat "$eventfile")
# 3. Publish (using double quotes to handle spaces/newlines in the file)
mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event -m "$PAYLOAD"
mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "$PAYLOAD"
# 4. Optional: check if the publish succeeded
if [ $? -eq 0 ]; then
echo "Successfully published event."
log "INFO" "Successfully published event."
else
echo "Failed to connect to mosquitto broker."
log "ERROR" "Failed to connect to mosquitto broker."
exit 1
fi
sleep 4
mosquitto_sub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event | while IFS="=" read -r key value
do
case "$key" in
"SaveSync") exit "$value" ;;
esac
done