Implemented adding stack trace to MessageLog

This commit is contained in:
dankito 2020-12-06 23:09:54 +01:00
parent 66f4eeb6ad
commit 87662d776a
4 changed files with 66 additions and 3 deletions

View File

@ -0,0 +1,8 @@
package net.dankito.utils.multiplatform
expect class StackTraceHelper {
fun getStackTrace(e: Throwable, maxCountStackTraceElements: Int? = null): String
}

View File

@ -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")
}
}

View File

@ -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
}
}

View File

@ -6,7 +6,8 @@ import net.dankito.banking.fints.model.MessageLogEntryType
import net.dankito.utils.multiplatform.log.Logger import net.dankito.utils.multiplatform.log.Logger
import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.utils.multiplatform.log.LoggerFactory
import net.dankito.utils.multiplatform.Date 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 { open class MessageLogCollector {
@ -15,6 +16,8 @@ open class MessageLogCollector {
val FindAccountTransactionsStartRegex = Regex("^HIKAZ:\\d:\\d:\\d\\+@\\d+@", RegexOption.MULTILINE) val FindAccountTransactionsStartRegex = Regex("^HIKAZ:\\d:\\d:\\d\\+@\\d+@", RegexOption.MULTILINE)
val FindAccountTransactionsEndRegex = Regex("^-'", RegexOption.MULTILINE) val FindAccountTransactionsEndRegex = Regex("^-'", RegexOption.MULTILINE)
const val MaxCountStackTraceElements = 15
private val log = LoggerFactory.getLogger(MessageLogCollector::class) 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) } 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) { open fun addMessageLog(message: String, type: MessageLogEntryType, bank: BankData) {
val timeStamp = Date() val timeStamp = Date()
val prettyPrintMessage = prettyPrintHbciMessage(message) val prettyPrintMessage = prettyPrintHbciMessage(message)
@ -51,8 +57,10 @@ open class MessageLogCollector {
loggerToUse.error(prettyPrintMessage) loggerToUse.error(prettyPrintMessage)
} }
val errorStackTrace = if (e != null) "\r\n" + getStackTrace(e) else ""
// TODO: what to do when bank is not set? // 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 { try {
return removeSensitiveDataFromMessage(message, bank) return removeSensitiveDataFromMessage(message, bank)
} catch (e: Exception) { } 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 return message
} }
protected open fun getStackTrace(e: Exception): String {
val innerException = e.getInnerException()
return stackTraceHelper.getStackTrace(innerException, MaxCountStackTraceElements)
}
} }