Skip to content

Commit

Permalink
Merge pull request #322 from waf/add-locking-on-console-nuget-logger
Browse files Browse the repository at this point in the history
Fix multithreading issue in ConsoleNugetLogger
  • Loading branch information
waf committed Nov 17, 2023
2 parents d56d924 + f0478df commit c4770f5
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions CSharpRepl.Services/Nuget/ConsoleNugetLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal sealed class ConsoleNugetLogger : ILogger
private readonly string successPrefix;
private readonly string errorPrefix;
private readonly List<Line> lines = new();
private readonly object linesLock = new(); // Lock for the lines list
private int linesRendered;

public ConsoleNugetLogger(IConsoleEx console, Configuration configuration)
Expand Down Expand Up @@ -58,15 +59,18 @@ public void LogMinimal(string data)
var line = CreateLine(data, isError: false);
if (line.IsEmpty) return;

lines.Add(line);
if (lines.Count(l => !l.IsError) > NumberOfMessagesToShow)
lock (linesLock)
{
for (int i = 0; i < lines.Count; i++)
lines.Add(line);
if (lines.Count(l => !l.IsError) > NumberOfMessagesToShow)
{
if (!lines[i].IsError)
for (int i = 0; i < lines.Count; i++)
{
lines.RemoveAt(i);
break;
if (!lines[i].IsError)
{
lines.RemoveAt(i);
break;
}
}
}
}
Expand All @@ -79,13 +83,19 @@ public void LogMinimal(string data)

public void LogError(string data)
{
lines.Add(CreateLine(data, isError: true));
lock(linesLock)
{
lines.Add(CreateLine(data, isError: true));
}
RenderLines();
}

public void Reset()
{
lines.Clear();
lock (linesLock)
{
lines.Clear();
}
linesRendered = 0;
}

Expand All @@ -99,15 +109,14 @@ public void LogFinish(string text, bool success)
}
linesRendered = 0;

//keep only errors
for (int i = lines.Count - 1; i >= 0; i--)
lock (linesLock)
{
if (!lines[i].IsError) lines.RemoveAt(i);
//keep only errors
lines.RemoveAll(line => !line.IsError);
//add final summary
lines.Add(CreateLine(text, isError: !success));
}

//add final summary
lines.Add(CreateLine(text, isError: !success));

//render summary + potential errors
RenderLines();
}
Expand All @@ -132,21 +141,24 @@ private void RenderLines()
console.Write(AnsiEscapeCodes.ClearLine);
}

linesRendered = 0;
foreach (var line in lines)
lock (linesLock)
{
if (line.IsError)
{
console.Write(AnsiColor.Red.GetEscapeSequence());
console.WriteLine(line.Text.Text ?? "");
console.Write(AnsiEscapeCodes.Reset);
}
else
linesRendered = 0;
foreach (var line in lines)
{
console.WriteLine(line.Text);
if (line.IsError)
{
console.Write(AnsiColor.Red.GetEscapeSequence());
console.WriteLine(line.Text.Text ?? "");
console.Write(AnsiEscapeCodes.Reset);
}
else
{
console.WriteLine(line.Text);
}

linesRendered += Math.DivRem(line.Text.Length, console.PrettyPromptConsole.BufferWidth, out var remainder) + (remainder == 0 ? 0 : 1);
}

linesRendered += Math.DivRem(line.Text.Length, console.PrettyPromptConsole.BufferWidth, out var remainder) + (remainder == 0 ? 0 : 1);
}
}
finally
Expand All @@ -167,7 +179,7 @@ public Line(string data, bool isError, string prefix, Configuration configuratio
{
data = Truncate(data);
IsEmpty = data.Length == 0;
Text = Format(data, prefix, configuration);
Text = IsEmpty ? prefix : Format(data, prefix, configuration);
IsError = isError;
}

Expand Down

0 comments on commit c4770f5

Please sign in to comment.