From 764e0a81b7caf732846c2363b48fa68e642fe3d3 Mon Sep 17 00:00:00 2001 From: Kaz Wolfe Date: Sat, 9 Sep 2023 15:10:52 -0700 Subject: [PATCH 1/2] Fix log filtering with IPluginLog - Rename `PluginLog`'s property to `Dalamud.PluginName` to match what `IPluginLog` is doing. - Change `ModuleLog` to use `Dalamud.ModuleName` as its context property. - Update the Console window to handle both changes. - Add the ability to filter to only Dalamud module log messages. --- .../Internal/Windows/ConsoleWindow.cs | 34 +++++++++++++------ Dalamud/Logging/Internal/ModuleLog.cs | 11 +++--- Dalamud/Logging/PluginLog.cs | 2 +- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs b/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs index 872fdcd37..3303a2280 100644 --- a/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs +++ b/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs @@ -180,17 +180,20 @@ internal class ConsoleWindow : Window, IDisposable } // Filter by specific plugin(s) - var pluginInternalNames = Service.Get().InstalledPlugins + var sourceNames = Service.Get().InstalledPlugins .Select(p => p.Manifest.InternalName) - .OrderBy(s => s).ToList(); + .OrderBy(s => s) + .Prepend("DalamudInternal") + .ToList(); + var sourcePreviewVal = this.sourceFilters.Count switch { - 0 => "All plugins...", - 1 => "1 plugin...", - _ => $"{this.sourceFilters.Count} plugins...", + 0 => "All sources...", + 1 => "1 source...", + _ => $"{this.sourceFilters.Count} sources...", }; - var sourceSelectables = pluginInternalNames.Union(this.sourceFilters).ToList(); - if (ImGui.BeginCombo("Plugins", sourcePreviewVal)) + var sourceSelectables = sourceNames.Union(this.sourceFilters).ToList(); + if (ImGui.BeginCombo("Sources", sourcePreviewVal)) { foreach (var selectable in sourceSelectables) { @@ -443,7 +446,8 @@ internal class ConsoleWindow : Window, IDisposable // TODO: Improve this, add partial completion // https://github.com/ocornut/imgui/blob/master/imgui_demo.cpp#L6443-L6484 - var candidates = Service.Get().Commands.Where(x => x.Key.Contains("/" + words[0])).ToList(); + var candidates = Service.Get().Commands.Where(x => x.Key.Contains("/" + words[0])) + .ToList(); if (candidates.Count > 0) { ptr.DeleteChars(0, ptr.BufTextLen); @@ -499,9 +503,13 @@ internal class ConsoleWindow : Window, IDisposable TimeStamp = logEvent.Timestamp, HasException = logEvent.Exception != null, }; - - if (logEvent.Properties.TryGetValue("SourceContext", out var sourceProp) && - sourceProp is ScalarValue { Value: string value }) + + if (logEvent.Properties.ContainsKey("Dalamud.ModuleName")) + { + entry.Source = "DalamudInternal"; + } + else if (logEvent.Properties.TryGetValue("Dalamud.PluginName", out var sourceProp) && + sourceProp is ScalarValue { Value: string value }) { entry.Source = value; } @@ -579,6 +587,10 @@ internal class ConsoleWindow : Window, IDisposable public bool IsMultiline { get; set; } + /// + /// Gets or sets the system responsible for generating this log entry. Generally will be a plugin's + /// InternalName. + /// public string? Source { get; set; } public bool HasException { get; set; } diff --git a/Dalamud/Logging/Internal/ModuleLog.cs b/Dalamud/Logging/Internal/ModuleLog.cs index c6c66e81a..2fb735640 100644 --- a/Dalamud/Logging/Internal/ModuleLog.cs +++ b/Dalamud/Logging/Internal/ModuleLog.cs @@ -12,6 +12,10 @@ public class ModuleLog { private readonly string moduleName; private readonly ILogger moduleLogger; + + // FIXME (v9): Deprecate this class in favor of using contextualized ILoggers with proper formatting. + // We can keep this class around as a Serilog helper, but ModuleLog should no longer be a returned + // type, instead returning a (prepared) ILogger appropriately. /// /// Initializes a new instance of the class. @@ -20,10 +24,8 @@ public class ModuleLog /// The module name. public ModuleLog(string? moduleName) { - // FIXME: Should be namespaced better, e.g. `Dalamud.PluginLoader`, but that becomes a relatively large - // change. this.moduleName = moduleName ?? "DalamudInternal"; - this.moduleLogger = Log.ForContext("SourceContext", this.moduleName); + this.moduleLogger = Log.ForContext("Dalamud.ModuleName", this.moduleName); } /// @@ -128,7 +130,8 @@ public class ModuleLog public void Fatal(Exception exception, string messageTemplate, params object[] values) => this.WriteLog(LogEventLevel.Fatal, messageTemplate, exception, values); - private void WriteLog(LogEventLevel level, string messageTemplate, Exception? exception = null, params object[] values) + private void WriteLog( + LogEventLevel level, string messageTemplate, Exception? exception = null, params object[] values) { // FIXME: Eventually, the `pluginName` tag should be removed from here and moved over to the actual log // formatter. diff --git a/Dalamud/Logging/PluginLog.cs b/Dalamud/Logging/PluginLog.cs index b2f2a5065..3ac98f15a 100644 --- a/Dalamud/Logging/PluginLog.cs +++ b/Dalamud/Logging/PluginLog.cs @@ -256,7 +256,7 @@ public static class PluginLog private static ILogger GetPluginLogger(string? pluginName) { - return Serilog.Log.ForContext("SourceContext", pluginName ?? string.Empty); + return Serilog.Log.ForContext("Dalamud.PluginName", pluginName ?? string.Empty); } private static void WriteLog(string? pluginName, LogEventLevel level, string messageTemplate, Exception? exception = null, params object[] values) From 8911d4ebc2e2134661360de3c2e0e26b6e202c0c Mon Sep 17 00:00:00 2001 From: Kaz Wolfe Date: Tue, 12 Sep 2023 18:33:41 -0700 Subject: [PATCH 2/2] Re-add SourceContext property check Technically an API breakage, some plugins are passing this for logging. Slated for removal in API 9, however. --- Dalamud/Interface/Internal/Windows/ConsoleWindow.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs b/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs index 3303a2280..0febc0fc4 100644 --- a/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs +++ b/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs @@ -504,14 +504,16 @@ internal class ConsoleWindow : Window, IDisposable HasException = logEvent.Exception != null, }; + // TODO (v9): Remove SourceContext property check. if (logEvent.Properties.ContainsKey("Dalamud.ModuleName")) { entry.Source = "DalamudInternal"; } - else if (logEvent.Properties.TryGetValue("Dalamud.PluginName", out var sourceProp) && - sourceProp is ScalarValue { Value: string value }) + else if ((logEvent.Properties.TryGetValue("Dalamud.PluginName", out var sourceProp) || + logEvent.Properties.TryGetValue("SourceContext", out sourceProp)) && + sourceProp is ScalarValue { Value: string sourceValue }) { - entry.Source = value; + entry.Source = sourceValue; } this.logText.Add(entry);