From 9f88293691193b2bbce6b344cded8b520d621091 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sun, 25 Oct 2020 23:16:58 +0800 Subject: [PATCH] Review LoggerController - Delete logger cache - Move MiraiConsoleLogger to internal package --- .../MiraiConsoleImplementationBridge.kt | 11 +--- .../internal/data/builtins/LoggerConfig.kt | 8 +++ .../logging/MiraiConsoleLogger.kt | 3 +- .../mirai/console/logging/LoggerController.kt | 11 ---- .../logging/LoggerControllerPlatformBase.kt | 8 +-- .../logging/MiraiConsoleLoggerUnused.kt | 56 ------------------- .../console/logging/MiraiDelegateLogger.kt | 24 -------- .../mamoe/mirai/console/TestMiraiConosle.kt | 2 +- 8 files changed, 15 insertions(+), 108 deletions(-) rename backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/{ => internal}/logging/MiraiConsoleLogger.kt (94%) delete mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLoggerUnused.kt delete mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiDelegateLogger.kt diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt index d4ea82228..b414d596d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt @@ -38,10 +38,8 @@ import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl import net.mamoe.mirai.console.internal.util.autoHexToBytes -import net.mamoe.mirai.console.logging.LoggerController -import net.mamoe.mirai.console.logging.LoggerControllerForFrontend -import net.mamoe.mirai.console.logging.MiraiConsoleLoggerUnused -import net.mamoe.mirai.console.logging.MiraiDelegateLogger +import net.mamoe.mirai.console.logging.* +import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger import net.mamoe.mirai.console.permission.PermissionService import net.mamoe.mirai.console.permission.PermissionService.Companion.grantPermission import net.mamoe.mirai.console.permission.RootPermission @@ -99,10 +97,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI override fun createLogger(identity: String?): MiraiLogger { val controller = loggerController - val logger = MiraiConsoleLoggerUnused(controller, identity) - val delegate = MiraiDelegateLogger(logger) - logger.delegateLogger = delegate - return delegate + return MiraiConsoleLogger(controller, controller.newLogger(identity)) } @Suppress("RemoveRedundantBackticks") diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtins/LoggerConfig.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtins/LoggerConfig.kt index da403aa53..a7fc11db2 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtins/LoggerConfig.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtins/LoggerConfig.kt @@ -11,12 +11,20 @@ package net.mamoe.mirai.console.internal.data.builtins import net.mamoe.mirai.console.data.AutoSavePluginConfig +import net.mamoe.mirai.console.data.ValueDescription import net.mamoe.mirai.console.data.value import net.mamoe.mirai.utils.SimpleLogger import java.util.* internal object LoggerConfig : AutoSavePluginConfig("Logger") { + @ValueDescription(""" + 日志输出等级 可选值: ALL, VERBOSE, DEBUG, INFO, WARNING, ERROR, NONE + """) val defaultPriority by value(LogPriority.INFO) + + @ValueDescription(""" + 特定日志记录器输出等级 + """) val loggers: Map by value( mapOf("example.logger" to LogPriority.NONE) ) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLogger.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/logging/MiraiConsoleLogger.kt similarity index 94% rename from backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLogger.kt rename to backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/logging/MiraiConsoleLogger.kt index de35aeac4..48b829ee9 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLogger.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/logging/MiraiConsoleLogger.kt @@ -8,8 +8,9 @@ * */ -package net.mamoe.mirai.console.logging +package net.mamoe.mirai.console.internal.logging +import net.mamoe.mirai.console.logging.LoggerController import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLoggerPlatformBase import net.mamoe.mirai.utils.SimpleLogger diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerController.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerController.kt index 1eaecff5c..75b280e89 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerController.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerController.kt @@ -28,8 +28,6 @@ import java.util.concurrent.atomic.AtomicReference public interface LoggerController { /** 是否应该记录该等级的日志 */ public fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean - /** 是否应该对 [newLogger] 的结果进行缓存 */ - public val cacheLoggers: Boolean /** * 创建一个新的 [MiraiLogger] * @@ -43,13 +41,4 @@ public interface LoggerController { * 因此不要在 [newLogger] 中调用 [net.mamoe.mirai.utils.DefaultLogger] */ public fun newLogger(identity: String?): MiraiLogger - /** - * 获取对应的 Registration - * - * 实现细节: - * - 如果 [identity] 存在对应 Registration, 直接返回先前的 Registration - * - [identity] 不存在对应 Registration时, 创建新的 Registration 并存储起来 - * - 注意并发性 - */ - public fun getLoggerRegistration(identity: String?): AtomicReference } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerControllerPlatformBase.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerControllerPlatformBase.kt index a77b0860c..2be82ab8a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerControllerPlatformBase.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/LoggerControllerPlatformBase.kt @@ -12,6 +12,7 @@ package net.mamoe.mirai.console.logging import net.mamoe.mirai.console.ConsoleFrontEndImplementation import net.mamoe.mirai.console.util.ConsoleExperimentalApi +import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.SimpleLogger import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicReference @@ -22,12 +23,5 @@ import java.util.concurrent.atomic.AtomicReference public abstract class LoggerControllerPlatformBase : LoggerController { override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true - override val cacheLoggers: Boolean get() = true - protected val registrations: ConcurrentHashMap> = ConcurrentHashMap() - - protected object NilIdentityPlaceholder - - override fun getLoggerRegistration(identity: String?): AtomicReference = - registrations.computeIfAbsent(identity ?: NilIdentityPlaceholder) { AtomicReference() } } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLoggerUnused.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLoggerUnused.kt deleted file mode 100644 index d2caa896e..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiConsoleLoggerUnused.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - * - */ - -package net.mamoe.mirai.console.logging - -import net.mamoe.mirai.utils.MiraiLogger -import net.mamoe.mirai.utils.MiraiLoggerPlatformBase - -internal class MiraiConsoleLoggerUnused( - val controller: LoggerController, - override val identity: String? -) : MiraiLoggerPlatformBase() { - internal object InitializeLock - - internal lateinit var delegateLogger: MiraiDelegateLogger - private val logger: MiraiLogger by lazy { - val logger = if (controller.cacheLoggers) { - val reference = controller.getLoggerRegistration(identity) - if (reference.compareAndSet(null, InitializeLock)) { - val logger = MiraiConsoleLogger(controller, controller.newLogger(identity)) - reference.set(logger) - logger - } else { - // initialized/initializing - val logger: MiraiLogger - while (true) { - val status = reference.get() - if (status is MiraiLogger) { - logger = status - break - } - } - logger - } - } else { - MiraiConsoleLogger(controller, controller.newLogger(identity)) - } - delegateLogger.logger = logger - logger - } - override val isEnabled: Boolean get() = logger.isEnabled - - override fun debug0(message: String?, e: Throwable?) = logger.debug(message, e) - override fun error0(message: String?, e: Throwable?) = logger.error(message, e) - override fun info0(message: String?, e: Throwable?) = logger.info(message, e) - override fun verbose0(message: String?, e: Throwable?) = logger.verbose(message, e) - override fun warning0(message: String?, e: Throwable?) = logger.warning(message, e) - -} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiDelegateLogger.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiDelegateLogger.kt deleted file mode 100644 index 5f61088f7..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/logging/MiraiDelegateLogger.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - * - */ - -package net.mamoe.mirai.console.logging - -import net.mamoe.mirai.utils.MiraiLogger -import net.mamoe.mirai.utils.MiraiLoggerPlatformBase - -internal class MiraiDelegateLogger(var logger: MiraiLogger) : MiraiLoggerPlatformBase() { - override val identity: String? get() = logger.identity - override val isEnabled: Boolean get() = logger.isEnabled - override fun debug0(message: String?, e: Throwable?) = logger.debug(message, e) - override fun error0(message: String?, e: Throwable?) = logger.error(message, e) - override fun info0(message: String?, e: Throwable?) = logger.info(message, e) - override fun verbose0(message: String?, e: Throwable?) = logger.verbose(message, e) - override fun warning0(message: String?, e: Throwable?) = logger.warning(message, e) -} diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt index 9af2aa7e2..ce6708193 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt @@ -72,7 +72,7 @@ fun initTestEnvironment() { override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver = LoginSolver.Default - override val loggerController: LoggerController = object:LoggerControllerPlatformBase(){ + override val loggerController: LoggerController = object : LoggerControllerPlatformBase() { override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true override fun newLogger(identity: String?): MiraiLogger = PlatformLogger(identity) }