From 462917467da9343728d0adfcdbbd7d065a6508e0 Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 13 Nov 2021 02:22:56 +0100 Subject: [PATCH] Creating message for message log now directly in fints4k, no need to translate it --- .../banking/fints/log/MessageLogCollector.kt | 33 +++++++++++-------- .../banking/fints/model/MessageLogEntry.kt | 4 +-- .../banking/ui/model/MessageLogEntry.kt | 2 -- .../banking/ui/presenter/BankingPresenter.kt | 11 +------ .../dankito/banking/fints4kBankingClient.kt | 4 +-- 5 files changed, 24 insertions(+), 30 deletions(-) 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 7363d55b..37a5f77b 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 @@ -20,6 +20,8 @@ open class MessageLogCollector { const val MaxCountStackTraceElements = 15 + private const val NewLine = "\r\n" + private val log = LoggerFactory.getLogger(MessageLogCollector::class) } @@ -28,39 +30,42 @@ open class MessageLogCollector { // in either case remove sensitive data after response is parsed as otherwise some information like account holder name and accounts may is not set yet on BankData open val messageLogWithoutSensitiveData: List - get() = messageLog.map { MessageLogEntry(it.type, safelyRemoveSensitiveDataFromMessage(it.message, it.context.bank), it.time, it.context) } + get() = messageLog.map { MessageLogEntry(it.type, safelyRemoveSensitiveDataFromMessage(it.message, it.context.bank), it.context, it.time) } protected open val stackTraceHelper = StackTraceHelper() open fun addMessageLog(type: MessageLogEntryType, message: String, context: MessageContext) { - val timeStamp = Date() - val prettyPrintMessage = prettyPrintHbciMessage(message) + val messageToLog = createMessage(type, prettyPrintHbciMessage(message), context, true) - messageLog.add(MessageLogEntry(type, prettyPrintMessage, timeStamp, context)) + messageLog.add(MessageLogEntry(type, messageToLog, context)) - val messageTrace = createMessageTraceString(type, context) - log.debug { "$messageTrace\r\n$prettyPrintMessage" } + log.debug { messageToLog } } - open fun logError(loggingClass: KClass<*>, message: String, context: MessageContext, e: Exception? = null) { val type = MessageLogEntryType.Error - val messageTrace = createMessageTraceString(type, context) + " " + val messageToLog = createMessage(type, message, context, false) if (e != null) { - getLogger(loggingClass).error(e) { messageTrace + message } + getLogger(loggingClass).error(e) { messageToLog } } else { - getLogger(loggingClass).error(messageTrace + message) + getLogger(loggingClass).error(messageToLog) } - val errorStackTrace = if (e != null) "\r\n" + getStackTrace(e) else "" + val errorStackTrace = if (e != null) NewLine + getStackTrace(e) else "" - messageLog.add(MessageLogEntry(type, message + errorStackTrace, Date(), context)) + messageLog.add(MessageLogEntry(type, messageToLog + errorStackTrace, context)) } + protected open fun createMessage(type: MessageLogEntryType, message: String, context: MessageContext, separateWithNewLine: Boolean): String { + val messageTrace = createMessageTraceString(type, context) + + return "$messageTrace${ if (separateWithNewLine) NewLine else " " }$message" + } + protected open fun createMessageTraceString(type: MessageLogEntryType, context: MessageContext): String { return "${twoDigits(context.jobNumber)}_${twoDigits(context.dialogNumber)}_${twoDigits(context.messageNumber)}_" + "${context.bank.bankCode}_${context.bank.customerId}" + @@ -82,7 +87,7 @@ open class MessageLogCollector { } protected open fun prettyPrintHbciMessage(message: String): String { - return message.replace("'", "'\r\n") + return message.replace("'", "'$NewLine") } @@ -90,7 +95,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${getStackTrace(e)}\r\n$message" + return "! WARNING !${NewLine}Could not remove sensitive data!$NewLine$e$NewLine${getStackTrace(e)}$NewLine$message" } } diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt index 6d1dfeea..4e8076e5 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/MessageLogEntry.kt @@ -7,8 +7,8 @@ import net.dankito.utils.multiplatform.Date open class MessageLogEntry( open val type: MessageLogEntryType, open val message: String, - open val time: Date, - open val context: MessageContext + open val context: MessageContext, + open val time: Date = Date() ) { override fun toString(): String { diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntry.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntry.kt index 55bcd415..f6aad9ec 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntry.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/MessageLogEntry.kt @@ -7,8 +7,6 @@ open class MessageLogEntry( open val message: String, open val type: MessageLogEntryType, open val time: Date, - open val jobNumber: Int, - open val dialogNumber: Int, open val bank: TypedBankData, open val account: TypedBankAccount? ) { diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt index cebaaa35..c8d00467 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -824,16 +824,7 @@ open class BankingPresenter( }) return logEntries.map { entry -> - MessageLogEntryDateFormatter.format(entry.time) + " " + entry.bank.bankCode + " " + getMessageLogPrefix(entry) + "\r\n" + entry.message - } - } - - protected open fun getMessageLogPrefix(entry: MessageLogEntry): String { - // TODO: translate - return when (entry.type) { - MessageLogEntryType.Sent -> "Sending message" - MessageLogEntryType.Received -> "Received message" - MessageLogEntryType.Error -> "Error" + MessageLogEntryDateFormatter.format(entry.time) + " " + entry.message } } diff --git a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt index 1097047e..24518e4d 100644 --- a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt +++ b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt @@ -215,8 +215,8 @@ open class fints4kBankingClient( protected open fun saveData(response: FinTsClientResponse) { try { _messageLogWithoutSensitiveData.addAll(response.messageLogWithoutSensitiveData - .map { MessageLogEntry(it.message, map(it.type), it.time, it.context.jobNumber, - it.context.dialogNumber, bank, it.context.account?.let { mapper.findMatchingAccount(bank, it) } ) }) + .map { MessageLogEntry(it.message, map(it.type), it.time, bank, + it.context.account?.let { mapper.findMatchingAccount(bank, it) } ) }) // TODO: fix that real (child) class get serialized and re-enable again // val clientDataFile = getFints4kClientDataFile(fintsBank.bankCode, fintsBank.customerId)