Improve messages logging

This commit is contained in:
Karlatemp 2020-12-13 12:10:53 +08:00
parent 0a5d3097f7
commit d43cfd1e08
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
3 changed files with 22 additions and 12 deletions

View File

@ -145,7 +145,7 @@ public sealed class MessageSource : Message, MessageMetadata, ConstrainSingle {
/**
* 返回 `"[mirai:source:$ids,$internalIds]"`
*/
public final override fun toString(): String = "[mirai:source:$ids,$internalIds]"
public final override fun toString(): String = "[mirai:source:${ids.contentToString()},${internalIds.contentToString()}]"
public companion object Key : AbstractMessageKey<MessageSource>({ it.safeCast() }) {
/**

View File

@ -82,26 +82,36 @@ internal suspend fun <T : User> Friend.sendMessageImpl(
internal fun Contact.logMessageSent(message: Message) {
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
if (message !is net.mamoe.mirai.message.data.LongMessage) {
bot.logger.verbose("$this <- ${message.toString().singleLine()}")
bot.logger.verbose("$this <- $message".replaceMagicCodes())
}
}
internal fun MessageEvent.logMessageReceived() {
when (this) {
is GroupMessageEvent -> bot.logger.verbose {
"[${group.name.singleLine()}(${group.id})] ${senderName.singleLine()}(${sender.id}) -> ${message.toString()
.singleLine()}"
"[${group.name}(${group.id})] ${senderName}(${sender.id}) -> $message".replaceMagicCodes()
}
is TempMessageEvent -> bot.logger.verbose {
"[${group.name.singleLine()}(${group.id})] ${senderName.singleLine()}(Temp ${sender.id}) -> ${message.toString()
.singleLine()}"
"[${group.name}(${group.id})] $senderName(Temp ${sender.id}) -> $message".replaceMagicCodes()
}
is FriendMessageEvent -> bot.logger.verbose {
"${sender.nick.singleLine()}(${sender.id}) -> ${message.toString().singleLine()}"
"${sender.nick}(${sender.id}) -> $message".replaceMagicCodes()
}
}
}
internal fun String.singleLine(): String {
return this.replace("\n", """\n""").replace("\r", "")
}
internal val charMappings = mapOf(
'\n' to """\n""",
'\r' to "",
'\u202E' to "<RTL>",
'\u202D' to "<LTR>",
)
internal fun String.applyCharMapping() = buildString(capacity = this.length) {
this@applyCharMapping.forEach { char ->
append(charMappings[char] ?: char)
}
}
internal fun String.replaceMagicCodes(): String = this
.applyCharMapping()

View File

@ -493,9 +493,9 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
}
packet is MessageEvent -> packet.logMessageReceived()
packet is Event && packet !is Packet.NoEventLog -> bot.logger.verbose {
"Event: ${packet.toString().singleLine()}"
"Event: $packet".replaceMagicCodes()
}
else -> logger.verbose { "Recv: ${packet.toString().singleLine()}" }
else -> logger.verbose { "Recv: $packet".replaceMagicCodes() }
}
}