Skip to content

Adding a static factory for the TerminalLogger #11318

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

Merged
merged 21 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a8e7cc0
Adds a static factory that can create a TerminalLogger or ConsoleLogg…
MichalPavlik Nov 21, 2024
f0cbec2
Disable TL progress ctrl codes when verbosity is 'quiet'
MichalPavlik Jan 20, 2025
1685284
Added --tl:<val> arg support
MichalPavlik Jan 21, 2025
db35e8a
Removing debugging code
MichalPavlik Jan 21, 2025
d38dd70
Merge branch 'main' into dev/mipavlik/tl-static-factory
MichalPavlik Jan 21, 2025
8dbfeb8
Merge branch 'main' into dev/mipavlik/tl-static-factory
MichalPavlik Feb 5, 2025
d040fd9
Fixed failing tests
MichalPavlik Feb 5, 2025
94a1378
The factory method is now public
MichalPavlik Feb 5, 2025
01607a8
Unnecessary usings were removed
MichalPavlik Feb 5, 2025
9eb5464
Moving the TerminalLogger back due to issues with shared sources. Add…
MichalPavlik Feb 7, 2025
d2dbfce
Updated parameter doc
MichalPavlik Feb 7, 2025
2defbfe
Addded support for more args prefixes and returned --tl:on option
MichalPavlik Feb 7, 2025
eb0ead3
Moving Terminal Logger once again, but making it public now. All ctor…
MichalPavlik Feb 19, 2025
8dbee80
Fixed tests
MichalPavlik Feb 20, 2025
6d014a6
Merge branch 'main' into dev/mipavlik/tl-static-factory
MichalPavlik Feb 20, 2025
74a4d23
Updated XLFs
MichalPavlik Feb 20, 2025
018d490
Fixed TL activation
MichalPavlik Feb 20, 2025
ab08182
Changing namespace to align with other public loggers. Some types wer…
MichalPavlik Feb 20, 2025
3ac2ee0
Added verbosity parsing capability
MichalPavlik Feb 20, 2025
74cf0c3
Merge branch 'main' into dev/mipavlik/tl-static-factory
MichalPavlik Feb 21, 2025
f7cbe77
Factory now recognizes the "false" value
MichalPavlik Feb 24, 2025
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
2 changes: 0 additions & 2 deletions src/Build.UnitTests/ConsoleLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using Xunit.NetCore.Extensions;
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;

#nullable disable
Expand Down Expand Up @@ -134,7 +133,6 @@ public ConsoleLoggerTest(ITestOutputHelper output)
_output = output;
}


/// <summary>
/// Verify when the project has not been named that we correctly get the same placeholder
/// project name for project started event and the target started event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="FakeItEasy" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="Microsoft.CodeAnalysis.Build.Tasks" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="NuGet.Frameworks">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.Logging;

namespace Microsoft.Build.CommandLine.UnitTests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Text;
using System.Threading.Tasks;

using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.Logging;

using VerifyTests;
using VerifyXunit;
Expand All @@ -22,7 +22,7 @@ namespace Microsoft.Build.CommandLine.UnitTests;
[UsesVerify]
public class NodeStatus_SizeChange_Tests : IDisposable
{
private readonly NodeStatus _status = new("Namespace.Project", "TargetFramework", "Target", new MockStopwatch());
private readonly TerminalNodeStatus _status = new("Namespace.Project", "TargetFramework", "Target", new MockStopwatch());
private CultureInfo _currentCulture;

public NodeStatus_SizeChange_Tests()
Expand All @@ -36,31 +36,31 @@ public NodeStatus_SizeChange_Tests()
[Fact]
public async Task EverythingFits()
{
NodesFrame frame = new([_status], width: 80, height: 5);
TerminalNodesFrame frame = new([_status], width: 80, height: 5);

await Verify(frame.RenderNodeStatus(0).ToString());
}

[Fact]
public async Task TargetIsTruncatedFirst()
{
NodesFrame frame = new([_status], width: 45, height: 5);
TerminalNodesFrame frame = new([_status], width: 45, height: 5);

await Verify(frame.RenderNodeStatus(0).ToString());
}

[Fact]
public async Task NamespaceIsTruncatedNext()
{
NodesFrame frame = new([_status], width: 40, height: 5);
TerminalNodesFrame frame = new([_status], width: 40, height: 5);

await Verify(frame.RenderNodeStatus(0).ToString());
}

[Fact]
public async Task GoesToProject()
{
NodesFrame frame = new([_status], width: 10, height: 5);
TerminalNodesFrame frame = new([_status], width: 10, height: 5);

await Verify(frame.RenderNodeStatus(0).ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.Framework.Logging;
using Microsoft.Build.Logging;
using Shouldly;
using VerifyTests;
using VerifyXunit;
Expand All @@ -33,7 +33,7 @@ public void NodeStatusTargetThrowsForInputWithAnsi()
{
#if DEBUG
// This is testing a Debug.Assert, which won't throw in Release mode.
Func<NodeStatus> newNodeStatus = () => new NodeStatus("project", "tfm", AnsiCodes.Colorize("colorized target", TerminalColor.Green), new MockStopwatch());
Func<TerminalNodeStatus> newNodeStatus = () => new TerminalNodeStatus("project", "tfm", AnsiCodes.Colorize("colorized target", TerminalColor.Green), new MockStopwatch());
newNodeStatus.ShouldThrow<ArgumentException>().Message.ShouldContain("Target should not contain any escape codes, if you want to colorize target use the other constructor.");
#endif
}
Expand All @@ -58,7 +58,7 @@ public async Task NodeTargetUpdatesTime()
// This test look like there is no change between the frames, but we ask the stopwatch for time they will increase the number.
// We need this because animations check that NodeStatus reference is the same.
// And we cannot use MockStopwatch because we don't know when to call Tick on them, and if we do it right away, the time will update in "both" nodes.
NodeStatus node = new("Namespace.Project", "TargetFramework", "Build", new TickingStopwatch());
TerminalNodeStatus node = new("Namespace.Project", "TargetFramework", "Build", new TickingStopwatch());
var rendered = Animate(
[
node,
Expand Down Expand Up @@ -90,7 +90,7 @@ public async Task NodeWithColoredTargetUpdatesTime()
// This test look like there is no change between the frames, but we ask the stopwatch for time they will increase the number.
// We need this because animations check that NodeStatus reference is the same.
// And we cannot use MockStopwatch because we don't know when to call Tick on them, and if we do it right away, the time will update in "both" nodes.
NodeStatus node = new("Namespace.Project", "TargetFramework", TerminalColor.Green, "passed", "MyTestName1", new TickingStopwatch());
TerminalNodeStatus node = new("Namespace.Project", "TargetFramework", TerminalColor.Green, "passed", "MyTestName1", new TickingStopwatch());
var rendered = Animate(
[
node,
Expand All @@ -107,16 +107,16 @@ public async Task NodeWithColoredTargetUpdatesTime()
/// </summary>
/// <param name="nodeStatusesUpdates">Takes array of arrays. The inner array is collection of nodes that are currently running. The outer array is how they update over time.</param>
/// <returns></returns>
private string Animate(params NodeStatus[][] nodeStatusesUpdates)
private string Animate(params TerminalNodeStatus[][] nodeStatusesUpdates)
{
var width = 80;
var height = 1;

NodesFrame previousFrame = new(Array.Empty<NodeStatus>(), 0, 0);
TerminalNodesFrame previousFrame = new(Array.Empty<TerminalNodeStatus>(), 0, 0);
StringBuilder result = new StringBuilder();
foreach (var nodeStatuses in nodeStatusesUpdates)
{
NodesFrame currentFrame = new NodesFrame(nodeStatuses, width, height);
TerminalNodesFrame currentFrame = new TerminalNodesFrame(nodeStatuses, width, height);
result.Append(currentFrame.Render(previousFrame));
previousFrame = currentFrame;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
]9;4;3;\directory/file(1,2,3,4): warning AA0000: Warning!
directory/file(1,2,3,4): warning AA0000: Warning!
directory/file(1,2,3,4): warning AA0000:
A
Multi
Line
Warning!
directory/file(1,2,3,4): error AA0000: Error!
]9;4;0;\
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
]9;4;3;\directory/file(1,2,3,4): warning AA0000: Warning!
directory/file(1,2,3,4): warning AA0000: Warning!
directory/file(1,2,3,4): warning AA0000:
A
Multi
Line
Warning!
directory/file(1,2,3,4): error AA0000: Error!
]9;4;0;\
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.Http.Headers;
using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.Logging;

namespace Microsoft.Build.CommandLine.UnitTests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.UnitTests.Shared;
using Shouldly;
using VerifyTests;
Expand Down Expand Up @@ -70,6 +69,42 @@ public TerminalLogger_Tests()
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
}

[Theory]
[InlineData(null, false, false, "", typeof(ConsoleLogger))]
[InlineData(null, true, false, "", typeof(ConsoleLogger))]
[InlineData(null, false, true, "", typeof(ConsoleLogger))]
[InlineData(null, true, true, "off", typeof(ConsoleLogger))]
[InlineData(null, true, true, "false", typeof(ConsoleLogger))]
[InlineData("--tl:off", true, true, "", typeof(ConsoleLogger))]
[InlineData(null, true, true, "", typeof(TerminalLogger))]
[InlineData("-tl:on", true, true, "off", typeof(TerminalLogger))]
public void CreateTerminalOrConsoleLogger_CreatesCorrectLoggerInstance(string? argsString, bool supportsAnsi, bool outputIsScreen, string evnVariableValue, Type expectedType)
{
using TestEnvironment testEnvironment = TestEnvironment.Create();
testEnvironment.SetEnvironmentVariable("MSBUILDTERMINALLOGGER", evnVariableValue);

string[]? args = argsString?.Split(' ');
ILogger logger = TerminalLogger.CreateTerminalOrConsoleLogger(args, supportsAnsi, outputIsScreen, default);

logger.ShouldNotBeNull();
logger.GetType().ShouldBe(expectedType);
}

[Theory]
[InlineData("-v:q", LoggerVerbosity.Quiet)]
[InlineData("-verbosity:minimal", LoggerVerbosity.Minimal)]
[InlineData("--v:d", LoggerVerbosity.Detailed)]
[InlineData("/verbosity:diag", LoggerVerbosity.Diagnostic)]
[InlineData(null, LoggerVerbosity.Normal)]
public void CreateTerminalOrConsoleLogger_ParsesVerbosity(string? argsString, LoggerVerbosity expectedVerbosity)
{
string[]? args = argsString?.Split(' ');
ILogger logger = TerminalLogger.CreateTerminalOrConsoleLogger(args, true, true, default);

logger.ShouldNotBeNull();
logger.Verbosity.ShouldBe(expectedVerbosity);
}

#region IEventSource implementation

#pragma warning disable CS0067
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.Logging;

namespace Microsoft.Build.CommandLine.UnitTests;

Expand Down
4 changes: 4 additions & 0 deletions src/Build/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Logging;
Expand Down
3 changes: 2 additions & 1 deletion src/Build/Logging/SimpleErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging.TerminalLogger;
using Microsoft.Build.Framework.Logging;
using Microsoft.Build.Logging;
using Microsoft.Build.Shared;

namespace Microsoft.Build.Logging.SimpleErrorLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Build.Framework.Logging;

namespace Microsoft.Build.Logging.TerminalLogger;
namespace Microsoft.Build.Logging;

/// <summary>
/// An abstraction of a terminal, built specifically to fit the <see cref="TerminalLogger"/> needs.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Build.Logging.TerminalLogger;
namespace Microsoft.Build.Logging;

internal abstract class StopwatchAbstraction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Diagnostics;

namespace Microsoft.Build.Logging.TerminalLogger;
namespace Microsoft.Build.Logging;

internal sealed class SystemStopwatch : StopwatchAbstraction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Build.Framework.Logging;
#if NETFRAMEWORK
using Microsoft.Build.Shared;
#endif

namespace Microsoft.Build.Logging.TerminalLogger;
namespace Microsoft.Build.Logging;

/// <summary>
/// An <see cref="ITerminal"/> implementation for ANSI/VT100 terminals.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Build.Logging.TerminalLogger;
namespace Microsoft.Build.Logging;

/// <summary>
/// Represents a piece of diagnostic output (message/warning/error).
/// </summary>
internal record struct BuildMessage(MessageSeverity Severity, string Message)
internal record struct TerminalBuildMessage(TerminalMessageSeverity Severity, string Message)
{ }
Loading