From c983a4b32c803890f78d41c2cd25680766c57afb Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Sun, 1 Aug 2021 14:48:19 -0600 Subject: [PATCH 1/9] Move console write to their own thread --- Libraries/Log.cs | 55 +++++++++++++++++++------------- Libraries/ToTripleSlashPorter.cs | 6 ++-- Program/DocsPortingTool.cs | 6 +++- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index 8d759fd..5d173d2 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -1,47 +1,56 @@ #nullable enable using System; +using System.Diagnostics; +using System.Threading.Tasks.Dataflow; namespace Libraries { - internal class Log + public static class Log { - private static void WriteLine(string format, params object[]? args) + private static BufferBlock<(ConsoleColor, string, object[]?)> bufferWrite = new(); + + public static async Task StartAsync() { - if (args == null || args.Length == 0) - { - Console.WriteLine(format); - } - else + ConsoleColor initialForeground = Console.ForegroundColor; + + while (await bufferWrite.OutputAvailableAsync()) { - Console.WriteLine(format, args); + (ConsoleColor, string, object[]) t = await bufferWrite.ReceiveAsync(); + + if (t.Item1 != (ConsoleColor)(-1)) + { + if (Console.ForegroundColor != t.Item1) + Console.ForegroundColor = t.Item1; + } + + if (t.Item3 == null) + { + Console.Write(t.Item2); + } + else + { + Console.Write(t.Item2, t.Item3); + } } + + Console.ForegroundColor = initialForeground; } - private static void Write(string format, params object[]? args) + public static void Finished() { - if (args == null || args.Length == 0) - { - Console.Write(format); - } - else - { - Console.Write(format, args); - } + bufferWrite.Complete(); } public static void Print(bool endline, ConsoleColor foregroundColor, string format, params object[]? args) { - ConsoleColor initialColor = Console.ForegroundColor; - Console.ForegroundColor = foregroundColor; if (endline) { - WriteLine(format, args); + bufferWrite.Post((foregroundColor, format + Environment.NewLine, args)); } else { - Write(format, args); + bufferWrite.Post((foregroundColor, format, args)); } - Console.ForegroundColor = initialColor; } public static void Info(string format) @@ -174,7 +183,7 @@ public static void Assert(bool endline, bool condition, string format, params ob public static void Line() { - Console.WriteLine(); + Print(endline: true, (ConsoleColor)(-1), "", null); } public delegate void PrintHelpFunction(); diff --git a/Libraries/ToTripleSlashPorter.cs b/Libraries/ToTripleSlashPorter.cs index 2780ec2..3d5246b 100644 --- a/Libraries/ToTripleSlashPorter.cs +++ b/Libraries/ToTripleSlashPorter.cs @@ -186,9 +186,8 @@ private void AddSymbolLocationsToResolvedLocations(ProjectInformation projectInf Log.Info(false, $"Symbol '{symbol.Name}' not found in locations of project '{path}'."); if (n < symbol.Locations.Count()) { - Log.Info(false, " Trying the next location..."); + Log.Info(true, " Trying the next location..."); } - Console.WriteLine(); } n++; } @@ -245,9 +244,8 @@ private void FindSymbolInReferencedProjects(DocsType docsType, IEnumerable Date: Sun, 1 Aug 2021 17:20:50 -0600 Subject: [PATCH 2/9] Batch console writes --- Libraries/Log.cs | 61 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index 5d173d2..f73eaa0 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -1,6 +1,7 @@ #nullable enable using System; using System.Diagnostics; +using System.Text; using System.Threading.Tasks.Dataflow; namespace Libraries @@ -12,28 +13,60 @@ public static class Log public static async Task StartAsync() { ConsoleColor initialForeground = Console.ForegroundColor; + ConsoleColor foreground = initialForeground; // cheaper than reading it each time + + StringBuilder combined = new(); + + bool unwrittenBlob = false; + (ConsoleColor color, string msg, object[]? args) blob = new ((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this + + Stopwatch sw = Stopwatch.StartNew(); while (await bufferWrite.OutputAvailableAsync()) { - (ConsoleColor, string, object[]) t = await bufferWrite.ReceiveAsync(); - - if (t.Item1 != (ConsoleColor)(-1)) + while (unwrittenBlob || await bufferWrite.OutputAvailableAsync()) { - if (Console.ForegroundColor != t.Item1) - Console.ForegroundColor = t.Item1; + if (unwrittenBlob && foreground != blob.color) + { + Console.ForegroundColor = blob.color; + foreground = blob.color; + } + + if (!unwrittenBlob) + { + blob = await bufferWrite.ReceiveAsync(); + + if (blob.color != (ConsoleColor)(-1) && foreground != blob.color) + { + unwrittenBlob = true; // New color - emit what we have + break; + } + } + + if (blob.args == null) + { + combined.Append(blob.msg); + } + else + { + combined.AppendFormat(blob.msg, blob.args); + } + + unwrittenBlob = false; + + if (sw.ElapsedMilliseconds > 1000) + break; } - if (t.Item3 == null) - { - Console.Write(t.Item2); - } - else - { - Console.Write(t.Item2, t.Item3); - } + sw.Restart(); + + Console.Write(combined); + + combined = combined.Length < ushort.MaxValue ? combined.Clear() : new StringBuilder(); } - Console.ForegroundColor = initialForeground; + if (foreground != initialForeground) + Console.ForegroundColor = initialForeground; } public static void Finished() From ccb0a52b73eeff23dc10c7c0f50c8bc6a10ed288 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Sun, 1 Aug 2021 21:31:29 -0600 Subject: [PATCH 3/9] Log to a file --- Libraries/Log.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index f73eaa0..41e6b35 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -12,15 +12,18 @@ public static class Log public static async Task StartAsync() { + using FileStream fs = new(Path.GetTempFileName(), FileMode.Open); + using StreamWriter sw = new(fs); + ConsoleColor initialForeground = Console.ForegroundColor; ConsoleColor foreground = initialForeground; // cheaper than reading it each time - StringBuilder combined = new(); + StringBuilder combined = new(65_536); bool unwrittenBlob = false; (ConsoleColor color, string msg, object[]? args) blob = new ((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this - Stopwatch sw = Stopwatch.StartNew(); + Stopwatch stopwatch = Stopwatch.StartNew(); while (await bufferWrite.OutputAvailableAsync()) { @@ -46,6 +49,7 @@ public static async Task StartAsync() if (blob.args == null) { combined.Append(blob.msg); + } else { @@ -54,19 +58,22 @@ public static async Task StartAsync() unwrittenBlob = false; - if (sw.ElapsedMilliseconds > 1000) + if (stopwatch.ElapsedMilliseconds > 1000) break; } - sw.Restart(); + stopwatch.Restart(); Console.Write(combined); + sw.Write(combined); - combined = combined.Length < ushort.MaxValue ? combined.Clear() : new StringBuilder(); + combined = combined.Length < 65_536 ? combined.Clear() : new StringBuilder(); } if (foreground != initialForeground) Console.ForegroundColor = initialForeground; + + Console.WriteLine("Written log to {0}", fs.Name); } public static void Finished() From 8ab0a150c9dd5c7320f00c7201a8fe16d9e3d3a8 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 2 Aug 2021 16:57:17 -0600 Subject: [PATCH 4/9] Update Libraries/Log.cs Co-authored-by: Jeff Handley --- Libraries/Log.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index 41e6b35..0ebdfa6 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -21,7 +21,7 @@ public static async Task StartAsync() StringBuilder combined = new(65_536); bool unwrittenBlob = false; - (ConsoleColor color, string msg, object[]? args) blob = new ((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this + (ConsoleColor color, string msg, object[]? args) blob = new((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this Stopwatch stopwatch = Stopwatch.StartNew(); @@ -498,4 +498,4 @@ the interface API. "); } } -} \ No newline at end of file +} From 97cf49f9f860d4feeab6b642e5f774eb79a9c6ca Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 2 Aug 2021 16:57:23 -0600 Subject: [PATCH 5/9] Update Libraries/Log.cs Co-authored-by: Jeff Handley --- Libraries/Log.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index 0ebdfa6..e4b5a32 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -49,7 +49,6 @@ public static async Task StartAsync() if (blob.args == null) { combined.Append(blob.msg); - } else { From 4621ab0d1c93ca78f10726b52ed8e9d5f00310c4 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 2 Aug 2021 21:50:54 -0600 Subject: [PATCH 6/9] Change to Channels --- Libraries/Log.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index e4b5a32..15e3a1c 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -2,13 +2,13 @@ using System; using System.Diagnostics; using System.Text; -using System.Threading.Tasks.Dataflow; +using System.Threading.Channels; namespace Libraries { public static class Log { - private static BufferBlock<(ConsoleColor, string, object[]?)> bufferWrite = new(); + private static Channel<(ConsoleColor, string, object[]?)> channel = Channel.CreateUnbounded<(ConsoleColor, string, object[]?)>(); public static async Task StartAsync() { @@ -25,9 +25,9 @@ public static async Task StartAsync() Stopwatch stopwatch = Stopwatch.StartNew(); - while (await bufferWrite.OutputAvailableAsync()) + while (await channel.Reader.WaitToReadAsync()) { - while (unwrittenBlob || await bufferWrite.OutputAvailableAsync()) + while (unwrittenBlob || await channel.Reader.WaitToReadAsync()) { if (unwrittenBlob && foreground != blob.color) { @@ -37,7 +37,7 @@ public static async Task StartAsync() if (!unwrittenBlob) { - blob = await bufferWrite.ReceiveAsync(); + blob = await channel.Reader.ReadAsync(); if (blob.color != (ConsoleColor)(-1) && foreground != blob.color) { @@ -77,18 +77,18 @@ public static async Task StartAsync() public static void Finished() { - bufferWrite.Complete(); + channel.Writer.Complete(); } public static void Print(bool endline, ConsoleColor foregroundColor, string format, params object[]? args) { if (endline) { - bufferWrite.Post((foregroundColor, format + Environment.NewLine, args)); + channel.Writer.WriteAsync((foregroundColor, format + Environment.NewLine, args)); } else { - bufferWrite.Post((foregroundColor, format, args)); + channel.Writer.WriteAsync((foregroundColor, format, args)); } } From e96b84b877c8889db8263210049012a800cea003 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 2 Aug 2021 23:39:10 -0600 Subject: [PATCH 7/9] Update Log.cs --- Libraries/Log.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index 15e3a1c..694ab76 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Text; using System.Threading.Channels; +using System.Threading.Tasks; namespace Libraries { From d97de977d401a185b8be9eba1247cbd14f9da283 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 3 Aug 2021 08:01:05 -0600 Subject: [PATCH 8/9] Update Log.cs --- Libraries/Log.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Libraries/Log.cs b/Libraries/Log.cs index 694ab76..e555484 100644 --- a/Libraries/Log.cs +++ b/Libraries/Log.cs @@ -1,6 +1,7 @@ #nullable enable using System; using System.Diagnostics; +using System.IO; using System.Text; using System.Threading.Channels; using System.Threading.Tasks; From 2e5691e737e87b7c925c126d9e92a0ec3e7c8bb0 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 3 Aug 2021 08:05:04 -0600 Subject: [PATCH 9/9] Update DocsPortingTool.cs --- Program/DocsPortingTool.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Program/DocsPortingTool.cs b/Program/DocsPortingTool.cs index 37d67ce..08d7ed1 100644 --- a/Program/DocsPortingTool.cs +++ b/Program/DocsPortingTool.cs @@ -1,6 +1,7 @@ #nullable enable using Libraries; using System; +using System.Threading.Tasks; namespace DocsPortingTool {