Skip to content

Commit 991e987

Browse files
committed
Clean up implementation of GitLabCILog
1 parent 35b04f0 commit 991e987

2 files changed

Lines changed: 50 additions & 44 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Cake.AzurePipelines.Module
2+
{
3+
internal static class AnsiEscapeCodes
4+
{
5+
public static readonly string Reset = string.Format(FORMAT, 0);
6+
public static readonly string ForegroundWhite = string.Format(FORMAT, 97);
7+
public static readonly string ForegroundYellow = string.Format(FORMAT, 33);
8+
public static readonly string ForegroundLightGray = string.Format(FORMAT, 37);
9+
public static readonly string ForegroundDarkGray = string.Format(FORMAT, 90);
10+
public static readonly string BackgroundMagenta = string.Format(FORMAT, 45);
11+
public static readonly string BackgroundRed = string.Format(FORMAT, 41);
12+
13+
private const string FORMAT = "\u001B[{0}m";
14+
}
15+
}
Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
using Cake.Core;
45
using Cake.Core.Diagnostics;
@@ -13,22 +14,21 @@ namespace Cake.AzurePipelines.Module
1314
[UsedImplicitly]
1415
public class GitLabCILog : ICakeLog
1516
{
16-
private static class AnsiEscapeCodes
17-
{
18-
public static readonly string Reset = string.Format(FORMAT, 0);
19-
public static readonly string ForegroundWhite = string.Format(FORMAT, 97);
20-
public static readonly string ForegroundYellow = string.Format(FORMAT, 33);
21-
public static readonly string ForegroundLightGray = string.Format(FORMAT, 37);
22-
public static readonly string ForegroundDarkGray = string.Format(FORMAT, 90);
23-
public static readonly string BackgroundMagenta = string.Format(FORMAT, 45);
24-
public static readonly string BackgroundRed = string.Format(FORMAT, 41);
25-
26-
private const string FORMAT = "\u001B[{0}m";
27-
}
28-
2917
private readonly ICakeLog _cakeLogImplementation;
3018
private readonly IConsole _console;
3119

20+
// Define the escape sequenes to make GitLab show colored messages
21+
// For reference, see https://docs.gitlab.com/ee/ci/yaml/script.html#add-color-codes-to-script-output
22+
// For the colors, match the colors used by Cake, see https://github.com/cake-build/cake/blob/ed612029b92f5da2b6cbdfe295c62e6b99a2963d/src/Cake.Core/Diagnostics/Console/ConsolePalette.cs#L34C17-L34C17
23+
private readonly Dictionary<LogLevel, string> _escapeSequences = new Dictionary<LogLevel, string>()
24+
{
25+
{ LogLevel.Fatal, $"{AnsiEscapeCodes.BackgroundMagenta}{AnsiEscapeCodes.ForegroundWhite}" },
26+
{ LogLevel.Error, $"{AnsiEscapeCodes.BackgroundRed}{AnsiEscapeCodes.ForegroundWhite}" },
27+
{ LogLevel.Warning, AnsiEscapeCodes.ForegroundYellow },
28+
{ LogLevel.Verbose, AnsiEscapeCodes.ForegroundLightGray },
29+
{ LogLevel.Debug, AnsiEscapeCodes.ForegroundDarkGray },
30+
};
31+
3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="GitLabCILog"/> class.
3434
/// </summary>
@@ -40,6 +40,13 @@ public GitLabCILog(IConsole console, Verbosity verbosity = Verbosity.Normal)
4040
_console = console;
4141
}
4242

43+
/// <inheritdoc />
44+
public Verbosity Verbosity
45+
{
46+
get { return _cakeLogImplementation.Verbosity; }
47+
set { _cakeLogImplementation.Verbosity = value; }
48+
}
49+
4350
/// <inheritdoc />
4451
public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args)
4552
{
@@ -53,40 +60,24 @@ public void Write(Verbosity verbosity, LogLevel level, string format, params obj
5360
return;
5461
}
5562

56-
// Use colored output for log messages on GitLab CI
57-
// For reference, see https://docs.gitlab.com/ee/ci/yaml/script.html#add-color-codes-to-script-output
58-
// 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
59-
// Not however, that the GitLab Web UI seems to render some colors the same (e.g. white and dark gray
60-
switch (level)
63+
string message;
64+
if (_escapeSequences.TryGetValue(level, out var sequence))
6165
{
62-
case LogLevel.Fatal:
63-
_console.WriteErrorLine($"{AnsiEscapeCodes.BackgroundMagenta}{AnsiEscapeCodes.ForegroundWhite}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
64-
break;
65-
case LogLevel.Error:
66-
_console.WriteErrorLine($"{AnsiEscapeCodes.BackgroundRed}{AnsiEscapeCodes.ForegroundWhite}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
67-
break;
68-
case LogLevel.Warning:
69-
_console.WriteLine($"{AnsiEscapeCodes.ForegroundYellow}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
70-
break;
71-
case LogLevel.Information:
72-
_console.WriteLine($"{level}: {string.Format(format, args)}");
73-
break;
74-
case LogLevel.Verbose:
75-
_console.WriteLine($"{AnsiEscapeCodes.ForegroundLightGray}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
76-
break;
77-
case LogLevel.Debug:
78-
_console.WriteLine($"{AnsiEscapeCodes.ForegroundDarkGray}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}");
79-
break;
80-
default:
81-
throw new ArgumentOutOfRangeException(nameof(level), level, null);
66+
message = $"{sequence}{level}: {string.Format(format, args)}{AnsiEscapeCodes.Reset}";
67+
}
68+
else
69+
{
70+
message = $"{level}: {string.Format(format, args)}";
8271
}
83-
}
8472

85-
/// <inheritdoc />
86-
public Verbosity Verbosity
87-
{
88-
get { return _cakeLogImplementation.Verbosity; }
89-
set { _cakeLogImplementation.Verbosity = value; }
73+
if (level > LogLevel.Error)
74+
{
75+
_console.WriteLine(message);
76+
}
77+
else
78+
{
79+
_console.WriteErrorLine(message);
80+
}
9081
}
9182
}
9283
}

0 commit comments

Comments
 (0)