GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: improve debug logging (#74)
lars-bergerf80c23f65682
diff --git a/.editorconfig b/.editorconfigindex 890061ff..47b4c308 100644--- a/.editorconfig+++ b/.editorconfig@@ -155,3 +155,6 @@ dotnet_diagnostic.CA1001.severity = none# Add rule exception "Fix formatting".[**/AssemblyInfo.cs]dotnet_diagnostic.IDE0055.severity = none+# Add rule exception "The logging message template should not vary between calls".+[**/LoggerExtensions.cs]+dotnet_diagnostic.CA2254.severity = nonediff --git a/GlazeWM.Bootstrapper/Program.cs b/GlazeWM.Bootstrapper/Program.csindex 3f82d56e..e8ebf7ac 100644--- a/GlazeWM.Bootstrapper/Program.cs+++ b/GlazeWM.Bootstrapper/Program.cs@@ -1,7 +1,11 @@using GlazeWM.Bar;using GlazeWM.Domain;using GlazeWM.Infrastructure;+using GlazeWM.Infrastructure.Logging;using Microsoft.Extensions.DependencyInjection;+using Microsoft.Extensions.Hosting;+using Microsoft.Extensions.Logging;+using Microsoft.Extensions.Logging.Console;using System;using System.Diagnostics;using System.Threading;@@ -31,22 +35,31 @@ namespace GlazeWM.Bootstrapperreturn;}- ServiceLocator.Provider = BuildServiceProvider();+ var host = CreateHost();+ ServiceLocator.Provider = host.Services;var startup = ServiceLocator.Provider.GetRequiredService<Startup>();startup.Run();Application.Run();}- private static ServiceProvider BuildServiceProvider()+ private static IHost CreateHost(){- var serviceCollection = new ServiceCollection();- serviceCollection.AddInfrastructureServices();- serviceCollection.AddDomainServices();- serviceCollection.AddBarServices();- serviceCollection.AddSingleton<Startup>();-- return serviceCollection.BuildServiceProvider();+ return Host.CreateDefaultBuilder()+ .ConfigureServices((_, services) =>+ {+ services.AddInfrastructureServices();+ services.AddDomainServices();+ services.AddBarServices();+ services.AddSingleton<Startup>();+ })+ .ConfigureLogging(builder =>+ {+ builder.ClearProviders();+ builder.AddConsole(options => options.FormatterName = "customFormatter")+ .AddConsoleFormatter<LogFormatter, ConsoleFormatterOptions>();+ })+ .Build();}}}diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.csindex bfc5f672..c3ce524e 100644--- a/GlazeWM.Bootstrapper/Startup.cs+++ b/GlazeWM.Bootstrapper/Startup.cs@@ -106,6 +106,9 @@ namespace GlazeWM.Bootstrappercontinue;}+ if (!WindowService.IsHandleManageable(windowHandle))+ continue;+// Get workspace that encompasses most of the window.var targetMonitor = _monitorService.GetMonitorFromHandleLocation(windowHandle);var targetWorkspace = targetMonitor.DisplayedWorkspace;diff --git a/GlazeWM.Bootstrapper/appsettings.json b/GlazeWM.Bootstrapper/appsettings.jsonnew file mode 100644index 00000000..12a52ad9--- /dev/null+++ b/GlazeWM.Bootstrapper/appsettings.json@@ -0,0 +1,13 @@+{+ "Logging": {+ "LogLevel": {+ "Default": "Debug"+ },+ "Console": {+ "FormatterOptions": {+ "SingleLine": true,+ "TimestampFormat": "HH:mm:ss "+ }+ }+ }+}diff --git a/GlazeWM.Domain/Common/Utils/LoggerExtensions.cs b/GlazeWM.Domain/Common/Utils/LoggerExtensions.csnew file mode 100644index 00000000..8c7c9f76--- /dev/null+++ b/GlazeWM.Domain/Common/Utils/LoggerExtensions.cs@@ -0,0 +1,20 @@+using GlazeWM.Domain.Windows;+using Microsoft.Extensions.Logging;++namespace GlazeWM.Domain.Common.Utils+{+ public static class LoggerExtensions+ {+ /// <summary>+ /// Extension method for consistently formatting window event logs.+ /// </summary>+ public static void LogWindowEvent<T>(this ILogger<T> logger, string message, Window window)+ {+ logger.LogDebug(+ $"{message}: {{processName}} {{className}}",+ window.ProcessName,+ window.ClassName+ );+ }+ }+}diff --git a/GlazeWM.Domain/GlazeWM.Domain.csproj b/GlazeWM.Domain/GlazeWM.Domain.csprojindex 0a18b56a..9b2b7b56 100644--- a/GlazeWM.Domain/GlazeWM.Domain.csproj+++ b/GlazeWM.Domain/GlazeWM.Domain.csproj@@ -11,8 +11,8 @@<ItemGroup><PackageReference- Include="Microsoft.Extensions.DependencyInjection"- Version="6.0.0" />+ Include="Microsoft.Extensions.Hosting"+ Version="6.0.1" /><PackageReferenceInclude="Newtonsoft.Json"Version="13.0.1" />diff --git a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.csindex 7f200b43..cd528200 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs@@ -1,5 +1,6 @@using System;using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors;@@ -8,6 +9,7 @@ using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi;+using Microsoft.Extensions.Logging;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Windows.CommandHandlers@@ -21,6 +23,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlersprivate readonly WindowService _windowService;private readonly MonitorService _monitorService;private readonly WorkspaceService _workspaceService;+ private readonly ILogger<AddWindowHandler> _logger;public AddWindowHandler(Bus bus,@@ -29,7 +32,8 @@ namespace GlazeWM.Domain.Windows.CommandHandlersCommandParsingService commandParsingService,WindowService windowService,MonitorService monitorService,- WorkspaceService workspaceService+ WorkspaceService workspaceService,+ ILogger<AddWindowHandler> logger){_bus = bus;@@ -39,6 +43,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers_windowService = windowService;_monitorService = monitorService;_workspaceService = workspaceService;+ _logger = logger;}public CommandResponse Handle(AddWindowCommand command)@@ -46,9 +51,6 @@ namespace GlazeWM.Domain.Windows.CommandHandlersvar windowHandle = command.WindowHandle;var shouldRedraw = command.ShouldRedraw;- if (!WindowService.IsHandleManageable(windowHandle))- return CommandResponse.Ok;-// Attach the new window as first child of the target parent (if provided), otherwise, add as// a sibling of the focused container.var (targetParent, targetIndex) = command.TargetParent != null@@ -69,6 +71,8 @@ namespace GlazeWM.Domain.Windows.CommandHandlersif (commandStrings.Contains("ignore"))return CommandResponse.Ok;+ _logger.LogWindowEvent("New window managed", window);+if (window is IResizable)_bus.Invoke(new AttachAndResizeContainerCommand(window, targetParent, targetIndex));elsediff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.csindex 2b9d3561..c0832d39 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.cs@@ -1,9 +1,11 @@using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;+using Microsoft.Extensions.Logging;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -11,11 +13,17 @@ namespace GlazeWM.Domain.Windows.EventHandlers{private readonly Bus _bus;private readonly WindowService _windowService;+ private readonly ILogger<WindowDestroyedHandler> _logger;- public WindowDestroyedHandler(Bus bus, WindowService windowService)+ public WindowDestroyedHandler(+ Bus bus,+ WindowService windowService,+ ILogger<WindowDestroyedHandler> logger+ ){_bus = bus;_windowService = windowService;+ _logger = logger;}public void Handle(WindowDestroyedEvent @event)@@ -35,6 +43,8 @@ namespace GlazeWM.Domain.Windows.EventHandlersif (window == null)return;+ _logger.LogWindowEvent("Window closed", window);+// If window is in tree, detach the removed window from its parent._bus.Invoke(new RemoveWindowCommand(window));_bus.Invoke(new RedrawContainersCommand());diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.csindex d8f163a5..bbb3adaa 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs@@ -1,4 +1,5 @@-using System.Linq;+using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Containers.Events;@@ -7,6 +8,7 @@ using GlazeWM.Domain.Workspaces;using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;+using Microsoft.Extensions.Logging;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -15,12 +17,19 @@ namespace GlazeWM.Domain.Windows.EventHandlersprivate readonly Bus _bus;private readonly WindowService _windowService;private readonly ContainerService _containerService;-- public WindowFocusedHandler(Bus bus, WindowService windowService, ContainerService containerService)+ private readonly ILogger<WindowFocusedHandler> _logger;++ public WindowFocusedHandler(+ Bus bus,+ WindowService windowService,+ ContainerService containerService,+ ILogger<WindowFocusedHandler> logger+ ){_bus = bus;_windowService = windowService;_containerService = containerService;+ _logger = logger;}public void Handle(WindowFocusedEvent @event)@@ -45,6 +54,8 @@ namespace GlazeWM.Domain.Windows.EventHandlersif (window == null)return;+ _logger.LogWindowEvent("Window focused", window);+_bus.Invoke(new SetFocusedDescendantCommand(window));_bus.RaiseEvent(new FocusChangedEvent(window));}diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.csindex daba8388..e920f33d 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs@@ -1,9 +1,11 @@using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;+using Microsoft.Extensions.Logging;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -11,11 +13,17 @@ namespace GlazeWM.Domain.Windows.EventHandlers{private readonly Bus _bus;private readonly WindowService _windowService;+ private readonly ILogger<WindowHiddenHandler> _logger;- public WindowHiddenHandler(Bus bus, WindowService windowService)+ public WindowHiddenHandler(+ Bus bus,+ WindowService windowService,+ ILogger<WindowHiddenHandler> logger+ ){_bus = bus;_windowService = windowService;+ _logger = logger;}public void Handle(WindowHiddenEvent @event)@@ -38,6 +46,8 @@ namespace GlazeWM.Domain.Windows.EventHandlersif (window?.IsHidden != false || WindowService.IsHandleVisible(window.Hwnd))return;+ _logger.LogWindowEvent("Window hidden", window);+// Detach the hidden window from its parent._bus.Invoke(new RemoveWindowCommand(window));_bus.Invoke(new RedrawContainersCommand());diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.csindex 85f8fe06..a9cfbb19 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.cs@@ -1,10 +1,12 @@using System;using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;+using Microsoft.Extensions.Logging;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -13,16 +15,19 @@ namespace GlazeWM.Domain.Windows.EventHandlersprivate readonly Bus _bus;private readonly WindowService _windowService;private readonly ContainerService _containerService;+ private readonly ILogger<WindowMinimizeEndedHandler> _logger;public WindowMinimizeEndedHandler(Bus bus,WindowService windowService,- ContainerService containerService+ ContainerService containerService,+ ILogger<WindowMinimizeEndedHandler> logger){_bus = bus;_windowService = windowService;_containerService = containerService;+ _logger = logger;}public void Handle(WindowMinimizeEndedEvent @event)@@ -33,6 +38,8 @@ namespace GlazeWM.Domain.Windows.EventHandlersif (window == null)return;+ _logger.LogWindowEvent("Window minimize ended", window);+var restoredWindow = CreateWindowFromPreviousState(window);_bus.Invoke(new ReplaceContainerCommand(restoredWindow, window.Parent, window.Index));diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.csindex 1c7c7d50..3dbd9880 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs@@ -1,4 +1,5 @@using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Windows.Commands;@@ -6,6 +7,7 @@ using GlazeWM.Domain.Workspaces;using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;+using Microsoft.Extensions.Logging;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -14,16 +16,19 @@ namespace GlazeWM.Domain.Windows.EventHandlersprivate readonly Bus _bus;private readonly WindowService _windowService;private readonly ContainerService _containerService;+ private readonly ILogger<WindowMinimizedHandler> _logger;public WindowMinimizedHandler(Bus bus,WindowService windowService,- ContainerService containerService+ ContainerService containerService,+ ILogger<WindowMinimizedHandler> logger){_bus = bus;_windowService = windowService;_containerService = containerService;+ _logger = logger;}public void Handle(WindowMinimizedEvent @event)@@ -34,6 +39,8 @@ namespace GlazeWM.Domain.Windows.EventHandlersif (window is null or MinimizedWindow)return;+ _logger.LogWindowEvent("Window minimized", window);+var workspace = WorkspaceService.GetWorkspaceFromChildContainer(window);// Move tiling windows to be direct children of workspace (in case they aren't already).diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.csindex e7783527..e0721fa2 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs@@ -1,10 +1,12 @@using System.Linq;+using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Containers.Events;using GlazeWM.Domain.Monitors;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;+using Microsoft.Extensions.Logging;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -13,16 +15,19 @@ namespace GlazeWM.Domain.Windows.EventHandlersprivate readonly Bus _bus;private readonly WindowService _windowService;private readonly MonitorService _monitorService;+ private readonly ILogger<WindowMovedOrResizedHandler> _logger;public WindowMovedOrResizedHandler(Bus bus,WindowService windowService,- MonitorService monitorService+ MonitorService monitorService,+ ILogger<WindowMovedOrResizedHandler> logger){_bus = bus;_windowService = windowService;_monitorService = monitorService;+ _logger = logger;}public void Handle(WindowMovedOrResizedEvent @event)@@ -33,6 +38,8 @@ namespace GlazeWM.Domain.Windows.EventHandlersif (window is null or not FloatingWindow)return;+ _logger.LogWindowEvent("Window moved/resized", window);+// Update state with new location of the floating window.UpdateWindowPlacement(window);diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowShownHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowShownHandler.csindex 19d657d4..bc800a33 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowShownHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowShownHandler.cs@@ -32,7 +32,7 @@ namespace GlazeWM.Domain.Windows.EventHandlers.FirstOrDefault(window => window.Hwnd == windowHandle);// Ignore cases where window is already managed.- if (window != null)+ if (window is not null || !WindowService.IsHandleManageable(windowHandle))return;_bus.Invoke(new AddWindowCommand(@event.WindowHandle));diff --git a/GlazeWM.Infrastructure/Bussing/Bus.cs b/GlazeWM.Infrastructure/Bussing/Bus.csindex fbb610c4..1ca40ac0 100644--- a/GlazeWM.Infrastructure/Bussing/Bus.cs+++ b/GlazeWM.Infrastructure/Bussing/Bus.cs@@ -1,9 +1,9 @@using GlazeWM.Infrastructure.Exceptions;using GlazeWM.Infrastructure.WindowsApi.Events;using Microsoft.Extensions.DependencyInjection;+using Microsoft.Extensions.Logging;using System;using System.Collections.Generic;-using System.Diagnostics;using System.IO;using System.Reactive.Subjects;using System.Windows.Forms;@@ -17,6 +17,12 @@ namespace GlazeWM.Infrastructure.Bussing{public readonly Subject<Event> Events = new();private readonly object _lockObj = new();+ private readonly ILogger<Bus> _logger;++ public Bus(ILogger<Bus> logger)+ {+ _logger = logger;+ }/// <summary>/// Sends command to appropriate command handler.@@ -25,7 +31,7 @@ namespace GlazeWM.Infrastructure.Bussing{try{- Debug.WriteLine($"Command {command.Name} invoked.");+ _logger.LogDebug("Command {commandName} invoked.", command.Name);// Create a `Type` object representing the constructed `ICommandHandler` generic.var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());@@ -52,8 +58,6 @@ namespace GlazeWM.Infrastructure.Bussing{try{- Debug.WriteLine($"Event {@event.Name} emitted.");-// Create a `Type` object representing the constructed `IEventHandler` generic.var handlerType = typeof(IEventHandler<>).MakeGenericType(@event.GetType());diff --git a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csprojindex d5b487c9..b1bdecb6 100644--- a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj+++ b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj@@ -8,8 +8,8 @@<ItemGroup><PackageReference- Include="Microsoft.Extensions.DependencyInjection"- Version="6.0.0" />+ Include="Microsoft.Extensions.Hosting"+ Version="6.0.1" /><PackageReferenceInclude="Newtonsoft.Json"Version="13.0.1" />diff --git a/GlazeWM.Infrastructure/Logging/LogFormatter.cs b/GlazeWM.Infrastructure/Logging/LogFormatter.csnew file mode 100644index 00000000..d697a4e9--- /dev/null+++ b/GlazeWM.Infrastructure/Logging/LogFormatter.cs@@ -0,0 +1,61 @@+using Microsoft.Extensions.Logging;+using Microsoft.Extensions.Logging.Abstractions;+using Microsoft.Extensions.Logging.Console;+using Microsoft.Extensions.Options;+using System;+using System.IO;++namespace GlazeWM.Infrastructure.Logging+{+ public sealed class LogFormatter : ConsoleFormatter, IDisposable+ {+ private readonly IDisposable _optionsReloadToken;+ private ConsoleFormatterOptions _formatterOptions;++ public LogFormatter(IOptionsMonitor<ConsoleFormatterOptions> options) : base("customFormatter")+ {+ (_optionsReloadToken, _formatterOptions)+ = (options.OnChange(ReloadLoggerOptions), options.CurrentValue);+ }++ private void ReloadLoggerOptions(ConsoleFormatterOptions options)+ {+ _formatterOptions = options;+ }++ public override void Write<T>(+ in LogEntry<T> logEntry,+ IExternalScopeProvider scopeProvider,+ TextWriter textWriter+ )+ {+ var message = logEntry.Formatter?.Invoke(logEntry.State, logEntry.Exception);++ if (message is null)+ return;++ AddTimestampPrefix(textWriter);+ AddTrimmedCategoryPrefix(textWriter, logEntry);+ textWriter.WriteLine(message);+ }++ private void AddTimestampPrefix(TextWriter textWriter)+ {+ var timestamp = _formatterOptions.UseUtcTimestamp+ ? DateTime.UtcNow+ : DateTime.Now;++ textWriter.Write($"{timestamp.ToString(_formatterOptions.TimestampFormat)}");+ }++ private static void AddTrimmedCategoryPrefix<T>(TextWriter textWriter, LogEntry<T> logEntry)+ {+ textWriter.Write($"[{logEntry.Category.Split(".")[^1]}] ");+ }++ public void Dispose()+ {+ _optionsReloadToken?.Dispose();+ }+ }+}