Allow FrontEnd to implement LoggerController

This commit is contained in:
Karlatemp 2020-10-26 23:30:35 +08:00
parent 544e11747c
commit abb55ac619
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 16 additions and 25 deletions

View File

@ -172,6 +172,11 @@ public interface MiraiConsoleImplementation : CoroutineScope {
*/ */
public fun createLogger(identity: String?): MiraiLogger public fun createLogger(identity: String?): MiraiLogger
/**
* 前端预先定义的 [LoggerController], 以允许前端使用自己的配置系统
*/
public val loggerController: LoggerController get() = LoggerControllerImpl
public companion object { public companion object {
internal lateinit var instance: MiraiConsoleImplementation internal lateinit var instance: MiraiConsoleImplementation
private val initLock = ReentrantLock() private val initLock = ReentrantLock()

View File

@ -12,13 +12,14 @@ package net.mamoe.mirai.console.extensions
import net.mamoe.mirai.console.extension.AbstractSingletonExtensionPoint import net.mamoe.mirai.console.extension.AbstractSingletonExtensionPoint
import net.mamoe.mirai.console.extension.SingletonExtension import net.mamoe.mirai.console.extension.SingletonExtension
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl
import net.mamoe.mirai.console.logging.LoggerController import net.mamoe.mirai.console.logging.LoggerController
import net.mamoe.mirai.console.permission.PermissionService import net.mamoe.mirai.console.permission.PermissionService
public interface LoggerControllerProvider : SingletonExtension<LoggerController> { public interface LoggerControllerProvider : SingletonExtension<LoggerController> {
public companion object ExtensionPoint : public companion object ExtensionPoint :
AbstractSingletonExtensionPoint<LoggerControllerProvider, LoggerController>(LoggerControllerProvider::class, LoggerControllerImpl) AbstractSingletonExtensionPoint<LoggerControllerProvider, LoggerController>(LoggerControllerProvider::class, MiraiConsoleImplementationBridge.frontendLoggerController)
} }

View File

@ -38,7 +38,6 @@ import net.mamoe.mirai.console.internal.extension.BuiltInSingletonExtensionSelec
import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage
import net.mamoe.mirai.console.internal.logging.LoggerControllerDelegate import net.mamoe.mirai.console.internal.logging.LoggerControllerDelegate
import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl
import net.mamoe.mirai.console.internal.logging.LoggerControllerTrusted
import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger
import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
@ -88,10 +87,12 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
override val dataStorageForBuiltIns: PluginDataStorage by instance::dataStorageForBuiltIns override val dataStorageForBuiltIns: PluginDataStorage by instance::dataStorageForBuiltIns
override val configStorageForBuiltIns: PluginDataStorage by instance::configStorageForBuiltIns override val configStorageForBuiltIns: PluginDataStorage by instance::configStorageForBuiltIns
override val consoleInput: ConsoleInput by instance::consoleInput override val consoleInput: ConsoleInput by instance::consoleInput
private val loggerController = LoggerControllerDelegate(LoggerControllerTrusted)
override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver = override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver =
instance.createLoginSolver(requesterBot, configuration) instance.createLoginSolver(requesterBot, configuration)
internal val frontendLoggerController = instance.loggerController
override val loggerController: LoggerControllerDelegate = LoggerControllerDelegate(frontendLoggerController)
init { init {
DefaultLogger = this::createLogger DefaultLogger = this::createLogger
} }
@ -106,9 +107,11 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
internal fun doStart() { internal fun doStart() {
phase `pre setup logger controller`@{ phase `pre setup logger controller`@{
loggerController.delegate = LoggerControllerImpl loggerController.delegate = frontendLoggerController
// Relaod LoggerConfig first. if (frontendLoggerController === LoggerControllerImpl) {
ConsoleDataScope.addAndReloadConfig(LoggerConfig) // Relaod LoggerConfig first.
ConsoleDataScope.addAndReloadConfig(LoggerConfig)
}
} }
phase `greeting`@{ phase `greeting`@{
@ -179,7 +182,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
phase `setup logger controller`@{ phase `setup logger controller`@{
val selected = LoggerControllerProvider.selectedInstance val selected = LoggerControllerProvider.selectedInstance
if (selected !== LoggerControllerImpl) { if (selected !== frontendLoggerController) {
ConsoleDataScope.dropConfig(LoggerConfig) ConsoleDataScope.dropConfig(LoggerConfig)
loggerController.delegate = selected loggerController.delegate = selected
} }

View File

@ -1,18 +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.internal.logging
import net.mamoe.mirai.console.logging.LoggerController
import net.mamoe.mirai.utils.SimpleLogger
internal object LoggerControllerTrusted: LoggerController {
override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true
}