Rename StackTraceUtils methods
All checks were successful
PRs & Pushes / test (push) Successful in 2m57s
PRs & Pushes / build-jars (push) Successful in 3m20s
PRs & Pushes / build-apidoc (push) Successful in 3m20s

This commit is contained in:
JeremyStar™ 2025-01-06 03:13:51 +01:00
parent 3adc48e8a2
commit 9781945d3a
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
6 changed files with 42 additions and 43 deletions

View file

@ -21,7 +21,7 @@ package de.staropensource.engine.base.implementable
import de.staropensource.engine.base.Engine.Companion.logger import de.staropensource.engine.base.Engine.Companion.logger
import de.staropensource.engine.base.extension.toStringType 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. * An event.
@ -114,7 +114,7 @@ abstract class Event {
try { try {
listener.invoke(arguments) listener.invoke(arguments)
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
logger.error("A listener on event ${this::class.qualifiedName ?: "<anonymous>"} threw ${throwable::class.qualifiedName ?: "<anonymous>"}\n\n${throwable.stacktraceRecursive()}") logger.error("A listener on event ${this::class.qualifiedName ?: "<anonymous>"} threw ${throwable::class.qualifiedName ?: "<anonymous>"}\n\n${throwable.toStringRecursive()}")
} }
} } } }

View file

@ -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.implementable.logging.CrashCategory
import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.Call
import de.staropensource.engine.base.type.logging.ChannelSettings 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 * [CrashCategory] implementation
@ -69,6 +69,6 @@ class InfoCrashCategory private constructor() : CrashCategory {
Pair("Channel", "'${call.channel}'"), Pair("Channel", "'${call.channel}'"),
Pair("Fatal", if (fatal) "yes" else "no"), Pair("Fatal", if (fatal) "yes" else "no"),
Pair("Message", call.message), Pair("Message", call.message),
Pair("Stacktrace", throwable?.stacktraceRecursive() ?: "Not available."), Pair("Stacktrace", throwable?.toStringRecursive() ?: "Not available."),
) )
} }

View file

@ -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.ChannelSettings
import de.staropensource.engine.base.type.logging.Feature import de.staropensource.engine.base.type.logging.Feature
import de.staropensource.engine.base.type.logging.OperationMode 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 import kotlin.reflect.full.primaryConstructor
/** /**
@ -105,7 +105,7 @@ class Processor private constructor() {
println( println(
"Logger system failure: Configured FormatBuilder implementation '" + "Logger system failure: Configured FormatBuilder implementation '" +
((EngineConfiguration.logFormatBuilder).qualifiedName ?: "<anonymous>") + ((EngineConfiguration.logFormatBuilder).qualifiedName ?: "<anonymous>") +
"' 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 return
} }

View file

@ -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.Environment.OperatingSystem.*
import de.staropensource.engine.base.utility.FileAccess.Companion.configDirectory import de.staropensource.engine.base.utility.FileAccess.Companion.configDirectory
import de.staropensource.engine.base.utility.FileAccess.Companion.format 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.File
import java.io.IOException import java.io.IOException
import java.nio.file.* import java.nio.file.*
@ -216,7 +216,7 @@ class FileAccess {
} }
} catch (_: NoSuchFileException) { } catch (_: NoSuchFileException) {
} catch (exception: Exception) { } 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()}")
} }
} }

View file

@ -50,13 +50,13 @@ fun Any.methodCaller(depth: UInt = 1u): Origin {
/** /**
* Returns the `Caused by: ` header * Returns the `Caused by: ` header
* of this [Throwable]. It is usually * for this [Throwable], usually
* found in JVM stack traces. * found in JVM stack traces.
* *
* @return stacktrace header * @return `Caused by: ` [Throwable] header
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun Throwable.stacktraceHeader(): String { fun Throwable.header(): String {
if (message == null) if (message == null)
return "Caused by: ${this.javaClass.name}" return "Caused by: ${this.javaClass.name}"
else { else {
@ -66,15 +66,15 @@ fun Throwable.stacktraceHeader(): String {
} }
/** /**
* Returns the body of the stacktrace * Returns the stacktrace of
* of this [Throwable]. It is usually * this [Throwable] in a
* found in JVM stacktraces. * printable/readable fashion.
* *
* @param indent if to add a tab character (`\t`) as indentation * @param indent if to add a tab character (`\t`) as indentation
* @return stacktrace body * @return stack trace of this [Throwable]
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString { fun Throwable.body(indent: Boolean = true): String = buildString {
for (element: StackTraceElement in stackTrace) { for (element: StackTraceElement in stackTrace) {
if (!isEmpty()) if (!isEmpty())
append("\n") append("\n")
@ -88,28 +88,27 @@ fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString {
/** /**
* Returns a recursively resolved * Returns a recursively resolved
* collection of stack traces, * collection of [Throwable]s and
* derived from the throwable it * their accompanying stack traces.
* is invoked from.
* *
* Use this method if you intend to * Use this method if you intend to
* print an exception's stacktrace or * print an exception's stacktrace or
* want to display it nicely elsewhere. * want to display it nicely elsewhere.
* *
* @param indent if to add a tab character (`\t`) as indentation * @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 * @return recursively resolved stacktrace
* @since v1-alpha10 * @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 // Append header
if (includeHeader) { if (includeHeader) {
append(stacktraceHeader()) append(header())
append("\n") append("\n")
} }
// Append body // Append body
append(stacktraceBody(indent = indent)) append(body(indent = indent))
// Recurse downwards // Recurse downwards
if (cause != null) { if (cause != null) {
@ -117,32 +116,32 @@ fun Throwable.stacktraceRecursive(indent: Boolean = true, includeHeader: Boolean
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else 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) { if (exception != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else 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) { if (exception != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else 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) { if (targetException != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else 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)))
} }
} }
} }

View file

@ -40,30 +40,30 @@ class StackTraceUtilsTest : TestBase() {
private fun methodCallerOrigin(): Origin = methodCaller() private fun methodCallerOrigin(): Origin = methodCaller()
@Test @Test
@DisplayName("stacktraceHeader (no message)") @DisplayName("header (no message)")
fun stacktraceHeaderNoMessage() { fun headerNoMessage() {
assertEquals("Caused by: java.lang.NullPointerException", NullPointerException().stacktraceHeader()) assertEquals("Caused by: java.lang.NullPointerException", NullPointerException().header())
} }
@Test @Test
@DisplayName("stacktraceHeader (with message)") @DisplayName("header (with message)")
fun stacktraceHeaderWithMessage() { fun headerWithMessage() {
assertEquals("Caused by: java.lang.NullPointerException: Hello World!", NullPointerException("Hello World!").stacktraceHeader()) assertEquals("Caused by: java.lang.NullPointerException: Hello World!", NullPointerException("Hello World!").header())
} }
@Test @Test
@DisplayName("stacktraceBody (no indent)") @DisplayName("body (no indent)")
fun stacktraceBodyNoIndent() { fun bodyNoIndent() {
val body: String = Throwable().stacktraceBody(indent = false) val body: String = Throwable().body(indent = false)
assertTrue(body.startsWith("at "), "Body '${body}' does not start with 'at '") 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") assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest")
} }
@Test @Test
@DisplayName("stacktraceBody (with indent)") @DisplayName("body (with indent)")
fun stacktraceBodyWithIndent() { fun bodyWithIndent() {
val body: String = Throwable().stacktraceBody(indent = true) val body: String = Throwable().body(indent = true)
assertTrue(body.startsWith("\tat "), "Body '${body}' does not start with '\tat '") 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") assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest")