Migrate StackTraceUtils & Miscellaneous
This commit is contained in:
parent
a1e843f61e
commit
06748dcdf1
8 changed files with 221 additions and 156 deletions
|
@ -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
|
||||||
import kotlin.jvm.JvmName
|
import kotlin.jvm.JvmName
|
||||||
|
@ -124,7 +124,7 @@ class Logger {
|
||||||
fun log(level: Level, message: String, levelData: Map<String, Any?> = emptyMap(), callerDepth: UInt = 0u) {
|
fun log(level: Level, message: String, levelData: Map<String, Any?> = 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
|
||||||
|
|
|
@ -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.StackTraceUtils
|
import de.staropensource.engine.base.utility.misc.stacktraceRecursive
|
||||||
import kotlin.jvm.JvmStatic
|
import kotlin.jvm.JvmStatic
|
||||||
import kotlin.reflect.full.primaryConstructor
|
import kotlin.reflect.full.primaryConstructor
|
||||||
|
|
||||||
|
@ -103,7 +103,7 @@ 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${stacktraceRecursive(throwable)}")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,12 +19,11 @@
|
||||||
|
|
||||||
package de.staropensource.engine.base.platform
|
package de.staropensource.engine.base.platform
|
||||||
|
|
||||||
import de.staropensource.engine.base.Engine
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bootstraps this platform.
|
* Bootstraps this platform.
|
||||||
*
|
*
|
||||||
* Invoked by [Engine.bootstrap] first.
|
* Invoked by [de.staropensource.engine.base.Engine.bootstrap] first.
|
||||||
*
|
*
|
||||||
* @throws Throwable on error
|
* @throws Throwable on error
|
||||||
* @since v1-alpha10
|
* @since v1-alpha10
|
||||||
|
@ -35,7 +34,7 @@ expect fun platformEngineBootstrap()
|
||||||
/**
|
/**
|
||||||
* Initializes this platform.
|
* Initializes this platform.
|
||||||
*
|
*
|
||||||
* Invoked by [Engine.initialize] first.
|
* Invoked by [de.staropensource.engine.base.Engine.initialize] first.
|
||||||
*
|
*
|
||||||
* @throws Throwable on error
|
* @throws Throwable on error
|
||||||
* @since v1-alpha10
|
* @since v1-alpha10
|
||||||
|
@ -46,7 +45,7 @@ expect fun platformEngineInitialize()
|
||||||
/**
|
/**
|
||||||
* Reloads this platform.
|
* Reloads this platform.
|
||||||
*
|
*
|
||||||
* Invoked by [Engine.reload] first.
|
* Invoked by [de.staropensource.engine.base.Engine.reload] first.
|
||||||
*
|
*
|
||||||
* @throws Throwable on error
|
* @throws Throwable on error
|
||||||
* @since v1-alpha10
|
* @since v1-alpha10
|
||||||
|
|
|
@ -31,16 +31,5 @@ class Miscellaneous private constructor() {
|
||||||
*
|
*
|
||||||
* @since v1-alpha10
|
* @since v1-alpha10
|
||||||
*/
|
*/
|
||||||
companion object {
|
companion object
|
||||||
/**
|
|
||||||
* Returns if currently
|
|
||||||
* running on the main thread.
|
|
||||||
*
|
|
||||||
* @return `true` if running on the main thread, `false` if not, `null` if not supported (e.g. not running on the JVM)
|
|
||||||
* @since v1-alpha10
|
|
||||||
*/
|
|
||||||
fun onMainThread(): Boolean? {
|
|
||||||
return Thread.currentThread().threadId() == 1L
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* STAROPENSOURCE ENGINE SOURCE FILE
|
||||||
|
* Copyright (c) 2024 The StarOpenSource Engine Authors
|
||||||
|
* Licensed under the GNU General Public License v3.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.staropensource.engine.base.utility.misc
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns if currently
|
||||||
|
* running on the main thread.
|
||||||
|
*
|
||||||
|
* @return `true` if running on the main thread, `false` if not, `null` if not supported
|
||||||
|
* @since v1-alpha10
|
||||||
|
*/
|
||||||
|
expect fun onMainThread(): Boolean?
|
|
@ -20,146 +20,87 @@
|
||||||
package de.staropensource.engine.base.utility.misc
|
package de.staropensource.engine.base.utility.misc
|
||||||
|
|
||||||
import de.staropensource.engine.base.type.Origin
|
import de.staropensource.engine.base.type.Origin
|
||||||
import java.lang.reflect.InvocationTargetException
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility methods for stack trace
|
* Returns the method caller.
|
||||||
* and exception analysis.
|
|
||||||
*
|
*
|
||||||
* @constructor Initializes this class
|
* @param depth how deep to go into the stack trace
|
||||||
|
* @return deepest method caller
|
||||||
* @since v1-alpha10
|
* @since v1-alpha10
|
||||||
*/
|
*/
|
||||||
class StackTraceUtils private constructor() {
|
expect fun methodCaller(depth: UInt = 1u): Origin
|
||||||
/**
|
|
||||||
* Companion object of [StackTraceUtils].
|
|
||||||
*
|
|
||||||
* @since v1-alpha10
|
|
||||||
*/
|
|
||||||
companion object {
|
|
||||||
/**
|
|
||||||
* Returns the method caller.
|
|
||||||
*
|
|
||||||
* @param depth how deep to go into the stack trace
|
|
||||||
* @return deepest method caller
|
|
||||||
* @since v1-alpha10
|
|
||||||
*/
|
|
||||||
fun methodCaller(depth: UInt = 1u): Origin {
|
|
||||||
val stacktrace: Array<StackTraceElement> = Throwable().stackTrace
|
|
||||||
var element: StackTraceElement? = null
|
|
||||||
|
|
||||||
// Get wanted element
|
/**
|
||||||
element = if (depth.plus(2u).toInt() > stacktrace.size)
|
* Returns the `Caused by: ` header
|
||||||
stacktrace[stacktrace.size - 1]
|
* usually found in JVM stacktraces.
|
||||||
else
|
*
|
||||||
stacktrace[depth.plus(2u).toInt()]
|
* @param throwable [Throwable] to use
|
||||||
|
* @return stacktrace header
|
||||||
// Return origin
|
* @since v1-alpha10
|
||||||
return Origin(
|
*/
|
||||||
packageName = element.className.substringBeforeLast('.'),
|
fun stacktraceHeader(throwable: Throwable): String {
|
||||||
className = element.className.substringAfterLast('.'),
|
return "Caused by: ${throwable::class.qualifiedName ?: "<anonymous>"}${if (throwable.message == null) "" else ": ${throwable.message}"}"
|
||||||
methodName = element.methodName,
|
|
||||||
lineNumber = element.lineNumber.toUInt(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the `Caused by: ` header
|
|
||||||
* 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}"}"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the body of the stacktrace
|
|
||||||
* as 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) {
|
|
||||||
if (!isEmpty())
|
|
||||||
append("\n")
|
|
||||||
if (indent)
|
|
||||||
append("\t")
|
|
||||||
|
|
||||||
append("at ")
|
|
||||||
append(element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a recursively resolved
|
|
||||||
* collection of stacktraces, all
|
|
||||||
* derived from the passed [throwable].
|
|
||||||
*
|
|
||||||
* 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 {
|
|
||||||
// Append header
|
|
||||||
if (includeHeader) {
|
|
||||||
append(stacktraceHeader(throwable))
|
|
||||||
append("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append body
|
|
||||||
append(stacktraceBody(throwable, indent = indent))
|
|
||||||
|
|
||||||
// Recurse downwards
|
|
||||||
if (throwable.cause != null) {
|
|
||||||
append("\n")
|
|
||||||
if (depth == 0u)
|
|
||||||
append("...")
|
|
||||||
else
|
|
||||||
append(stacktraceRecursive(throwable.cause!!, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (throwable is ClassNotFoundException) {
|
|
||||||
val exception: ClassNotFoundException = throwable
|
|
||||||
if (exception.exception != null) {
|
|
||||||
append("\n")
|
|
||||||
if (depth == 0u)
|
|
||||||
append("...")
|
|
||||||
else
|
|
||||||
append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
|
||||||
}
|
|
||||||
} else if (throwable is ExceptionInInitializerError) {
|
|
||||||
val exception: ExceptionInInitializerError = throwable
|
|
||||||
if (exception.exception != null) {
|
|
||||||
append("\n")
|
|
||||||
if (depth == 0u)
|
|
||||||
append("...")
|
|
||||||
else
|
|
||||||
append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
|
||||||
}
|
|
||||||
} else if (throwable is InvocationTargetException) {
|
|
||||||
val exception: InvocationTargetException = throwable
|
|
||||||
if (exception.targetException != null) {
|
|
||||||
append("\n")
|
|
||||||
if (depth == 0u)
|
|
||||||
append("...")
|
|
||||||
else
|
|
||||||
append(stacktraceRecursive(exception.targetException, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the body of the stacktrace
|
||||||
|
* as 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
|
||||||
|
*/
|
||||||
|
expect fun stacktraceBody(throwable: Throwable, indent: Boolean = true): String
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a recursively resolved
|
||||||
|
* collection of stacktraces, all
|
||||||
|
* derived from the passed [throwable].
|
||||||
|
*
|
||||||
|
* 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])
|
||||||
|
* @param depth how deep to recurse into [Throwable]s
|
||||||
|
* @return recursively resolved stacktrace
|
||||||
|
* @since v1-alpha10
|
||||||
|
*/
|
||||||
|
fun stacktraceRecursive(throwable: Throwable, indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String = buildString {
|
||||||
|
// Append header
|
||||||
|
if (includeHeader) {
|
||||||
|
append(stacktraceHeader(throwable))
|
||||||
|
append("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append body
|
||||||
|
append(stacktraceBody(throwable, indent = indent))
|
||||||
|
|
||||||
|
// Recurse downwards
|
||||||
|
if (throwable.cause != null) {
|
||||||
|
append("\n")
|
||||||
|
if (depth == 0u)
|
||||||
|
append("...")
|
||||||
|
else
|
||||||
|
append(stacktraceRecursive(throwable.cause!!, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
||||||
|
}
|
||||||
|
|
||||||
|
platformSTUHandleRecursiveThrowables(throwable, indent, includeHeader, depth)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles platform-specific [Throwable]s recursively.
|
||||||
|
*
|
||||||
|
* @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])
|
||||||
|
* @param depth how deep to recurse into [Throwable]s
|
||||||
|
* @return stacktraces
|
||||||
|
* @since v1-alpha10
|
||||||
|
*/
|
||||||
|
expect fun platformSTUHandleRecursiveThrowables(throwable: Throwable, indent: Boolean = true, includeHeader: Boolean = true, depth: UInt = 10u): String
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* STAROPENSOURCE ENGINE SOURCE FILE
|
||||||
|
* Copyright (c) 2024 The StarOpenSource Engine Authors
|
||||||
|
* Licensed under the GNU General Public License v3.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.staropensource.engine.base.utility.misc
|
||||||
|
|
||||||
|
actual fun onMainThread(): Boolean? = Thread.currentThread().threadId() == 1L
|
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* STAROPENSOURCE ENGINE SOURCE FILE
|
||||||
|
* Copyright (c) 2024 The StarOpenSource Engine Authors
|
||||||
|
* Licensed under the GNU General Public License v3.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.staropensource.engine.base.utility.misc
|
||||||
|
|
||||||
|
import de.staropensource.engine.base.type.Origin
|
||||||
|
import java.lang.reflect.InvocationTargetException
|
||||||
|
|
||||||
|
actual fun methodCaller(depth: UInt): Origin {
|
||||||
|
val stacktrace: Array<StackTraceElement> = Throwable().stackTrace
|
||||||
|
var element: StackTraceElement? = null
|
||||||
|
|
||||||
|
// Get wanted element
|
||||||
|
element = if (depth.plus(2u).toInt() > stacktrace.size)
|
||||||
|
stacktrace[stacktrace.size - 1]
|
||||||
|
else
|
||||||
|
stacktrace[depth.plus(2u).toInt()]
|
||||||
|
|
||||||
|
// Return origin
|
||||||
|
return Origin(
|
||||||
|
packageName = element.className.substringBeforeLast('.'),
|
||||||
|
className = element.className.substringAfterLast('.'),
|
||||||
|
methodName = element.methodName,
|
||||||
|
lineNumber = element.lineNumber.toUInt(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun stacktraceBody(throwable: Throwable, indent: Boolean): String = buildString {
|
||||||
|
for (element: StackTraceElement in throwable.stackTrace) {
|
||||||
|
if (!isEmpty())
|
||||||
|
append("\n")
|
||||||
|
if (indent)
|
||||||
|
append("\t")
|
||||||
|
|
||||||
|
append("at ")
|
||||||
|
append(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun platformSTUHandleRecursiveThrowables(throwable: Throwable, indent: Boolean, includeHeader: Boolean, depth: UInt): String = buildString {
|
||||||
|
if (throwable is ClassNotFoundException) {
|
||||||
|
val exception: ClassNotFoundException = throwable
|
||||||
|
if (exception.exception != null) {
|
||||||
|
append("\n")
|
||||||
|
if (depth == 0u)
|
||||||
|
append("...")
|
||||||
|
else
|
||||||
|
append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
||||||
|
}
|
||||||
|
} else if (throwable is ExceptionInInitializerError) {
|
||||||
|
val exception: ExceptionInInitializerError = throwable
|
||||||
|
if (exception.exception != null) {
|
||||||
|
append("\n")
|
||||||
|
if (depth == 0u)
|
||||||
|
append("...")
|
||||||
|
else
|
||||||
|
append(stacktraceRecursive(exception.exception, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
||||||
|
}
|
||||||
|
} else if (throwable is InvocationTargetException) {
|
||||||
|
val exception: InvocationTargetException = throwable
|
||||||
|
if (exception.targetException != null) {
|
||||||
|
append("\n")
|
||||||
|
if (depth == 0u)
|
||||||
|
append("...")
|
||||||
|
else
|
||||||
|
append(stacktraceRecursive(exception.targetException, indent = indent, includeHeader = includeHeader, depth = depth.minus(1u)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue