Make methods of StackTraceUtils class extensions
All checks were successful
PRs & Pushes / test (push) Successful in 3m2s
PRs & Pushes / build-jars (push) Successful in 3m40s
PRs & Pushes / build-apidoc (push) Successful in 3m39s

This commit is contained in:
JeremyStar™ 2025-01-06 02:34:44 +01:00
parent 90158850ef
commit 24a11572ec
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
8 changed files with 136 additions and 154 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.StackTraceUtils import de.staropensource.engine.base.utility.misc.stacktraceRecursive
/** /**
* 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${StackTraceUtils.stacktraceRecursive(throwable)}") logger.error("A listener on event ${this::class.qualifiedName ?: "<anonymous>"} threw ${throwable::class.qualifiedName ?: "<anonymous>"}\n\n${throwable.stacktraceRecursive()}")
} }
} } } }

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.StackTraceUtils import de.staropensource.engine.base.utility.misc.stacktraceRecursive
/** /**
* [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", if (throwable == null) "Not available." else StackTraceUtils.stacktraceRecursive(throwable)), Pair("Stacktrace", throwable?.stacktraceRecursive() ?: "Not available."),
) )
} }

View file

@ -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.implementation.stream.LoggerStream
import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.Call
import de.staropensource.engine.base.type.logging.Level 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.Clock
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
@ -139,7 +139,7 @@ class Logger {
fun log(level: Level, message: String, levelData: Map<String, Object?> = emptyMap(), callerDepth: UInt = 0u) { fun log(level: Level, message: String, levelData: Map<String, Object?> = emptyMap(), callerDepth: UInt = 0u) {
// Create 'Call' instance // Create 'Call' instance
var call: Call = Call( var call: Call = Call(
StackTraceUtils.methodCaller(depth = callerDepth.plus(1u)), methodCaller(depth = callerDepth.plus(1u)),
level, level,
message, message,
channel channel

View file

@ -20,15 +20,15 @@
package de.staropensource.engine.base.logging package de.staropensource.engine.base.logging
import de.staropensource.engine.base.EngineConfiguration 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.Adapter
import de.staropensource.engine.base.implementable.logging.FormatBuilder 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.implementable.logging.ThreadingHandler
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.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.StackTraceUtils import de.staropensource.engine.base.utility.misc.stacktraceRecursive
import kotlin.reflect.full.primaryConstructor import kotlin.reflect.full.primaryConstructor
/** /**
@ -102,7 +102,11 @@ class Processor private constructor() {
try { try {
format = (EngineConfiguration.logFormatBuilder).primaryConstructor!!.call(call, channelSettings) format = (EngineConfiguration.logFormatBuilder).primaryConstructor!!.call(call, channelSettings)
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
println("Logger system failure: Configured FormatBuilder implementation '" + ((EngineConfiguration.logFormatBuilder).qualifiedName ?: "<anonymous>") + "' 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 ?: "<anonymous>") +
"' does not have a primary 'constructor(Call, ChannelSettings?)'. Log messages cannot be processed.\n${throwable.stacktraceRecursive()}"
)
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.StackTraceUtils import de.staropensource.engine.base.utility.misc.stacktraceRecursive
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${StackTraceUtils.stacktraceRecursive(exception)}") logger.sarn("Unable to delete file or directory '${unformatFromPath(path)}' scheduled for deletion.\n${exception.stacktraceRecursive()}")
} }
} }

View file

@ -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.logging.ChannelSettings
import de.staropensource.engine.base.type.versioning.VersionType import de.staropensource.engine.base.type.versioning.VersionType
import de.staropensource.engine.base.utility.FileAccess 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.Instant
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
@ -34,6 +34,7 @@ import java.io.InputStreamReader
import java.io.Reader import java.io.Reader
import java.io.StringReader import java.io.StringReader
import java.util.* import java.util.*
import kotlin.Throws
/** /**
* Loads the specified `-git.properties` and * Loads the specified `-git.properties` and
@ -342,7 +343,7 @@ open class BuildInformation
protected fun getFileContent(file: String): String? { protected fun getFileContent(file: String): String? {
if (loadLocation == null) { if (loadLocation == null) {
// Read from resources // Read from resources
val origin: Origin = StackTraceUtils.methodCaller(depth = 2u) val origin: Origin = methodCaller(depth = 2u)
val reader: Reader val reader: Reader
try { try {
reader = InputStreamReader( reader = InputStreamReader(

View file

@ -23,27 +23,13 @@ import de.staropensource.engine.base.type.Origin
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
/** /**
* Utility methods for stack trace * Returns the caller of the executing method.
* and exception analysis.
*
* @constructor Initializes this class
* @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 * @param depth how deep to go into the stack trace
* @return deepest method caller * @return deepest method caller
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun methodCaller(depth: UInt = 1u): Origin { fun Any.methodCaller(depth: UInt = 1u): Origin {
val stacktrace: Array<StackTraceElement> = Throwable().stackTrace val stacktrace: Array<StackTraceElement> = Throwable().stackTrace
var element: StackTraceElement? = null var element: StackTraceElement? = null
@ -62,31 +48,29 @@ class StackTraceUtils private constructor() {
) )
} }
/** /**
* Returns the `Caused by: ` header * Returns the `Caused by: ` header
* usually found in JVM stacktraces. * of this [Throwable]. It is usually
* found in JVM stacktraces.
* *
* @param throwable [Throwable] to use
* @return stacktrace header * @return stacktrace header
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun stacktraceHeader(throwable: Throwable): String { fun Throwable.stacktraceHeader(): String {
return "Caused by: ${throwable.javaClass.name}${if (throwable.message == null) "" else ": ${throwable.message}"}" return "Caused by: ${this.javaClass.name}${if (this.message == null) "" else ": ${this.message}"}"
} }
/** /**
* Returns the body of the stacktrace * Returns the body of the stacktrace
* as it is usually found in JVM * of this [Throwable]. It is usually
* stacktraces. * found in JVM stacktraces.
* *
* @param throwable [Throwable] to use
* @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 stacktrace body
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun stacktraceBody(throwable: Throwable, indent: Boolean = true): String = buildString { fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString {
for (element: StackTraceElement in throwable.stackTrace) { for (element: StackTraceElement in stackTrace) {
if (!isEmpty()) if (!isEmpty())
append("\n") append("\n")
if (indent) if (indent)
@ -99,67 +83,61 @@ class StackTraceUtils private constructor() {
/** /**
* Returns a recursively resolved * Returns a recursively resolved
* collection of stacktraces, all * collection of stack traces,
* derived from the passed [throwable]. * derived from the throwable it
* 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 throwable [Throwable] to use
* @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 [stacktraceHeader])
* @return recursively resolved stacktrace * @return recursively resolved stacktrace
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun stacktraceRecursive(throwable: Throwable, indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString { fun Throwable.stacktraceRecursive(indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString {
// Append header // Append header
if (includeHeader) { if (includeHeader) {
append(stacktraceHeader(throwable)) append(stacktraceHeader())
append("\n") append("\n")
} }
// Append body // Append body
append(stacktraceBody(throwable, indent = indent)) append(stacktraceBody(indent = indent))
// Recurse downwards // Recurse downwards
if (throwable.cause != null) { if (cause != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else else
append(stacktraceRecursive(throwable.cause!!, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) append(cause!!.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
} }
if (throwable is ClassNotFoundException) { if (this@stacktraceRecursive is ClassNotFoundException) {
val exception: ClassNotFoundException = throwable if (exception != null) {
if (exception.exception != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else else
append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
} }
} else if (throwable is ExceptionInInitializerError) { } else if (this@stacktraceRecursive is ExceptionInInitializerError) {
val exception: ExceptionInInitializerError = throwable if (exception != null) {
if (exception.exception != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else else
append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
} }
} else if (throwable is InvocationTargetException) { } else if (this@stacktraceRecursive is InvocationTargetException) {
val exception: InvocationTargetException = throwable if (targetException != null) {
if (exception.targetException != null) {
append("\n") append("\n")
if (depth == 0u) if (depth == 0u)
append("...") append("...")
else else
append(stacktraceRecursive(exception.targetException, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u))) append(this@stacktraceRecursive.stacktraceRecursive(indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
}
}
} }
} }
} }

View file

@ -21,41 +21,40 @@ package de.staropensource.engine.base.utility.misc
import de.staropensource.engine.base.type.Origin import de.staropensource.engine.base.type.Origin
import de.staropensource.engine.testing.TestBase 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 org.junit.jupiter.api.DisplayName
import java.lang.NullPointerException
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertContains
class StackTraceUtilsTest : TestBase() { class StackTraceUtilsTest : TestBase() {
@Test @Test
fun methodCaller() { fun methodCallerTest() {
val origin: Origin = methodCallerOrigin() val origin: Origin = methodCallerOrigin()
assertEquals("de.staropensource.engine.base.utility.misc", origin.packageName) assertEquals("de.staropensource.engine.base.utility.misc", origin.packageName)
assertEquals("StackTraceUtilsTest", origin.className) assertEquals("StackTraceUtilsTest", origin.className)
assertEquals("methodCaller", origin.methodName) assertEquals("methodCallerTest", origin.methodName)
assertEquals(33u, origin.lineNumber) assertEquals(32u, origin.lineNumber)
} }
private fun methodCallerOrigin(): Origin = StackTraceUtils.methodCaller() private fun methodCallerOrigin(): Origin = methodCaller()
@Test @Test
@DisplayName("stacktraceHeader (no message)") @DisplayName("stacktraceHeader (no message)")
fun stacktraceHeaderNoMessage() { fun stacktraceHeaderNoMessage() {
assertEquals("Caused by: java.lang.NullPointerException", StackTraceUtils.stacktraceHeader(NullPointerException())) assertEquals("Caused by: java.lang.NullPointerException", NullPointerException().stacktraceHeader())
} }
@Test @Test
@DisplayName("stacktraceHeader (with message)") @DisplayName("stacktraceHeader (with message)")
fun stacktraceHeaderWithMessage() { 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 @Test
@DisplayName("stacktraceBody (no indent)") @DisplayName("stacktraceBody (no indent)")
fun stacktraceBodyNoIndent() { 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.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")
@ -64,7 +63,7 @@ class StackTraceUtilsTest : TestBase() {
@Test @Test
@DisplayName("stacktraceBody (with indent)") @DisplayName("stacktraceBody (with indent)")
fun stacktraceBodyWithIndent() { 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.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")