Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Move logger messages to resource file
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 13, 2022
1 parent 78c7a82 commit 593071b
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 24 deletions.
12 changes: 12 additions & 0 deletions BrackeysBot/BrackeysBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ExceptionMessages.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Resources\LoggerMessages.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>LoggerMessages.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Update="Resources\LoggerMessages.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>LoggerMessages.resx</DependentUpon>
</Compile>
</ItemGroup>

</Project>
45 changes: 21 additions & 24 deletions BrackeysBot/Plugins/SimplePluginManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -58,8 +58,7 @@ public void DisablePlugin(IPlugin plugin)
}
catch (Exception exception)
{
Logger.Error(exception,
$"An exception was thrown when attempting to stop hosted service {hostedService.GetType()}");
Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenStoppingService, hostedService.GetType()));
}
}

Expand All @@ -69,13 +68,13 @@ public void DisablePlugin(IPlugin plugin)
}
catch (Exception exception)
{
Logger.Error(exception, $"An exception was thrown when attempting to disable {plugin.PluginInfo.Name}");
Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenDisablingPlugin, plugin.PluginInfo.Name));
}

_loadedPlugins[plugin] = false;

monoPlugin.DiscordClient?.DisconnectAsync();
Logger.Info($"Disabled plugin {plugin.PluginInfo.Name} {plugin.PluginInfo.Version}");
Logger.Info(string.Format(LoggerMessages.DisabledPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version));
}

/// <inheritdoc />
Expand All @@ -94,8 +93,7 @@ public void EnablePlugin(IPlugin plugin)
}
catch (Exception exception)
{
Logger.Error(exception,
$"An exception was thrown when attempting to start hosted service {hostedService.GetType()}");
Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenStartingService, hostedService.GetType()));
}
}

Expand All @@ -105,14 +103,14 @@ public void EnablePlugin(IPlugin plugin)
}
catch (Exception exception)
{
Logger.Error(exception, $"An exception was thrown when attempting to enable {plugin.PluginInfo.Name}");
Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenEnablingPlugin, plugin.PluginInfo.Name));
return;
}

monoPlugin.DiscordClient?.ConnectAsync();

_loadedPlugins[plugin] = true;
Logger.Info($"Enabled {plugin.PluginInfo.Name} {plugin.PluginInfo.Version}");
Logger.Info(string.Format(LoggerMessages.EnabledPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version));
}

/// <inheritdoc />
Expand All @@ -139,7 +137,7 @@ public IPlugin LoadPlugin(string name)
_loadedPlugins.Keys.FirstOrDefault(p => string.Equals(p.PluginInfo.Name, name, StringComparison.Ordinal));
if (loadedPlugin is not null)
{
Logger.Debug($"Plugin {name} was requested to load, but is already loaded. Skipping!");
Logger.Debug(string.Format(LoggerMessages.PluginAlreadyLoaded, name));
return loadedPlugin;
}

Expand All @@ -153,15 +151,15 @@ public IPlugin LoadPlugin(string name)
Assembly assembly = Assembly.LoadFile(pluginFileName); // DO NOT LoadFrom here. LoadFile does not load into domain
if (_loadedAssemblies.Exists(a => a.Location == assembly.Location))
{
Logger.Debug($"Assembly {assembly} already loaded. Using cache");
Logger.Debug(string.Format(LoggerMessages.AssemblyAlreadyLoaded, assembly));
assembly = _loadedAssemblies.Find(a => a.Location == assembly.Location)!;
}
else
{
assembly = Assembly.LoadFrom(pluginFileName); // loads into domain
_loadedAssemblies.Add(assembly);

Logger.Debug($"Loaded new assembly {assembly}");
Logger.Debug(string.Format(LoggerMessages.LoadedNewAssembly, assembly));
}

Type[] pluginTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(MonoPlugin))).ToArray();
Expand All @@ -184,8 +182,8 @@ public IPlugin LoadPlugin(string name)
var dependenciesAttribute = type.GetCustomAttribute<PluginDependenciesAttribute>();
if (dependenciesAttribute?.Dependencies is {Length: > 0} dependencyNames)
{
Logger.Debug(
$"{pluginAttribute.Name} requires {dependencyNames.Length} dependencies: {string.Join(", ", dependencyNames)}");
Logger.Debug(string.Format(LoggerMessages.PluginRequiresDependencies, pluginAttribute.Name, dependencyNames.Length,
string.Join(", ", dependencyNames)));

foreach (string dependencyName in dependencyNames)
{
Expand All @@ -196,7 +194,7 @@ public IPlugin LoadPlugin(string name)
}
catch (Exception exception)
{
Logger.Error(exception, $"Could not load dependency '{dependencyName}'");
Logger.Error(exception, string.Format(LoggerMessages.CouldNotLoadDependency, dependencyName));
return null!; // return value will be ignored anyway
}
}
Expand All @@ -205,8 +203,8 @@ public IPlugin LoadPlugin(string name)
string assemblyVersion = assembly.GetName().Version?.ToString(3) ?? pluginAttribute.Version;
if (!string.Equals(pluginAttribute.Version, assemblyVersion))
{
Logger.Warn(
$"Plugin version {pluginAttribute.Version} and assembly version {assemblyVersion} do not match for {pluginAttribute.Name}!");
Logger.Warn(string.Format(LoggerMessages.PluginVersionMismatch, pluginAttribute.Version, assemblyVersion,
pluginAttribute.Name));
}

PluginInfo.PluginAuthorInfo? author = null;
Expand Down Expand Up @@ -256,8 +254,7 @@ public IPlugin LoadPlugin(string name)
var token = plugin.Configuration.Get<string>("discord.token");
if (string.IsNullOrWhiteSpace(token))
{
Logger.Warn(
$"No token was specified in the config file for {plugin.PluginInfo.Name}! No client will be created for this plugin.");
Logger.Warn(string.Format(LoggerMessages.NoPluginToken, plugin.PluginInfo.Name));
}
else
{
Expand Down Expand Up @@ -289,7 +286,7 @@ public IPlugin LoadPlugin(string name)

_loadedPlugins.Add(plugin, false);

Logger.Info($"Loaded plugin {pluginInfo.Name} {pluginInfo.Version}");
Logger.Info(string.Format(LoggerMessages.LoadedPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version));
_pluginLoadStack.Pop();
return plugin;
}
Expand All @@ -303,7 +300,7 @@ public IReadOnlyList<IPlugin> LoadPlugins()
}
catch (IOException exception)
{
Logger.Warn(exception, $"The plugin directory '{PluginDirectory.FullName}' could not be created.");
Logger.Warn(exception, string.Format(LoggerMessages.PluginDirectoryCantBeCreated, PluginDirectory.FullName));
return ArraySegment<IPlugin>.Empty;
}

Expand All @@ -329,7 +326,7 @@ public IReadOnlyList<IPlugin> LoadPlugins()
}

plugin?.Dispose();
Logger.Error(exception, $"Could not load plugin {pluginName}");
Logger.Info(exception, string.Format(LoggerMessages.ExceptionWhenLoadingPlugin, pluginName));
}
}

Expand All @@ -349,14 +346,14 @@ public void UnloadPlugin(IPlugin plugin)
}
catch (Exception exception)
{
Logger.Error(exception, $"An exception was thrown when attempting to unload {plugin.PluginInfo.Name}");
Logger.Info(exception, string.Format(LoggerMessages.ExceptionWhenUnloadingPlugin, plugin.PluginInfo.Name));
}

monoPlugin.DiscordClient?.Dispose();
monoPlugin.DiscordClient = null;
plugin.Dispose();

Logger.Info($"Unloaded plugin {plugin.PluginInfo.Name} {plugin.PluginInfo.Version}");
Logger.Info(string.Format(LoggerMessages.UnloadedPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version));
_loadedPlugins.Remove(plugin);
}
}

0 comments on commit 593071b

Please sign in to comment.