Skip to content

Commit 2a5c041

Browse files
authored
Improve Server Launch Setup (#731)
* clarify log level access via an enum and automatically set the right log level for server on startup * fix LOG_LEVEL being syntaxed as if it was a constant
1 parent 7f4f283 commit 2a5c041

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

common/lib/logger.lua

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,54 @@ local logger = {
1414
messageBuffer = RingBuffer(2048)
1515
}
1616

17-
logger.TRACE = 0 -- Log something that is very detailed verbose debug logging
18-
logger.DEBUG = 1 -- Log something that is only useful when debugging
19-
logger.INFO = 2 -- Log something that is useful in most normal conditions
20-
logger.WARN = 3 -- Log something that could be a problem
21-
logger.ERROR = 4 -- Log something that definitely is a problem
17+
---@enum LogLevel
18+
logger.levels = {
19+
TRACE = 0, -- Log something that is very detailed verbose debug logging
20+
DEBUG = 1, -- Log something that is only useful when debugging
21+
INFO = 2, -- Log something that is useful in most normal conditions
22+
WARN = 3, -- Log something that could be a problem
23+
ERROR = 4 -- Log something that definitely is a problem
24+
}
2225

23-
local LOG_LEVEL = logger.DEBUG
26+
---@type LogLevel
27+
logger.logLevel = logger.levels.DEBUG
2428

29+
---@param level LogLevel use logger.levels. to access presets
2530
function logger.setLogLevel(level)
26-
LOG_LEVEL = level
31+
logger.logLevel = level
2732
end
2833

2934
-- See comments above about when you should use each logging level
3035
function logger.trace(msg)
31-
if LOG_LEVEL <= logger.TRACE then
36+
if logger.logLevel <= logger.levels.TRACE then
3237
direct_log("TRACE", msg);
3338
end
3439
end
3540

3641
-- See comments above about when you should use each logging level
3742
function logger.debug(msg)
38-
if LOG_LEVEL <= logger.DEBUG then
43+
if logger.logLevel <= logger.levels.DEBUG then
3944
direct_log("DEBUG", msg);
4045
end
4146
end
4247

4348
-- See comments above about when you should use each logging level
4449
function logger.info(msg)
45-
if LOG_LEVEL <= logger.INFO then
50+
if logger.logLevel <= logger.levels.INFO then
4651
direct_log(" INFO", msg);
4752
end
4853
end
4954

5055
-- See comments above about when you should use each logging level
5156
function logger.warn(msg)
52-
if LOG_LEVEL <= logger.WARN then
57+
if logger.logLevel <= logger.levels.WARN then
5358
direct_log(" WARN", msg);
5459
end
5560
end
5661

5762
-- See comments above about when you should use each logging level
5863
function logger.error(msg)
59-
if LOG_LEVEL <= logger.ERROR then
64+
if logger.logLevel <= logger.levels.ERROR then
6065
direct_log("ERROR", msg);
6166
end
6267
end

serverLauncher.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
local logger = require("common.lib.logger")
2+
13
if arg[1] == "debug" then
24
-- for debugging in visual studio code
3-
pcall(function() require("lldebugger").start() end)
5+
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
6+
-- VS Code / VS Codium
7+
require("lldebugger").start()
8+
elseif pcall(function() require("mobdebug") end) then
9+
-- ZeroBrane
10+
-- afaik there is no good way to detect whether the game was started with zerobrane other than trying the require and succeeding
11+
require("mobdebug").start()
12+
require('mobdebug').coro()
13+
end
14+
logger.setLogLevel(logger.levels.DEBUG)
15+
else
16+
logger.setLogLevel(logger.levels.INFO)
417
end
518

619
-- We must launch the server from the root directory so all the requires are the right path relatively.

testLauncher.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ local logger = require("common.lib.logger")
2929

3030
-- Set log level based on debug argument
3131
if arg[2] == "debug" then
32-
logger.setLogLevel(logger.DEBUG)
32+
logger.setLogLevel(logger.levels.DEBUG)
3333
else
34-
logger.setLogLevel(logger.INFO)
34+
logger.setLogLevel(logger.levels.INFO)
3535
end
3636

3737
require("client.src.globals")

0 commit comments

Comments
 (0)