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