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

Adding a static factory for the TerminalLogger #11318

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

MichalPavlik
Copy link
Member

@MichalPavlik MichalPavlik commented Jan 21, 2025

Fixes #10998

Context

Although I created the SDK hotfix to always use the ConsoleLogger, we want to use TerminalLogger when it's possible.

Changes Made

Added a static factory that can return instance of Terminal or Console logger based on current environment. The usage of TerminalLogger in this scenario can be explicitly disabled by using of already existing env. variable or CLI argument --tl:off.
This change also prevents emitting of progress indication when the verbosity is set to quiet.

Notes

I tried to move TL to Microsoft.Build, where it belongs, but we don't want to make it public. Unfortunately, using the InternalsVisibleTo attribute to expose this type to the MSBuild project causes many type conflicts due to shared sources. Using the reflection is an ugly, but unnecessary workaround to make the factory public :(

@MichalPavlik MichalPavlik requested a review from Copilot January 21, 2025 13:38

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

src/MSBuild/TerminalLogger/TerminalLogger.cs:1089

  • [nitpick] The variable name 'tlArg' is ambiguous. It should be renamed to 'terminalLoggerArgument' for better readability.
string tlArg = args?.FirstOrDefault(a => a.StartsWith("--tl:", StringComparison.InvariantCultureIgnoreCase)) ?? string.Empty;

src/MSBuild/TerminalLogger/TerminalLogger.cs:1087

  • Ensure that the new behavior introduced by the 'CreateTerminalOrConsoleLogger' method is covered by tests.
public static ILogger CreateTerminalOrConsoleLogger(LoggerVerbosity verbosity, string[]? args)
@MichalPavlik
Copy link
Member Author

@baronfel, I know you would like to have full support for CLI logger configuration, but it will require some refactoring as the code for argument parsing is mostly placed in XMake. I wanted to break up XMake years ago, but now we have a valid reason to refactor at least part of the huge file :) If you don't mind, I would create a separate issue for this work.

@MichalPavlik MichalPavlik requested a review from baronfel January 21, 2025 13:50
@SimaTian SimaTian added the Area: Terminal Logger Problems with the livelogger/fancylogger/terminallogger -tl functionality. label Jan 21, 2025
Copy link
Member

@baronfel baronfel left a comment

Choose a reason for hiding this comment

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

LGTM, but should we use this factory in our own codebase to dogfood?

@baronfel
Copy link
Member

@baronfel, I know you would like to have full support for CLI logger configuration, but it will require some refactoring as the code for argument parsing is mostly placed in XMake. I wanted to break up XMake years ago, but now we have a valid reason to refactor at least part of the huge file :) If you don't mind, I would create a separate issue for this work.

Yeah, that is quite a large unit of work - makes sense to carve it out.

@MichalPavlik
Copy link
Member Author

MichalPavlik commented Jan 22, 2025

LGTM, but should we use this factory in our own codebase to dogfood?

I wanted to do it from the beginning, but the entry point logic emits global messages when it detects that TL couldn't be used. The easiest solution is to make the list of deferred messages in XMake internal, but I don't like it from an engineering point of view (separation of concerns, class coupling, etc.).

I think we could move the factory to some already public type like ConsoleLogger, so the CLI doesn't have to use reflection. Then we could move the list of deferred messages from XMake to the Logger class and use it directly in the factory or, in a cleaner way, return some indication of why the TL cannot be used and let XMake create the messages. Let's sync offline.

Edit: I'm looking at XMake once again, and actually, there are multiple things that make dogfooding the factory... problematic :(

@MichalPavlik
Copy link
Member Author

We decided to reuse parameters parsing logic to support all CL and TL logger parameters in this case.

@MichalPavlik MichalPavlik marked this pull request as draft January 27, 2025 14:13
@MichalPavlik
Copy link
Member Author

We agreed to use the factory as is and make it public. Support for all parameters will be added later.

@MichalPavlik MichalPavlik marked this pull request as ready for review February 7, 2025 10:25
Comment on lines +351 to +357
a.StartsWith("/tl:", StringComparison.InvariantCultureIgnoreCase) ||
a.StartsWith("-tl:", StringComparison.InvariantCultureIgnoreCase) ||
a.StartsWith("--tl:", StringComparison.InvariantCultureIgnoreCase)) ?? string.Empty;

bool isDisabled =
tlArg.EndsWith("tl:on", StringComparison.InvariantCultureIgnoreCase) ? false :
tlArg.EndsWith("tl:off", StringComparison.InvariantCultureIgnoreCase) ? true :
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to support the long form too, which makes this complex enough that I lean to "screw it use a regex"

(?:/|-|--)(?:tl|terminallogger):(?'value'on|off)

@@ -136,7 +136,6 @@
<Compile Include="CommandLineSwitchException.cs" />
<Compile Include="..\Shared\CoreCLRAssemblyLoader.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'" />
<Compile Include="DistributedLoggerRecord.cs" />
<Compile Include="TerminalLogger\*.cs" />
Copy link
Member

Choose a reason for hiding this comment

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

Why un-glob?

@@ -396,7 +403,7 @@ private void BuildStarted(object sender, BuildStartedEventArgs e)

_buildStartTime = e.Timestamp;

if (Terminal.SupportsProgressReporting)
if (Terminal.SupportsProgressReporting && Verbosity != LoggerVerbosity.Quiet)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I see why "quiet" implies "don't make the spinner go", can you elaborate on that a bit?

[InlineData("-tl:on", true, true, "off", "TerminalLogger")]
public void CreateTerminalOrConsoleLogger_CreatesCorrectLoggerInstance(string argsString, bool supportsAnsi, bool outputIsScreen, string evnVariableValue, string expectedLoggerName)
{
string originalValue = Environment.GetEnvironmentVariable("MSBUILDTERMINALLOGGER");
Copy link
Member

Choose a reason for hiding this comment

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

TestEnvironment has helpers for this, can you switch to them?

/// </summary>
/// <param name="verbosity">Level of detail to show in the log.</param>
/// <param name="args">Command line arguments for the logger configuration. Currently, only '--tl:off' and '--tl:on' are supported right now.</param>
public static ILogger CreateTerminalOrConsoleLogger(LoggerVerbosity verbosity, string[] args)
Copy link
Member

Choose a reason for hiding this comment

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

It is surprising to me that we're not extracting the verbosity from the command line too. Should we?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Terminal Logger Problems with the livelogger/fancylogger/terminallogger -tl functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird progress indicators started appearing in stdout
6 participants