Skip to content

Commit

Permalink
feat: add command history to error logs (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Feb 2, 2023
1 parent feae4ad commit e4ab7cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion GlazeWM.Bootstrapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
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;
Expand Down Expand Up @@ -69,7 +71,7 @@ private static IHost CreateHost(string[] args)
// 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),
Expand All @@ -83,8 +85,14 @@ private static IHost CreateHost(string[] args)
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";
};
});
Expand Down
7 changes: 7 additions & 0 deletions GlazeWM.Infrastructure/Bussing/Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +31,12 @@ public CommandResponse Invoke<T>(T command) where T : Command
{
_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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CommandResponse Handle(HandleFatalExceptionCommand command)
{
// Alert the user of the error.
var exception = command.Exception;
// TODO: Show foreground window.
MessageBox.Show($"Unhandled exception: {exception.Message}");

WriteToErrorLog(exception);
Expand Down

0 comments on commit e4ab7cc

Please sign in to comment.