diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 1132dd043..1d42735e6 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -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() } \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt index e474dfcc0..baf256cff 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt @@ -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() diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt index 3bd5d3895..64aa41784 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt @@ -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 } } }