diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt new file mode 100644 index 00000000..04f41734 --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt @@ -0,0 +1,8 @@ +package net.dankito.utils.multiplatform + + +expect class StackTraceHelper { + + fun getStackTrace(e: Throwable, maxCountStackTraceElements: Int? = null): String + +} \ No newline at end of file diff --git a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt new file mode 100644 index 00000000..11c5714f --- /dev/null +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt @@ -0,0 +1,16 @@ +package net.dankito.utils.multiplatform + + +actual class StackTraceHelper { + + actual fun getStackTrace(e: Throwable, maxCountStackTraceElements: Int?): String { + var stackTrace = e.getStackTrace() + + maxCountStackTraceElements?.let { + stackTrace = stackTrace.take(maxCountStackTraceElements) + } + + return stackTrace.joinToString("\r\n") + } + +} \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt new file mode 100644 index 00000000..33aae61d --- /dev/null +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/StackTraceHelper.kt @@ -0,0 +1,24 @@ +package net.dankito.utils.multiplatform + +import java.io.PrintWriter +import java.io.StringWriter + + +actual class StackTraceHelper { + + actual fun getStackTrace(e: Throwable, maxCountStackTraceElements: Int?): String { + val stringWriter = StringWriter() + e.printStackTrace(PrintWriter(stringWriter)) + + val stackTrace = stringWriter.toString() + + maxCountStackTraceElements?.let { + val elements = stackTrace.split(System.lineSeparator()).take(maxCountStackTraceElements) + + return elements.joinToString(System.lineSeparator()) + } + + return stackTrace + } + +} \ No newline at end of file diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt index 3a658972..7bc89e3b 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt @@ -6,7 +6,8 @@ import net.dankito.banking.fints.model.MessageLogEntryType import net.dankito.utils.multiplatform.log.Logger import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.utils.multiplatform.Date -import net.dankito.utils.multiplatform.getInnerExceptionMessage +import net.dankito.utils.multiplatform.StackTraceHelper +import net.dankito.utils.multiplatform.getInnerException open class MessageLogCollector { @@ -15,6 +16,8 @@ open class MessageLogCollector { val FindAccountTransactionsStartRegex = Regex("^HIKAZ:\\d:\\d:\\d\\+@\\d+@", RegexOption.MULTILINE) val FindAccountTransactionsEndRegex = Regex("^-'", RegexOption.MULTILINE) + const val MaxCountStackTraceElements = 15 + private val log = LoggerFactory.getLogger(MessageLogCollector::class) } @@ -26,6 +29,9 @@ open class MessageLogCollector { get() = messageLog.map { MessageLogEntry(safelyRemoveSensitiveDataFromMessage(it.message, it.bank), it.type, it.time, it.bank) } + protected val stackTraceHelper = StackTraceHelper() + + open fun addMessageLog(message: String, type: MessageLogEntryType, bank: BankData) { val timeStamp = Date() val prettyPrintMessage = prettyPrintHbciMessage(message) @@ -51,8 +57,10 @@ open class MessageLogCollector { loggerToUse.error(prettyPrintMessage) } + val errorStackTrace = if (e != null) "\r\n" + getStackTrace(e) else "" + // TODO: what to do when bank is not set? - messageLog.add(MessageLogEntry(prettyPrintMessage, MessageLogEntryType.Error, Date(), bank)) + messageLog.add(MessageLogEntry(prettyPrintMessage + errorStackTrace, MessageLogEntryType.Error, Date(), bank)) } @@ -60,7 +68,7 @@ open class MessageLogCollector { try { return removeSensitiveDataFromMessage(message, bank) } catch (e: Exception) { - return "! WARNING !\r\nCould not remove sensitive data!\r\n$e\r\n$message" + return "! WARNING !\r\nCould not remove sensitive data!\r\n$e\r\n${getStackTrace(e)}\r\n$message" } } @@ -101,4 +109,11 @@ open class MessageLogCollector { return message } + + protected open fun getStackTrace(e: Exception): String { + val innerException = e.getInnerException() + + return stackTraceHelper.getStackTrace(innerException, MaxCountStackTraceElements) + } + } \ No newline at end of file