mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
61 lines
1.3 KiB
Lua
61 lines
1.3 KiB
Lua
local M = {}
|
|
|
|
local LOG_LEVEL_PRIORITY = {
|
|
debug = 10,
|
|
info = 20,
|
|
warn = 30,
|
|
error = 40,
|
|
}
|
|
|
|
function M.create(ctx)
|
|
local mp = ctx.mp
|
|
local msg = ctx.msg
|
|
local opts = ctx.opts
|
|
|
|
local function normalize_log_level(level)
|
|
local normalized = (level or "info"):lower()
|
|
if LOG_LEVEL_PRIORITY[normalized] then
|
|
return normalized
|
|
end
|
|
return "info"
|
|
end
|
|
|
|
local function should_log(level)
|
|
local current = normalize_log_level(opts.log_level)
|
|
local target = normalize_log_level(level)
|
|
return LOG_LEVEL_PRIORITY[target] >= LOG_LEVEL_PRIORITY[current]
|
|
end
|
|
|
|
local function subminer_log(level, scope, message)
|
|
if not should_log(level) then
|
|
return
|
|
end
|
|
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
|
|
local line = string.format("[subminer] - %s - %s - [%s] %s", timestamp, string.upper(level), scope, message)
|
|
if level == "error" then
|
|
msg.error(line)
|
|
elseif level == "warn" then
|
|
msg.warn(line)
|
|
elseif level == "debug" then
|
|
msg.debug(line)
|
|
else
|
|
msg.info(line)
|
|
end
|
|
end
|
|
|
|
local function show_osd(message)
|
|
if opts.osd_messages then
|
|
mp.osd_message("SubMiner: " .. message, 3)
|
|
end
|
|
end
|
|
|
|
return {
|
|
normalize_log_level = normalize_log_level,
|
|
should_log = should_log,
|
|
subminer_log = subminer_log,
|
|
show_osd = show_osd,
|
|
}
|
|
end
|
|
|
|
return M
|