8000 Merge pull request #75 from danmoseley/speed.up.logging · dotnet/api-docs-sync@8e30f01 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8e30f01

Browse files
authored
Merge pull request #75 from danmoseley/speed.up.logging
2 parents bcbb8d3 + 2e5691e commit 8e30f01

File tree

3 files changed

+82
-29
lines changed

3 files changed

+82
-29
lines changed

Libraries/Log.cs

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,97 @@
11
#nullable enable
22
using System;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Text;
6+
using System.Threading.Channels;
7+
using System.Threading.Tasks;
38

49
namespace Libraries
510
{
6-
internal class Log
11+
public static class Log
712
{
8-
private static void WriteLine(string format, params object[]? args)
13+
private static Channel<(ConsoleColor, string, object[ 10000 ]?)> channel = Channel.CreateUnbounded<(ConsoleColor, string, object[]?)>();
14+
15+
public static async Task StartAsync()
916
{
10-
if (args == null || args.Length == 0)
11-
{
12-
Console.WriteLine(format);
13-
}
14-
else
17+
using FileStream fs = new(Path.GetTempFileName(), FileMode.Open);
18+
using StreamWriter sw = new(fs);
19+
20+
ConsoleColor initialForeground = Console.ForegroundColor;
21+
ConsoleColor foreground = initialForeground; // cheaper than reading it each time
22+
23+
StringBuilder combined = new(65_536);
24+
25+
bool unwrittenBlob = false;
26+
(ConsoleColor color, string msg, object[]? args) blob = new((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this
27+
28+
Stopwatch stopwatch = Stopwatch.StartNew();
29+
30+
while (await channel.Reader.WaitToReadAsync())
1531
{
16-
Console.WriteLine(format, args);
32+
while (unwrittenBlob || await channel.Reader.WaitToReadAsync())
33+
{
34+
if (unwrittenBlob && foreground != blob.color)
35+
{
36+
Console.ForegroundColor = blob.color;
37+
foreground = blob.color;
38+
}
39+
40+
if (!unwrittenBlob)
41+
{
42+
blob = await channel.Reader.ReadAsync();
43+
44+
if (blob.color != (ConsoleColor)(-1) && foreground != blob.color)
45+
{
46+
unwrittenBlob = true; // New color - emit what we have
47+
break;
48+
}
49+
}
50+
51+
if (blob.args == null)
52+
{
53+
combined.Append(blob.msg);
54+
}
55+
else
56+
{
57+
combined.AppendFormat(blob.msg, blob.args);
58+
}
59+
60+
unwrittenBlob = false;
61+
62+
if (stopwatch.ElapsedMilliseconds > 1000)
63+
break;
64+
}
65+
66+
stopwatch.Restart();
67+
68+
Console.Write(combined);
69+
sw.Write(combined);
70+
71+
combined = combined.Length < 65_536 ? combined.Clear() : new StringBuilder();
1772
}
73+
74+
if (foreground != initialForeground)
75+
Console.ForegroundColor = initialForeground;
76+
77+
Console.WriteLine("Written log to {0}", fs.Name);
1878
}
1979

20-
private static void Write(string format, params object[]? args)
80+
public static void Finished()
2181
{
22-
if (args == null || args.Length == 0)
23-
{
24-
Console.Write(format);
25-
}
26-
else
27-
{
28-
Console.Write(format, args);
29-
}
82+
channel.Writer.Complete();
3083
}
3184

3285
public static void Print(bool endline, ConsoleColor foregroundColor, string format, params object[]? args)
3386
{
34-
ConsoleColor initialColor = Console.ForegroundColor;
35-
Console.ForegroundColor = foregroundColor;
3687
if (endline)
3788
{
38-
WriteLine(format, args);
89+
channel.Writer.WriteAsync((foregroundColor, format + Environment.NewLine, args));
3990
}
4091
else
4192
{
42-
Write(format, args);
93+
channel.Writer.WriteAsync((foregroundColor, format, args));
4394
}
44-
Console.ForegroundColor = initialColor;
4595
}
4696

4797
public static void Info(string format)
@@ -174,7 +224,7 @@ public static void Assert(bool endline, bool condition, string format, params ob
174224

175225
public static void Line()
176226
{
177-
Console.WriteLine();
227+
Print(endline: true, (ConsoleColor)(-1), "", null);
178228
}
179229

180230
public delegate void PrintHelpFunction();
@@ -449,4 +499,4 @@ the interface API.
449499
");
450500
}
451501
}
452-
}
502+
}

Libraries/ToTripleSlashPorter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ private void AddSymbolLocationsToResolvedLocations(ProjectInformation projectInf
186186
Log.Info(false, $"Symbol '{symbol.Name}' not found in locations of project '{path}'.");
187187
if (n < symbol.Locations.Count())
188188
{
189-
Log.Info(false, " Trying the next location...");
189+
Log.Info(true, " Trying the next location...");
190190
}
191-
Console.WriteLine();
192191
}
193192
n++;
194193
}
@@ -245,9 +244,8 @@ private void FindSymbolInReferencedProjects(DocsType docsType, IEnumerable<Proje
245244
Log.Info(false, $"Symbol for '{docsType.FullName}' not found in referenced project '{projectPath}'.");
246245
if (n < projectReferences.Count())
247246
{
248-
Log.Info(false, $" Trying the next project...");
247+
Log.Info(true, $" Trying the next project...");
249248
}
250-
Console.WriteLine();
251249
}
252250
n++;
253251
}

Program/DocsPortingTool.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#nullable enable
22
using Libraries;
33
using System;
4+
using System.Threading.Tasks;
45

56
namespace DocsPortingTool
67
{
78
class DocsPortingTool
89
{
9-
public static void Main(string[] args)
10+
public static async Task Main(string[] args)
1011
{
12+
Task loggingTask = Log.StartAsync();
1113
Configuration config = Configuration.GetCLIArgumentsForDocsPortingTool(args);
1214
switch (config.Direction)
1315
{
@@ -25,6 +27,9 @@ public static void Main(string[] args)
2527
default:
2628
throw new ArgumentOutOfRangeException($"Unrecognized porting direction: {config.Direction}");
2729
}
30+
31+
Log.Finished();
32+
await loggingTask;
2833
}
2934
}
3035
}

0 commit comments

Comments
 (0)
0