Skip to content
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

Fix multithreading issue in ConsoleNugetLogger #322

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading