commit d86ec87c95ae47ac634cc7f43f490eacf49f7d98 Author: x1a0 Date: Fri May 15 14:38:26 2026 +0200 Hello, blightmud-batmud-logger diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..bd043f9 --- /dev/null +++ b/.luarc.json @@ -0,0 +1,3 @@ +{ + "Lua.diagnostics.globals": ["cformat", "mud"] +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..572c07b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM alpine:3.19 + +# zstd for compression, tzdata so cron fires at correct local time +RUN apk add --no-cache zstd tzdata + +COPY compress.sh /usr/local/bin/compress.sh +RUN chmod +x /usr/local/bin/compress.sh + +# Install crontab: run compress at 02:00 every night +RUN echo "0 2 * * * /usr/local/bin/compress.sh > /proc/1/fd/1 2>&1" \ + | crontab - + +# crond -f: foreground (required for containers) +# crond -l 2: log level warning and above +CMD ["crond", "-f", "-l", "2"] diff --git a/compress.sh b/compress.sh new file mode 100644 index 0000000..478e100 --- /dev/null +++ b/compress.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env sh +# Compress all .log files not modified today across all character subdirs. +# Expects /logs to be mounted from the host. + +LOG_DIR="${LOG_DIR:-/logs}" +TODAY=$(date +%Y-%m-%d) + +find "$LOG_DIR" -maxdepth 2 -name "*.log" | while read -r f; do + FILE_DATE=$(date -r "$f" +%Y-%m-%d 2>/dev/null) + + if [ "$FILE_DATE" = "$TODAY" ]; then + echo "[$(date +%H:%M:%S)] Skipping active log: $f" + continue + fi + + echo "[$(date +%H:%M:%S)] Compressing: $f" + if zstd -19 --rm -q "$f"; then + echo "[$(date +%H:%M:%S)] Done: ${f}.zst" + else + echo "[$(date +%H:%M:%S)] ERROR compressing: $f" >&2 + fi +done + +echo "[$(date +%H:%M:%S)] Compression run complete." diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..08f0d3f --- /dev/null +++ b/main.lua @@ -0,0 +1,44 @@ +OUTPUT = ":" +INPUT = ">" + +local current_log_filepath = nil +local current_log_file = nil + +local get_current_log_filepath = function() + local date = os.date("%Y-%m-%d") + local data_home = os.getenv("XDG_DATA_HOME") or string.format("%s/.local/share", os.getenv("HOME")) + return string.format("%s/blightmud/logs/batmud/%s/%s.log", data_home, os.getenv("BATMUD_CHAR"), date) +end + +local write_log = function(line, type) + local path = get_current_log_filepath() + if current_log_filepath == nil or current_log_filepath ~= path then + current_log_filepath = path + os.execute(string.format("mkdir -p %q", path:match("^(.*)/[^/]+$"))) + current_log_file = assert(io.open(path, "a")) + end + + local log_line = cformat("[%s]%s %s\n", os.date("%H:%M:%S"), type, line) + if current_log_file ~= nil then + assert(current_log_file:write(log_line)) + assert(current_log_file:flush()) + end +end + +mud.add_output_listener(function(line) + if not line:skip_log() then + if line:replacement() == nil then + write_log(line:raw(), OUTPUT) + else + write_log(line:replacement(), OUTPUT) + end + end + return line +end) + +mud.add_input_listener(function(line) + if not line:skip_log() then + write_log(line:raw(), INPUT) + end + return line +end)