Repository navigation
feat: diagnostics screen with a copyable report #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace ConsoleMode.Services; | ||
|
|
||
| public sealed record DiagnosticsSystem(string AppVersion, string Channel, string Windows, string Runtime); | ||
|
|
||
| /// <param name="Mode">"3840 × 2160 @ 120 Hz", or empty when unknown.</param> | ||
| /// <param name="Hdr">"on" | "off" | "unsupported" | "" (unknown).</param> | ||
| public sealed record DiagnosticsDisplay(string Name, string Mode, string Hdr, bool IsActive, bool IsPrimary, bool IsFocus, bool IsHidden); | ||
|
|
||
| public sealed record DiagnosticsAudio(string Name, bool IsDefault, bool IsConfigured); | ||
|
|
||
| /// <summary> | ||
| /// One connected controller. <paramref name="Detail"/> is free text printed after the type | ||
| /// (e.g. a battery level); the collector leaves it empty for now. | ||
| /// </summary> | ||
| public sealed record DiagnosticsController(string Name, string Type, string? Detail = null); | ||
|
|
||
| /// <param name="Path">Null when the program wasn't found.</param> | ||
| public sealed record DiagnosticsApp(string Name, string? Path, string? Version = null); | ||
|
|
||
| /// <summary>Everything the report shows. A null section is one that failed to load ("unavailable").</summary> | ||
| public sealed class DiagnosticsData | ||
| { | ||
| public DiagnosticsSystem? System { get; init; } | ||
| public IReadOnlyList<DiagnosticsDisplay>? Displays { get; init; } | ||
| public IReadOnlyList<DiagnosticsAudio>? Audio { get; init; } | ||
| public IReadOnlyList<DiagnosticsController>? Controllers { get; init; } | ||
| public IReadOnlyList<DiagnosticsApp>? Apps { get; init; } | ||
| public string? FullscreenMode { get; init; } | ||
| public IReadOnlyList<string>? Log { get; init; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Settings → Diagnostics: the plain-text report users paste into an issue. English on purpose | ||
| /// (it's read by whoever answers the issue), and redacted: no user name in paths, no secrets. | ||
| /// </summary> | ||
| public static class DiagnosticsReport | ||
| { | ||
| public const string Unavailable = "unavailable"; | ||
| public const int LogLines = 50; | ||
|
|
||
| public static string Format(DiagnosticsData data) | ||
| { | ||
| var sb = new StringBuilder(); | ||
|
|
||
| Section(sb, "Console Mode", data.System is null ? null : [ | ||
| $"Version: {data.System.AppVersion} ({data.System.Channel})", | ||
| $"Windows: {data.System.Windows}", | ||
| $".NET: {data.System.Runtime}" | ||
| ]); | ||
|
|
||
| Section(sb, "Displays", data.Displays?.Select(d => | ||
| { | ||
| var tags = new List<string>(); | ||
| if (d.IsFocus) tags.Add("game screen"); | ||
| if (d.IsHidden) tags.Add("hidden in console mode"); | ||
| if (d.IsPrimary) tags.Add("primary"); | ||
| if (!d.IsActive) tags.Add("off"); | ||
| var mode = d.Mode.Length > 0 ? d.Mode : "mode unknown"; | ||
| var hdr = d.Hdr.Length > 0 ? $", HDR {d.Hdr}" : ""; | ||
| var tagText = tags.Count > 0 ? $" [{string.Join(", ", tags)}]" : ""; | ||
| return $"- {d.Name}: {mode}{hdr}{tagText}"; | ||
| })); | ||
|
|
||
| Section(sb, "Audio outputs", data.Audio?.Select(a => | ||
| { | ||
| var tags = new List<string>(); | ||
| if (a.IsDefault) tags.Add("Windows default"); | ||
| if (a.IsConfigured) tags.Add("chosen in Console Mode"); | ||
| return $"- {a.Name}{(tags.Count > 0 ? $" [{string.Join(", ", tags)}]" : "")}"; | ||
| })); | ||
|
|
||
| Section(sb, "Controllers", data.Controllers?.Select(c => | ||
| $"- {c.Name}: {c.Type}{(string.IsNullOrWhiteSpace(c.Detail) ? "" : $", {c.Detail}")}")); | ||
|
|
||
| IEnumerable<string>? apps = data.Apps?.Select(a => | ||
| a.Path is null ? $"- {a.Name}: not found" | ||
| : $"- {a.Name}: {a.Path}{(string.IsNullOrWhiteSpace(a.Version) ? "" : $" ({a.Version})")}"); | ||
| if (apps is not null && !string.IsNullOrEmpty(data.FullscreenMode)) | ||
| apps = apps.Prepend($"Launches: {data.FullscreenMode}"); | ||
| Section(sb, "Programs", apps); | ||
|
|
||
| Section(sb, $"Log (last {LogLines} lines)", data.Log?.TakeLast(LogLines)); | ||
|
|
||
| return Redact(sb.ToString().TrimEnd() + Environment.NewLine); | ||
| } | ||
|
|
||
| private static void Section(StringBuilder sb, string title, IEnumerable<string>? lines) | ||
| { | ||
| if (sb.Length > 0) sb.AppendLine(); | ||
| sb.AppendLine($"== {title} =="); | ||
| if (lines is null) | ||
| { | ||
| sb.AppendLine(Unavailable); | ||
| return; | ||
| } | ||
| var any = false; | ||
| foreach (var line in lines) | ||
| { | ||
| sb.AppendLine(line); | ||
| any = true; | ||
| } | ||
| if (!any) sb.AppendLine("(none)"); | ||
| } | ||
|
|
||
| private static readonly Regex UserPath = new(@"(?i)\b([a-z]:[\\/]+(?:users|documents and settings)[\\/]+)[^\\/\r\n""']+", RegexOptions.Compiled); | ||
| private static readonly Regex Pem = new(@"-----BEGIN [A-Z ]+-----.*?(-----END [A-Z ]+-----|$)", RegexOptions.Compiled | RegexOptions.Singleline); | ||
| private static readonly Regex Dpapi = new(SecretPrefix + @"[A-Za-z0-9+/=]+", RegexOptions.Compiled); | ||
| private static readonly Regex NamedSecret = new( | ||
| @"(?i)\b(token|password|passwd|secret|api[_-]?key|private[_-]?key|pairing[_-]?key|auth[_-]?key|authorization)(""?\s*[:=]\s*""?)(?:bearer\s+)?[^\s"",;]+", | ||
| RegexOptions.Compiled); | ||
|
|
||
| // SecretProtector.Prefix, repeated so this file stays free of DPAPI (and testable anywhere). | ||
| private const string SecretPrefix = "dpapi:"; | ||
|
|
||
| /// <summary>Hides the Windows user name in profile paths and anything that looks like a key or token.</summary> | ||
| public static string Redact(string text) | ||
| { | ||
| text = Pem.Replace(text, "[redacted key]"); | ||
| text = Dpapi.Replace(text, SecretPrefix + "[redacted]"); | ||
| text = NamedSecret.Replace(text, "$1$2[redacted]"); | ||
| return UserPath.Replace(text, "$1…"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only matches drive-letter profile paths. A roaming or network profile can produce a UNC path such as
\\server\Users\alice\...in an app path or log line, leaving the account name visible in the copied report despite the privacy guarantee. Please redact UNCUsers/Documents and Settingsprofile prefixes too and cover that form with a test.Technical details