Implemented adding stack trace to MessageLog
This commit is contained in:
parent
66f4eeb6ad
commit
87662d776a
|
@ -0,0 +1,8 @@
|
|||
package net.dankito.utils.multiplatform
|
||||
|
||||
|
||||
expect class StackTraceHelper {
|
||||
|
||||
fun getStackTrace(e: Throwable, maxCountStackTraceElements: Int? = null): String
|
||||
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue