Skip to content

Commit

Permalink
Fix SimpleErrorLoger Small refactor of SimpleErrorLoger (#9191)
Browse files Browse the repository at this point in the history
* Fix SimpleErrorLoger Small refactor of SimpleErrorLoger
* Fix comment
* Reusing defined ansi codes constans
  • Loading branch information
rokonec authored Sep 19, 2023
1 parent 107edc1 commit 74f7ebb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/Build/Logging/SimpleErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

namespace Microsoft.Build.Logging.SimpleErrorLogger
Expand Down Expand Up @@ -44,30 +45,36 @@ public void Initialize(IEventSource eventSource, int nodeCount)
{
eventSource.ErrorRaised += HandleErrorEvent;
eventSource.WarningRaised += HandleWarningEvent;

// This needs to happen so binary loggers can get evaluation properties and items
if (eventSource is IEventSource4 eventSource4)
{
eventSource4.IncludeEvaluationPropertiesAndItems();
}
}

private void HandleErrorEvent(object sender, BuildErrorEventArgs e)
{
HasLoggedErrors = true;
LogErrorEvent(EventArgsFormatting.FormatEventMessage(e, showProjectFile: true), "\x1b[31;1m");
LogWithColor(EventArgsFormatting.FormatEventMessage(e, showProjectFile: true),
TerminalColor.Red);
}

private void HandleWarningEvent(object sender, BuildWarningEventArgs e)
{
LogErrorEvent(EventArgsFormatting.FormatEventMessage(e, showProjectFile: true), "\x1b[33;1m");
LogWithColor(EventArgsFormatting.FormatEventMessage(e, showProjectFile: true),
TerminalColor.Yellow);
}

private void LogErrorEvent(string s, string color)
private void LogWithColor(string message, TerminalColor color)
{
if (acceptAnsiColorCodes)
{
Console.Error.Write(color);
Console.Error.Write(s);
Console.Error.WriteLine("\x1b[m");
Console.Error.Write(AnsiCodes.Colorize(message, color));
}
else
{
Console.Error.Write(s);
Console.Error.Write(message);
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Microsoft.Build.Logging.TerminalLogger;

/// <summary>
/// Enumerates the text colors supported by <see cref="ITerminal"/>.
/// Enumerates the text colors supported by VT100 terminal.
/// </summary>
internal enum TerminalColor
{
Expand Down
1 change: 0 additions & 1 deletion src/MSBuild/TerminalLogger/TerminalLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using System.Runtime.InteropServices;
#if NETFRAMEWORK
using Microsoft.IO;
#else
Expand Down

0 comments on commit 74f7ebb

Please sign in to comment.