From 9781945d3ad962a84c13faba96e2cdd6ca4d403b Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 6 Jan 2025 03:13:51 +0100 Subject: [PATCH] Rename StackTraceUtils methods --- .../engine/base/implementable/Event.kt | 4 +- .../crashcategory/InfoCrashCategory.kt | 4 +- .../engine/base/logging/Processor.kt | 4 +- .../engine/base/utility/FileAccess.kt | 4 +- .../base/utility/misc/StackTraceUtils.kt | 45 +++++++++---------- .../base/utility/misc/StackTraceUtilsTest.kt | 24 +++++----- 6 files changed, 42 insertions(+), 43 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 6256c28..e094b1a 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.stacktraceRecursive +import de.staropensource.engine.base.utility.misc.toStringRecursive /** * 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${throwable.stacktraceRecursive()}") + logger.error("A listener on event ${this::class.qualifiedName ?: ""} threw ${throwable::class.qualifiedName ?: ""}\n\n${throwable.toStringRecursive()}") } } } 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 d0315db..ab4a55a 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.stacktraceRecursive +import de.staropensource.engine.base.utility.misc.toStringRecursive /** * [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", throwable?.stacktraceRecursive() ?: "Not available."), + Pair("Stacktrace", throwable?.toStringRecursive() ?: "Not available."), ) } 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 bdcd0cd..5c84b4a 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 @@ -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.stacktraceRecursive +import de.staropensource.engine.base.utility.misc.toStringRecursive import kotlin.reflect.full.primaryConstructor /** @@ -105,7 +105,7 @@ class Processor private constructor() { 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()}" + "' does not have a primary 'constructor(Call, ChannelSettings?)'. Log messages cannot be processed.\n${throwable.toStringRecursive()}" ) 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 4f085aa..347c686 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.stacktraceRecursive +import de.staropensource.engine.base.utility.misc.toStringRecursive 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${exception.stacktraceRecursive()}") + logger.sarn("Unable to delete file or directory '${unformatFromPath(path)}' scheduled for deletion.\n${exception.toStringRecursive()}") } } 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 163c492..38431de 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 @@ -50,13 +50,13 @@ fun Any.methodCaller(depth: UInt = 1u): Origin { /** * Returns the `Caused by: ` header - * of this [Throwable]. It is usually - * found in JVM stacktraces. + * for this [Throwable], usually + * found in JVM stack traces. * - * @return stacktrace header + * @return `Caused by: ` [Throwable] header * @since v1-alpha10 */ -fun Throwable.stacktraceHeader(): String { +fun Throwable.header(): String { if (message == null) return "Caused by: ${this.javaClass.name}" else { @@ -66,15 +66,15 @@ fun Throwable.stacktraceHeader(): String { } /** - * Returns the body of the stacktrace - * of this [Throwable]. It is usually - * found in JVM stacktraces. + * Returns the stacktrace of + * this [Throwable] in a + * printable/readable fashion. * * @param indent if to add a tab character (`\t`) as indentation - * @return stacktrace body + * @return stack trace of this [Throwable] * @since v1-alpha10 */ -fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString { +fun Throwable.body(indent: Boolean = true): String = buildString { for (element: StackTraceElement in stackTrace) { if (!isEmpty()) append("\n") @@ -88,28 +88,27 @@ fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString { /** * Returns a recursively resolved - * collection of stack traces, - * derived from the throwable it - * is invoked from. + * collection of [Throwable]s and + * their accompanying stack traces. * * 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]) + * @param includeHeader if to include the `Caused by` stacktrace header (see [header]) * @return recursively resolved stacktrace * @since v1-alpha10 */ -fun Throwable.stacktraceRecursive(indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString { +fun Throwable.toStringRecursive(indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString { // Append header if (includeHeader) { - append(stacktraceHeader()) + append(header()) append("\n") } // Append body - append(stacktraceBody(indent = indent)) + append(body(indent = indent)) // Recurse downwards if (cause != null) { @@ -117,32 +116,32 @@ fun Throwable.stacktraceRecursive(indent: Boolean = true, includeHeader: Boolean if (depth == 0u) append("...") else - append(cause!!.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + append(cause!!.toStringRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) } - if (this@stacktraceRecursive is ClassNotFoundException) { + if (this@toStringRecursive is ClassNotFoundException) { if (exception != null) { append("\n") if (depth == 0u) append("...") else - append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + append(this@toStringRecursive.toStringRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) } - } else if (this@stacktraceRecursive is ExceptionInInitializerError) { + } else if (this@toStringRecursive is ExceptionInInitializerError) { if (exception != null) { append("\n") if (depth == 0u) append("...") else - append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + append(this@toStringRecursive.toStringRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) } - } else if (this@stacktraceRecursive is InvocationTargetException) { + } else if (this@toStringRecursive is InvocationTargetException) { if (targetException != null) { append("\n") if (depth == 0u) append("...") else - append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) + append(this@toStringRecursive.toStringRecursive(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 ecbfb0d..d6c4f48 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 @@ -40,30 +40,30 @@ class StackTraceUtilsTest : TestBase() { private fun methodCallerOrigin(): Origin = methodCaller() @Test - @DisplayName("stacktraceHeader (no message)") - fun stacktraceHeaderNoMessage() { - assertEquals("Caused by: java.lang.NullPointerException", NullPointerException().stacktraceHeader()) + @DisplayName("header (no message)") + fun headerNoMessage() { + assertEquals("Caused by: java.lang.NullPointerException", NullPointerException().header()) } @Test - @DisplayName("stacktraceHeader (with message)") - fun stacktraceHeaderWithMessage() { - assertEquals("Caused by: java.lang.NullPointerException: Hello World!", NullPointerException("Hello World!").stacktraceHeader()) + @DisplayName("header (with message)") + fun headerWithMessage() { + assertEquals("Caused by: java.lang.NullPointerException: Hello World!", NullPointerException("Hello World!").header()) } @Test - @DisplayName("stacktraceBody (no indent)") - fun stacktraceBodyNoIndent() { - val body: String = Throwable().stacktraceBody(indent = false) + @DisplayName("body (no indent)") + fun bodyNoIndent() { + val body: String = Throwable().body(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") } @Test - @DisplayName("stacktraceBody (with indent)") - fun stacktraceBodyWithIndent() { - val body: String = Throwable().stacktraceBody(indent = true) + @DisplayName("body (with indent)") + fun bodyWithIndent() { + val body: String = Throwable().body(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")