From 1a4ad94d20370e95fee0389bab70e67b6106b357 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 18 Jan 2020 12:32:14 +0800 Subject: [PATCH 1/2] Simplify logger structure --- .../net/mamoe/mirai/utils/PlatformLoggerAndroid.kt | 4 +--- .../kotlin/net.mamoe.mirai/utils/MiraiLogger.kt | 11 ++++++----- .../kotlin/net/mamoe/mirai/utils/MiraiLoggerJvm.kt | 6 ++---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformLoggerAndroid.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformLoggerAndroid.kt index 9bd8b298d..9bc015143 100644 --- a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformLoggerAndroid.kt +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/PlatformLoggerAndroid.kt @@ -2,13 +2,11 @@ package net.mamoe.mirai.utils import android.util.Log -actual typealias PlatformLogger = AndroidLogger - /** * Android 平台的默认的日志记录器, 使用 [Log] * 不应该直接构造这个类的实例. 需使用 [DefaultLogger] */ -open class AndroidLogger(override val identity: String? = "Mirai") : MiraiLoggerPlatformBase() { +actual open class PlatformLogger actual constructor(override val identity: String?) : MiraiLoggerPlatformBase() { override fun verbose0(any: Any?) { Log.v(identity, any?.toString() ?: "") } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt index 7e4543401..24e5fee3e 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt @@ -15,7 +15,7 @@ var DefaultLogger: (identity: String?) -> MiraiLogger = { PlatformLogger(it) } * 在 _JVM 控制台_ 端的实现为 [println] * 在 _Android_ 端的实现为 [android.util.Log] * - * 不应该直接构造这个类的实例. 请使用 [DefaultLogger] + * 不应该直接构造这个类的实例. 请使用 [DefaultLogger], 或使用默认的顶层日志记录 [MiraiLogger.Companion] */ expect open class PlatformLogger @JvmOverloads internal constructor(identity: String? = "Mirai") : MiraiLoggerPlatformBase @@ -35,7 +35,7 @@ fun MiraiLogger.withSwitch(default: Boolean = true): MiraiLoggerWithSwitch = Mir */ interface MiraiLogger { /** - * 顶层日志记录器 + * 顶层日志记录器. */ companion object : MiraiLogger by DefaultLogger("Mirai") @@ -146,13 +146,14 @@ object SilentLogger : PlatformLogger() { } } -@Suppress("FunctionName") -fun SimpleLogger(logger: (String?, Throwable?) -> Unit): SimpleLogger = SimpleLogger(null, logger) - /** * 简易日志记录, 所有类型日志都会被重定向 [logger] */ class SimpleLogger(override val identity: String?, private val logger: (String?, Throwable?) -> Unit) : MiraiLoggerPlatformBase() { + companion object { + operator fun invoke(logger: (String?, Throwable?) -> Unit): SimpleLogger = SimpleLogger(null, logger) + } + override fun verbose0(any: Any?) = logger(any?.toString(), null) override fun verbose0(message: String?, e: Throwable?) = logger(message, e) override fun debug0(any: Any?) = logger(any?.toString(), null) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLoggerJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLoggerJvm.kt index 846500af2..b86576ce9 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLoggerJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/MiraiLoggerJvm.kt @@ -3,13 +3,11 @@ package net.mamoe.mirai.utils import java.text.SimpleDateFormat import java.util.* -actual typealias PlatformLogger = Console - /** * JVM 控制台日志实现 */ -open class Console @JvmOverloads internal constructor( - override val identity: String? = "Mirai" +actual open class PlatformLogger @JvmOverloads internal actual constructor( + override val identity: String? ) : MiraiLoggerPlatformBase() { override fun verbose0(any: Any?) = println(any, LoggerTextFormat.RESET) override fun verbose0(message: String?, e: Throwable?) { From 3c662a6e7bcdfd1f0af09cde63915369e3e4ea9c Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 18 Jan 2020 12:54:27 +0800 Subject: [PATCH 2/2] Simplify logger structure --- .../net.mamoe.mirai/utils/MiraiLogger.kt | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt index 24e5fee3e..1c8258e08 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/MiraiLogger.kt @@ -130,20 +130,11 @@ interface MiraiLogger { object SilentLogger : PlatformLogger() { override val identity: String? = null - override fun error0(any: Any?) { - } - - override fun debug0(any: Any?) { - } - - override fun warning0(any: Any?) { - } - - override fun verbose0(any: Any?) { - } - - override fun info0(any: Any?) { - } + override fun error0(any: Any?) = Unit + override fun debug0(any: Any?) = Unit + override fun warning0(any: Any?) = Unit + override fun verbose0(any: Any?) = Unit + override fun info0(any: Any?) = Unit } /**