Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: add command history to error logs (#237)

lars-bergere4ab7cc8647f
diff --git a/GlazeWM.Bootstrapper/Program.cs b/GlazeWM.Bootstrapper/Program.cs
index 45020cb7..b9db405d 100644
--- a/GlazeWM.Bootstrapper/Program.cs
+++ b/GlazeWM.Bootstrapper/Program.cs
@@ -2,12 +2,14 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
+using System.Linq;
using System.Text.Json.Serialization;
using System.Threading;
using GlazeWM.Bar;
using GlazeWM.Domain;
using GlazeWM.Domain.Containers;
using GlazeWM.Infrastructure;
+using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Exceptions;
using GlazeWM.Infrastructure.Logging;
using GlazeWM.Infrastructure.Serialization;
@@ -69,7 +71,7 @@ namespace GlazeWM.Bootstrapper
// Configure exception handler.
services
.AddOptions<ExceptionHandlingOptions>()
- .Configure<ContainerService, JsonService>((options, containerService, jsonService) =>
+ .Configure<Bus, ContainerService, JsonService>((options, bus, containerService, jsonService) =>
{
options.ErrorLogPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
@@ -83,8 +85,14 @@ namespace GlazeWM.Bootstrapper
new List<JsonConverter> { new JsonContainerConverter() }
);
+ // History of latest command invocations. Most recent is first.
+ var commandHistory = bus.CommandHistory
+ .Select(command => command.Name)
+ .Reverse();
+
return $"{DateTime.Now}\n"
+ $"{exception}\n"
+ + $"Command history: {string.Join(", ", commandHistory)} \n"
+ $"State dump: {stateDump}\n\n";
};
});
diff --git a/GlazeWM.Infrastructure/Bussing/Bus.cs b/GlazeWM.Infrastructure/Bussing/Bus.cs
index 3ebffad7..46eba713 100644
--- a/GlazeWM.Infrastructure/Bussing/Bus.cs
+++ b/GlazeWM.Infrastructure/Bussing/Bus.cs
@@ -13,6 +13,7 @@ namespace GlazeWM.Infrastructure.Bussing
public sealed class Bus
{
public readonly Subject<Event> Events = new();
+ public readonly Queue<Command> CommandHistory = new();
public readonly object LockObj = new();
private readonly ILogger<Bus> _logger;
@@ -30,6 +31,12 @@ namespace GlazeWM.Infrastructure.Bussing
{
_logger.LogDebug("Command {CommandName} invoked.", command.Name);
+ CommandHistory.Enqueue(command);
+
+ // Maintain a history of the last 15 command invocations for debugging.
+ if (CommandHistory.Count > 15)
+ CommandHistory.Dequeue();
+
// Create a `Type` object representing the constructed `ICommandHandler` generic.
var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
diff --git a/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs b/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs
index 4587a565..8afe4d99 100644
--- a/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs
+++ b/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs
@@ -23,6 +23,7 @@ namespace GlazeWM.Infrastructure.Common.CommandHandlers
{
// Alert the user of the error.
var exception = command.Exception;
+ // TODO: Show foreground window.
MessageBox.Show($"Unhandled exception: {exception.Message}");
WriteToErrorLog(exception);