From 340bf9d4fa4ab6175ab1e9c04500cecf555cb742 Mon Sep 17 00:00:00 2001 From: Gregory Paidis <115458417+gregory-paidis-sonarsource@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:17:35 +0200 Subject: [PATCH] SCAN4NET-100 Remove timestamps from UI Warnings (#2214) --- .../ILoggerTests.cs | 28 ++++----------- .../ConsoleLogger.cs | 35 +++++++++---------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/Tests/SonarScanner.MSBuild.Common.Test/ILoggerTests.cs b/Tests/SonarScanner.MSBuild.Common.Test/ILoggerTests.cs index c448f0d4e..839cb0fef 100644 --- a/Tests/SonarScanner.MSBuild.Common.Test/ILoggerTests.cs +++ b/Tests/SonarScanner.MSBuild.Common.Test/ILoggerTests.cs @@ -318,20 +318,21 @@ public void ConsoleLogger_SuspendAndResume() [TestMethod] public void ConsoleLogger_WriteUIWarnings_GenerateFile() { + const string prefixRegex = @"\d{2}:\d{2}:\d{2}(.\d{1,3})? WARNING:"; using var output = new OutputCaptureScope(); - var logger = new ConsoleLogger(includeTimestamp: false, fileWrapper); + var logger = new ConsoleLogger(includeTimestamp: true, fileWrapper); logger.LogUIWarning("uiWarn1"); - output.AssertLastMessageEndsWith("uiWarn1"); // UI warnings should also be logged in the console + output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn1"); logger.LogUIWarning("uiWarn2", null); - output.AssertLastMessageEndsWith("uiWarn2"); + output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn2"); logger.LogUIWarning("uiWarn3 {0}", "xxx"); - output.AssertLastMessageEndsWith("uiWarn3 xxx"); + output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn3 xxx"); const string outputDir = "outputDir"; - logger.WriteUIWarnings(outputDir); + logger.WriteUIWarnings(outputDir); // this should not contain any timestamps. fileWrapper.Received(1).WriteAllText( Path.Combine(outputDir, FileConstants.UIWarningsFileName), """ @@ -349,23 +350,6 @@ public void ConsoleLogger_WriteUIWarnings_GenerateFile() """); } - [TestMethod] - public void ConsoleLogger_WriteUIWarnings_ContainsTimeStampOnce() - { - const string prefixRegex = @"\d{2}:\d{2}:\d{2}(.\d{1,3})? WARNING:"; - using var output = new OutputCaptureScope(); - var logger = new ConsoleLogger(includeTimestamp: true, fileWrapper); - - logger.LogUIWarning("uiWarn1"); - output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn1"); - - logger.LogUIWarning("uiWarn2", null); - output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn2"); - - logger.LogUIWarning("uiWarn3 {0}", "xxx"); - output.AssertExpectedLastMessageRegex($"{prefixRegex} uiWarn3 xxx"); - } - [TestMethod] public void ILogger_Log_Debug() { diff --git a/src/SonarScanner.MSBuild.Common/ConsoleLogger.cs b/src/SonarScanner.MSBuild.Common/ConsoleLogger.cs index 841a8f61a..156845bdf 100644 --- a/src/SonarScanner.MSBuild.Common/ConsoleLogger.cs +++ b/src/SonarScanner.MSBuild.Common/ConsoleLogger.cs @@ -124,7 +124,7 @@ public void LogInfo(string message, params object[] args) => public void LogUIWarning(string message, params object[] args) { - uiWarnings.Add(GetFormattedMessage(message, args)); + uiWarnings.Add(FormatMessage(message, args)); LogWarning(message, args); } @@ -137,22 +137,6 @@ public void WriteUIWarnings(string outputFolder) } } - private string GetFormattedMessage(string message, params object[] args) - { - var finalMessage = message; - if (args is not null && args.Length > 0) - { - finalMessage = string.Format(CultureInfo.CurrentCulture, finalMessage ?? string.Empty, args); - } - - if (IncludeTimestamp) - { - finalMessage = string.Format(CultureInfo.CurrentCulture, "{0} {1}", DateTime.Now.ToString("HH:mm:ss.FFF", - CultureInfo.InvariantCulture), finalMessage); - } - return finalMessage; - } - private void FlushOutput() { Debug.Assert(isOutputSuspended, "Not expecting FlushOutput to be called unless output is currently suspended"); @@ -170,7 +154,7 @@ private void FlushOutput() /// Formats a message and writes or records it. /// private void Write(MessageType messageType, string message, params object[] args) => - WriteFormatted(messageType, GetFormattedMessage(message, args)); + WriteFormatted(messageType, FormatAndTimestampMessage(message, args)); /// /// Either writes the message to the output stream, or records it @@ -196,6 +180,21 @@ private void WriteFormatted(MessageType messageType, string formatted) } } + private string FormatAndTimestampMessage(string message, params object[] args) + { + var formatted = FormatMessage(message, args); + if (IncludeTimestamp) + { + formatted = string.Format(CultureInfo.CurrentCulture, "{0} {1}", DateTime.Now.ToString("HH:mm:ss.FFF", CultureInfo.InvariantCulture), formatted); + } + return formatted; + } + + private static string FormatMessage(string message, params object[] args) => + args is not null && args.Length > 0 + ? string.Format(CultureInfo.CurrentCulture, message ?? string.Empty, args) + : message; + private static ConsoleColor GetConsoleColor(MessageType messageType) => messageType switch {