Use constants for log levels

This commit is contained in:
crater2150 2014-04-25 15:14:42 +02:00
parent 97b844e6fd
commit e392d4842d
2 changed files with 12 additions and 20 deletions

4
rc.lua
View file

@ -14,8 +14,8 @@ layouts = require('layouts')
-- {{{ Logging -- {{{ Logging
log = require("simplelog") log = require("simplelog")
log.add_logger(log.loggers.stdio, 0) log.add_logger(log.loggers.stdio, log.level.debug)
log.add_logger(log.loggers.naughty, 2) log.add_logger(log.loggers.naughty, log.level.warn)
-- }}} -- }}}

View file

@ -12,7 +12,7 @@ for key, value in pairs(defaults) do
settings[key] = value settings[key] = value
end end
level = { local level = {
ERROR = 3, ERROR = 3,
WARNING = 2, WARNING = 2,
NORMAL = 1, NORMAL = 1,
@ -20,33 +20,29 @@ level = {
} }
simplelog.level = level simplelog.level = level
function loglv(msg, level) local function loglv(msg, level)
for _,logger in ipairs(settings.loggers) do for _,logger in ipairs(settings.loggers) do
logger(msg, level) logger(msg, level)
end end
end end
function dbg(msg) function simplelog.dbg(msg)
loglv(msg, 0) loglv(msg, 0)
end end
simplelog.debug = dbg
function log(msg) function simplelog.log(msg)
loglv(msg, 1) loglv(msg, 1)
end end
simplelog.log = log
function warn(msg) function simplelog.warn(msg)
loglv(msg, 2) loglv(msg, 2)
end end
simplelog.warn = warn
function error(msg) function simplelog.error(msg)
loglv(msg, 3) loglv(msg, 3)
end end
simplelog.error = error
function add_logger(logger, level) function simplelog.add_logger(logger, level)
if level == nil then if level == nil then
level = settings.defaultlevel level = settings.defaultlevel
end end
@ -56,9 +52,8 @@ function add_logger(logger, level)
end end
end) end)
end end
simplelog.add_logger = add_logger
function logger_naughty(msg, severity) function simplelog.loggers.naughty(msg, severity)
if severity == level.WARNING then if severity == level.WARNING then
msg = "<span color=\"#ff6\">".. msg .. "</span>" msg = "<span color=\"#ff6\">".. msg .. "</span>"
elseif severity == level.ERROR then elseif severity == level.ERROR then
@ -66,14 +61,11 @@ function logger_naughty(msg, severity)
end end
naughty.notify({ text = msg }) naughty.notify({ text = msg })
end end
simplelog.loggers.naughty = logger_naughty
function simplelog.loggers.stdio(msg, severity)
function logger_print(msg, severity)
print(msg) print(msg)
end end
simplelog.loggers.stdio = logger_print
simplelog.mt.__call = log simplelog.mt.__call = simplelog.log
return setmetatable(simplelog, simplelog.mt) return setmetatable(simplelog, simplelog.mt)