logger now allows log messages >= 4096 chars

when displaying big tables they often got cut off before
their end. This is avoided by splitting log messages at the
4000th char.
This commit is contained in:
Lukas Kropatschek 2016-01-12 14:59:33 +01:00
parent f1a3c3e0f2
commit 42e67cf481

135
mist.lua
View File

@ -6362,6 +6362,53 @@ end
do -- mist.Logger scope do -- mist.Logger scope
mist.Logger = {} mist.Logger = {}
--- parses text and substitutes keywords with values from given array.
-- @param text string containing keywords to substitute with values
-- or a variable.
-- @param ... variables to use for substitution in string.
-- @treturn string new string with keywords substituted or
-- value of variable as string.
local function formatText(text, ...)
if type(text) ~= 'string' then
if type(text) == 'table' then
text = mist.utils.oneLineSerialize(text)
else
text = tostring(text)
end
else
for index,value in ipairs(arg) do
-- TODO: check for getmetatabel(value).__tostring
if type(value) == 'table' then
value = mist.utils.oneLineSerialize(value)
else
value = tostring(value)
end
text = text:gsub('$' .. index, value)
end
end
local dInfo = debug.getinfo(3)
local fName = dInfo.name
-- local fsrc = dinfo.short_src
--local fLine = dInfo.linedefined
local cLine = dInfo.currentline
if fName then
return fName .. '|' .. cLine .. ': ' .. text
else
return cLine .. ': ' .. text
end
end
local function splitText(text)
local tbl = {}
while text:len() > 4000 do
local sub = text:sub(1, 4000)
text = text:sub(4001)
table.insert(tbl, sub)
end
table.insert(tbl, text)
return tbl
end
--- 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 string tag tag which appears at the start of -- @tparam string tag tag which appears at the start of
@ -6407,41 +6454,6 @@ do -- mist.Logger scope
end end
end end
--- parses text and substitutes keywords with values from given array.
-- @tparam string text string containing keywords to substitute with values
-- @param ... variables to use for substitution
-- @treturn string new string with keywords substituted
local function formatText(text, ...)
for index,value in ipairs(arg) do
-- TODO: check for getmetatabel(value).__tostring
if type(value) == 'table' then
value = mist.utils.oneLineSerialize(value)
else
value = tostring(value)
end
text = text:gsub('$' .. index, value)
end
local dInfo = debug.getinfo(3)
local fName = dInfo.name
-- local fsrc = dinfo.short_src
--local fLine = dInfo.linedefined
local cLine = dInfo.currentline
if fName then
return fName .. '|' .. cLine .. ': ' .. text
else
return cLine .. ': ' .. text
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
@ -6450,13 +6462,20 @@ 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))
if text:len() > 4000 then
local texts = splitText(text)
for i = 1, #texts do
if i == 1 then
env.error(self.tag .. '|' .. texts[i], true)
else
env.error(texts[i])
end end
end
else
env.error(self.tag .. '|' .. text, true) env.error(self.tag .. '|' .. text, true)
end end
end
--- Logs an error. --- Logs an error.
-- logs a message prefixed with this loggers tag to dcs.log as -- logs a message prefixed with this loggers tag to dcs.log as
@ -6467,14 +6486,21 @@ 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))
if text:len() > 4000 then
local texts = splitText(text)
for i = 1, #texts do
if i == 1 then
env.error(self.tag .. '|' .. texts[i])
else
env.error(texts[i])
end end
end
else
env.error(self.tag .. '|' .. text) env.error(self.tag .. '|' .. text)
end end
end end
end
--- Logs a warning. --- Logs a warning.
-- logs a message prefixed with this loggers tag to dcs.log as -- logs a message prefixed with this loggers tag to dcs.log as
@ -6484,14 +6510,21 @@ 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))
if text:len() > 4000 then
local texts = splitText(text)
for i = 1, #texts do
if i == 1 then
env.warning(self.tag .. '|' .. texts[i])
else
env.warning(texts[i])
end end
end
else
env.warning(self.tag .. '|' .. text) env.warning(self.tag .. '|' .. text)
end end
end end
end
--- Logs a info. --- Logs a info.
-- logs a message prefixed with this loggers tag to dcs.log as -- logs a message prefixed with this loggers tag to dcs.log as
@ -6501,16 +6534,24 @@ 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))
if text:len() > 4000 then
local texts = splitText(text)
for i = 1, #texts do
if i == 1 then
env.info(self.tag .. '|' .. texts[i])
else
env.info(texts[i])
end end
end
else
env.info(self.tag .. '|' .. text) env.info(self.tag .. '|' .. text)
end end
end end
end end
end
-- initialize mist -- initialize mist
mist.init() mist.init()