Skip to content

Commit

Permalink
SCAN4NET-100 Remove timestamps from UI Warnings (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource authored Sep 26, 2024
1 parent 9ace0f0 commit 340bf9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
28 changes: 6 additions & 22 deletions Tests/SonarScanner.MSBuild.Common.Test/ILoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
"""
Expand All @@ -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()
{
Expand Down
35 changes: 17 additions & 18 deletions src/SonarScanner.MSBuild.Common/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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");
Expand All @@ -170,7 +154,7 @@ private void FlushOutput()
/// Formats a message and writes or records it.
/// </summary>
private void Write(MessageType messageType, string message, params object[] args) =>
WriteFormatted(messageType, GetFormattedMessage(message, args));
WriteFormatted(messageType, FormatAndTimestampMessage(message, args));

/// <summary>
/// Either writes the message to the output stream, or records it
Expand All @@ -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
{
Expand Down

0 comments on commit 340bf9d

Please sign in to comment.