From ee48adfd8cfe0215bc31355c30dd3d10be2069d5 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Tue, 13 Jul 2021 18:25:11 +0800 Subject: [PATCH] Improve logging of plugin loading; close #369 - Log plugin version when loading plugin - Log plugin loading/enabling status when verbose enabled --- .../plugin/BuiltInJvmPluginLoaderImpl.kt | 8 +++++++- .../src/internal/plugin/PluginDescriptionUtil.kt | 16 ++++++++++++++++ .../src/internal/plugin/PluginManagerImpl.kt | 8 ++++---- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 backend/mirai-console/src/internal/plugin/PluginDescriptionUtil.kt diff --git a/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt b/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt index bd87bb6c8..ad7c117ec 100644 --- a/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt +++ b/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt @@ -23,6 +23,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoadException import net.mamoe.mirai.console.plugin.name import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.verbose import java.io.File import java.util.concurrent.ConcurrentHashMap @@ -103,11 +104,12 @@ internal object BuiltInJvmPluginLoaderImpl : if (loadedPlugins.put(plugin, Unit) != null) { error("Plugin '${plugin.name}' is already loaded and cannot be reloaded.") } + logger.verbose { "Loading plugin ${plugin.description.smartToString()}" } runCatching { check(plugin is JvmPluginInternal) { "A JvmPlugin must extend AbstractJvmPlugin to be loaded by JvmPluginLoader.BuiltIn" } plugin.internalOnLoad() }.getOrElse { - throw PluginLoadException("Exception while loading ${plugin.description.name}", it) + throw PluginLoadException("Exception while loading ${plugin.description.smartToString()}", it) } } @@ -115,9 +117,13 @@ internal object BuiltInJvmPluginLoaderImpl : if (plugin.isEnabled) error("Plugin '${plugin.name}' is already enabled and cannot be re-enabled.") ensureActive() runCatching { + logger.verbose { "Enabling plugin ${plugin.description.smartToString()}" } if (plugin is JvmPluginInternal) { plugin.internalOnEnable() } else plugin.onEnable() + + // Extra space for logging align + logger.verbose { "Enabled plugin ${plugin.description.smartToString()}" } }.getOrElse { throw PluginLoadException("Exception while loading ${plugin.description.name}", it) } diff --git a/backend/mirai-console/src/internal/plugin/PluginDescriptionUtil.kt b/backend/mirai-console/src/internal/plugin/PluginDescriptionUtil.kt new file mode 100644 index 000000000..9bd80d3de --- /dev/null +++ b/backend/mirai-console/src/internal/plugin/PluginDescriptionUtil.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/dev/LICENSE + */ + +package net.mamoe.mirai.console.internal.plugin + +import net.mamoe.mirai.console.plugin.description.PluginDescription + +internal fun PluginDescription.smartToString(): String { + return "$name v$version" +} diff --git a/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt b/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt index 2182f8dd2..0faebb324 100644 --- a/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt +++ b/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt @@ -86,10 +86,10 @@ internal object PluginManagerImpl : PluginManager, CoroutineScope by MiraiConsol resolvedPlugins.add(plugin) }.fold( onSuccess = { - logger.info { "Successfully loaded plugin ${getPluginDescription(plugin).name}" } + logger.info { "Successfully loaded plugin ${getPluginDescription(plugin).smartToString()}" } }, onFailure = { - logger.info { "Cannot load plugin ${getPluginDescription(plugin).name}" } + logger.info { "Cannot load plugin ${getPluginDescription(plugin).smartToString()}" } throw it } ) @@ -101,10 +101,10 @@ internal object PluginManagerImpl : PluginManager, CoroutineScope by MiraiConsol this.enable(plugin as P) }.fold( onSuccess = { - logger.info { "Successfully enabled plugin ${getPluginDescription(plugin).name}" } + logger.info { "Successfully enabled plugin ${getPluginDescription(plugin).smartToString()}" } }, onFailure = { - logger.info { "Cannot enable plugin ${getPluginDescription(plugin).name}" } + logger.info { "Cannot enable plugin ${getPluginDescription(plugin).smartToString()}" } throw it } )