From b0542f37f6cfc6342bf6615338e72487ae1d7f82 Mon Sep 17 00:00:00 2001 From: Ladi Prosek Date: Fri, 31 Mar 2023 16:30:14 +0200 Subject: [PATCH] Add #region's and minor cleanup --- src/MSBuild/LiveLogger/ITerminal.cs | 4 +- src/MSBuild/LiveLogger/LiveLogger.cs | 227 ++++++++++++++------------- 2 files changed, 124 insertions(+), 107 deletions(-) diff --git a/src/MSBuild/LiveLogger/ITerminal.cs b/src/MSBuild/LiveLogger/ITerminal.cs index 11ef5e0564..4fc2ccae0c 100644 --- a/src/MSBuild/LiveLogger/ITerminal.cs +++ b/src/MSBuild/LiveLogger/ITerminal.cs @@ -11,12 +11,12 @@ namespace Microsoft.Build.Logging.LiveLogger; internal interface ITerminal : IDisposable { /// - /// Width of terminal buffer + /// Width of the terminal buffer. /// int Width { get; } /// - /// Height of terminal buffer + /// Height of the terminal buffer. /// int Height { get; } diff --git a/src/MSBuild/LiveLogger/LiveLogger.cs b/src/MSBuild/LiveLogger/LiveLogger.cs index 3502c179f0..7bfb0aa4d1 100644 --- a/src/MSBuild/LiveLogger/LiveLogger.cs +++ b/src/MSBuild/LiveLogger/LiveLogger.cs @@ -59,11 +59,6 @@ internal sealed class LiveLogger : INodeLogger /// private readonly CancellationTokenSource _cts = new(); - /// - /// Tracks the work currently being done by build nodes. Null means the node is not doing any work worth reporting. - /// - private NodeStatus?[] _nodes = Array.Empty(); - /// /// Tracks the status of all relevant projects seen so far. /// @@ -72,6 +67,11 @@ internal sealed class LiveLogger : INodeLogger /// private readonly Dictionary _projects = new(); + /// + /// Tracks the work currently being done by build nodes. Null means the node is not doing any work worth reporting. + /// + private NodeStatus?[] _nodes = Array.Empty(); + /// /// The timestamp of the event. /// @@ -108,12 +108,6 @@ internal sealed class LiveLogger : INodeLogger /// private ITerminal Terminal { get; } - /// - public LoggerVerbosity Verbosity { get => LoggerVerbosity.Minimal; set { } } - - /// - public string Parameters { get => ""; set { } } - /// /// List of events the logger needs as parameters to the . /// @@ -153,6 +147,14 @@ internal sealed class LiveLogger : INodeLogger Terminal = terminal; } + #region INodeLogger implementation + + /// + public LoggerVerbosity Verbosity { get => LoggerVerbosity.Minimal; set { } } + + /// + public string Parameters { get => ""; set { } } + /// public void Initialize(IEventSource eventSource, int nodeCount) { @@ -177,24 +179,16 @@ internal sealed class LiveLogger : INodeLogger eventSource.ErrorRaised += ErrorRaised; } - /// - /// The thread proc. - /// - private void ThreadProc() + /// + public void Shutdown() { - while (!_cts.IsCancellationRequested) - { - Thread.Sleep(1_000 / 30); // poor approx of 30Hz - - lock (_lock) - { - DisplayNodes(); - } - } - - EraseNodes(); + Terminal.Dispose(); } + #endregion + + #region Logger callbacks + /// /// The callback. /// @@ -266,35 +260,6 @@ internal sealed class LiveLogger : INodeLogger } } - /// - /// Print a build result summary to the output. - /// - /// True if the build completed with success. - /// True if the build has logged at least one error. - /// True if the build has logged at least one warning. - private void PrintBuildResult(bool succeeded, bool hasError, bool hasWarning) - { - if (!succeeded) - { - // If the build failed, we print one of three red strings. - string text = (hasError, hasWarning) switch - { - (true, _) => "failed with errors", - (false, true) => "failed with warnings", - _ => "failed", - }; - Terminal.WriteColor(TerminalColor.Red, text); - } - else if (hasWarning) - { - Terminal.WriteColor(TerminalColor.Yellow, "succeeded with warnings"); - } - else - { - Terminal.WriteColor(TerminalColor.Green, "succeeded"); - } - } - /// /// The callback. /// @@ -414,43 +379,6 @@ internal sealed class LiveLogger : INodeLogger } } - /// - /// Render Nodes section. - /// It shows what all build nodes do. - /// - private void DisplayNodes() - { - NodesFrame newFrame = new NodesFrame(_nodes, width: Terminal.Width, height: Terminal.Height); - - // Do not render delta but clear everything is Terminal width or height have changed - if (newFrame.Width != _currentFrame.Width || newFrame.Height != _currentFrame.Height) - { - EraseNodes(); - } - - string rendered = newFrame.Render(_currentFrame); - - // Move cursor back to 1st line of nodes - Terminal.WriteLine($"\x1b[{_currentFrame.NodesCount + 1}F"); - Terminal.Write(rendered); - - _currentFrame = newFrame; - } - - /// - /// Erases the previously printed live node output. - /// - private void EraseNodes() - { - if (_currentFrame.NodesCount == 0) - { - return; - } - Terminal.WriteLine($"\x1b[{_currentFrame.NodesCount + 1}F"); - Terminal.Write($"\x1b[0J"); - _currentFrame.Clear(); - } - /// /// The callback. /// @@ -470,15 +398,6 @@ internal sealed class LiveLogger : INodeLogger } } - /// - /// Returns the index corresponding to the given . - /// - private int NodeIndexForContext(BuildEventContext context) - { - // Node IDs reported by the build are 1-based. - return context.NodeId - 1; - } - /// /// The callback. Unused. /// @@ -564,16 +483,69 @@ internal sealed class LiveLogger : INodeLogger } } - /// - public void Shutdown() + #endregion + + #region Refresher thread implementation + + /// + /// The thread proc. + /// + private void ThreadProc() { - Terminal.Dispose(); + while (!_cts.IsCancellationRequested) + { + Thread.Sleep(1_000 / 30); // poor approx of 30Hz + + lock (_lock) + { + DisplayNodes(); + } + } + + EraseNodes(); + } + + /// + /// Render Nodes section. + /// It shows what all build nodes do. + /// + private void DisplayNodes() + { + NodesFrame newFrame = new NodesFrame(_nodes, width: Terminal.Width, height: Terminal.Height); + + // Do not render delta but clear everything is Terminal width or height have changed + if (newFrame.Width != _currentFrame.Width || newFrame.Height != _currentFrame.Height) + { + EraseNodes(); + } + + string rendered = newFrame.Render(_currentFrame); + + // Move cursor back to 1st line of nodes + Terminal.WriteLine($"\x1b[{_currentFrame.NodesCount + 1}F"); + Terminal.Write(rendered); + + _currentFrame = newFrame; + } + + /// + /// Erases the previously printed live node output. + /// + private void EraseNodes() + { + if (_currentFrame.NodesCount == 0) + { + return; + } + Terminal.WriteLine($"\x1b[{_currentFrame.NodesCount + 1}F"); + Terminal.Write($"\x1b[0J"); + _currentFrame.Clear(); } /// /// Capture states on nodes to be rendered on display. /// - private class NodesFrame + private sealed class NodesFrame { private readonly List _nodeStrings = new(); private readonly StringBuilder _renderBuilder = new(); @@ -699,6 +671,51 @@ internal sealed class LiveLogger : INodeLogger NodesCount = 0; } } + + #endregion + + #region Helpers + + /// + /// Print a build result summary to the output. + /// + /// True if the build completed with success. + /// True if the build has logged at least one error. + /// True if the build has logged at least one warning. + private void PrintBuildResult(bool succeeded, bool hasError, bool hasWarning) + { + if (!succeeded) + { + // If the build failed, we print one of three red strings. + string text = (hasError, hasWarning) switch + { + (true, _) => "failed with errors", + (false, true) => "failed with warnings", + _ => "failed", + }; + Terminal.WriteColor(TerminalColor.Red, text); + } + else if (hasWarning) + { + Terminal.WriteColor(TerminalColor.Yellow, "succeeded with warnings"); + } + else + { + Terminal.WriteColor(TerminalColor.Green, "succeeded"); + } + } + + /// + /// Returns the index corresponding to the given . + /// + private int NodeIndexForContext(BuildEventContext context) + { + // Node IDs reported by the build are 1-based. + return context.NodeId - 1; + } + + #endregion + } internal record ProjectContext(int Id)