Skip to content

Commit

Permalink
Match behaviour and coloring of Cake in GitLab CI log
Browse files Browse the repository at this point in the history
In GitLabCILog, use the same color palette as used by Cake on the local console.
Write "Error" and "Fatal" messages to the error stream instead of the output stream.
  • Loading branch information
ap0llo committed Dec 2, 2023
1 parent 9d069d8 commit 0080832
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Cake.GitLabCI.Module/GitLabCILog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;

using Cake.Core;
using Cake.Core.Diagnostics;

using JetBrains.Annotations;

namespace Cake.AzurePipelines.Module
Expand All @@ -14,8 +16,12 @@ public class GitLabCILog : ICakeLog
private static class AnsiEscapeCodes
{
public static readonly string Reset = string.Format(FORMAT, 0);
public static readonly string ForegroundRed = string.Format(FORMAT, 31);
public static readonly string ForegroundWhite = string.Format(FORMAT, 97);
public static readonly string ForegroundYellow = string.Format(FORMAT, 33);
public static readonly string ForegroundLightGray = string.Format(FORMAT, 37);
public static readonly string ForegroundDarkGray = string.Format(FORMAT, 90);
public static readonly string BackgroundMagenta = string.Format(FORMAT, 45);
public static readonly string BackgroundRed = string.Format(FORMAT, 41);

private const string FORMAT = "\u001B[{0}m";
}
Expand Down Expand Up @@ -49,19 +55,27 @@ public void Write(Verbosity verbosity, LogLevel level, string format, params obj

// Use colored output for log messages on GitLab CI
// For reference, see https://docs.gitlab.com/ee/ci/yaml/script.html#add-color-codes-to-script-output
// For the colors, mostly match the colors used by Cake, see https://github.com/cake-build/cake/blob/ed612029b92f5da2b6cbdfe295c62e6b99a2963d/src/Cake.Core/Diagnostics/Console/ConsolePalette.cs#L34C17-L34C17
// Not however, that the GitLab Web UI seems to render some colors the same (e.g. white and dark gray
switch (level)
{
case LogLevel.Fatal:
_console.WriteErrorLine($"{AnsiEscapeCodes.BackgroundMagenta}{AnsiEscapeCodes.ForegroundWhite}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
break;
case LogLevel.Error:
_console.WriteLine($"{AnsiEscapeCodes.ForegroundRed}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
_console.WriteErrorLine($"{AnsiEscapeCodes.BackgroundRed}{AnsiEscapeCodes.ForegroundWhite}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
break;
case LogLevel.Warning:
_console.WriteLine($"{AnsiEscapeCodes.ForegroundYellow}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
break;
case LogLevel.Information:
_console.WriteLine($"{level}: {string.Format(format, args)}");
break;
case LogLevel.Verbose:
_console.WriteLine($"{AnsiEscapeCodes.ForegroundLightGray}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
break;
case LogLevel.Debug:
_console.WriteLine($"{level}: {string.Format(format, args)}");
_console.WriteLine($"{AnsiEscapeCodes.ForegroundDarkGray}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
break;
default:
throw new ArgumentOutOfRangeException(nameof(level), level, null);
Expand Down

0 comments on commit 0080832

Please sign in to comment.