improved logger

This commit is contained in:
Lukas Kropatschek 2016-01-09 20:26:38 +01:00
parent 5803be3904
commit 4d0617a963

View File

@ -2823,34 +2823,30 @@ do -- mist.Logger scope
--- Creates a new logger. --- Creates a new logger.
-- Each logger has it's own tag and log level. -- Each logger has it's own tag and log level.
-- @tparam[opt] table l optional logger object. -- @tparam string tag tag which appears at the start of
-- @usage myLogger = mist.Logger:new -- every log line produced by this logger.
-- @usage myLogger = mist.Logger:new{tag = "MyScript", level = 2} -- @tparam[opt] number|string level the log level defines which messages
-- will be logged and which will be omitted. Log level 3 beeing the most verbose
-- and 0 disabling all output. This can also be a string. Allowed strings are:
-- "none" (0), "error" (1), "warning" (2) and "info" (3).
-- @usage myLogger = mist.Logger:new("MyScript")
-- @usage myLogger = mist.Logger:new("MyScript", 2)
-- @usage myLogger = mist.Logger:new("MyScript", "info")
-- @treturn mist.Logger -- @treturn mist.Logger
function mist.Logger:new(l) function mist.Logger:new(tag, level)
l = l or { local l = {}
tag = "MIST", l.tag = tag
level = 1,
}
-- in case the user only supplied
-- either tag or level
if not l.tag then
l.tag = "MIST"
end
if not l.level then
l.level = 1
end
setmetatable(l, self) setmetatable(l, self)
self.__index = self self.__index = self
mist.Logger.setLevel(self, level)
return l return l
end end
--- Sets the level of verbosity for this logger. --- Sets the level of verbosity for this logger.
-- "none" or 0 disables all logging -- @tparam[opt] number|string level the log level defines which messages
-- "error" or 1 shows error messages only -- will be logged and which will be omitted. Log level 3 beeing the most verbose
-- "warning" or 2 shows error and warning messages -- and 0 disabling all output. This can also be a string. Allowed strings are:
-- "info" or 3 shows error, warning and info messages -- "none" (0), "error" (1), "warning" (2) and "info" (3).
-- @param level this can either be a string or an integer level
-- @usage myLogger:setLevel("info") -- @usage myLogger:setLevel("info")
-- @usage -- log everything -- @usage -- log everything
--myLogger:setLevel(3) --myLogger:setLevel(3)
@ -2896,6 +2892,15 @@ do -- mist.Logger scope
end end
end end
local function serializeVar(var)
if type(var) == 'table' then
var = mist.utils.oneLineSerialize(var)
else
var = tostring(var)
end
return var
end
--- Logs error and shows alert window. --- Logs error and shows alert window.
-- This logs an error to the dcs.log and shows a popup window, -- This logs an error to the dcs.log and shows a popup window,
-- pausing the simulation. This works always even if logging is -- pausing the simulation. This works always even if logging is
@ -2904,7 +2909,11 @@ do -- mist.Logger scope
-- @param ... variables to be used for substitution. -- @param ... variables to be used for substitution.
-- @usage myLogger:alert("Shit just hit the fan! WEEEE!!!11") -- @usage myLogger:alert("Shit just hit the fan! WEEEE!!!11")
function mist.Logger:alert(text, ...) function mist.Logger:alert(text, ...)
if type(text) ~= 'string' then
text = serializeVar(text)
else
text = formatText(text, unpack(arg)) text = formatText(text, unpack(arg))
end
env.error(self.tag .. '|' .. text, true) env.error(self.tag .. '|' .. text, true)
end end
@ -2917,7 +2926,11 @@ do -- mist.Logger scope
-- @usage myLogger:error("Foo is $1 instead of $2", foo, "bar") -- @usage myLogger:error("Foo is $1 instead of $2", foo, "bar")
function mist.Logger:error(text, ...) function mist.Logger:error(text, ...)
if self.level >= 1 then if self.level >= 1 then
if type(text) ~= 'string' then
text = serializeVar(text)
else
text = formatText(text, unpack(arg)) text = formatText(text, unpack(arg))
end
env.error(self.tag .. '|' .. text) env.error(self.tag .. '|' .. text)
end end
end end
@ -2930,7 +2943,11 @@ do -- mist.Logger scope
-- @usage myLogger:warn("Mother warned you! Those $1 from the interwebs are $2", {"geeks", 1337}) -- @usage myLogger:warn("Mother warned you! Those $1 from the interwebs are $2", {"geeks", 1337})
function mist.Logger:warn(text, ...) function mist.Logger:warn(text, ...)
if self.level >= 2 then if self.level >= 2 then
if type(text) ~= 'string' then
text = serializeVar(text)
else
text = formatText(text, unpack(arg)) text = formatText(text, unpack(arg))
end
env.warning(self.tag .. '|' .. text) env.warning(self.tag .. '|' .. text)
end end
end end
@ -2943,7 +2960,11 @@ do -- mist.Logger scope
-- @see warn -- @see warn
function mist.Logger:info(text, ...) function mist.Logger:info(text, ...)
if self.level >= 3 then if self.level >= 3 then
if type(text) ~= 'string' then
text = serializeVar(text)
else
text = formatText(text, unpack(arg)) text = formatText(text, unpack(arg))
end
env.info(self.tag .. '|' .. text) env.info(self.tag .. '|' .. text)
end end
end end