From c984974252c8055b526a0e603e28de0052ef965e Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 31 Jul 2024 15:02:29 +0200 Subject: [PATCH] Implement logic for logging thread control The logging thread now automatically starts or shuts down based on EngineConfiguration#optimizeLogging and Engine#state == EngineState.RUNNING --- .../sosengine/base/EngineConfiguration.java | 9 ++++++++- .../de/staropensource/sosengine/base/logging/Logger.java | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java b/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java index fcde538..10e7d50 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java +++ b/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java @@ -24,6 +24,7 @@ import de.staropensource.sosengine.base.classes.Configuration; import de.staropensource.sosengine.base.classes.helpers.EventHelper; import de.staropensource.sosengine.base.logging.CrashHandler; import de.staropensource.sosengine.base.logging.Logger; +import de.staropensource.sosengine.base.types.EngineState; import de.staropensource.sosengine.base.types.logging.LogLevel; import de.staropensource.sosengine.base.types.vectors.Vec2f; import de.staropensource.sosengine.base.utility.parser.PropertyParser; @@ -306,7 +307,13 @@ public final class EngineConfiguration extends Configuration { case "errorShortcodeConverter" -> errorShortcodeConverter = parser.getBoolean(group + property); - case "optimizeLogging" -> optimizeLogging = parser.getBoolean(group + property); + case "optimizeLogging" -> { + optimizeLogging = parser.getBoolean(group + property); + + // Start logging thread automatically + if (optimizeLogging && Engine.getInstance().getState() == EngineState.RUNNING) + Logger.startLoggingThread(); + } case "optimizeEvents" -> optimizeEvents = parser.getBoolean(group + property); case "optimizeSubsystemInitialization" -> optimizeSubsystemInitialization = parser.getBoolean(group + property); diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java index 37f4017..baf915a 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java @@ -28,6 +28,7 @@ import de.staropensource.sosengine.base.events.LogEvent; import de.staropensource.sosengine.base.internal.placeholders.logger.*; import de.staropensource.sosengine.base.internal.types.QueuedLogMessage; import de.staropensource.sosengine.base.logging.implementation.PlainLoggerImplementation; +import de.staropensource.sosengine.base.types.EngineState; import de.staropensource.sosengine.base.types.logging.LogLevel; import de.staropensource.sosengine.base.types.logging.LogRule; import de.staropensource.sosengine.base.types.logging.LogRuleType; @@ -126,7 +127,9 @@ public final class Logger { Runnable threadLogic = () -> { while (true) { // Run in loop // Stop thread when engine is shutting down - if (Engine.getInstance().isShuttingDown()) + if (Engine.getInstance().getState() == EngineState.SHUTDOWN + || Engine.getInstance().getState() == EngineState.CRASHED + || !EngineConfiguration.getInstance().isOptimizeLogging()) return; // Process all log messages