Moved TUM.logger to its own file

This commit is contained in:
Ambroise Garel 2025-08-05 17:04:24 +02:00
parent 52ad4156a4
commit 6658dbecf9
11 changed files with 169 additions and 160 deletions

View File

@ -9,146 +9,15 @@ TUM.VERSION_STRING = "0.1.250722"
TUM.DEBUG_MODE = __DEBUG_MODE__ TUM.DEBUG_MODE = __DEBUG_MODE__
--------------------------------------
--- Logging system
--------------------------------------
TUM.logLevel = {
TRACE = -2,
DEBUG = -1,
INFO = 0,
WARNING = 1,
ERROR = 2
}
TUM.Logger = {}
function TUM.Logger.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
function TUM.Logger.formatText(text, ...)
if not text then
return ""
end
if type(text) ~= 'string' then
text = TUM.p(text)
else
local args = ...
if args and args.n and args.n > 0 then
local pArgs = {}
for i=1,args.n do
pArgs[i] = TUM.p(args[i])
end
text = text:format(unpack(pArgs))
end
end
local fName = nil
local cLine = nil
if debug and debug.getinfo then
local dInfo = debug.getinfo(3)
fName = dInfo.name
cLine = dInfo.currentline
-- local fsrc = dinfo.short_src
--local fLine = dInfo.linedefined
end
if fName and cLine then
return fName .. '|' .. cLine .. ': ' .. text
elseif cLine then
return cLine .. ': ' .. text
else
return ' ' .. text
end
end
function TUM.Logger.print(level, text)
local texts = TUM.Logger.splitText(text)
local levelChar = 'E'
local logFunction = function(messageForLogfile, messageForUser)
trigger.action.outText("ERROR: "..messageForUser, 3600)
env.error(messageForLogfile)
end
if level == TUM.logLevel.WARNING then
levelChar = 'W'
logFunction = function(messageForLogfile, messageForUser)
trigger.action.outText("WARNING: "..messageForUser, 10)
env.warning(messageForLogfile)
end
elseif level == TUM.logLevel.INFO then
levelChar = 'I'
logFunction = function(messageForLogfile, messageForUser)
if TUM.DEBUG_MODE then -- Info messages are only printed out if debug mode is enabled
trigger.action.outText(messageForUser, 3)
end
env.info(messageForLogfile)
end
elseif level == TUM.logLevel.DEBUG then
levelChar = 'D'
logFunction = env.info
elseif level == TUM.logLevel.TRACE then
levelChar = 'T'
logFunction = env.info
end
for i = 1, #texts do
if i == 1 then
local theText = 'TUM|' .. levelChar .. '|' .. texts[i]
logFunction(theText, texts[i])
else
local theText = texts[i]
logFunction(theText, theText)
end
end
end
function TUM.Logger.error(text, ...)
text = TUM.Logger.formatText(text, arg)
local mText = text
if debug and debug.traceback then
mText = mText .. "\n" .. debug.traceback()
end
TUM.Logger.print(TUM.logLevel.ERROR, mText)
end
function TUM.Logger.warn(text, ...)
text = TUM.Logger.formatText(text, arg)
TUM.Logger.print(TUM.logLevel.WARNING, text)
end
function TUM.Logger.info(text, ...)
text = TUM.Logger.formatText(text, arg)
TUM.Logger.print(TUM.logLevel.INFO, text)
end
function TUM.Logger.debug(text, ...)
if TUM.DEBUG_MODE then
text = TUM.Logger.formatText(text, arg)
TUM.Logger.print(TUM.logLevel.DEBUG, text)
end
end
function TUM.Logger.trace(text, ...)
if TUM.DEBUG_MODE then
text = TUM.Logger.formatText(text, arg)
TUM.Logger.print(TUM.logLevel.TRACE, text)
end
end
------------------------------------- -------------------------------------
-- Prints and logs a debug message -- Prints and logs a debug message
-- @param message The message -- @param message The message
-- @param logLevel Is it a warning, error or info messages (as defined in TUM.logLevel). Info messages are not printed out unless debug mode is enabled. -- @param logLevel Is it a warning, error or info messages (as defined in TUM.logger.logLevel). Info messages are not printed out unless debug mode is enabled.
------------------------------------- -------------------------------------
function TUM.log(message, logLevel) function TUM.log(message, logLevel)
logLevel = logLevel or TUM.logLevel.INFO logLevel = logLevel or TUM.logger.logLevel.INFO
TUM.Logger.print(logLevel, message) TUM.logger.print(logLevel, message)
end end
-------------------------------------- --------------------------------------
@ -199,7 +68,7 @@ function TUM.administrativeSettings.setValue(key, value)
end end
end end
if not foundKey then if not foundKey then
TUM.log("Tried to set an unknown administrative setting: "..tostring(key), TUM.logLevel.ERROR) TUM.log("Tried to set an unknown administrative setting: "..tostring(key), TUM.logger.logLevel.ERROR)
return nil return nil
end end
@ -276,12 +145,12 @@ function TUM.initialize()
} }
if not net or not net.dostring_in then if not net or not net.dostring_in then
TUM.log("Mission failed to execute. Please copy the provided \"autoexec.cfg\" file to the [Saved Games]\\DCS\\Config directory.\nThe file can be downloaded from github.com/akaAgar/the-universal-mission-for-dcs-world", TUM.logLevel.ERROR) TUM.log("Mission failed to execute. Please copy the provided \"autoexec.cfg\" file to the [Saved Games]\\DCS\\Config directory.\nThe file can be downloaded from github.com/akaAgar/the-universal-mission-for-dcs-world", TUM.logger.logLevel.ERROR)
return nil return nil
end end
if #DCSEx.envMission.getPlayerGroups() == 0 then if #DCSEx.envMission.getPlayerGroups() == 0 then
TUM.log("No \"Player\" or \"Client\" aircraft slots have been found. Please fix this problem in the mission editor.", TUM.logLevel.ERROR) TUM.log("No \"Player\" or \"Client\" aircraft slots have been found. Please fix this problem in the mission editor.", TUM.logger.logLevel.ERROR)
return nil return nil
end end
@ -289,19 +158,19 @@ function TUM.initialize()
coreSettings.multiplayer = false coreSettings.multiplayer = false
if #DCSEx.envMission.getPlayerGroups() > 1 then if #DCSEx.envMission.getPlayerGroups() > 1 then
TUM.log("Multiple players slots have been found in addition to the single-player \"Player\" aircraft. Please fix this problem in the mission editor.", TUM.logLevel.ERROR) TUM.log("Multiple players slots have been found in addition to the single-player \"Player\" aircraft. Please fix this problem in the mission editor.", TUM.logger.logLevel.ERROR)
return nil return nil
end end
else else
coreSettings.multiplayer = true coreSettings.multiplayer = true
if #DCSEx.envMission.getPlayerGroups(coalition.side.BLUE) == 0 and #DCSEx.envMission.getPlayerGroups(coalition.side.RED) == 0 then if #DCSEx.envMission.getPlayerGroups(coalition.side.BLUE) == 0 and #DCSEx.envMission.getPlayerGroups(coalition.side.RED) == 0 then
TUM.log("Neither BLUE nor RED coalitions have player slots. Please make sure one coalition has player slots in the mission editor.", TUM.logLevel.ERROR) TUM.log("Neither BLUE nor RED coalitions have player slots. Please make sure one coalition has player slots in the mission editor.", TUM.logger.logLevel.ERROR)
return nil return nil
end end
if #DCSEx.envMission.getPlayerGroups(coalition.side.BLUE) > 0 and #DCSEx.envMission.getPlayerGroups(coalition.side.RED) > 0 then if #DCSEx.envMission.getPlayerGroups(coalition.side.BLUE) > 0 and #DCSEx.envMission.getPlayerGroups(coalition.side.RED) > 0 then
TUM.log("Both coalitions have player slots. The Universal Mission is a purely singleplayer/PvE experience and does not support PvP. Please make sure only one coalition has player slots in the mission editor.", TUM.logLevel.ERROR) TUM.log("Both coalitions have player slots. The Universal Mission is a purely singleplayer/PvE experience and does not support PvP. Please make sure only one coalition has player slots in the mission editor.", TUM.logger.logLevel.ERROR)
return nil return nil
end end
end end
@ -387,5 +256,5 @@ end
if TUM.administrativeSettings.getValue(TUM.administrativeSettings.INITIALIZE_AUTOMATICALLY) then if TUM.administrativeSettings.getValue(TUM.administrativeSettings.INITIALIZE_AUTOMATICALLY) then
TUM.initialize() TUM.initialize()
else else
TUM.log("TUM has been loaded, but not initialized. Call TUM.initialize() to start the mission.", TUM.logLevel.INFO) TUM.log("TUM has been loaded, but not initialized. Call TUM.initialize() to start the mission.", TUM.logger.logLevel.INFO)
end end

View File

@ -109,7 +109,7 @@ do
if addAirDefenseGroup(side, faction, unitFamily, point) then if addAirDefenseGroup(side, faction, unitFamily, point) then
realCount = realCount + 1 realCount = realCount + 1
else else
TUM.log("Failed to add point air defense group near objective "..TUM.objectives.getObjective(i).name..".", TUM.logLevel.WARNING) TUM.log("Failed to add point air defense group near objective "..TUM.objectives.getObjective(i).name..".", TUM.logger.logLevel.WARNING)
end end
end end
end end
@ -134,7 +134,7 @@ do
if addAirDefenseGroup(side, faction, unitFamily, point) then if addAirDefenseGroup(side, faction, unitFamily, point) then
realCount = realCount + 1 realCount = realCount + 1
else else
TUM.log("Failed to add local air defense group.", TUM.logLevel.WARNING) TUM.log("Failed to add local air defense group.", TUM.logger.logLevel.WARNING)
end end
end end
@ -154,7 +154,7 @@ do
if addAirDefenseGroup(side, faction, DCSEx.enums.unitFamily.AIRDEFENSE_MANPADS, point) then if addAirDefenseGroup(side, faction, DCSEx.enums.unitFamily.AIRDEFENSE_MANPADS, point) then
realCount = realCount + 1 realCount = realCount + 1
else else
TUM.log("Failed to add local MANPADS group.", TUM.logLevel.WARNING) TUM.log("Failed to add local MANPADS group.", TUM.logger.logLevel.WARNING)
end end
end end

View File

@ -0,0 +1,140 @@
-- ====================================================================================
-- TUM.LOGGER - LOGS WARNINGS, ERRORS AND DEBUG INFO
-- ====================================================================================
-- (enum) TUM.logger.
-- TUM.logger.debug(text, ...)
-- TUM.logger.error(text, ...)
-- TUM.logger.formatText(text, ...)
-- TUM.logger.info(text, ...)
-- TUM.logger.print(level, text)
-- TUM.logger.splitText(text)
-- TUM.logger.trace(text, ...)
-- TUM.logger.warn(text, ...)
-- ====================================================================================
TUM.logger = {}
TUM.logger.logLevel = {
TRACE = -2,
DEBUG = -1,
INFO = 0,
WARNING = 1,
ERROR = 2
}
function TUM.logger.debug(text, ...)
if TUM.DEBUG_MODE then
text = TUM.logger.formatText(text, arg)
TUM.logger.print(TUM.logger.logLevel.DEBUG, text)
end
end
function TUM.logger.error(text, ...)
text = TUM.logger.formatText(text, arg)
local mText = text
if debug and debug.traceback then
mText = mText .. "\n" .. debug.traceback()
end
TUM.logger.print(TUM.logger.logLevel.ERROR, mText)
end
function TUM.logger.formatText(text, ...)
if not text then
return ""
end
if type(text) ~= 'string' then
text = TUM.p(text)
else
local args = ...
if args and args.n and args.n > 0 then
local pArgs = {}
for i=1,args.n do
pArgs[i] = TUM.p(args[i])
end
text = text:format(unpack(pArgs))
end
end
local fName = nil
local cLine = nil
if debug and debug.getinfo then
local dInfo = debug.getinfo(3)
fName = dInfo.name
cLine = dInfo.currentline
-- local fsrc = dinfo.short_src
--local fLine = dInfo.linedefined
end
if fName and cLine then
return fName .. '|' .. cLine .. ': ' .. text
elseif cLine then
return cLine .. ': ' .. text
else
return ' ' .. text
end
end
function TUM.logger.info(text, ...)
text = TUM.logger.formatText(text, arg)
TUM.logger.print(TUM.logger.logLevel.INFO, text)
end
function TUM.logger.print(level, text)
local texts = TUM.logger.splitText(text)
local levelChar = 'E'
local logFunction = function(messageForLogfile, messageForUser)
trigger.action.outText("ERROR: "..messageForUser, 3600)
env.error(messageForLogfile)
end
if level == TUM.logger.logLevel.WARNING then
levelChar = 'W'
logFunction = function(messageForLogfile, messageForUser)
trigger.action.outText("WARNING: "..messageForUser, 10)
env.warning(messageForLogfile)
end
elseif level == TUM.logger.logLevel.INFO then
levelChar = 'I'
logFunction = function(messageForLogfile, messageForUser)
if TUM.DEBUG_MODE then -- Info messages are only printed out if debug mode is enabled
trigger.action.outText(messageForUser, 3)
end
env.info(messageForLogfile)
end
elseif level == TUM.logger.logLevel.DEBUG then
levelChar = 'D'
logFunction = env.info
elseif level == TUM.logger.logLevel.TRACE then
levelChar = 'T'
logFunction = env.info
end
for i = 1, #texts do
if i == 1 then
local theText = 'TUM|' .. levelChar .. '|' .. texts[i]
logFunction(theText, texts[i])
else
local theText = texts[i]
logFunction(theText, theText)
end
end
end
function TUM.logger.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
function TUM.logger.trace(text, ...)
if TUM.DEBUG_MODE then
text = TUM.logger.formatText(text, arg)
TUM.logger.print(TUM.logger.logLevel.TRACE, text)
end
end
function TUM.logger.warn(text, ...)
text = TUM.logger.formatText(text, arg)
TUM.logger.print(TUM.logger.logLevel.WARNING, text)
end

View File

@ -67,7 +67,7 @@ do
end end
if TUM.objectives.getCount() == 0 then if TUM.objectives.getCount() == 0 then
TUM.log("Couldn't create any objective, mission creation failed.", TUM.logLevel.WARNING) TUM.log("Couldn't create any objective, mission creation failed.", TUM.logger.logLevel.WARNING)
closeMission(true) closeMission(true)
return return
end end

View File

@ -55,7 +55,7 @@ do
local u = DCSEx.world.getUnitByID(id) local u = DCSEx.world.getUnitByID(id)
if u then u:destroy() end if u then u:destroy() end
end end
TUM.log("Removed "..tostring(#aiWingMenToRemove).." AI wingmen from the mission.\nPlease do not add AI wingmen to the mission, The Universal Mission uses its own wingman system.", TUM.logLevel.WARNING) TUM.log("Removed "..tostring(#aiWingMenToRemove).." AI wingmen from the mission.\nPlease do not add AI wingmen to the mission, The Universal Mission uses its own wingman system.", TUM.logger.logLevel.WARNING)
end end
end end

View File

@ -25,7 +25,7 @@ do
local objective = TUM.objectivesMaker.create() local objective = TUM.objectivesMaker.create()
if not objective then if not objective then
TUM.log("Failed to spawn a group for objective #"..tostring(#objectives + 1)..".", TUM.logLevel.WARNING) TUM.log("Failed to spawn a group for objective #"..tostring(#objectives + 1)..".", TUM.logger.logLevel.WARNING)
return false return false
end end

View File

@ -46,7 +46,7 @@ do
local taskID = pickRandomTask() local taskID = pickRandomTask()
if not taskID then if not taskID then
TUM.log("Failed to find a valid task.", TUM.logLevel.WARNING) TUM.log("Failed to find a valid task.", TUM.logger.logLevel.WARNING)
return nil return nil
end end
local objectiveDB = Library.tasks[taskID] local objectiveDB = Library.tasks[taskID]
@ -58,7 +58,7 @@ do
if DCSEx.table.contains(objectiveDB.flags, DCSEx.enums.taskFlag.SCENERY_TARGET) then if DCSEx.table.contains(objectiveDB.flags, DCSEx.enums.taskFlag.SCENERY_TARGET) then
local validSceneries = DCSEx.world.getSceneriesInZone(zone, DCSEx.zones.getRadius(zone), 250) local validSceneries = DCSEx.world.getSceneriesInZone(zone, DCSEx.zones.getRadius(zone), 250)
if not validSceneries or #validSceneries == 0 then if not validSceneries or #validSceneries == 0 then
TUM.log("Failed to find a valid scenery object to use as target.", TUM.logLevel.WARNING) TUM.log("Failed to find a valid scenery object to use as target.", TUM.logger.logLevel.WARNING)
return nil return nil
end end
@ -76,7 +76,7 @@ do
end end
if not spawnPoint2 then if not spawnPoint2 then
TUM.log("Failed to find a spawn point for objective.", TUM.logLevel.WARNING) TUM.log("Failed to find a spawn point for objective.", TUM.logger.logLevel.WARNING)
return nil return nil
end end
@ -136,7 +136,7 @@ do
end end
if not groupInfo then if not groupInfo then
TUM.log("Failed to spawn a group for objective.", TUM.logLevel.WARNING) TUM.log("Failed to spawn a group for objective.", TUM.logger.logLevel.WARNING)
return nil return nil
end end
objective.groupID = groupInfo.groupID objective.groupID = groupInfo.groupID

View File

@ -274,7 +274,7 @@ do
msg = msg.."To enable the IO module, comment or remove the \"sanitizeModule('io')\" line in \n" msg = msg.."To enable the IO module, comment or remove the \"sanitizeModule('io')\" line in \n"
msg = msg.."[DCSWorld installation directory]\\Scripts\\MissionScripting.lua and restart the game." msg = msg.."[DCSWorld installation directory]\\Scripts\\MissionScripting.lua and restart the game."
TUM.log(msg, TUM.logLevel.WARNING) TUM.log(msg, TUM.logger.logLevel.WARNING)
end end
return true return true

View File

@ -132,7 +132,7 @@ do
end end
TUM.log("Spawned AWACS aircraft") TUM.log("Spawned AWACS aircraft")
else else
TUM.log("Failed to create AWACS aircraft", TUM.logLevel.WARNING) TUM.log("Failed to create AWACS aircraft", TUM.logger.logLevel.WARNING)
end end
else else
TUM.log("No AWACS aircraft available") TUM.log("No AWACS aircraft available")

View File

@ -155,18 +155,18 @@ do
local zoneName = "BLUFOR" local zoneName = "BLUFOR"
if side == 1 then zoneName = "REDFOR" end if side == 1 then zoneName = "REDFOR" end
TUM.log("Coalition "..name.." has no territory zones and/or controls no airfields. Please add zone with a name starting with "..zoneName.." in the mission editor and make sure at least one contains an airbase.", TUM.logLevel.ERROR) TUM.log("Coalition "..name.." has no territory zones and/or controls no airfields. Please add zone with a name starting with "..zoneName.." in the mission editor and make sure at least one contains an airbase.", TUM.logger.logLevel.ERROR)
return false return false
end end
end end
if #missionZones == 0 then if #missionZones == 0 then
TUM.log("No mission zones found. Create at least one mission zone in the mission editor.", TUM.logLevel.ERROR) TUM.log("No mission zones found. Create at least one mission zone in the mission editor.", TUM.logger.logLevel.ERROR)
return false return false
end end
if #missionZones > 10 then if #missionZones > 10 then
TUM.log("Too many mission zones, extra zones removed.", TUM.logLevel.WARNING) TUM.log("Too many mission zones, extra zones removed.", TUM.logger.logLevel.WARNING)
while #missionZones > 10 do while #missionZones > 10 do
table.remove(missionZones, 11) table.remove(missionZones, 11)
end end
@ -177,10 +177,10 @@ do
-- zones[coalition.side.RED] = DCSEx.zones.getByName("REDFOR") -- zones[coalition.side.RED] = DCSEx.zones.getByName("REDFOR")
-- if not zones[coalition.side.BLUE] then -- if not zones[coalition.side.BLUE] then
-- TUM.log("BLUFOR zone not found.", TUM.logLevel.ERROR) -- TUM.log("BLUFOR zone not found.", TUM.logger.logLevel.ERROR)
-- return false -- return false
-- elseif not zones[coalition.side.RED] then -- elseif not zones[coalition.side.RED] then
-- TUM.log("REDFOR zone not found.", TUM.logLevel.ERROR) -- TUM.log("REDFOR zone not found.", TUM.logger.logLevel.ERROR)
-- return false -- return false
-- end -- end

View File

@ -48,7 +48,7 @@ do
-- Retrive player unit type -- Retrive player unit type
local playerTypeName = player:getTypeName() local playerTypeName = player:getTypeName()
if not Library.aircraft[playerTypeName] then if not Library.aircraft[playerTypeName] then
TUM.log("Cannot spawn AI wingmen, aircraft \""..playerTypeName.."\" not found in the database.", TUM.logLevel.WARNING) TUM.log("Cannot spawn AI wingmen, aircraft \""..playerTypeName.."\" not found in the database.", TUM.logger.logLevel.WARNING)
return return
end end
local playerCategory = Group.Category.AIRPLANE local playerCategory = Group.Category.AIRPLANE
@ -88,7 +88,7 @@ do
) )
if not groupInfo then if not groupInfo then
TUM.log("Failed to spawn AI wingmen", TUM.logLevel.WARNING) TUM.log("Failed to spawn AI wingmen", TUM.logger.logLevel.WARNING)
return return
end end
wingmenGroupID = groupInfo.groupID wingmenGroupID = groupInfo.groupID