From cde17d9298226794f1c5e0cad9116ff31ba1928c Mon Sep 17 00:00:00 2001 From: Endaris Date: Thu, 27 Nov 2025 17:28:08 +0100 Subject: [PATCH 1/2] clarify log level access via an enum and automatically set the right log level for server on startup --- common/lib/logger.lua | 27 ++++++++++++++++----------- serverLauncher.lua | 15 ++++++++++++++- testLauncher.lua | 4 ++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/common/lib/logger.lua b/common/lib/logger.lua index b984665e..8d2aafca 100644 --- a/common/lib/logger.lua +++ b/common/lib/logger.lua @@ -14,49 +14,54 @@ local logger = { messageBuffer = RingBuffer(2048) } -logger.TRACE = 0 -- Log something that is very detailed verbose debug logging -logger.DEBUG = 1 -- Log something that is only useful when debugging -logger.INFO = 2 -- Log something that is useful in most normal conditions -logger.WARN = 3 -- Log something that could be a problem -logger.ERROR = 4 -- Log something that definitely is a problem +---@enum LogLevel +logger.levels = { + TRACE = 0, -- Log something that is very detailed verbose debug logging + DEBUG = 1, -- Log something that is only useful when debugging + INFO = 2, -- Log something that is useful in most normal conditions + WARN = 3, -- Log something that could be a problem + ERROR = 4 -- Log something that definitely is a problem +} -local LOG_LEVEL = logger.DEBUG +---@type LogLevel +local LOG_LEVEL = logger.levels.DEBUG +---@param level LogLevel use logger.levels. to access presets function logger.setLogLevel(level) LOG_LEVEL = level end -- See comments above about when you should use each logging level function logger.trace(msg) - if LOG_LEVEL <= logger.TRACE then + if LOG_LEVEL <= logger.levels.TRACE then direct_log("TRACE", msg); end end -- See comments above about when you should use each logging level function logger.debug(msg) - if LOG_LEVEL <= logger.DEBUG then + if LOG_LEVEL <= logger.levels.DEBUG then direct_log("DEBUG", msg); end end -- See comments above about when you should use each logging level function logger.info(msg) - if LOG_LEVEL <= logger.INFO then + if LOG_LEVEL <= logger.levels.INFO then direct_log(" INFO", msg); end end -- See comments above about when you should use each logging level function logger.warn(msg) - if LOG_LEVEL <= logger.WARN then + if LOG_LEVEL <= logger.levels.WARN then direct_log(" WARN", msg); end end -- See comments above about when you should use each logging level function logger.error(msg) - if LOG_LEVEL <= logger.ERROR then + if LOG_LEVEL <= logger.levels.ERROR then direct_log("ERROR", msg); end end diff --git a/serverLauncher.lua b/serverLauncher.lua index c50a95fb..bf2aca33 100644 --- a/serverLauncher.lua +++ b/serverLauncher.lua @@ -1,6 +1,19 @@ +local logger = require("common.lib.logger") + if arg[1] == "debug" then -- for debugging in visual studio code - pcall(function() require("lldebugger").start() end) + if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then + -- VS Code / VS Codium + require("lldebugger").start() + elseif pcall(function() require("mobdebug") end) then + -- ZeroBrane + -- afaik there is no good way to detect whether the game was started with zerobrane other than trying the require and succeeding + require("mobdebug").start() + require('mobdebug').coro() + end + logger.setLogLevel(logger.levels.DEBUG) +else + logger.setLogLevel(logger.levels.INFO) end -- We must launch the server from the root directory so all the requires are the right path relatively. diff --git a/testLauncher.lua b/testLauncher.lua index b38496d9..f314f582 100644 --- a/testLauncher.lua +++ b/testLauncher.lua @@ -29,9 +29,9 @@ local logger = require("common.lib.logger") -- Set log level based on debug argument if arg[2] == "debug" then - logger.setLogLevel(logger.DEBUG) + logger.setLogLevel(logger.levels.DEBUG) else - logger.setLogLevel(logger.INFO) + logger.setLogLevel(logger.levels.INFO) end require("client.src.globals") From fb42a1896628efe3602303aed0658ac13428b76d Mon Sep 17 00:00:00 2001 From: Endaris Date: Thu, 27 Nov 2025 17:38:40 +0100 Subject: [PATCH 2/2] fix LOG_LEVEL being syntaxed as if it was a constant --- common/lib/logger.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/common/lib/logger.lua b/common/lib/logger.lua index 8d2aafca..5d1a20b1 100644 --- a/common/lib/logger.lua +++ b/common/lib/logger.lua @@ -24,44 +24,44 @@ logger.levels = { } ---@type LogLevel -local LOG_LEVEL = logger.levels.DEBUG +logger.logLevel = logger.levels.DEBUG ---@param level LogLevel use logger.levels. to access presets function logger.setLogLevel(level) - LOG_LEVEL = level + logger.logLevel = level end -- See comments above about when you should use each logging level function logger.trace(msg) - if LOG_LEVEL <= logger.levels.TRACE then + if logger.logLevel <= logger.levels.TRACE then direct_log("TRACE", msg); end end -- See comments above about when you should use each logging level function logger.debug(msg) - if LOG_LEVEL <= logger.levels.DEBUG then + if logger.logLevel <= logger.levels.DEBUG then direct_log("DEBUG", msg); end end -- See comments above about when you should use each logging level function logger.info(msg) - if LOG_LEVEL <= logger.levels.INFO then + if logger.logLevel <= logger.levels.INFO then direct_log(" INFO", msg); end end -- See comments above about when you should use each logging level function logger.warn(msg) - if LOG_LEVEL <= logger.levels.WARN then + if logger.logLevel <= logger.levels.WARN then direct_log(" WARN", msg); end end -- See comments above about when you should use each logging level function logger.error(msg) - if LOG_LEVEL <= logger.levels.ERROR then + if logger.logLevel <= logger.levels.ERROR then direct_log("ERROR", msg); end end