Rename StackTraceUtils methods
This commit is contained in:
parent
3adc48e8a2
commit
9781945d3a
6 changed files with 42 additions and 43 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.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 ?: "<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()}")
|
||||
}
|
||||
} }
|
||||
|
||||
|
|
|
@ -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."),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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 ?: "<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
|
||||
}
|
||||
|
|
|
@ -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()}")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue