From 6658dbecf92366bb3cf3d1a0f0c80c43e0b3e2ae Mon Sep 17 00:00:00 2001 From: Ambroise Garel <47314805+akaAgar@users.noreply.github.com> Date: Tue, 5 Aug 2025 17:04:24 +0200 Subject: [PATCH] Moved TUM.logger to its own file --- Script/Script.lua | 151 ++---------------- .../The Universal Mission/EnemyAirDefense.lua | 6 +- Script/The Universal Mission/Logger.lua | 140 ++++++++++++++++ Script/The Universal Mission/Mission.lua | 2 +- Script/The Universal Mission/MizCleaner.lua | 2 +- Script/The Universal Mission/Objectives.lua | 2 +- .../The Universal Mission/ObjectivesMaker.lua | 8 +- Script/The Universal Mission/PlayerCareer.lua | 2 +- Script/The Universal Mission/SupportAWACS.lua | 2 +- Script/The Universal Mission/Territories.lua | 10 +- Script/The Universal Mission/Wingmen.lua | 4 +- 11 files changed, 169 insertions(+), 160 deletions(-) create mode 100644 Script/The Universal Mission/Logger.lua diff --git a/Script/Script.lua b/Script/Script.lua index 8bc1fe4..dd106f8 100644 --- a/Script/Script.lua +++ b/Script/Script.lua @@ -9,146 +9,15 @@ TUM.VERSION_STRING = "0.1.250722" 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 -- @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) - logLevel = logLevel or TUM.logLevel.INFO - TUM.Logger.print(logLevel, message) + logLevel = logLevel or TUM.logger.logLevel.INFO + TUM.logger.print(logLevel, message) end -------------------------------------- @@ -199,7 +68,7 @@ function TUM.administrativeSettings.setValue(key, value) end end 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 end @@ -276,12 +145,12 @@ function TUM.initialize() } 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 end 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 end @@ -289,19 +158,19 @@ function TUM.initialize() coreSettings.multiplayer = false 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 end else coreSettings.multiplayer = true 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 end 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 end end @@ -387,5 +256,5 @@ end if TUM.administrativeSettings.getValue(TUM.administrativeSettings.INITIALIZE_AUTOMATICALLY) then TUM.initialize() 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 \ No newline at end of file diff --git a/Script/The Universal Mission/EnemyAirDefense.lua b/Script/The Universal Mission/EnemyAirDefense.lua index e19e14e..d6cb94b 100644 --- a/Script/The Universal Mission/EnemyAirDefense.lua +++ b/Script/The Universal Mission/EnemyAirDefense.lua @@ -109,7 +109,7 @@ do if addAirDefenseGroup(side, faction, unitFamily, point) then realCount = realCount + 1 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 @@ -134,7 +134,7 @@ do if addAirDefenseGroup(side, faction, unitFamily, point) then realCount = realCount + 1 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 @@ -154,7 +154,7 @@ do if addAirDefenseGroup(side, faction, DCSEx.enums.unitFamily.AIRDEFENSE_MANPADS, point) then realCount = realCount + 1 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 diff --git a/Script/The Universal Mission/Logger.lua b/Script/The Universal Mission/Logger.lua new file mode 100644 index 0000000..fa043f9 --- /dev/null +++ b/Script/The Universal Mission/Logger.lua @@ -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 diff --git a/Script/The Universal Mission/Mission.lua b/Script/The Universal Mission/Mission.lua index 22d3ceb..ae9c275 100644 --- a/Script/The Universal Mission/Mission.lua +++ b/Script/The Universal Mission/Mission.lua @@ -67,7 +67,7 @@ do end 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) return end diff --git a/Script/The Universal Mission/MizCleaner.lua b/Script/The Universal Mission/MizCleaner.lua index f35bc0d..36dd02d 100644 --- a/Script/The Universal Mission/MizCleaner.lua +++ b/Script/The Universal Mission/MizCleaner.lua @@ -55,7 +55,7 @@ do local u = DCSEx.world.getUnitByID(id) if u then u:destroy() 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 diff --git a/Script/The Universal Mission/Objectives.lua b/Script/The Universal Mission/Objectives.lua index d0d5914..091d9a3 100644 --- a/Script/The Universal Mission/Objectives.lua +++ b/Script/The Universal Mission/Objectives.lua @@ -25,7 +25,7 @@ do local objective = TUM.objectivesMaker.create() 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 end diff --git a/Script/The Universal Mission/ObjectivesMaker.lua b/Script/The Universal Mission/ObjectivesMaker.lua index d0068cc..9146b48 100644 --- a/Script/The Universal Mission/ObjectivesMaker.lua +++ b/Script/The Universal Mission/ObjectivesMaker.lua @@ -46,7 +46,7 @@ do local taskID = pickRandomTask() 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 end local objectiveDB = Library.tasks[taskID] @@ -58,7 +58,7 @@ do if DCSEx.table.contains(objectiveDB.flags, DCSEx.enums.taskFlag.SCENERY_TARGET) then local validSceneries = DCSEx.world.getSceneriesInZone(zone, DCSEx.zones.getRadius(zone), 250) 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 end @@ -76,7 +76,7 @@ do end 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 end @@ -136,7 +136,7 @@ do end 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 end objective.groupID = groupInfo.groupID diff --git a/Script/The Universal Mission/PlayerCareer.lua b/Script/The Universal Mission/PlayerCareer.lua index 24f4240..117431b 100644 --- a/Script/The Universal Mission/PlayerCareer.lua +++ b/Script/The Universal Mission/PlayerCareer.lua @@ -274,7 +274,7 @@ do 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." - TUM.log(msg, TUM.logLevel.WARNING) + TUM.log(msg, TUM.logger.logLevel.WARNING) end return true diff --git a/Script/The Universal Mission/SupportAWACS.lua b/Script/The Universal Mission/SupportAWACS.lua index 95df77f..a638aec 100644 --- a/Script/The Universal Mission/SupportAWACS.lua +++ b/Script/The Universal Mission/SupportAWACS.lua @@ -132,7 +132,7 @@ do end TUM.log("Spawned AWACS aircraft") else - TUM.log("Failed to create AWACS aircraft", TUM.logLevel.WARNING) + TUM.log("Failed to create AWACS aircraft", TUM.logger.logLevel.WARNING) end else TUM.log("No AWACS aircraft available") diff --git a/Script/The Universal Mission/Territories.lua b/Script/The Universal Mission/Territories.lua index b41b1e3..5b3b92e 100644 --- a/Script/The Universal Mission/Territories.lua +++ b/Script/The Universal Mission/Territories.lua @@ -155,18 +155,18 @@ do local zoneName = "BLUFOR" 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 end end 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 end 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 table.remove(missionZones, 11) end @@ -177,10 +177,10 @@ do -- zones[coalition.side.RED] = DCSEx.zones.getByName("REDFOR") -- 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 -- 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 -- end diff --git a/Script/The Universal Mission/Wingmen.lua b/Script/The Universal Mission/Wingmen.lua index 8c16dc5..9e4f2c0 100644 --- a/Script/The Universal Mission/Wingmen.lua +++ b/Script/The Universal Mission/Wingmen.lua @@ -48,7 +48,7 @@ do -- Retrive player unit type local playerTypeName = player:getTypeName() 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 end local playerCategory = Group.Category.AIRPLANE @@ -88,7 +88,7 @@ do ) 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 end wingmenGroupID = groupInfo.groupID