From 06748dcdf12a0bb7c9f82c82ff1c95898eab4e8c Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 29 Dec 2024 16:20:54 +0100 Subject: [PATCH] Migrate StackTraceUtils & Miscellaneous --- .../engine/base/logging/Logger.kt | 4 +- .../engine/base/logging/Processor.kt | 4 +- .../engine/base/platform/EnginePlatform.kt | 7 +- .../engine/base/utility/misc/Miscellaneous.kt | 13 +- .../engine/base/utility/misc/Platform.kt | 29 +++ .../base/utility/misc/StackTraceUtils.kt | 213 +++++++----------- .../engine/base/utility/misc/Platform.kt | 22 ++ .../base/utility/misc/StackTraceUtils.kt | 85 +++++++ 8 files changed, 221 insertions(+), 156 deletions(-) create mode 100644 base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt create mode 100644 base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt create mode 100644 base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Logger.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Logger.kt index 9ddab8b..815406d 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Logger.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Logger.kt @@ -27,7 +27,7 @@ import de.staropensource.engine.base.implementation.stream.FileAccessStream import de.staropensource.engine.base.implementation.stream.LoggerStream import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.Level -import de.staropensource.engine.base.utility.misc.StackTraceUtils +import de.staropensource.engine.base.utility.misc.methodCaller import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlin.jvm.JvmName @@ -124,7 +124,7 @@ class Logger { fun log(level: Level, message: String, levelData: Map = emptyMap(), callerDepth: UInt = 0u) { // Create 'Call' instance var call: Call = Call( - StackTraceUtils.methodCaller(depth = callerDepth.plus(1u)), + methodCaller(depth = callerDepth.plus(1u)), level, message, channel diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Processor.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Processor.kt index 4df346f..3ce83f1 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Processor.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/logging/Processor.kt @@ -28,7 +28,7 @@ import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.ChannelSettings import de.staropensource.engine.base.type.logging.Feature import de.staropensource.engine.base.type.logging.OperationMode -import de.staropensource.engine.base.utility.misc.StackTraceUtils +import de.staropensource.engine.base.utility.misc.stacktraceRecursive import kotlin.jvm.JvmStatic import kotlin.reflect.full.primaryConstructor @@ -103,7 +103,7 @@ class Processor private constructor() { try { format = (EngineConfiguration.logFormatBuilder).primaryConstructor!!.call(call, channelSettings) } catch (throwable: Throwable) { - println("Logger system failure: Configured FormatBuilder implementation '" + ((EngineConfiguration.logFormatBuilder).qualifiedName ?: "") + "' does not have a primary 'constructor(Call, ChannelSettings?)'. Log messages cannot be processed.\n${StackTraceUtils.stacktraceRecursive(throwable)}") + println("Logger system failure: Configured FormatBuilder implementation '" + ((EngineConfiguration.logFormatBuilder).qualifiedName ?: "") + "' does not have a primary 'constructor(Call, ChannelSettings?)'. Log messages cannot be processed.\n${stacktraceRecursive(throwable)}") return } diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/EnginePlatform.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/EnginePlatform.kt index c2dea57..e9b3c0e 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/EnginePlatform.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/EnginePlatform.kt @@ -19,12 +19,11 @@ package de.staropensource.engine.base.platform -import de.staropensource.engine.base.Engine /** * Bootstraps this platform. * - * Invoked by [Engine.bootstrap] first. + * Invoked by [de.staropensource.engine.base.Engine.bootstrap] first. * * @throws Throwable on error * @since v1-alpha10 @@ -35,7 +34,7 @@ expect fun platformEngineBootstrap() /** * Initializes this platform. * - * Invoked by [Engine.initialize] first. + * Invoked by [de.staropensource.engine.base.Engine.initialize] first. * * @throws Throwable on error * @since v1-alpha10 @@ -46,7 +45,7 @@ expect fun platformEngineInitialize() /** * Reloads this platform. * - * Invoked by [Engine.reload] first. + * Invoked by [de.staropensource.engine.base.Engine.reload] first. * * @throws Throwable on error * @since v1-alpha10 diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Miscellaneous.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Miscellaneous.kt index 3047502..de05947 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Miscellaneous.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Miscellaneous.kt @@ -31,16 +31,5 @@ class Miscellaneous private constructor() { * * @since v1-alpha10 */ - companion object { - /** - * Returns if currently - * running on the main thread. - * - * @return `true` if running on the main thread, `false` if not, `null` if not supported (e.g. not running on the JVM) - * @since v1-alpha10 - */ - fun onMainThread(): Boolean? { - return Thread.currentThread().threadId() == 1L - } - } + companion object } diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt new file mode 100644 index 0000000..e61ecc8 --- /dev/null +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt @@ -0,0 +1,29 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.utility.misc + +/** + * Returns if currently + * running on the main thread. + * + * @return `true` if running on the main thread, `false` if not, `null` if not supported + * @since v1-alpha10 + */ +expect fun onMainThread(): Boolean? diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt index 7a778e8..45e94b5 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt @@ -20,146 +20,87 @@ package de.staropensource.engine.base.utility.misc import de.staropensource.engine.base.type.Origin -import java.lang.reflect.InvocationTargetException /** - * Utility methods for stack trace - * and exception analysis. + * Returns the method caller. * - * @constructor Initializes this class + * @param depth how deep to go into the stack trace + * @return deepest method caller * @since v1-alpha10 */ -class StackTraceUtils private constructor() { - /** - * Companion object of [StackTraceUtils]. - * - * @since v1-alpha10 - */ - companion object { - /** - * Returns the method caller. - * - * @param depth how deep to go into the stack trace - * @return deepest method caller - * @since v1-alpha10 - */ - fun methodCaller(depth: UInt = 1u): Origin { - val stacktrace: Array = Throwable().stackTrace - var element: StackTraceElement? = null +expect fun methodCaller(depth: UInt = 1u): Origin - // Get wanted element - element = if (depth.plus(2u).toInt() > stacktrace.size) - stacktrace[stacktrace.size - 1] - else - stacktrace[depth.plus(2u).toInt()] - - // Return origin - return Origin( - packageName = element.className.substringBeforeLast('.'), - className = element.className.substringAfterLast('.'), - methodName = element.methodName, - lineNumber = element.lineNumber.toUInt(), - ) - } - - - /** - * Returns the `Caused by: ` header - * usually found in JVM stacktraces. - * - * @param throwable [Throwable] to use - * @return stacktrace header - * @since v1-alpha10 - */ - fun stacktraceHeader(throwable: Throwable): String { - return "Caused by: ${throwable.javaClass.name}${if (throwable.message == null) "" else ": ${throwable.message}"}" - } - - /** - * Returns the body of the stacktrace - * as it is usually found in JVM - * stacktraces. - * - * @param throwable [Throwable] to use - * @param indent if to add a tab character (`\t`) as indentation - * @return stacktrace body - * @since v1-alpha10 - */ - fun stacktraceBody(throwable: Throwable, indent: Boolean = true): String = buildString { - for (element: StackTraceElement in throwable.stackTrace) { - if (!isEmpty()) - append("\n") - if (indent) - append("\t") - - append("at ") - append(element) - } - } - - /** - * Returns a recursively resolved - * collection of stacktraces, all - * derived from the passed [throwable]. - * - * Use this method if you intend to - * print an exception's stacktrace or - * want to display it nicely elsewhere. - * - * - * @param throwable [Throwable] to use - * @param indent if to add a tab character (`\t`) as indentation - * @param includeHeader if to include the `Caused by` stacktrace header (see [stacktraceHeader]) - * @return recursively resolved stacktrace - * @since v1-alpha10 - */ - fun stacktraceRecursive(throwable: Throwable, indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString { - // Append header - if (includeHeader) { - append(stacktraceHeader(throwable)) - append("\n") - } - - // Append body - append(stacktraceBody(throwable, indent = indent)) - - // Recurse downwards - if (throwable.cause != null) { - append("\n") - if (depth == 0u) - append("...") - else - append(stacktraceRecursive(throwable.cause!!, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) - } - - if (throwable is ClassNotFoundException) { - val exception: ClassNotFoundException = throwable - if (exception.exception != null) { - append("\n") - if (depth == 0u) - append("...") - else - append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) - } - } else if (throwable is ExceptionInInitializerError) { - val exception: ExceptionInInitializerError = throwable - if (exception.exception != null) { - append("\n") - if (depth == 0u) - append("...") - else - append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) - } - } else if (throwable is InvocationTargetException) { - val exception: InvocationTargetException = throwable - if (exception.targetException != null) { - append("\n") - if (depth == 0u) - append("...") - else - append(stacktraceRecursive(exception.targetException, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) - } - } - } - } +/** + * Returns the `Caused by: ` header + * usually found in JVM stacktraces. + * + * @param throwable [Throwable] to use + * @return stacktrace header + * @since v1-alpha10 + */ +fun stacktraceHeader(throwable: Throwable): String { + return "Caused by: ${throwable::class.qualifiedName ?: ""}${if (throwable.message == null) "" else ": ${throwable.message}"}" } + +/** + * Returns the body of the stacktrace + * as it is usually found in JVM + * stacktraces. + * + * @param throwable [Throwable] to use + * @param indent if to add a tab character (`\t`) as indentation + * @return stacktrace body + * @since v1-alpha10 + */ +expect fun stacktraceBody(throwable: Throwable, indent: Boolean = true): String + +/** + * Returns a recursively resolved + * collection of stacktraces, all + * derived from the passed [throwable]. + * + * Use this method if you intend to + * print an exception's stacktrace or + * want to display it nicely elsewhere. + * + * + * @param throwable [Throwable] to use + * @param indent if to add a tab character (`\t`) as indentation + * @param includeHeader if to include the `Caused by` stacktrace header (see [stacktraceHeader]) + * @param depth how deep to recurse into [Throwable]s + * @return recursively resolved stacktrace + * @since v1-alpha10 + */ +fun stacktraceRecursive(throwable: Throwable, indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString { + // Append header + if (includeHeader) { + append(stacktraceHeader(throwable)) + append("\n") + } + + // Append body + append(stacktraceBody(throwable, indent = indent)) + + // Recurse downwards + if (throwable.cause != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(stacktraceRecursive(throwable.cause!!, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + } + + platformSTUHandleRecursiveThrowables(throwable, indent, includeHeader, depth) +} + +/** + * Handles platform-specific [Throwable]s recursively. + * + * @param throwable [Throwable] to use + * @param indent if to add a tab character (`\t`) as indentation + * @param includeHeader if to include the `Caused by` stacktrace header (see [stacktraceHeader]) + * @param depth how deep to recurse into [Throwable]s + * @return stacktraces + * @since v1-alpha10 + */ +expect fun platformSTUHandleRecursiveThrowables(throwable: Throwable, indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String diff --git a/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt b/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt new file mode 100644 index 0000000..95507f0 --- /dev/null +++ b/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/Platform.kt @@ -0,0 +1,22 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.utility.misc + +actual fun onMainThread(): Boolean? = Thread.currentThread().threadId() == 1L diff --git a/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt b/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt new file mode 100644 index 0000000..cec3f2d --- /dev/null +++ b/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt @@ -0,0 +1,85 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.utility.misc + +import de.staropensource.engine.base.type.Origin +import java.lang.reflect.InvocationTargetException + +actual fun methodCaller(depth: UInt): Origin { + val stacktrace: Array = Throwable().stackTrace + var element: StackTraceElement? = null + + // Get wanted element + element = if (depth.plus(2u).toInt() > stacktrace.size) + stacktrace[stacktrace.size - 1] + else + stacktrace[depth.plus(2u).toInt()] + + // Return origin + return Origin( + packageName = element.className.substringBeforeLast('.'), + className = element.className.substringAfterLast('.'), + methodName = element.methodName, + lineNumber = element.lineNumber.toUInt(), + ) +} + +actual fun stacktraceBody(throwable: Throwable, indent: Boolean): String = buildString { + for (element: StackTraceElement in throwable.stackTrace) { + if (!isEmpty()) + append("\n") + if (indent) + append("\t") + + append("at ") + append(element) + } +} + +actual fun platformSTUHandleRecursiveThrowables(throwable: Throwable, indent: Boolean, includeHeader: Boolean, depth: UInt): String = buildString { + if (throwable is ClassNotFoundException) { + val exception: ClassNotFoundException = throwable + if (exception.exception != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + } + } else if (throwable is ExceptionInInitializerError) { + val exception: ExceptionInInitializerError = throwable + if (exception.exception != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + } + } else if (throwable is InvocationTargetException) { + val exception: InvocationTargetException = throwable + if (exception.targetException != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(stacktraceRecursive(exception.targetException, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + } + } +}