mirror of
https://github.com/mrSkortch/MissionScriptingTools.git
synced 2025-08-15 10:47:23 +00:00
fixed logger and added alert
This commit is contained in:
83
mist.lua
83
mist.lua
@@ -24,7 +24,7 @@ mist.build = 60
|
|||||||
--- Logger class.
|
--- Logger class.
|
||||||
-- @type mist.Logger
|
-- @type mist.Logger
|
||||||
mist.Logger = {
|
mist.Logger = {
|
||||||
tag = "",
|
tag = "MIST",
|
||||||
level = 1,
|
level = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,69 +69,70 @@ do
|
|||||||
|
|
||||||
--- parses text and substitutes keywords with values from given array
|
--- parses text and substitutes keywords with values from given array
|
||||||
-- @tparam string text string containing keywords to substitute with values
|
-- @tparam string text string containing keywords to substitute with values
|
||||||
-- @tparam array vars values to use for substitution
|
-- @param ... variables to use for substitution
|
||||||
-- @treturn string new string with keywords substituted
|
-- @treturn string new string with keywords substituted
|
||||||
local function substituteKeys(text, vars)
|
local function formatText(text, ...)
|
||||||
local substText = text
|
for index,value in ipairs(arg) do
|
||||||
for identifier in text:gmatch("%%%d") do
|
-- TODO: check for getmetatabel(value).__tostring
|
||||||
local index = tonumber(identifier:match("%d+"))
|
|
||||||
local value = vars[index]
|
|
||||||
if type(value) == 'table' then
|
if type(value) == 'table' then
|
||||||
value = mist.utils.oneLineSerialize(value)
|
value = mist.utils.oneLineSerialize(value)
|
||||||
|
else
|
||||||
|
value = tostring(value)
|
||||||
end
|
end
|
||||||
substText:gsub(identifier, value)
|
text = text:gsub('$' .. index, value)
|
||||||
end
|
end
|
||||||
return substText
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Logs error and shows alert window.
|
||||||
|
-- This logs an error to the dcs.log and shows a popup window,
|
||||||
|
-- pausing the simulation. This works always even if logging is
|
||||||
|
-- disabled by setting a log level of "none" or 0.
|
||||||
|
-- @tparam string text the text with keywords to substitute.
|
||||||
|
-- @param ... variables to be used for substitution.
|
||||||
|
-- @usage myLogger:alert("Shit just hit the fan! WEEEE!!!11")
|
||||||
|
function mist.Logger:alert(text, ...)
|
||||||
|
text = formatText(text, unpack(arg))
|
||||||
|
env.error(self.tag .. ' | ' .. text, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Logs an error.
|
--- Logs an error.
|
||||||
-- logs to dcs.log as long as at least the "error" log level is set.
|
-- logs a message prefixed with this loggers tag to dcs.log as
|
||||||
|
-- long as at least the "error" log level (1) is set.
|
||||||
-- @tparam string text the text with keywords to substitute.
|
-- @tparam string text the text with keywords to substitute.
|
||||||
-- @tparam[opt] table vars this the values to be used for substitution.
|
-- @param ... variables to be used for substitution.
|
||||||
-- @tparam[opt] boolean showBox whether to show a box and pause the game.
|
|
||||||
-- @usage myLogger:error("Just an error!")
|
-- @usage myLogger:error("Just an error!")
|
||||||
-- @usage myLogger:error("This is a serious error! Foo is %1 instead of 'bar'", {'derp'})
|
-- @usage myLogger:error("Foo is $1 instead of $2", foo, "bar")
|
||||||
-- @usage myLogger:error("It's a trap!", {}, true)
|
function mist.Logger:error(text, ...)
|
||||||
function mist.Logger:error(text, vars, showBox)
|
|
||||||
showBox = showBox or false
|
|
||||||
if self.level >= 1 then
|
if self.level >= 1 then
|
||||||
if vars then
|
text = formatText(text, unpack(arg))
|
||||||
-- check if table is empty
|
env.error(self.tag .. ' | ' .. text)
|
||||||
if not next(vars) == nil then
|
|
||||||
text = subsituteKeys(text, vars)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
env.error(tag .. ' | ' .. text, showBox)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Logs a warning.
|
--- Logs a warning.
|
||||||
|
-- logs a message prefixed with this loggers tag to dcs.log as
|
||||||
|
-- long as at least the "warning" log level (2) is set.
|
||||||
-- @tparam string text the text with keywords to substitute.
|
-- @tparam string text the text with keywords to substitute.
|
||||||
-- @tparam[opt] table vars this the values to be used for substitution.
|
-- @param ... variables to be used for substitution.
|
||||||
-- @usage myLogger:warn("I 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, vars)
|
function mist.Logger:warn(text, ...)
|
||||||
if self.level >= 2 then
|
if self.level >= 2 then
|
||||||
if vars then
|
text = formatText(text, unpack(arg))
|
||||||
if not next(vars) == nil then
|
env.warning(self.tag .. ' | ' .. text)
|
||||||
text = subsituteKeys(text, vars)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
env.warning(tag .. ' | ' .. text)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Logs a info
|
--- Logs a info
|
||||||
|
-- logs a message prefixed with this loggers tag to dcs.log as
|
||||||
|
-- long as the highest log level (3) "info" is set.
|
||||||
-- @tparam string text the text with keywords to substitute.
|
-- @tparam string text the text with keywords to substitute.
|
||||||
-- @tparam[opt] table vars this the values to be used for substitution.
|
-- @param ... variables to be used for substitution.
|
||||||
-- @see warn
|
-- @see warn
|
||||||
function mist.Logger:info(text, vars)
|
function mist.Logger:info(text, ...)
|
||||||
if self.level >= 4 then
|
if self.level >= 3 then
|
||||||
if vars then
|
text = formatText(text, unpack(arg))
|
||||||
if not next(vars) == nil then
|
env.info(self.tag .. ' | ' .. text)
|
||||||
text = subsituteKeys(text, vars)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
env.info(tag .. ' | ' .. text)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user