Skip to content

Commit

Permalink
replace direct console writes with log wrapper
Browse files Browse the repository at this point in the history
also workaround the issue with overwritten lines that's only reproducing under non-debug run
  • Loading branch information
13xforever committed Mar 3, 2024
1 parent 7d8f3d5 commit 1f187ad
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 38 deletions.
2 changes: 1 addition & 1 deletion VkDiag/Program.AppxPackages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static void CheckAppxPackages()
if (found is { Count: > 0 })
{
everythingIsFine = false;
Console.WriteLine();
WriteLogLine();
WriteLogLine(ConsoleColor.DarkYellow, "!", "Potentially incompatible software:");
foreach (var pkg in found)
WriteLogLine(ConsoleColor.DarkYellow, "!", $" {pkg.name}{pkg.version}");
Expand Down
8 changes: 3 additions & 5 deletions VkDiag/Program.GpuDriverInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ private static bool CheckGpuDrivers()
{
if (videoKey == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed to enumerate GPU drivers");
Console.ForegroundColor = defaultFgColor;
WriteLogLine(ConsoleColor.Red, "Failed to enumerate GPU drivers");
return false;
}

Expand All @@ -66,8 +64,8 @@ private static bool CheckGpuDrivers()
}
}
}
Console.WriteLine();
Console.WriteLine($"Found {gpuGuidList.Count} active GPU{(gpuGuidList.Count == 1 ? "" : "s")}:");
WriteLogLine();
WriteLogLine($"Found {gpuGuidList.Count} active GPU{(gpuGuidList.Count == 1 ? "" : "s")}:");
foreach (var gpuGuid in inactiveGpuGuidList.Concat(gpuGuidList))
using (var gpuKey = videoKey.OpenSubKey(gpuGuid))
{
Expand Down
40 changes: 32 additions & 8 deletions VkDiag/Program.Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,42 @@ namespace VkDiag;
internal static partial class Program
{
private static readonly ConsoleColor defaultFgColor = Console.ForegroundColor;

private static readonly object theDoor = new();

private static void WriteLogLine(ConsoleColor statusColor, string status, string description)
{
description = description ?? "???";
var val = description.TrimStart();
string prefix = "";
var prefix = "";
if (val.Length < description.Length)
prefix = description.Substring(0, description.Length - val.Length);
Console.Write(prefix + '[');
Console.ForegroundColor = statusColor;
Console.Write(status);
Console.ForegroundColor = defaultFgColor;
Console.WriteLine("] " + val);

lock (theDoor)
{
Console.Write(prefix + '[');
Console.ForegroundColor = statusColor;
Console.Write(status);
Console.ForegroundColor = defaultFgColor;
Console.WriteLine("] " + val);
}
}

private static void WriteLogLine(ConsoleColor statusColor, string line)
{
lock (theDoor)
{
Console.ForegroundColor = statusColor;
Console.WriteLine(line);
Console.ForegroundColor = defaultFgColor;
}
}

private static void WriteLogLine(string line)
{
lock (theDoor) Console.WriteLine(line);
}

private static void WriteLogLine()
{
lock (theDoor) Console.WriteLine('\u200b'); // zero width space to workaround bug with emitted \r instead of \r\n
}
}
5 changes: 2 additions & 3 deletions VkDiag/Program.VulkanMetaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ internal static partial class Program

private static void CheckVulkanMeta()
{
Console.WriteLine();
Console.WriteLine("Vulkan registration information:");

WriteLogLine();
WriteLogLine("Vulkan registration information:");
var basePaths = new[] {@"SOFTWARE\Khronos\Vulkan", @"SOFTWARE\WOW6432Node\Khronos\Vulkan"};
var broken = false;
var removedBroken = true;
Expand Down
42 changes: 21 additions & 21 deletions VkDiag/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async Task Main(string[] args)

if (!Environment.Is64BitOperatingSystem)
{
Console.WriteLine("Only 64-bit OS is supported");
WriteLogLine(ConsoleColor.Red, "Only 64-bit OS is supported");
Environment.Exit(-1);
}

Expand All @@ -61,8 +61,8 @@ public static async Task Main(string[] args)
var hasInactiveGpus = CheckGpuDrivers();
if (hasInactiveGpus && osVer.Major >= 10)
{
Console.WriteLine();
Console.WriteLine("User GPU Preferences:");
WriteLogLine();
WriteLogLine("User GPU Preferences:");
try
{
if (!HasPerformanceModeProfile())
Expand Down Expand Up @@ -138,11 +138,11 @@ private static void GetOptions(string[] args)

if (help)
{
Console.WriteLine("RPCS3 Vulkan diagnostics tool");
Console.WriteLine("Usage:");
Console.WriteLine(" vkdiag [OPTIONS]");
Console.WriteLine("Available options:");
options.WriteOptionDescriptions(Console.Out);
WriteLogLine("RPCS3 Vulkan diagnostics tool");
WriteLogLine("Usage:");
WriteLogLine(" vkdiag [OPTIONS]");
WriteLogLine("Available options:");
lock (theDoor) options.WriteOptionDescriptions(Console.Out);
Environment.Exit(0);
}
}
Expand All @@ -158,9 +158,9 @@ private static void Restart(bool onlyToElevate = true, bool requireElevation = t
return;

if (requireElevation)
Console.WriteLine("Restarting with elevated permissions...");
WriteLogLine("Restarting with elevated permissions...");
else
Console.WriteLine("Restarting...");
WriteLogLine("Restarting...");
var args = "";
if (autofix)
args += " -f";
Expand Down Expand Up @@ -203,16 +203,16 @@ private static void ShowMenu()
menu.Add(('n', "Do nothing and exit (default)"));
var validResponses = new HashSet<char>{'\r', '\n'};

Console.WriteLine();
Console.WriteLine("Remember to screenshot or copy this screen content for support.");
Console.WriteLine();
Console.WriteLine("There are some issues, what would you like to do?");
WriteLogLine();
WriteLogLine("Remember to screenshot or copy this screen content for support.");
WriteLogLine();
WriteLogLine("There are some issues, what would you like to do?");
foreach (var (key, prompt) in menu)
{
WriteLogLine(ConsoleColor.Cyan, key.ToString(), prompt);
validResponses.Add(key);
}
Console.Write("Selected option: ");
lock (theDoor) Console.Write("Selected option: ");
char result;
do
{
Expand Down Expand Up @@ -247,13 +247,13 @@ private static void ShowMenu()
}

if (everythingIsFine)
Console.WriteLine("Everything seems to be fine.");
WriteLogLine("Everything seems to be fine.");
else
Console.WriteLine("There are some issues that require manual checks and/or fixes.");
Console.WriteLine();
Console.WriteLine("Remember to screenshot or copy this screen content for support.");
Console.WriteLine();
Console.WriteLine("Press any key to exit the tool...");
WriteLogLine("There are some issues that require manual checks and/or fixes.");
WriteLogLine();
WriteLogLine("Remember to screenshot or copy this screen content for support.");
WriteLogLine();
WriteLogLine("Press any key to exit the tool...");
Console.ReadKey();
Environment.Exit(0);
}
Expand Down

0 comments on commit 1f187ad

Please sign in to comment.