From bed591d890f5be69b5178d5f8d12df75bfe636fe Mon Sep 17 00:00:00 2001 From: KazWolfe Date: Thu, 9 Jan 2025 13:11:41 -0800 Subject: [PATCH] fix: Forcefully load stacktrace if assert UI is shown (#2164) - Add DiagnosticUtil to automatically filter out the "boring" stack entries. Co-authored-by: goat <16760685+goaaats@users.noreply.github.com> --- .../Internal/Asserts/AssertHandler.cs | 5 ++- Dalamud/Utility/DiagnosticUtil.cs | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 Dalamud/Utility/DiagnosticUtil.cs diff --git a/Dalamud/Interface/Internal/Asserts/AssertHandler.cs b/Dalamud/Interface/Internal/Asserts/AssertHandler.cs index 376aaed5b..56050cdfb 100644 --- a/Dalamud/Interface/Internal/Asserts/AssertHandler.cs +++ b/Dalamud/Interface/Internal/Asserts/AssertHandler.cs @@ -82,7 +82,7 @@ internal class AssertHandler : IDisposable if (!this.ShowAsserts && !this.everShownAssertThisSession) return; - Lazy stackTrace = new(() => new StackTrace(3).ToString()); + Lazy stackTrace = new(() => DiagnosticUtil.GetUsefulTrace(new StackTrace()).ToString()); if (!this.EnableVerboseLogging) { @@ -133,6 +133,9 @@ internal class AssertHandler : IDisposable return $"https://github.com/{userName}/{repoName}/blob/{branch}/{fileName}#L{line}"; } + // grab the stack trace now that we've decided to show UI. + _ = stackTrace.Value; + var gitHubUrl = GetRepoUrl(); var showOnGitHubButton = new TaskDialogButton { diff --git a/Dalamud/Utility/DiagnosticUtil.cs b/Dalamud/Utility/DiagnosticUtil.cs new file mode 100644 index 000000000..9c9718c4e --- /dev/null +++ b/Dalamud/Utility/DiagnosticUtil.cs @@ -0,0 +1,32 @@ +using System.Diagnostics; +using System.Linq; + +namespace Dalamud.Utility; + +/// +/// A set of utilities for diagnostics. +/// +public static class DiagnosticUtil +{ + private static readonly string[] IgnoredNamespaces = [ + nameof(System), + nameof(ImGuiNET.ImGuiNative) + ]; + + /// + /// Gets a stack trace that filters out irrelevant frames. + /// + /// The source stacktrace to filter. + /// Returns a stack trace with "extra" frames removed. + public static StackTrace GetUsefulTrace(StackTrace source) + { + var frames = source.GetFrames().SkipWhile( + f => + { + var frameNs = f.GetMethod()?.DeclaringType?.Namespace; + return frameNs == null || IgnoredNamespaces.Any(i => frameNs.StartsWith(i, true, null)); + }); + + return new StackTrace(frames); + } +}