From b7b5b7da34ff583851b661dff92cdb478bf61fb4 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 27 Jan 2025 21:10:44 +0000 Subject: [PATCH] added if checks to some debug logs --- Libraries/Core/Controllers/BotController.cs | 32 +++++++++------ Libraries/Core/Generators/BotGenerator.cs | 7 +++- .../Core/Generators/BotInventoryGenerator.cs | 10 +++-- Libraries/Core/Generators/BotLootGenerator.cs | 41 +++++++++++-------- Libraries/Core/Models/Spt/Logging/LogLevel.cs | 4 +- Libraries/Core/Models/Utils/ISptLogger.cs | 2 + Server/Logger/WebApplicationLogger.cs | 22 ++++++++++ Tools/ItemTplGenerator/SptBasicLogger.cs | 6 +++ UnitTests/Mock/MockLogger.cs | 6 +++ 9 files changed, 93 insertions(+), 37 deletions(-) diff --git a/Libraries/Core/Controllers/BotController.cs b/Libraries/Core/Controllers/BotController.cs index 6af9717a..2e6bc274 100644 --- a/Libraries/Core/Controllers/BotController.cs +++ b/Libraries/Core/Controllers/BotController.cs @@ -15,6 +15,7 @@ using Core.Services; using Core.Utils; using Core.Utils.Cloners; using SptCommon.Extensions; +using LogLevel = Core.Models.Spt.Logging.LogLevel; namespace Core.Controllers; @@ -102,7 +103,8 @@ public class BotController( { // No bot of this type found, copy details from assault result[botTypeLower] = result["assault"]; - _logger.Debug($"Unable to find bot: {botTypeLower} in db, copying 'assault'"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Unable to find bot: {botTypeLower} in db, copying 'assault'"); continue; } @@ -201,15 +203,17 @@ public class BotController( if (botCacheCount >= botGenerationDetails.BotCountToGenerate) { - _logger.Debug($"Cache already has sufficient {cacheKey} bots: {botCacheCount}"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Cache already has sufficient {cacheKey} bots: {botCacheCount}"); return; } // We're below desired count, add bots to cache var botsToGenerate = botGenerationDetails.BotCountToGenerate - botCacheCount; var progressWriter = new ProgressWriter(botGenerationDetails.BotCountToGenerate.GetValueOrDefault(30)); - - _logger.Debug($"Generating {botsToGenerate} bots for cacheKey: {cacheKey}"); + + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Generating {botsToGenerate} bots for cacheKey: {cacheKey}"); for (var i = 0; i < botsToGenerate; i++) { @@ -225,10 +229,11 @@ public class BotController( // } } - _logger.Debug( - $"Generated {botGenerationDetails.BotCountToGenerate} {botGenerationDetails.Role}" + - $"({botGenerationDetails.EventRole ?? botGenerationDetails.Role ?? ""}) {botGenerationDetails.BotDifficulty}bots" - ); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug( + $"Generated {botGenerationDetails.BotCountToGenerate} {botGenerationDetails.Role}" + + $"({botGenerationDetails.EventRole ?? botGenerationDetails.Role ?? ""}) {botGenerationDetails.BotDifficulty}bots" + ); } private List ReturnSingleBotFromCache(string sessionId, GenerateBotsRequestData request) @@ -313,11 +318,12 @@ public class BotController( { // No bot in cache, generate new and store in cache GenerateSingleBotAndStoreInCache(botGenerationDetails, sessionId, cacheKey); - - _logger.Debug( - $"Generated {botGenerationDetails.BotCountToGenerate} " + - $"{botGenerationDetails.Role} ({botGenerationDetails.EventRole ?? ""}) {botGenerationDetails.BotDifficulty} bots" - ); + + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug( + $"Generated {botGenerationDetails.BotCountToGenerate} " + + $"{botGenerationDetails.Role} ({botGenerationDetails.EventRole ?? ""}) {botGenerationDetails.BotDifficulty} bots" + ); } var desiredBot = _botGenerationCacheService.GetBot(cacheKey); diff --git a/Libraries/Core/Generators/BotGenerator.cs b/Libraries/Core/Generators/BotGenerator.cs index 86f33b76..10dff8a8 100644 --- a/Libraries/Core/Generators/BotGenerator.cs +++ b/Libraries/Core/Generators/BotGenerator.cs @@ -12,6 +12,7 @@ using Core.Services; using Core.Utils; using Core.Utils.Cloners; using BodyPart = Core.Models.Eft.Common.Tables.BodyPart; +using LogLevel = Core.Models.Spt.Logging.LogLevel; namespace Core.Generators; @@ -322,7 +323,8 @@ public class BotGenerator( if (!experiences.TryGetValue(botDifficulty.ToLower(), out var result)) { - _logger.Debug($"Unable to find experience: {botDifficulty} for {role} bot, falling back to `normal`"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Unable to find experience: {botDifficulty} for {role} bot, falling back to `normal`"); return _randomUtil.GetDouble(experiences["normal"].Min.Value, experiences["normal"].Max.Value); } @@ -479,7 +481,8 @@ public class BotGenerator( public void LogPmcGeneratedCount(List output) { var pmcCount = output.Aggregate(0, (acc, cur) => { return cur.Info.Side is "Bear" or "Usec" ? acc + 1 : acc; }); - _logger.Debug($"Generated {output.Count} total bots. Replaced {pmcCount} with PMCs"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Generated {output.Count} total bots. Replaced {pmcCount} with PMCs"); } /// diff --git a/Libraries/Core/Generators/BotInventoryGenerator.cs b/Libraries/Core/Generators/BotInventoryGenerator.cs index 50e4c608..4947fbb6 100644 --- a/Libraries/Core/Generators/BotInventoryGenerator.cs +++ b/Libraries/Core/Generators/BotInventoryGenerator.cs @@ -10,6 +10,7 @@ using Core.Models.Utils; using Core.Servers; using Core.Services; using Core.Utils; +using LogLevel = Core.Models.Spt.Logging.LogLevel; namespace Core.Generators; @@ -342,7 +343,8 @@ public class BotInventoryGenerator( if (!tacVestsWithArmor.Any()) { - _logger.Debug($"Unable to filter to only armored rigs as bot: {botRole} has none in pool"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Unable to filter to only armored rigs as bot: {botRole} has none in pool"); return; } @@ -365,7 +367,8 @@ public class BotInventoryGenerator( if (!allowEmptyResult && !tacVestsWithoutArmor.Any()) { - _logger.Debug($"Unable to filter to only unarmored rigs as bot: {botRole} has none in pool"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Unable to filter to only unarmored rigs as bot: {botRole} has none in pool"); return; } @@ -420,7 +423,8 @@ public class BotInventoryGenerator( if (!dbResult.Key) { _logger.Error(_localisationService.GetText("bot-missing_item_template", chosenItemTpl)); - _logger.Debug($"EquipmentSlot-> {settings.RootEquipmentSlot}"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"EquipmentSlot-> {settings.RootEquipmentSlot}"); // Remove picked item settings.RootEquipmentPool.Remove(chosenItemTpl); diff --git a/Libraries/Core/Generators/BotLootGenerator.cs b/Libraries/Core/Generators/BotLootGenerator.cs index 2007ce9b..f88f4b0d 100644 --- a/Libraries/Core/Generators/BotLootGenerator.cs +++ b/Libraries/Core/Generators/BotLootGenerator.cs @@ -9,6 +9,7 @@ using Core.Models.Utils; using Core.Services; using Core.Servers; using Core.Utils.Cloners; +using LogLevel = Core.Models.Spt.Logging.LogLevel; namespace Core.Generators; @@ -530,18 +531,20 @@ public class BotLootGenerator( if (itemAddedResult == ItemAddedResult.NO_CONTAINERS) { // Bot has no container to put item in, exit - _logger.Debug($"Unable to add: {totalItemCount} items to bot as it lacks a container to include them"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Unable to add: {totalItemCount} items to bot as it lacks a container to include them"); break; } fitItemIntoContainerAttempts++; if (fitItemIntoContainerAttempts >= 4) { - _logger.Debug( - $"Failed placing item: {itemToAddTemplate.Name}: {i} of: {totalItemCount} items into: {botRole} " + - $"containers: {string.Join(",", equipmentSlots)}. Tried: {fitItemIntoContainerAttempts} " + - $"times, reason: {itemAddedResult}, skipping" - ); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug( + $"Failed placing item: {itemToAddTemplate.Name}: {i} of: {totalItemCount} items into: {botRole} " + + $"containers: {string.Join(",", equipmentSlots)}. Tried: {fitItemIntoContainerAttempts} " + + $"times, reason: {itemAddedResult}, skipping" + ); break; } @@ -697,7 +700,8 @@ public class BotLootGenerator( if (result != ItemAddedResult.SUCCESS) { - _logger.Debug($"Failed to add additional weapon {generatedWeapon.Weapon[0].Id} to bot backpack, reason: {result.ToString()}"); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug($"Failed to add additional weapon {generatedWeapon.Weapon[0].Id} to bot backpack, reason: {result.ToString()}"); } } } @@ -763,17 +767,18 @@ public class BotLootGenerator( // Prevent edge-case of small loot pools + code trying to add limited item over and over infinitely if (itemSpawnLimits.CurrentLimits[idToCheckFor] > itemSpawnLimits.CurrentLimits[idToCheckFor] * 10) { - _logger.Debug( - _localisationService.GetText( - "bot-item_spawn_limit_reached_skipping_item", - new - { - botRole = botRole, - itemName = itemTemplate.Name, - attempts = itemSpawnLimits.CurrentLimits[idToCheckFor] - } - ) - ); + if(_logger.IsLogEnabled(LogLevel.Debug)) + _logger.Debug( + _localisationService.GetText( + "bot-item_spawn_limit_reached_skipping_item", + new + { + botRole = botRole, + itemName = itemTemplate.Name, + attempts = itemSpawnLimits.CurrentLimits[idToCheckFor] + } + ) + ); return false; } diff --git a/Libraries/Core/Models/Spt/Logging/LogLevel.cs b/Libraries/Core/Models/Spt/Logging/LogLevel.cs index ee66cd1d..08def7b5 100644 --- a/Libraries/Core/Models/Spt/Logging/LogLevel.cs +++ b/Libraries/Core/Models/Spt/Logging/LogLevel.cs @@ -2,12 +2,14 @@ public enum LogLevel { + Fatal, Error, Warn, Success, Info, Custom, - Debug + Debug, + Trace } // TODO: needs to be moved to enums namespace diff --git a/Libraries/Core/Models/Utils/ISptLogger.cs b/Libraries/Core/Models/Utils/ISptLogger.cs index ba7add11..49c2cc5e 100644 --- a/Libraries/Core/Models/Utils/ISptLogger.cs +++ b/Libraries/Core/Models/Utils/ISptLogger.cs @@ -1,4 +1,5 @@ using Core.Models.Logging; +using LogLevel = Core.Models.Spt.Logging.LogLevel; namespace Core.Models.Utils; @@ -14,4 +15,5 @@ public interface ISptLogger void Debug(string data, Exception? ex = null); void Critical(string data, Exception? ex = null); void WriteToLogFile(string body); + bool IsLogEnabled(LogLevel level); } diff --git a/Server/Logger/WebApplicationLogger.cs b/Server/Logger/WebApplicationLogger.cs index eb938ae3..ea7ec3d4 100644 --- a/Server/Logger/WebApplicationLogger.cs +++ b/Server/Logger/WebApplicationLogger.cs @@ -2,6 +2,7 @@ using SptCommon.Annotations; using Core.Models.Eft.ItemEvent; using Core.Models.Logging; using Core.Models.Utils; +using LogLevel = Core.Models.Spt.Logging.LogLevel; namespace Server.Logger; @@ -88,4 +89,25 @@ public class SptWebApplicationLogger : ISptLogger //TODO - implement + turn object into json _logger.LogError("NOT IMPLEMENTED - WriteToLogFile"); } + + public bool IsLogEnabled(LogLevel level) + { + return _logger.IsEnabled(ConvertLogLevel(level)); + } + + protected Microsoft.Extensions.Logging.LogLevel ConvertLogLevel(LogLevel level) + { + return level switch + { + LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace, + LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug, + LogLevel.Success + or LogLevel.Info + or LogLevel.Custom => Microsoft.Extensions.Logging.LogLevel.Information, + LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning, + LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error, + LogLevel.Fatal => Microsoft.Extensions.Logging.LogLevel.Critical, + _ => throw new ArgumentOutOfRangeException(nameof(level), level, null) + }; + } } diff --git a/Tools/ItemTplGenerator/SptBasicLogger.cs b/Tools/ItemTplGenerator/SptBasicLogger.cs index 2e8a45a0..90301601 100644 --- a/Tools/ItemTplGenerator/SptBasicLogger.cs +++ b/Tools/ItemTplGenerator/SptBasicLogger.cs @@ -1,4 +1,5 @@ using Core.Models.Logging; +using Core.Models.Spt.Logging; using Core.Models.Utils; using SptCommon.Annotations; @@ -53,4 +54,9 @@ public class SptBasicLogger : ISptLogger { Console.WriteLine($"{categoryName}: {body}"); } + + public bool IsLogEnabled(LogLevel level) + { + return true; + } } diff --git a/UnitTests/Mock/MockLogger.cs b/UnitTests/Mock/MockLogger.cs index a43b2458..57449f47 100644 --- a/UnitTests/Mock/MockLogger.cs +++ b/UnitTests/Mock/MockLogger.cs @@ -1,4 +1,5 @@ using Core.Models.Logging; +using Core.Models.Spt.Logging; using Core.Models.Utils; namespace UnitTests.Mock; @@ -55,6 +56,11 @@ public class MockLogger : ISptLogger throw new NotImplementedException(); } + public bool IsLogEnabled(LogLevel level) + { + return true; + } + public void WriteToLogFile(object body) { Console.WriteLine(body);