From aacf5fbcfe1e6f2cdb0c6ab19b3e8dd87e3fd488 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 4 May 2019 16:07:24 -0400 Subject: Refactored logging utility refactored logging so that everything is much cleaner. --- gamemode/shared/log.lua | 62 +++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 22 deletions(-) (limited to 'gamemode/shared/log.lua') diff --git a/gamemode/shared/log.lua b/gamemode/shared/log.lua index 8005000..5fc043f 100644 --- a/gamemode/shared/log.lua +++ b/gamemode/shared/log.lua @@ -13,30 +13,48 @@ elseif CLIENT then domain = "[CLIENT]" end ---- Print a debug message. --- This can be suppressed in release versions -function log.debug(...) - MsgC(col.console.gray,domain,"[DEBUG]",table.concat({...}," "),"\n") +local log_levels = { + debug = { + color = col.console.gray,domain, + prefix = "[DEBUG]" + }, + info = { + color = col.console.cyan,domain, + prefix = "[INFO]", + }, + warn = { + color = col.console.yellow,domain, + prefix = "[WARNING]", + }, + error = { + color = col.console.red,domain, + prefix = "[ERROR]", + }, +} +local level = "debug" +local rev_levels = {} +local rev_level_counter = 1 +for k,v in pairs(log_levels) do + rev_levels[k] = rev_level_counter + rev_level_counter = rev_level_counter + 1 end ---- Print an information message. --- Things server owners might want to see -function log.info(...) - MsgC(col.console.cyan,domain,"[INFO]",table.concat({...}," "),"\n") -end - ---- Prints a warning. --- Things developers need to look at --- Maybe print this to a file in the future? -function log.warn(...) - MsgC(col.console.yellow,domain,"[WARNING]",table.concat({...}," "),"\n") -end - ---- Prints an error. --- Things that should never happen --- Maybe have this automatically make bugs on the bug tracker with the above log files? -function log.error(...) - MsgC(col.console.red,domain,"[ERROR]",table.concat({...}," "),"\n") +for mode,settings in pairs(log_levels) do + log[mode] = function(...) + if rev_levels[mode] >= rev_levels[level] then + local args = {...} + local toprint = {domain, settings.prefix} + for _,part in pairs(args) do + toprint[#toprint + 1] = tostring(part) + end + toprint[#toprint + 1] = "\n" + MsgC(settings.color,table.concat(toprint," ")) + end + end + log[mode .. "f"] = function(...) + local args = {...} + log[mode](string.format(unpack(args))) + end end log.report = fn.curry( -- cgit v1.2.3-70-g09d2