From 9f538fe6852b52d8f1e0b5b7225d38dcc3d6a682 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 30 Dec 2024 22:46:30 +0100 Subject: [PATCH] Smol code optimization --- .../threading/NativeThreadingHandler.kt | 13 ++----- .../engine/base/logging/Logger.kt | 35 ++++++------------- 2 files changed, 13 insertions(+), 35 deletions(-) diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/threading/NativeThreadingHandler.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/threading/NativeThreadingHandler.kt index 4faa50e..d812a35 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/threading/NativeThreadingHandler.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/threading/NativeThreadingHandler.kt @@ -78,21 +78,14 @@ class NativeThreadingHandler : ThreadingHandler { .uncaughtExceptionHandler { thread, throwable -> logger.crash("Logging thread terminated unexpectedly", throwable) } .start { var time: ULong - var waitTime: ULong while (active) { time = measureTimeMillis { flush() }.toULong() if (EngineConfiguration.logThreadingPollDelay > 0u) if (time > EngineConfiguration.logThreadingPollDelay) { logger.warn("Logging thread is unable to keep up! Processing for last message batch took ${time}ms, which is longer than the configured logThreadingPollDelay of ${EngineConfiguration.logThreadingPollDelay}ms") - waitTime = 0u } else - waitTime = EngineConfiguration.logThreadingPollDelay.minus(time) - else - waitTime = 0u - - if (waitTime > 0u) - Thread.sleep(waitTime.toLong()) + Thread.sleep(EngineConfiguration.logThreadingPollDelay.minus(time).toLong()) } } } @@ -122,8 +115,8 @@ class NativeThreadingHandler : ThreadingHandler { this@NativeThreadingHandler.queue.clear() } - for (call: Call in queue) - Processor.process(call) + for (call: Call in queue) + Processor.process(call) } } } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt b/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt index cc9323c..20c3819 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt @@ -149,11 +149,9 @@ class Logger { if (level == Level.CRASH) CrashHandler.handle(call, levelData["throwable"] as Throwable?, levelData.getOrDefault("fatal", true) as Boolean) else { - if (Processor.check(call)) - return - - if (EngineConfiguration.logThreadingHandler?.queue(call) == null) - Processor.process(call) + if (!Processor.check(call)) + if (EngineConfiguration.logThreadingHandler?.queue(call) == null) + Processor.process(call) } } @@ -163,9 +161,7 @@ class Logger { * @param message message * @since v1-alpha10 */ - fun diag(message: String) { - log(Level.DIAGNOSTIC, message, callerDepth = 1u) - } + fun diag(message: String) = log(Level.DIAGNOSTIC, message, callerDepth = 1u) /** * Logs a verbose message. @@ -173,9 +169,7 @@ class Logger { * @param message message * @since v1-alpha10 */ - fun verb(message: String) { - log(Level.VERBOSE, message, callerDepth = 1u) - } + fun verb(message: String) = log(Level.VERBOSE, message, callerDepth = 1u) /** * Logs a silent warning. @@ -183,9 +177,7 @@ class Logger { * @param message message * @since v1-alpha10 */ - fun sarn(message: String) { - log(Level.SILENT_WARNING, message, callerDepth = 1u) - } + fun sarn(message: String) = log(Level.SILENT_WARNING, message, callerDepth = 1u) /** * Logs an informational message. @@ -193,9 +185,7 @@ class Logger { * @param message message * @since v1-alpha10 */ - fun info(message: String) { - log(Level.INFORMATIONAL, message, callerDepth = 1u) - } + fun info(message: String) = log(Level.INFORMATIONAL, message, callerDepth = 1u) /** * Logs a silent warning. @@ -203,9 +193,7 @@ class Logger { * @param message message * @since v1-alpha10 */ - fun warn(message: String) { - log(Level.WARNING, message, callerDepth = 1u) - } + fun warn(message: String) = log(Level.WARNING, message, callerDepth = 1u) /** * Logs a non-fatal error. @@ -213,9 +201,7 @@ class Logger { * @param message message * @since v1-alpha10 */ - fun error(message: String) { - log(Level.ERROR, message, callerDepth = 1u) - } + fun error(message: String) = log(Level.ERROR, message, callerDepth = 1u) /** * Logs a fatal error. @@ -225,12 +211,11 @@ class Logger { * @param fatal terminates the engine and application if `true` * @since v1-alpha10 */ - fun crash(error: String, throwable: Throwable? = null, fatal: Boolean = true) { + fun crash(error: String, throwable: Throwable? = null, fatal: Boolean = true) = log(Level.CRASH, error, levelData = mapOf( Pair("throwable", throwable as Object?), Pair("fatal", fatal as Object?) ), callerDepth = 1u) - } // -----> Utility