From 24a11572ec993061bfd3523b1619eda7a6503855 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 6 Jan 2025 02:34:44 +0100 Subject: [PATCH] Make methods of StackTraceUtils class extensions --- .../engine/base/implementable/Event.kt | 4 +- .../crashcategory/InfoCrashCategory.kt | 4 +- .../engine/base/logging/Logger.kt | 4 +- .../engine/base/logging/Processor.kt | 10 +- .../engine/base/utility/FileAccess.kt | 4 +- .../base/utility/dnihbd/BuildInformation.kt | 5 +- .../base/utility/misc/StackTraceUtils.kt | 238 ++++++++---------- .../base/utility/misc/StackTraceUtilsTest.kt | 21 +- 8 files changed, 136 insertions(+), 154 deletions(-) diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt index dbf964f..6256c28 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt @@ -21,7 +21,7 @@ package de.staropensource.engine.base.implementable import de.staropensource.engine.base.Engine.Companion.logger import de.staropensource.engine.base.extension.toStringType -import de.staropensource.engine.base.utility.misc.StackTraceUtils +import de.staropensource.engine.base.utility.misc.stacktraceRecursive /** * An event. @@ -114,7 +114,7 @@ abstract class Event { try { listener.invoke(arguments) } catch (throwable: Throwable) { - logger.error("A listener on event ${this::class.qualifiedName ?: ""} threw ${throwable::class.qualifiedName ?: ""}\n\n${StackTraceUtils.stacktraceRecursive(throwable)}") + logger.error("A listener on event ${this::class.qualifiedName ?: ""} threw ${throwable::class.qualifiedName ?: ""}\n\n${throwable.stacktraceRecursive()}") } } } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt index a8de561..d0315db 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt @@ -22,7 +22,7 @@ package de.staropensource.engine.base.implementation.logging.crashcategory import de.staropensource.engine.base.implementable.logging.CrashCategory import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.ChannelSettings -import de.staropensource.engine.base.utility.misc.StackTraceUtils +import de.staropensource.engine.base.utility.misc.stacktraceRecursive /** * [CrashCategory] implementation @@ -69,6 +69,6 @@ class InfoCrashCategory private constructor() : CrashCategory { Pair("Channel", "'${call.channel}'"), Pair("Fatal", if (fatal) "yes" else "no"), Pair("Message", call.message), - Pair("Stacktrace", if (throwable == null) "Not available." else StackTraceUtils.stacktraceRecursive(throwable)), + Pair("Stacktrace", throwable?.stacktraceRecursive() ?: "Not available."), ) } 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 20c3819..c72bebc 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 @@ -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 @@ -139,7 +139,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/main/kotlin/de/staropensource/engine/base/logging/Processor.kt b/base/src/main/kotlin/de/staropensource/engine/base/logging/Processor.kt index 2f9f0e2..bdcd0cd 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/logging/Processor.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/logging/Processor.kt @@ -20,15 +20,15 @@ package de.staropensource.engine.base.logging import de.staropensource.engine.base.EngineConfiguration +import de.staropensource.engine.base.implementable.formatter.Formatter import de.staropensource.engine.base.implementable.logging.Adapter import de.staropensource.engine.base.implementable.logging.FormatBuilder -import de.staropensource.engine.base.implementable.formatter.Formatter import de.staropensource.engine.base.implementable.logging.ThreadingHandler 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.reflect.full.primaryConstructor /** @@ -102,7 +102,11 @@ 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${throwable.stacktraceRecursive()}" + ) return } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt index 5126518..4f085aa 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt @@ -30,7 +30,7 @@ import de.staropensource.engine.base.implementation.stream.FileAccessStream import de.staropensource.engine.base.utility.Environment.OperatingSystem.* import de.staropensource.engine.base.utility.FileAccess.Companion.configDirectory import de.staropensource.engine.base.utility.FileAccess.Companion.format -import de.staropensource.engine.base.utility.misc.StackTraceUtils +import de.staropensource.engine.base.utility.misc.stacktraceRecursive import java.io.File import java.io.IOException import java.nio.file.* @@ -216,7 +216,7 @@ class FileAccess { } } catch (_: NoSuchFileException) { } catch (exception: Exception) { - logger.sarn("Unable to delete file or directory '${unformatFromPath(path)}' scheduled for deletion.\n${StackTraceUtils.stacktraceRecursive(exception)}") + logger.sarn("Unable to delete file or directory '${unformatFromPath(path)}' scheduled for deletion.\n${exception.stacktraceRecursive()}") } } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt index 3653057..ac2ae05 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt @@ -25,7 +25,7 @@ import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.ChannelSettings import de.staropensource.engine.base.type.versioning.VersionType import de.staropensource.engine.base.utility.FileAccess -import de.staropensource.engine.base.utility.misc.StackTraceUtils +import de.staropensource.engine.base.utility.misc.methodCaller import kotlinx.datetime.Instant import kotlinx.datetime.LocalDateTime import kotlinx.datetime.TimeZone @@ -34,6 +34,7 @@ import java.io.InputStreamReader import java.io.Reader import java.io.StringReader import java.util.* +import kotlin.Throws /** * Loads the specified `-git.properties` and @@ -342,7 +343,7 @@ open class BuildInformation protected fun getFileContent(file: String): String? { if (loadLocation == null) { // Read from resources - val origin: Origin = StackTraceUtils.methodCaller(depth = 2u) + val origin: Origin = methodCaller(depth = 2u) val reader: Reader try { reader = InputStreamReader( diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt index 7a778e8..c7b39ea 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtils.kt @@ -23,143 +23,121 @@ import de.staropensource.engine.base.type.Origin import java.lang.reflect.InvocationTargetException /** - * Utility methods for stack trace - * and exception analysis. + * Returns the caller of the executing method. * - * @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 +fun Any.methodCaller(depth: UInt = 1u): 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] + // 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 + * of this [Throwable]. It is usually + * found in JVM stacktraces. + * + * @return stacktrace header + * @since v1-alpha10 + */ +fun Throwable.stacktraceHeader(): String { + return "Caused by: ${this.javaClass.name}${if (this.message == null) "" else ": ${this.message}"}" +} + +/** + * Returns the body of the stacktrace + * of this [Throwable]. It is usually + * found in JVM stacktraces. + * + * @param indent if to add a tab character (`\t`) as indentation + * @return stacktrace body + * @since v1-alpha10 + */ +fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString { + for (element: StackTraceElement in stackTrace) { + if (!isEmpty()) + append("\n") + if (indent) + append("\t") + + append("at ") + append(element) + } +} + +/** + * Returns a recursively resolved + * collection of stack traces, + * derived from the throwable it + * is invoked from. + * + * Use this method if you intend to + * print an exception's stacktrace or + * want to display it nicely elsewhere. + * + * @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 Throwable.stacktraceRecursive(indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString { + // Append header + if (includeHeader) { + append(stacktraceHeader()) + append("\n") + } + + // Append body + append(stacktraceBody(indent = indent)) + + // Recurse downwards + if (cause != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(cause!!.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + } + + if (this@stacktraceRecursive is ClassNotFoundException) { + if (exception != null) { + append("\n") + if (depth == 0u) + append("...") 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(), - ) + append(this@stacktraceRecursive.stacktraceRecursive(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.javaClass.name}${if (throwable.message == null) "" else ": ${throwable.message}"}" + } else if (this@stacktraceRecursive is ExceptionInInitializerError) { + if (exception != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) } - - /** - * 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))) - } - } + } else if (this@stacktraceRecursive is InvocationTargetException) { + if (targetException != null) { + append("\n") + if (depth == 0u) + append("...") + else + append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) } } } diff --git a/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt b/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt index f8b4e24..ecbfb0d 100644 --- a/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt +++ b/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt @@ -21,41 +21,40 @@ package de.staropensource.engine.base.utility.misc import de.staropensource.engine.base.type.Origin import de.staropensource.engine.testing.TestBase -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.DisplayName -import java.lang.NullPointerException import kotlin.test.Test -import kotlin.test.assertContains class StackTraceUtilsTest : TestBase() { @Test - fun methodCaller() { + fun methodCallerTest() { val origin: Origin = methodCallerOrigin() assertEquals("de.staropensource.engine.base.utility.misc", origin.packageName) assertEquals("StackTraceUtilsTest", origin.className) - assertEquals("methodCaller", origin.methodName) - assertEquals(33u, origin.lineNumber) + assertEquals("methodCallerTest", origin.methodName) + assertEquals(32u, origin.lineNumber) } - private fun methodCallerOrigin(): Origin = StackTraceUtils.methodCaller() + private fun methodCallerOrigin(): Origin = methodCaller() @Test @DisplayName("stacktraceHeader (no message)") fun stacktraceHeaderNoMessage() { - assertEquals("Caused by: java.lang.NullPointerException", StackTraceUtils.stacktraceHeader(NullPointerException())) + assertEquals("Caused by: java.lang.NullPointerException", NullPointerException().stacktraceHeader()) } @Test @DisplayName("stacktraceHeader (with message)") fun stacktraceHeaderWithMessage() { - assertEquals("Caused by: java.lang.NullPointerException: Hello World!", StackTraceUtils.stacktraceHeader(NullPointerException("Hello World!"))) + assertEquals("Caused by: java.lang.NullPointerException: Hello World!", NullPointerException("Hello World!").stacktraceHeader()) } @Test @DisplayName("stacktraceBody (no indent)") fun stacktraceBodyNoIndent() { - val body: String = StackTraceUtils.stacktraceBody(Throwable(), indent = false) + val body: String = Throwable().stacktraceBody(indent = false) assertTrue(body.startsWith("at "), "Body '${body}' does not start with 'at '") assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest") @@ -64,7 +63,7 @@ class StackTraceUtilsTest : TestBase() { @Test @DisplayName("stacktraceBody (with indent)") fun stacktraceBodyWithIndent() { - val body: String = StackTraceUtils.stacktraceBody(Throwable(), indent = true) + val body: String = Throwable().stacktraceBody(indent = true) assertTrue(body.startsWith("\tat "), "Body '${body}' does not start with '\tat '") assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest")