Make methods of StackTraceUtils class extensions
This commit is contained in:
parent
90158850ef
commit
24a11572ec
8 changed files with 136 additions and 154 deletions
|
@ -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 ?: "<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()}")
|
||||
}
|
||||
} }
|
||||
|
||||
|
|
|
@ -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."),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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<String, Object?> = 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
|
||||
|
|
|
@ -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 ?: "<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
|
||||
}
|
||||
|
||||
|
|
|
@ -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()}")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -23,27 +23,13 @@ import de.staropensource.engine.base.type.Origin
|
|||
import java.lang.reflect.InvocationTargetException
|
||||
|
||||
/**
|
||||
* Utility methods for stack trace
|
||||
* 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.
|
||||
* Returns the caller of the executing method.
|
||||
*
|
||||
* @param depth how deep to go into the stack trace
|
||||
* @return deepest method caller
|
||||
* @since v1-alpha10
|
||||
*/
|
||||
fun methodCaller(depth: UInt = 1u): Origin {
|
||||
fun Any.methodCaller(depth: UInt = 1u): Origin {
|
||||
val stacktrace: Array<StackTraceElement> = Throwable().stackTrace
|
||||
var element: StackTraceElement? = null
|
||||
|
||||
|
@ -60,33 +46,31 @@ class StackTraceUtils private constructor() {
|
|||
methodName = element.methodName,
|
||||
lineNumber = element.lineNumber.toUInt(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* 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
|
||||
* @since v1-alpha10
|
||||
*/
|
||||
fun stacktraceHeader(throwable: Throwable): String {
|
||||
return "Caused by: ${throwable.javaClass.name}${if (throwable.message == null) "" else ": ${throwable.message}"}"
|
||||
}
|
||||
fun Throwable.stacktraceHeader(): String {
|
||||
return "Caused by: ${this.javaClass.name}${if (this.message == null) "" else ": ${this.message}"}"
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Returns the body of the stacktrace
|
||||
* as it is usually found in JVM
|
||||
* stacktraces.
|
||||
* of this [Throwable]. 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) {
|
||||
fun Throwable.stacktraceBody(indent: Boolean = true): String = buildString {
|
||||
for (element: StackTraceElement in stackTrace) {
|
||||
if (!isEmpty())
|
||||
append("\n")
|
||||
if (indent)
|
||||
|
@ -95,71 +79,65 @@ class StackTraceUtils private constructor() {
|
|||
append("at ")
|
||||
append(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Returns a recursively resolved
|
||||
* collection of stacktraces, all
|
||||
* derived from the passed [throwable].
|
||||
* 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 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 {
|
||||
fun Throwable.stacktraceRecursive(indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString {
|
||||
// Append header
|
||||
if (includeHeader) {
|
||||
append(stacktraceHeader(throwable))
|
||||
append(stacktraceHeader())
|
||||
append("\n")
|
||||
}
|
||||
|
||||
// Append body
|
||||
append(stacktraceBody(throwable, indent = indent))
|
||||
append(stacktraceBody(indent = indent))
|
||||
|
||||
// Recurse downwards
|
||||
if (throwable.cause != null) {
|
||||
if (cause != null) {
|
||||
append("\n")
|
||||
if (depth == 0u)
|
||||
append("...")
|
||||
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) {
|
||||
val exception: ClassNotFoundException = throwable
|
||||
if (exception.exception != null) {
|
||||
if (this@stacktraceRecursive is ClassNotFoundException) {
|
||||
if (exception != null) {
|
||||
append("\n")
|
||||
if (depth == 0u)
|
||||
append("...")
|
||||
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) {
|
||||
val exception: ExceptionInInitializerError = throwable
|
||||
if (exception.exception != null) {
|
||||
} else if (this@stacktraceRecursive is ExceptionInInitializerError) {
|
||||
if (exception != null) {
|
||||
append("\n")
|
||||
if (depth == 0u)
|
||||
append("...")
|
||||
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) {
|
||||
val exception: InvocationTargetException = throwable
|
||||
if (exception.targetException != null) {
|
||||
} else if (this@stacktraceRecursive is InvocationTargetException) {
|
||||
if (targetException != null) {
|
||||
append("\n")
|
||||
if (depth == 0u)
|
||||
append("...")
|
||||
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)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue