From d6125cd9d49a3f649da48bd0c1f129908369ba32 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 9 Oct 2019 18:05:34 +0800 Subject: [PATCH] Move logging extensions for bot to BotHelper.kt --- .../kotlin/net/mamoe/mirai/BotHelper.kt | 42 ++++++++++++++++++- .../net/mamoe/mirai/utils/MiraiLogger.kt | 35 ---------------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt index 82d646eba..3c0c0615e 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/BotHelper.kt @@ -3,8 +3,14 @@ package net.mamoe.mirai import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket +import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket +import net.mamoe.mirai.network.protocol.tim.packet.goto import net.mamoe.mirai.network.protocol.tim.packet.login.LoginState import net.mamoe.mirai.utils.ContactList +import net.mamoe.mirai.utils.LoggerTextFormat +import net.mamoe.mirai.utils.toUHexString +import java.text.SimpleDateFormat +import java.util.* /** * The mirror of functions in inner classes of [Bot] @@ -30,4 +36,38 @@ suspend fun Bot.sendPacket(packet: ClientPacket) = this.network.socket.sendPacke suspend fun Bot.login(): LoginState = this.network.login() //BotAccount -val Bot.qqNumber: Long get() = this.account.qqNumber \ No newline at end of file +val Bot.qqNumber: Long get() = this.account.qqNumber + + +//logging +fun Bot.log(o: Any?) = info(o) + +fun Bot.println(o: Any?) = info(o) +fun Bot.info(o: Any?) = print(this, o.toString(), LoggerTextFormat.RESET) + +fun Bot.error(o: Any?) = print(this, o.toString(), LoggerTextFormat.RED) + +fun Bot.notice(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_BLUE) + +fun Bot.purple(o: Any?) = print(this, o.toString(), LoggerTextFormat.PURPLE) + +fun Bot.cyan(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_CYAN) +fun Bot.green(o: Any?) = print(this, o.toString(), LoggerTextFormat.GREEN) + +fun Bot.debug(o: Any?) = print(this, o.toString(), LoggerTextFormat.YELLOW) + +fun Bot.debugPacket(packet: ServerPacket) { + debug("Packet=$packet") + debug("Packet size=" + packet.input.goto(0).readAllBytes().size) + debug("Packet data=" + packet.input.goto(0).readAllBytes().toUHexString()) +} + +private fun print(bot: Bot, value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { + val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) + kotlin.io.println("$color[Mirai] $s #R${bot.id}: $value") +} + +private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { + val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) + kotlin.io.println("$color[Mirai] $s : $value") +} \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLogger.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLogger.kt index 93722d68e..555a46479 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLogger.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLogger.kt @@ -1,8 +1,5 @@ package net.mamoe.mirai.utils -import net.mamoe.mirai.Bot -import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket -import net.mamoe.mirai.network.protocol.tim.packet.goto import java.text.SimpleDateFormat import java.util.* @@ -60,36 +57,4 @@ open class Console( println("$color$identity $s : $value") } -} - -fun Bot.log(o: Any?) = info(o) -fun Bot.println(o: Any?) = info(o) -fun Bot.info(o: Any?) = print(this, o.toString(), LoggerTextFormat.RESET) - -fun Bot.error(o: Any?) = print(this, o.toString(), LoggerTextFormat.RED) - -fun Bot.notice(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_BLUE) - -fun Bot.purple(o: Any?) = print(this, o.toString(), LoggerTextFormat.PURPLE) - -fun Bot.cyan(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_CYAN) -fun Bot.green(o: Any?) = print(this, o.toString(), LoggerTextFormat.GREEN) - -fun Bot.debug(o: Any?) = print(this, o.toString(), LoggerTextFormat.YELLOW) - -fun Bot.debugPacket(packet: ServerPacket) { - debug("Packet=$packet") - debug("Packet size=" + packet.input.goto(0).readAllBytes().size) - debug("Packet data=" + packet.input.goto(0).readAllBytes().toUHexString()) -} - - -private fun print(bot: Bot, value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { - val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) - kotlin.io.println("$color[Mirai] $s #R${bot.id}: $value") -} - -private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { - val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) - kotlin.io.println("$color[Mirai] $s : $value") } \ No newline at end of file