From 71db1d99731d8a35b480265b9cd7d2d538e9c75a Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 22 Mar 2026 01:59:15 -0400 Subject: [PATCH] Add structured logging functionality to enhance debugging capabilities --- lib/log.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/log.lua diff --git a/lib/log.lua b/lib/log.lua new file mode 100644 index 0000000..f8ef392 --- /dev/null +++ b/lib/log.lua @@ -0,0 +1,45 @@ +-- lib/log.lua — Structured logging for CC:Tweaked programs +-- Usage: +-- local log = dofile("lib/log.lua") +-- log.info("SMELT", "Loaded %d recipes", count) +-- log.warn("NET", "No modem found") +-- log.setLevel("DEBUG") -- show everything +-- +-- Levels: DEBUG < INFO < WARN < ERROR +-- Default level: INFO (suppresses DEBUG) + +local LEVELS = { DEBUG = 1, INFO = 2, WARN = 3, ERROR = 4 } +local minLevel = LEVELS.INFO + +local log = {} + +--- Set minimum log level (string: "DEBUG", "INFO", "WARN", "ERROR") +function log.setLevel(level) + minLevel = LEVELS[level] or LEVELS.INFO +end + +--- Get the current minimum level name +function log.getLevel() + for name, val in pairs(LEVELS) do + if val == minLevel then return name end + end + return "INFO" +end + +local function emit(levelName, tag, msg, ...) + if LEVELS[levelName] < minLevel then return end + local text + if select("#", ...) > 0 then + text = string.format(msg, ...) + else + text = msg + end + print(string.format("[%s][%s] %s", levelName, tag, text)) +end + +function log.debug(tag, msg, ...) emit("DEBUG", tag, msg, ...) end +function log.info(tag, msg, ...) emit("INFO", tag, msg, ...) end +function log.warn(tag, msg, ...) emit("WARN", tag, msg, ...) end +function log.error(tag, msg, ...) emit("ERROR", tag, msg, ...) end + +return log