diff --git a/Logger/ILogger.cs b/Logger/ILogger.cs index 3e410d1..7e6366b 100644 --- a/Logger/ILogger.cs +++ b/Logger/ILogger.cs @@ -4,5 +4,6 @@ public interface ILogger { void Setup(); void Log(string message, LogLevel level); + bool CanBeLogged(LogLevel level); void Stop(); } \ No newline at end of file diff --git a/Logger/QueueLogger.cs b/Logger/QueueLogger.cs index 31dca7e..831992c 100644 --- a/Logger/QueueLogger.cs +++ b/Logger/QueueLogger.cs @@ -72,6 +72,11 @@ public class QueueLogger : ILogger queuedMessages.Add(new LoggedMessage { Message = message, LogLevel = level }); } + public bool CanBeLogged(LogLevel level) + { + return GetLogLevel(level) <= logLevel; + } + // Wait for graceful termination of the logging thread public void Stop() { diff --git a/Process/Processor/DumpProcessor/MultithreadSteppedDumpProcessor.cs b/Process/Processor/DumpProcessor/MultithreadSteppedDumpProcessor.cs index 1d968d2..bed80d7 100644 --- a/Process/Processor/DumpProcessor/MultithreadSteppedDumpProcessor.cs +++ b/Process/Processor/DumpProcessor/MultithreadSteppedDumpProcessor.cs @@ -26,11 +26,13 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor public Dictionary ProcessDumps(List dumps) { - LoggerFactory.GetInstance().Log("Starting final dump processing", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Starting final dump processing", LogLevel.Info); var output = new Dictionary(); var dumpProcessData = GetDumpProcessData(dumps); - LoggerFactory.GetInstance().Log("Heavy processing done!", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Heavy processing done!", LogLevel.Info); var staticContainers = new Dictionary(); var staticContainersLock = new object(); @@ -43,13 +45,15 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor Runners.Clear(); // BSG changed the map data so static containers are now dynamic, so we need to scan all dumps for the static containers. - LoggerFactory.GetInstance().Log("Queuing dumps for static data processing", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Queuing dumps for static data processing", LogLevel.Info); foreach (var dumped in dumps) { Runners.Add( Task.Factory.StartNew(() => { - LoggerFactory.GetInstance().Log($"Processing static data for file {dumped.BasicInfo.FileName}", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Debug)) + LoggerFactory.GetInstance().Log($"Processing static data for file {dumped.BasicInfo.FileName}", LogLevel.Debug); var data = _jsonSerializer.Deserialize(File.ReadAllText(dumped.BasicInfo.FileName)); // the if statement below takes care of processing "forced" or real static data for each map, we only need // to do this once per map, so we dont care about doing it again @@ -57,7 +61,8 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor { if (!staticContainers.ContainsKey(data.Data.Name)) { - LoggerFactory.GetInstance().Log($"Doing first time process for map {data.Data.Name} of real static data", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log($"Doing first time process for map {data.Data.Name} of real static data", LogLevel.Info); var mapStaticLoot = StaticLootProcessor.CreateRealStaticContainers(data); staticContainers[mapStaticLoot.Item1] = mapStaticLoot.Item2; } @@ -114,7 +119,8 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor } Task.WaitAll(Runners.ToArray()); - LoggerFactory.GetInstance().Log("All static data processing threads finished", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("All static data processing threads finished", LogLevel.Info); // Aggregate and calculate the probability of a static container mapStaticContainersAggregated.ToDictionary( kv => kv.Key, @@ -129,35 +135,39 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor // Static containers output.Add(OutputFileType.StaticContainer, staticContainers); - - LoggerFactory.GetInstance().Log("Processing ammo distribution", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Processing ammo distribution", LogLevel.Info); // Ammo distribution output.Add( OutputFileType.StaticAmmo, StaticLootProcessor.CreateAmmoDistribution(dumpProcessData.ContainerCounts) ); - LoggerFactory.GetInstance().Log("Processing static loot distribution", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Processing static loot distribution", LogLevel.Info); // Static loot distribution output.Add( OutputFileType.StaticLoot, StaticLootProcessor.CreateStaticLootDistribution(dumpProcessData.ContainerCounts) ); - LoggerFactory.GetInstance().Log("Processing loose loot distribution", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Processing loose loot distribution", LogLevel.Info); // Loose loot distribution var looseLootDistribution = LooseLootProcessor.CreateLooseLootDistribution( dumpProcessData.MapCounts, dumpProcessData.LooseLootCounts ); - LoggerFactory.GetInstance().Log("Collecting loose loot distribution information", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Collecting loose loot distribution information", LogLevel.Info); var loot = dumpProcessData.MapCounts .Select(mapCount => mapCount.Key) .ToDictionary(mi => mi, mi => looseLootDistribution[mi]); output.Add(OutputFileType.LooseLoot, loot); - LoggerFactory.GetInstance().Log("Dump processing fully completed!", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log("Dump processing fully completed!", LogLevel.Info); return output; } @@ -176,10 +186,11 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor { var mapi = tuple.Key; var g = tuple.ToList(); - LoggerFactory.GetInstance().Log( - $"Processing map {mapi}, total dump data to process: {g.Count}", - LogLevel.Info - ); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log( + $"Processing map {mapi}, total dump data to process: {g.Count}", + LogLevel.Info + ); dumpProcessData.MapCounts[mapi] = g.Count; var lockObjectContainerCounts = new object(); @@ -275,18 +286,16 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor lock (lockObjectCounts) { - counts.MapSpawnpointCount.AddRange(new List - { - dumpData.LooseLoot.MapSpawnpointCount - }); + counts.MapSpawnpointCount.Add(dumpData.LooseLoot.MapSpawnpointCount); } } catch (Exception e) { - LoggerFactory.GetInstance().Log( - $"ERROR OCCURRED:{e.Message}\n{e.StackTrace}", - LogLevel.Error - ); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Error)) + LoggerFactory.GetInstance().Log( + $"ERROR OCCURRED:{e.Message}\n{e.StackTrace}", + LogLevel.Error + ); } } }, @@ -297,10 +306,11 @@ public class MultithreadSteppedDumpProcessor : IDumpProcessor // Wait until all runners are done processing while (!Runners.All(r => r.IsCompleted)) { - LoggerFactory.GetInstance().Log( - $"One or more file processors are still processing files. Waiting {LootDumpProcessorContext.GetConfig().ThreadPoolingTimeoutMs}ms before checking again", - LogLevel.Info - ); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Info)) + LoggerFactory.GetInstance().Log( + $"One or more file processors are still processing files. Waiting {LootDumpProcessorContext.GetConfig().ThreadPoolingTimeoutMs}ms before checking again", + LogLevel.Info + ); Thread.Sleep( TimeSpan.FromMilliseconds(LootDumpProcessorContext.GetConfig().ThreadPoolingTimeoutMs)); } diff --git a/Process/Processor/FileProcessor/FileProcessor.cs b/Process/Processor/FileProcessor/FileProcessor.cs index ba02cdb..f38ac2f 100644 --- a/Process/Processor/FileProcessor/FileProcessor.cs +++ b/Process/Processor/FileProcessor/FileProcessor.cs @@ -9,7 +9,8 @@ public class FileProcessor : IFileProcessor { public PartialData Process(BasicInfo parsedData) { - LoggerFactory.GetInstance().Log($"Processing file {parsedData.FileName}...", LogLevel.Info); + if (LoggerFactory.GetInstance().CanBeLogged(LogLevel.Debug)) + LoggerFactory.GetInstance().Log($"Processing file {parsedData.FileName}...", LogLevel.Debug); List