diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/EngineCrashCategory.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/EngineCrashCategory.kt index fa3af05..2f56f91 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/EngineCrashCategory.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/EngineCrashCategory.kt @@ -19,7 +19,11 @@ package de.staropensource.engine.base.implementation.logging.crashcategory +import de.staropensource.engine.base.Engine +import de.staropensource.engine.base.implementable.Subsystem 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.dnihbd.BuildInformation /** @@ -52,4 +56,24 @@ class EngineCrashCategory private constructor() : BuildInformation.BuildInformat override fun getName(): String { return "sos!engine" } + + override fun execute( + call: Call, + channelSettings: ChannelSettings?, + throwable: Throwable?, + fatal: Boolean + ): LinkedHashMap { + val subsystemsList: LinkedHashSet = linkedSetOf() + + // Populate 'subsystemsList' + for (subsystem: Subsystem in Engine.getSubsystems()) + subsystemsList.add("${subsystem::class.qualifiedName ?: ""} [${subsystem.getName()}]") + + // Return + return linkedMapOf( + Pair("State", Engine.state.name), + Pair("Subsystems", subsystemsList), + Pair("Build Information", super.execute(call, channelSettings, throwable, fatal)) + ) + } } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt b/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt index 30883c4..7b06417 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/logging/CrashHandler.kt @@ -21,9 +21,10 @@ package de.staropensource.engine.base.logging import de.staropensource.engine.base.Engine import de.staropensource.engine.base.EngineConfiguration +import de.staropensource.engine.base.extension.toStringType +import de.staropensource.engine.base.implementable.formatter.Formatter import de.staropensource.engine.base.implementable.logging.Adapter import de.staropensource.engine.base.implementable.logging.CrashCategory -import de.staropensource.engine.base.implementable.formatter.Formatter import de.staropensource.engine.base.type.logging.Call import de.staropensource.engine.base.type.logging.ChannelSettings @@ -40,6 +41,14 @@ class CrashHandler private constructor() { * @since v1-alpha10 */ companion object { + /** + * Dictates how much the + * indent increases per level. + * + * @since v1-alpha10 + */ + const val INDENT_INCREASE: Int = 3 + /** * Handles crashes. * @@ -94,44 +103,77 @@ class CrashHandler private constructor() { /** * Compiles a [CrashCategory]. * - * @param category [CrashCategory] to compile + * @param map map to compile + * @param indent indentation level * @return compiled output * @since v1-alpha10 */ private fun compileCategory(map: LinkedHashMap<*, *>, indent: Int = 1): String = buildString { - var entryString: String? = null - // Iterate over all entries - for (entry in map.keys) { + for (entry: Any in map.keys) { // Check if key is a string if (entry !is String) continue append("\n${" ".repeat(indent)}-> ${entry}") - if (map[entry] == null) // Value is null - true - else if (map[entry] is LinkedHashMap<*, *>) // Value is a map - append( - compileCategory( - map[entry] as LinkedHashMap<*, *>, - indent = indent + 3 // increase the 2nd addend to change the indent size during recursion + val value: String? = compileValue(map[entry], true, indent) + if (value != null) + append(value) + } + } + + /** + * Handles values and anything + * other interesting returned + * by [CrashCategory]s. + * + * @param value value to handle + * @param ofKey if to prepend `: ` to certain return values + * @param indent indentation level + * @return compiled output + * @since v1-alpha10 + */ + private fun compileValue(value: Any?, ofKey: Boolean, indent: Int = 1): String? = buildString { + // Value is null + if (value == null) + true + + // Value is a CharSequence or Char + else if (value is CharSequence || value is Char) { + // Put on separate line if contains newline + if (value.toString().contains("\n")) + append("\n${value}" + .replace( + "\n", + "\n ${" ".repeat(indent)}" ) ) - else { - entryString = map[entry].toString() + else + append("${if (ofKey) ": " else ""}${value}") - // Put on separate line if contains newline - if (entryString.contains("\n")) - append("\n${entryString}" - .replace( - "\n", - "\n ${" ".repeat(indent)}" - ) - ) - else - append(": ${entryString}") - } + // Value is a map + } else if (value is LinkedHashMap<*, *>) + append( + compileCategory( + value, + indent = indent + INDENT_INCREASE + ) + ) + + // Value is an Array + else if (value is Array<*>) + for (item: Any? in value) + append("\n${" ".repeat(indent.plus(INDENT_INCREASE))}-> ${compileValue(item, false, indent.plus(INDENT_INCREASE.times(2)))}") + + // Value is an Iterable + else if (value is Iterable<*>) + for (item: Any? in value) + append("\n${" ".repeat(indent.plus(INDENT_INCREASE))}-> ${compileValue(item, false, indent.plus(INDENT_INCREASE.times(2)))}") + + // Value is something this method doesn't recognize + else { + append("${if (ofKey) ": " else ""}${value.toStringType()}") } } }