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

[17.12] Adding a static factory for the TerminalLogger #11016

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the MIT license. See License.txt in the project root for full license information. -->
<Project>
<PropertyGroup>
<VersionPrefix>17.12.12</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<VersionPrefix>17.12.13</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
<PackageValidationBaselineVersion>17.11.4</PackageValidationBaselineVersion>
<AssemblyVersion>15.1.0.0</AssemblyVersion>
<PreReleaseVersionLabel>preview</PreReleaseVersionLabel>
Expand Down
29 changes: 27 additions & 2 deletions src/MSBuild/TerminalLogger/TerminalLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ internal TerminalLogger(ITerminal terminal)
_manualRefresh = true;
}

/// <summary>
/// Private constructor invoked by static factory.
/// </summary>
private TerminalLogger(LoggerVerbosity verbosity, uint? originalConsoleMode) : this()
{
Verbosity = verbosity;
_originalConsoleMode = originalConsoleMode;
}

#region INodeLogger implementation

/// <inheritdoc/>
Expand All @@ -265,8 +274,6 @@ public void Initialize(IEventSource eventSource, int nodeCount)
/// <inheritdoc/>
public void Initialize(IEventSource eventSource)
{
(_, _, _originalConsoleMode) = NativeMethodsShared.QueryIsScreenAndTryEnableAnsiColorCodes();

ParseParameters();

eventSource.BuildStarted += BuildStarted;
Expand Down Expand Up @@ -1050,6 +1057,24 @@ private void EraseNodes()

#region Helpers

/// <summary>
/// Creates a Terminal logger or Console logger based on the environment.
/// This method is called by reflection from dotnet. Do not modify the name or parameters without adapting the SDK.
/// </summary>
public static ILogger CreateTerminalOrConsoleLogger(LoggerVerbosity verbosity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One missing piece here is that while this does check the environment variable, the CLI flags (--tl:off, etc) are not respected. We may need a way to pass in an argv and have this method check for the presence/absence of the TL-related (and --console logger parameter!) flags to ensure uniform behavior.

{
bool isDisabled = (Environment.GetEnvironmentVariable("MSBUILDTERMINALLOGGER") ?? string.Empty).Equals("off", StringComparison.InvariantCultureIgnoreCase);
(bool supportsAnsi, bool outputIsScreen, uint? originalConsoleMode) = NativeMethodsShared.QueryIsScreenAndTryEnableAnsiColorCodes();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can skip this if TL is requested to be disabled

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with that intention, but this approach has lower complexity and the performance impact is negligible.


if (isDisabled || !supportsAnsi || !outputIsScreen)
{
NativeMethodsShared.RestoreConsoleMode(originalConsoleMode);
return new ConsoleLogger(verbosity);
}

return new TerminalLogger(verbosity, originalConsoleMode);
}

/// <summary>
/// Print a build result summary to the output.
/// </summary>
Expand Down