logging over mqtt, TODO: Write log handler
This commit is contained in:
@@ -1,12 +1,32 @@
|
|||||||
#!/bin/ash
|
#!/bin/ash
|
||||||
# shellcheck shell=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"
|
eventfile="/tmp/es_state.inf"
|
||||||
|
|
||||||
# 1. Check if the file exists and is not empty
|
# 1. Check if the file exists and is not empty
|
||||||
if [ ! -s "$eventfile" ]; then
|
if [ ! -s "$eventfile" ]; then
|
||||||
echo "Error: $eventfile is missing or empty."
|
log "ERROR" "$eventfile is missing or empty."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -14,13 +34,19 @@ fi
|
|||||||
PAYLOAD=$(cat "$eventfile")
|
PAYLOAD=$(cat "$eventfile")
|
||||||
|
|
||||||
# 3. Publish (using double quotes to handle spaces/newlines in the file)
|
# 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
|
# 4. Optional: check if the publish succeeded
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "Successfully published event."
|
log "INFO" "Successfully published event."
|
||||||
else
|
else
|
||||||
echo "Failed to connect to mosquitto broker."
|
log "ERROR" "Failed to connect to mosquitto broker."
|
||||||
|
exit 1
|
||||||
fi
|
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
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
export LOG_FILE="/recalbox/share/system/logs/es_event_monitor.log"
|
LOG_FILE="/recalbox/share/system/logs/savesync_monitor.log"
|
||||||
export DEBUG_MODE=1 # Set to 0 to only log to file, 1 to also print to screen
|
DEBUG_MODE=1
|
||||||
export RNAME="saves"
|
REMOTE_BASE="saves:gamepi-tv"
|
||||||
export RPATH="gamepi-tv"
|
|
||||||
|
|||||||
@@ -2,20 +2,34 @@
|
|||||||
# shellcheck shell=ash
|
# shellcheck shell=ash
|
||||||
|
|
||||||
# --- Configuration ---
|
# --- Configuration ---
|
||||||
LOG_FILE="/recalbox/share/system/logs/es_event_monitor.log"
|
. /recalbox/share/system/config/savesync/savesync.conf
|
||||||
DEBUG_MODE=1
|
|
||||||
REMOTE_BASE="saves:gamepi-tv"
|
|
||||||
|
# --- MQTT Publish Function ---
|
||||||
|
mqtt_publish() {
|
||||||
|
|
||||||
|
local msg="$1"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# --- Logger Function ---
|
# --- Logger Function ---
|
||||||
log() {
|
log() {
|
||||||
local level="$1"
|
local timestamp
|
||||||
local msg="$2"
|
local level="$1"
|
||||||
mkdir -p "$(dirname "$LOG_FILE")"
|
local msg="$2"
|
||||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
mkdir -p "$(dirname "$LOG_FILE")"
|
||||||
local log_line="[$timestamp] [$level] $msg"
|
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
|
local log_line="[$timestamp] [$level] $msg"
|
||||||
|
|
||||||
printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null
|
mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "SaveLog=${log_line}"
|
||||||
[ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line"
|
|
||||||
|
# printf "%s\n" "$log_line" >> "$LOG_FILE" 2>/dev/null
|
||||||
|
[ "$DEBUG_MODE" -eq 1 ] && printf "%s\n" "$log_line"
|
||||||
|
}
|
||||||
|
|
||||||
|
send_continue() {
|
||||||
|
mosquitto_pub -h 127.0.0.1 -p 1883 -t /Recalbox/EmulationStation/Event "SaveContinue=1"
|
||||||
}
|
}
|
||||||
|
|
||||||
log "INFO" "--- ES Event Daemon Started ---"
|
log "INFO" "--- ES Event Daemon Started ---"
|
||||||
@@ -31,7 +45,8 @@ do
|
|||||||
"GamePath")
|
"GamePath")
|
||||||
this_game_path="$value"
|
this_game_path="$value"
|
||||||
# Logic: Change .gba/.zip to .srm
|
# Logic: Change .gba/.zip to .srm
|
||||||
this_save_path="${value%.*}.srm"
|
this_save_path="$(echo "${value%.*}.srm" | sed 's|roms|saves|')"
|
||||||
|
this_backup_path="$(dirname "$this_save_path" | sed 's|roms|archives|')"
|
||||||
;;
|
;;
|
||||||
"Action") this_action="$value" ;;
|
"Action") this_action="$value" ;;
|
||||||
"State")
|
"State")
|
||||||
@@ -52,7 +67,8 @@ do
|
|||||||
|
|
||||||
if [ "$local_size" -lt "$remote_size" ]; then
|
if [ "$local_size" -lt "$remote_size" ]; then
|
||||||
log "WARN" "Cloud save is LARGER ($remote_size vs $local_size). Restoring..."
|
log "WARN" "Cloud save is LARGER ($remote_size vs $local_size). Restoring..."
|
||||||
rclone copyto "$remote_full" "$this_save_path"
|
mkdir -p "$(dirname "${this_backup_path}")"
|
||||||
|
rclone copyto "$remote_full" "$this_save_path" --backup-dir "$this_backup_path"
|
||||||
else
|
else
|
||||||
log "INFO" "Local save is safe. Running update check..."
|
log "INFO" "Local save is safe. Running update check..."
|
||||||
rclone update "$remote_full" "$this_save_path"
|
rclone update "$remote_full" "$this_save_path"
|
||||||
|
|||||||
Reference in New Issue
Block a user