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)