switched to new logger

created a init function, creating a logger and calling main
This commit is contained in:
Lukas Kropatschek 2016-01-07 21:07:57 +01:00
parent 33aa8ec3d5
commit 5803be3904

120
mist.lua
View File

@ -21,6 +21,9 @@ mist.majorVersion = 4
mist.minorVersion = 0 mist.minorVersion = 0
mist.build = 60 mist.build = 60
-- forward declaration of log shorthand
local log
do -- the main scope do -- the main scope
local coroutines = {} local coroutines = {}
@ -45,7 +48,6 @@ do -- the main scope
mist.nextGroupId = 1 mist.nextGroupId = 1
mist.nextUnitId = 1 mist.nextUnitId = 1
local function updateAliveUnits() -- coroutine function local function updateAliveUnits() -- coroutine function
local lalive_units = mist.DBs.aliveUnits -- local references for faster execution local lalive_units = mist.DBs.aliveUnits -- local references for faster execution
local lunits = mist.DBs.unitsByNum local lunits = mist.DBs.unitsByNum
@ -105,9 +107,9 @@ do -- the main scope
elseif StaticObject.getByName(event) then elseif StaticObject.getByName(event) then
newObject = StaticObject.getByName(event) newObject = StaticObject.getByName(event)
newType = 'static' newType = 'static'
-- env.info('its static') -- log:info('its static')
else else
env.info('WTF') log:info('WTF')
return false return false
end end
@ -413,8 +415,28 @@ do -- the main scope
end end
end end
--- init function.
-- creates logger, adds default event handler
-- and calls main the first time.
function mist.init()
-- create logger
mist.log = mist.Logger:new()
log = mist.log
-- set info log level
log:setLevel("info")
-- add event handler for group spawns
mist.addEventHandler(groupSpawned)
-- call main the first time therafter it reschedules itself.
mist.main()
log:info('MIST version $1.$2.$3 loaded', mist.majorVersion, mist.minorVersion, mist.build)
end
--- The main function. --- The main function.
-- Run 100 times per second. -- Run 100 times per second.
-- You shouldn't call this function.
function mist.main() function mist.main()
timer.scheduleFunction(mist.main, {}, timer.getTime() + 0.01) --reschedule first in case of Lua error timer.scheduleFunction(mist.main, {}, timer.getTime() + 0.01) --reschedule first in case of Lua error
@ -708,7 +730,7 @@ do -- the main scope
newGroup.units[unitIndex].alt_type = 'RADIO' newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 60 newGroup.units[unitIndex].speed = 60
else else
--[[env.info('check height') --[[log:info('check height')
newGroup.units[unitIndex].alt = land.getHeight({x = newGroup.units[unitIndex].x, y = newGroup.units[unitIndex].y}) newGroup.units[unitIndex].alt = land.getHeight({x = newGroup.units[unitIndex].x, y = newGroup.units[unitIndex].y})
newGroup.units[unitIndex].alt_type = 'BARO']] newGroup.units[unitIndex].alt_type = 'BARO']]
end end
@ -811,7 +833,7 @@ do -- the main scope
table.remove(Tasks, i) table.remove(Tasks, i)
local err, errmsg = pcall(Task.f, unpack(Task.vars, 1, table.maxn(Task.vars))) local err, errmsg = pcall(Task.f, unpack(Task.vars, 1, table.maxn(Task.vars)))
if not err then if not err then
env.info('mist.scheduleFunction, error in scheduled function: ' .. errmsg) log:error('mist.scheduleFunction, error in scheduled function: $1', errmsg)
end end
--Task.f(unpack(Task.vars, 1, table.maxn(Task.vars))) -- do the task, do not increment i --Task.f(unpack(Task.vars, 1, table.maxn(Task.vars))) -- do the task, do not increment i
else else
@ -825,7 +847,7 @@ do -- the main scope
Task.t = timer.getTime() + Task.rep --schedule next run Task.t = timer.getTime() + Task.rep --schedule next run
local err, errmsg = pcall(Task.f, unpack(Task.vars, 1, table.maxn(Task.vars))) local err, errmsg = pcall(Task.f, unpack(Task.vars, 1, table.maxn(Task.vars)))
if not err then if not err then
env.info('mist.scheduleFunction, error in scheduled function: ' .. errmsg) log:error('mist.scheduleFunction, error in scheduled function: $1' .. errmsg)
end end
--Tasks[i].f(unpack(Tasks[i].vars, 1, table.maxn(Tasks[i].vars))) -- do the task --Tasks[i].f(unpack(Tasks[i].vars, 1, table.maxn(Tasks[i].vars))) -- do the task
i = i + 1 i = i + 1
@ -1137,7 +1159,7 @@ do -- the main scope
end end
return { Heading = Heading, Pitch = Pitch, Roll = Roll, Yaw = Yaw, AoA = AoA, ClimbAngle = ClimbAngle} return { Heading = Heading, Pitch = Pitch, Roll = Roll, Yaw = Yaw, AoA = AoA, ClimbAngle = ClimbAngle}
else else
env.info('unit:getPosition() is nil!') log:error("Couldn't get unit's position")
end end
end end
@ -2093,7 +2115,6 @@ function mist.getLeadingBRString(vars)
end end
end end
mist.addEventHandler(groupSpawned)
end end
do -- group functions scope do -- group functions scope
@ -2232,7 +2253,7 @@ do -- group functions scope
return newData return newData
else else
env.info(gpName .. ' not found in mist.getGroupData') log:error('$1 not found in MIST database', gpName)
return return
end end
end end
@ -2270,10 +2291,10 @@ do -- group functions scope
end end
end end
else else
env.info('mist.getPayload got ' .. type(unitIdent)) log:error('Need string or number. Got: $1', type(unitIdent))
return false return false
end end
env.info('mist.getPayload, payload not found') log:warn("Couldn't find payload for unit: $1", unitIdent)
return return
end end
@ -2308,10 +2329,10 @@ do -- group functions scope
end end
end end
else else
env.info('mist.getGroupPayload got ' .. type(groupName)) log:error('Need string or number. Got: $1', type(groupIdent))
return false return false
end end
env.info('mist.getGroupPayload, payload not found') log:warn("Couldn't find payload for group: $1", groupIdent)
return return
end end
@ -2325,7 +2346,7 @@ do -- group functions scope
elseif vars.groupName then elseif vars.groupName then
gpName = vars.groupName gpName = vars.groupName
else else
env.info('teleportToPoint missing either vars.groupName or vars.gpName') log:error('Missing field groupName or gpName in variable table')
end end
local action = vars.action local action = vars.action
@ -2387,7 +2408,7 @@ do -- group functions scope
end end
end end
if valid == false then if valid == false then
env.info('mist.teleportToPoint; vars.point not a valid coordinate') log:error('point supplied in variable table is not a valid coordinate.')
return false return false
end end
end end
@ -2647,7 +2668,7 @@ do -- group functions scope
end end
--[[ --[[
for i = 1, #newTable do for i = 1, #newTable do
env.info(newTable[i]) log:info(newTable[i])
end end
]] ]]
return newTable return newTable
@ -2765,7 +2786,7 @@ do -- group functions scope
return false return false
end end
else else
env.info('mist.stringMatch; Either the first or second variable were not a string') log:error('Either the first or second variable were not a string')
return false return false
end end
end end
@ -2798,10 +2819,7 @@ end
do -- mist.Logger scope do -- mist.Logger scope
--- Logger class. --- Logger class.
-- @type mist.Logger -- @type mist.Logger
mist.Logger = { mist.Logger = {}
tag = "MIST",
level = 1,
}
--- 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.
@ -2810,7 +2828,12 @@ do -- mist.Logger scope
-- @usage myLogger = mist.Logger:new{tag = "MyScript", level = 2} -- @usage myLogger = mist.Logger:new{tag = "MyScript", level = 2}
-- @treturn mist.Logger -- @treturn mist.Logger
function mist.Logger:new(l) function mist.Logger:new(l)
l = l or {} l = l or {
tag = "MIST",
level = 1,
}
-- in case the user only supplied
-- either tag or level
if not l.tag then if not l.tag then
l.tag = "MIST" l.tag = "MIST"
end end
@ -2861,7 +2884,16 @@ do -- mist.Logger scope
end end
text = text:gsub('$' .. index, value) text = text:gsub('$' .. index, value)
end end
return text 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 end
--- Logs error and shows alert window. --- Logs error and shows alert window.
@ -3207,7 +3239,7 @@ do -- mist.util scope
-- mist.utils.typeCheck(type_tbl, my_tb) -- mist.utils.typeCheck(type_tbl, my_tb)
-- @return true if table passes the check, false otherwise. -- @return true if table passes the check, false otherwise.
function mist.utils.typeCheck(fname, type_tbl, var_tbl) function mist.utils.typeCheck(fname, type_tbl, var_tbl)
--env.info('type check') --log:info('type check')
for type_key, type_val in pairs(type_tbl) do for type_key, type_val in pairs(type_tbl) do
--print('type_key:') --print('type_key:')
--print(type_key) --print(type_key)
@ -3340,7 +3372,7 @@ function mist.utils.serialize(name, value, level)
end end
else else
env.info("Cannot serialize a "..type(value)) log:error('Cannot serialize a $1', type(value))
end end
return var_str_tbl return var_str_tbl
end end
@ -3427,7 +3459,7 @@ function mist.utils.oneLineSerialize(tbl)
tbl_str[#tbl_str + 1] = mist.utils.oneLineSerialize(val) tbl_str[#tbl_str + 1] = mist.utils.oneLineSerialize(val)
tbl_str[#tbl_str + 1] = ', ' --I think this is right, I just added it tbl_str[#tbl_str + 1] = ', ' --I think this is right, I just added it
else else
env.info('unable to serialize value type ' .. mist.utils.basicSerialize(type(val)) .. ' at index ' .. tostring(ind)) log:war('Unable to serialize value type $1 at index $2', mist.utils.basicSerialize(type(val)), tostring(ind))
end end
end end
@ -3531,13 +3563,11 @@ do -- mist.debug scope
local f = io.open(fdir, 'w') local f = io.open(fdir, 'w')
f:write(mist.utils.tableShow(_G)) f:write(mist.utils.tableShow(_G))
f:close() f:close()
local errmsg = 'mist.debug.dump_G wrote data to ' .. fdir log:info('Wrote debug data to $1', fdir)
env.info(errmsg) --trigger.action.outText(errmsg, 10)
trigger.action.outText(errmsg, 10)
else else
local errmsg = 'Error: insufficient libraries to run mist.debug.dump_G, you must disable the sanitization of the io and lfs libraries in ./Scripts/MissionScripting.lua' log:alert('insufficient libraries to run mist.debug.dump_G, you must disable the sanitization of the io and lfs libraries in ./Scripts/MissionScripting.lua')
env.info(errmsg) --trigger.action.outText(errmsg, 10)
trigger.action.outText(errmsg, 10)
end end
end end
@ -3554,13 +3584,11 @@ do -- mist.debug scope
local f = io.open(fdir, 'w') local f = io.open(fdir, 'w')
f:write(fcn(unpack(fcnVars, 1, table.maxn(fcnVars)))) f:write(fcn(unpack(fcnVars, 1, table.maxn(fcnVars))))
f:close() f:close()
local errmsg = 'mist.debug.writeData wrote data to ' .. fdir log:info('Wrote debug data to $1', fdir)
env.info(errmsg) --trigger.action.outText(errmsg, 10)
trigger.action.outText(errmsg, 10)
else else
local errmsg = 'Error: insufficient libraries to run mist.debug.writeData, you must disable the sanitization of the io and lfs libraries in ./Scripts/MissionScripting.lua' log:alert('insufficient libraries to run mist.debug.dump_G, you must disable the sanitization of the io and lfs libraries in ./Scripts/MissionScripting.lua')
env.info(errmsg) --trigger.action.outText(errmsg, 10)
trigger.action.outText(errmsg, 10)
end end
end end
@ -4412,11 +4440,11 @@ end]]
local function msgSpamFilter(recList, spamBlockOn) local function msgSpamFilter(recList, spamBlockOn)
for id, name in pairs(recList) do for id, name in pairs(recList) do
if name == spamBlockOn then if name == spamBlockOn then
-- env.info('already on recList') -- log:info('already on recList')
return recList return recList
end end
end end
--env.info('add to recList') --log:info('add to recList')
table.insert(recList, spamBlockOn) table.insert(recList, spamBlockOn)
return recList return recList
end end
@ -4523,7 +4551,7 @@ end]]
for i = 1, #messageList do for i = 1, #messageList do
if messageList[i].name then if messageList[i].name then
if messageList[i].name == vars.name then if messageList[i].name == vars.name then
--env.info('updateMessage') --log:info('updateMessage')
messageList[i].displayedFor = 0 messageList[i].displayedFor = 0
messageList[i].addedAt = timer.getTime() messageList[i].addedAt = timer.getTime()
messageList[i].sound = new.sound messageList[i].sound = new.sound
@ -5175,9 +5203,9 @@ do
--[[ --[[
local function addClientsToActive(event) local function addClientsToActive(event)
if event.id == world.event.S_EVENT_PLAYER_ENTER_UNIT or event.id == world.event.S_EVENT_BIRTH then if event.id == world.event.S_EVENT_PLAYER_ENTER_UNIT or event.id == world.event.S_EVENT_BIRTH then
env.info(mist.utils.tableShow(event)) log:info(mist.utils.tableShow(event))
if Unit.getPlayerName(event.initiator) then if Unit.getPlayerName(event.initiator) then
env.info(Unit.getPlayerName(event.initiator)) log:info(Unit.getPlayerName(event.initiator))
local newU = mist.utils.deepCopy(mist.DBs.unitsByName[Unit.getName(event.initiator)]) local newU = mist.utils.deepCopy(mist.DBs.unitsByName[Unit.getName(event.initiator)])
newU.playerName = Unit.getPlayerName(event.initiator) newU.playerName = Unit.getPlayerName(event.initiator)
mist.DBs.activeHumans[Unit.getName(event.initiator)] = newU mist.DBs.activeHumans[Unit.getName(event.initiator)] = newU
@ -5250,7 +5278,7 @@ do -- mist.time scope
tbl.s = timeInSec tbl.s = timeInSec
return tbl return tbl
else else
env.info('mist.time.getDHMS didnt recieve number') log:error("Didn't recieve number")
return return
end end
end end
@ -6297,8 +6325,8 @@ do -- mist unitID funcs
end end
end end
mist.main() -- initialize mist
env.info(('Mist version ' .. mist.majorVersion .. '.' .. mist.minorVersion .. '.' .. mist.build .. ' loaded.')) mist.init()
-- vim: sw=2:ts=2 -- vim: sw=2:ts=2