Fix stacktrace logging

This commit is contained in:
Him188 2020-03-22 22:20:01 +08:00
parent 1171267ecc
commit 9e77b7b7c5
3 changed files with 24 additions and 10 deletions

View File

@ -19,6 +19,9 @@ import net.mamoe.mirai.console.plugins.PluginManager
import net.mamoe.mirai.console.utils.MiraiConsoleUI
import net.mamoe.mirai.utils.SimpleLogger.LogPriority
import net.mamoe.mirai.utils.cryptor.ECDH
import net.mamoe.mirai.utils.io.encodeToString
import java.io.ByteArrayOutputStream
import java.io.PrintStream
object MiraiConsole {
@ -164,7 +167,7 @@ object MiraiConsole {
internal object MiraiConsoleLogger {
operator fun invoke(any: Any? = null) {
operator fun invoke(any: Any?) {
invoke(
"[Mirai ${MiraiConsole.version} ${MiraiConsole.build}]",
0L,
@ -172,6 +175,14 @@ internal object MiraiConsoleLogger {
)
}
operator fun invoke(e: Throwable?) {
invoke(
"[Mirai ${MiraiConsole.version} ${MiraiConsole.build}]",
0L,
e
)
}
operator fun invoke(priority: LogPriority, identityStr: String, identity: Long, any: Any? = null) {
if (any != null) {
MiraiConsole.frontEnd.pushLog(priority, identityStr, identity, "$any")
@ -180,7 +191,7 @@ internal object MiraiConsoleLogger {
operator fun invoke(priority: LogPriority, identityStr: String, identity: Long, e: Throwable? = null) {
if (e != null) {
MiraiConsole.frontEnd.pushLog(priority, identityStr, identity, e.stackTrace.joinToString("\n"))
MiraiConsole.frontEnd.pushLog(priority, identityStr, identity, e.stacktraceString)
}
}
@ -193,9 +204,13 @@ internal object MiraiConsoleLogger {
operator fun invoke(identityStr: String, identity: Long, e: Throwable? = null) {
if (e != null) {
MiraiConsole.frontEnd.pushLog(LogPriority.INFO, identityStr, identity, e.stackTrace.joinToString("\n"))
MiraiConsole.frontEnd.pushLog(LogPriority.INFO, identityStr, identity, e.stacktraceString)
}
}
}
internal val Throwable.stacktraceString: String
get() =
ByteArrayOutputStream().apply {
printStackTrace(PrintStream(this))
}.use { it.toByteArray().encodeToString() }

View File

@ -13,6 +13,7 @@ package net.mamoe.mirai.console.command
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.CommandManager.processCommandQueue
import net.mamoe.mirai.console.plugins.PluginBase
import net.mamoe.mirai.console.plugins.PluginManager
@ -159,7 +160,7 @@ object CommandManager : Job by {
}
} catch (e: Exception) {
sender.sendMessage("在运行指令时出现了未知错误")
e.printStackTrace()
MiraiConsole.logger(e)
false
} finally {
(sender as AbstractCommandSender).flushMessage()
@ -181,7 +182,7 @@ object CommandManager : Job by {
} catch (e: UnknownCommandException) {
command.sender.sendMessage("未知指令 " + command.commandStr)
} catch (e: Throwable) {//should never happen
e.printStackTrace()
MiraiConsole.logger(e)
}
if(isActive) {
processCommandQueue()

View File

@ -123,8 +123,7 @@ object DefaultCommands {
SimpleLogger("BOT $qqNumber]") { _, message, e ->
MiraiConsole.logger("[BOT $qqNumber]", qqNumber, message)
if (e != null) {
MiraiConsole.logger("[NETWORK ERROR]", qqNumber, e.toString())//因为在一页 所以可以不打QQ
e.printStackTrace()
MiraiConsole.logger("[NETWORK ERROR]", qqNumber, e)//因为在一页 所以可以不打QQ
}
}
}
@ -132,8 +131,7 @@ object DefaultCommands {
SimpleLogger("BOT $qqNumber") { _, message, e ->
MiraiConsole.logger("[NETWORK]", qqNumber, message)//因为在一页 所以可以不打QQ
if (e != null) {
MiraiConsole.logger("[NETWORK ERROR]", qqNumber, e.toString())//因为在一页 所以可以不打QQ
e.printStackTrace()
MiraiConsole.logger("[NETWORK ERROR]", qqNumber, e)//因为在一页 所以可以不打QQ
}
}
}