diff --git a/backend/mirai-console/src/MiraiConsoleImplementation.kt b/backend/mirai-console/src/MiraiConsoleImplementation.kt index 7aa2f239f..418b50536 100644 --- a/backend/mirai-console/src/MiraiConsoleImplementation.kt +++ b/backend/mirai-console/src/MiraiConsoleImplementation.kt @@ -18,6 +18,8 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start import net.mamoe.mirai.console.command.ConsoleCommandSender import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge +import net.mamoe.mirai.console.logging.LoggerController +import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.util.ConsoleInput @@ -170,6 +172,11 @@ public interface MiraiConsoleImplementation : CoroutineScope { */ public fun createLogger(identity: String?): MiraiLogger + /** + * 前端预先定义的 [LoggerController], 以允许前端使用自己的配置系统 + */ + public val loggerController: LoggerController get() = LoggerControllerImpl + public companion object { internal lateinit var instance: MiraiConsoleImplementation private val initLock = ReentrantLock() diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 8a2d7e0ee..7ce79204e 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -31,12 +31,16 @@ import net.mamoe.mirai.console.extensions.SingletonExtensionSelector import net.mamoe.mirai.console.internal.command.CommandManagerImpl import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScope +import net.mamoe.mirai.console.internal.data.builtins.LoggerConfig import net.mamoe.mirai.console.internal.data.castOrNull import net.mamoe.mirai.console.internal.extension.BuiltInSingletonExtensionSelector import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage +import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl +import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger 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.permission.PermissionService import net.mamoe.mirai.console.permission.PermissionService.Companion.permit import net.mamoe.mirai.console.permission.RootPermission @@ -82,18 +86,31 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI override val dataStorageForBuiltIns: PluginDataStorage by instance::dataStorageForBuiltIns override val configStorageForBuiltIns: PluginDataStorage by instance::configStorageForBuiltIns override val consoleInput: ConsoleInput by instance::consoleInput - override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver = instance.createLoginSolver(requesterBot, configuration) + override val loggerController: LoggerController by instance::loggerController + init { DefaultLogger = this::createLogger } - override fun createLogger(identity: String?): MiraiLogger = instance.createLogger(identity) + + override fun createLogger(identity: String?): MiraiLogger { + val controller = loggerController + return MiraiConsoleLogger(controller, instance.createLogger(identity)) + } @Suppress("RemoveRedundantBackticks") internal fun doStart() { + + phase `setup logger controller`@{ + if (loggerController === LoggerControllerImpl) { + // Reload LoggerConfig. + ConsoleDataScope.addAndReloadConfig(LoggerConfig) + } + } + phase `greeting`@{ val buildDateFormatted = buildDate.atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) @@ -160,6 +177,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI } } + phase `load PermissionService`@{ mainLogger.verbose { "Loading PermissionService..." } diff --git a/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt b/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt index 76ed0a104..8520c788a 100644 --- a/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt +++ b/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt @@ -11,10 +11,7 @@ package net.mamoe.mirai.console.internal.data.builtins import kotlinx.coroutines.CoroutineScope import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.data.AutoSavePluginDataHolder -import net.mamoe.mirai.console.data.PluginConfig -import net.mamoe.mirai.console.data.PluginData -import net.mamoe.mirai.console.data.PluginDataStorage +import net.mamoe.mirai.console.data.* import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.utils.minutesToMillis diff --git a/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt b/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt new file mode 100644 index 000000000..f860e104a --- /dev/null +++ b/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt @@ -0,0 +1,30 @@ +/* + * 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 through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +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.console.logging.AbstractLoggerController + +internal object LoggerConfig : AutoSavePluginConfig("Logger") { + @ValueDescription(""" + 日志输出等级 可选值: ALL, VERBOSE, DEBUG, INFO, WARNING, ERROR, NONE + """) + val defaultPriority by value(AbstractLoggerController.LogPriority.INFO) + + @ValueDescription(""" + 特定日志记录器输出等级 + """) + val loggers: Map by value( + mapOf("example.logger" to AbstractLoggerController.LogPriority.NONE) + ) + +} diff --git a/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt b/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt new file mode 100644 index 000000000..c65a23237 --- /dev/null +++ b/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt @@ -0,0 +1,27 @@ +/* + * 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 through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.internal.logging + +import net.mamoe.mirai.console.ConsoleFrontEndImplementation +import net.mamoe.mirai.console.internal.data.builtins.LoggerConfig +import net.mamoe.mirai.console.logging.AbstractLoggerController +import net.mamoe.mirai.console.util.ConsoleInternalApi + +@ConsoleFrontEndImplementation +@ConsoleInternalApi +internal object LoggerControllerImpl : AbstractLoggerController() { + + override fun getPriority(identity: String?): LogPriority = if (identity == null) { + LoggerConfig.defaultPriority + } else { + LoggerConfig.loggers[identity] ?: LoggerConfig.defaultPriority + } + +} \ No newline at end of file diff --git a/backend/mirai-console/src/internal/logging/MiraiConsoleLogger.kt b/backend/mirai-console/src/internal/logging/MiraiConsoleLogger.kt new file mode 100644 index 000000000..6d4fa04a5 --- /dev/null +++ b/backend/mirai-console/src/internal/logging/MiraiConsoleLogger.kt @@ -0,0 +1,47 @@ +/* + * 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 through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +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 + +internal class MiraiConsoleLogger( + private val controller: LoggerController, + val logger: MiraiLogger +) : MiraiLoggerPlatformBase() { + override val identity: String? get() = logger.identity + override val isEnabled: Boolean get() = logger.isEnabled + override fun info0(message: String?, e: Throwable?) { + if (controller.shouldLog(identity, SimpleLogger.LogPriority.INFO)) + logger.info(message, e) + } + + override fun warning0(message: String?, e: Throwable?) { + if (controller.shouldLog(identity, SimpleLogger.LogPriority.WARNING)) + logger.warning(message, e) + } + + override fun debug0(message: String?, e: Throwable?) { + if (controller.shouldLog(identity, SimpleLogger.LogPriority.DEBUG)) + logger.debug(message, e) + } + + override fun error0(message: String?, e: Throwable?) { + if (controller.shouldLog(identity, SimpleLogger.LogPriority.ERROR)) + logger.error(message, e) + } + + override fun verbose0(message: String?, e: Throwable?) { + if (controller.shouldLog(identity, SimpleLogger.LogPriority.VERBOSE)) + logger.verbose(message, e) + } +} diff --git a/backend/mirai-console/src/logging/AbstractLoggerController.kt b/backend/mirai-console/src/logging/AbstractLoggerController.kt new file mode 100644 index 000000000..3d4725990 --- /dev/null +++ b/backend/mirai-console/src/logging/AbstractLoggerController.kt @@ -0,0 +1,62 @@ +/* + * 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 through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.logging + +import net.mamoe.mirai.console.util.ConsoleExperimentalApi +import net.mamoe.mirai.utils.SimpleLogger +import java.util.* + +@ConsoleExperimentalApi +public abstract class AbstractLoggerController : LoggerController { + + protected open fun shouldLog( + priority: LogPriority, + settings: LogPriority + ): Boolean = settings <= priority + + protected abstract fun getPriority(identity: String?): LogPriority + + override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = + shouldLog(LogPriority.by(priority), getPriority(identity)) + + @Suppress("unused") + @ConsoleExperimentalApi + public enum class LogPriority { + ALL(null), + VERBOSE, + DEBUG, + INFO, + WARNING, + ERROR, + NONE(null); + + private var mapped: SimpleLogger.LogPriority? = null + + public companion object { + private val mapping = EnumMap(SimpleLogger.LogPriority::class.java) + + public fun by(priority: SimpleLogger.LogPriority): LogPriority = mapping[priority]!! + + init { + values().forEach { priority -> + mapping[priority.mapped ?: return@forEach] = priority + } + } + } + + @Suppress("UNUSED_PARAMETER") + constructor(void: Nothing?) + constructor() { + mapped = SimpleLogger.LogPriority.valueOf(name) + } + + } + +} diff --git a/backend/mirai-console/src/logging/LoggerController.kt b/backend/mirai-console/src/logging/LoggerController.kt new file mode 100644 index 000000000..59b60457b --- /dev/null +++ b/backend/mirai-console/src/logging/LoggerController.kt @@ -0,0 +1,26 @@ +/* + * 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 through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.logging + +import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl +import net.mamoe.mirai.console.util.ConsoleExperimentalApi +import net.mamoe.mirai.utils.SimpleLogger + +/** + * 日志控制系统 + * + * @see [LoggerControllerImpl] + */ +@ConsoleExperimentalApi +public interface LoggerController { + /** 是否应该记录该等级的日志 */ + public fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean + +} diff --git a/backend/mirai-console/test/TestMiraiConosle.kt b/backend/mirai-console/test/TestMiraiConosle.kt index c1eb1145c..d5186fabe 100644 --- a/backend/mirai-console/test/TestMiraiConosle.kt +++ b/backend/mirai-console/test/TestMiraiConosle.kt @@ -14,6 +14,7 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.data.MemoryPluginDataStorage import net.mamoe.mirai.console.data.PluginDataStorage +import net.mamoe.mirai.console.logging.LoggerController import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.util.ConsoleExperimentalApi @@ -21,10 +22,7 @@ import net.mamoe.mirai.console.util.ConsoleInput import net.mamoe.mirai.console.util.ConsoleInternalApi import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.message.data.Message -import net.mamoe.mirai.utils.BotConfiguration -import net.mamoe.mirai.utils.LoginSolver -import net.mamoe.mirai.utils.MiraiLogger -import net.mamoe.mirai.utils.PlatformLogger +import net.mamoe.mirai.utils.* import java.nio.file.Path import kotlin.coroutines.Continuation import kotlin.coroutines.CoroutineContext diff --git a/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt b/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt index 8708e197a..9cd5a3ead 100644 --- a/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt +++ b/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt @@ -150,6 +150,7 @@ val terminal: Terminal = run { private object ConsoleFrontEndDescImpl : MiraiConsoleFrontEndDescription { override val name: String get() = "Terminal" override val vendor: String get() = "Mamoe Technologies" + // net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version // is console's version not frontend's version override val version: SemVersion = SemVersion(net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.versionConst)