-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
175 lines (158 loc) · 5.66 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Collections.ObjectModel;
using System.Text;
namespace ActionsMinUtils;
/// <summary>
/// A simple logger for GitHub Actions
/// </summary>
public static class Logger
{
/// <summary>
/// Write a debug message
/// </summary>
/// <param name="message"></param>
public static void Debug(string? message)
{
IssueCommand(new CommandEnvelope("debug", null, message));
}
/// <summary>
/// Write an error message (potentially along with an annotation)
/// </summary>
/// <param name="message"></param>
/// <param name="annotationProps"></param>
/// <returns></returns>
public static void Error(string? message, AnnotationProperties? annotationProps = null)
{
IssueCommand(new CommandEnvelope("error", annotationProps.ToDictionary(), message));
}
/// <summary>
/// Like #Error, but also throws an exception
/// </summary>
/// <param name="message"></param>
/// <param name="annotationProps"></param>
/// <exception cref="Exception"></exception>
public static void Fatal(string? message, AnnotationProperties? annotationProps = null)
{
Error(message, annotationProps);
throw new Exception(message);
}
/// <summary>
/// Write a warning message (potentially along with an annotation)
/// </summary>
/// <param name="message"></param>
/// <param name="annotationProps"></param>
public static void Warning(string? message, AnnotationProperties? annotationProps = null)
{
IssueCommand(new CommandEnvelope("warning", annotationProps.ToDictionary(), message));
}
/// <summary>
/// Write a notice message (potentially along with an annotation)
/// </summary>
/// <param name="message"></param>
/// <param name="annotationProps"></param>
public static void Notice(string? message, AnnotationProperties? annotationProps = null)
{
IssueCommand(new CommandEnvelope("notice", annotationProps.ToDictionary(), message));
}
/// <summary>
/// Write an info message
/// </summary>
/// <param name="message"></param>
public static void Info(string message)
{
Console.WriteLine(message);
}
/// <summary>
/// Write a group and return an IDisposable to be used in a using statement (for closing the group)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static IDisposable Group(string name)
{
IssueCommand(new CommandEnvelope("group", null, name));
return new EndGroupDisposable();
}
/// <summary>
/// Issue a command on the standard output
/// </summary>
/// <param name="command"></param>
private static void IssueCommand(CommandEnvelope command)
{
Console.WriteLine(command.ToString());
}
// Convert an annotation properties object to a dictionary
private static Dictionary<string, string> ToDictionary(this AnnotationProperties? annotationProps)
{
return annotationProps == null
? new Dictionary<string, string>()
: new Dictionary<string, string>
{
{ "title", annotationProps.Title ?? "" },
{ "file", annotationProps.File ?? "" },
{ "line", annotationProps.StartLine ?? "" },
{ "endLine", annotationProps.EndLine ?? "" },
{ "col", annotationProps.StartColumn ?? "" },
{ "endColumn", annotationProps.EndColumn ?? "" }
};
}
// Disposable responsible for closing a group
private class EndGroupDisposable : IDisposable
{
public void Dispose()
{
IssueCommand(new CommandEnvelope("endgroup", null, ""));
}
}
/// <summary>
/// Annotation properties object
/// </summary>
public class AnnotationProperties
{
public string? Title { get; init; }
public string? File { get; init; }
public string? StartLine { get; init; }
public string? EndLine { get; init; }
public string? StartColumn { get; init; }
public string? EndColumn { get; init; }
}
// Envelope objects for console commands
public class CommandEnvelope(string command, Dictionary<string, string>? parameters, string? message)
{
private string Command { get; } = command;
private ReadOnlyDictionary<string, string>? Parameters { get; } = parameters?.AsReadOnly();
private string? Message { get; } = message;
// Output the envelope as a string
public override string ToString()
{
var builder = new StringBuilder();
builder.Append($"::{Command}");
if (Parameters?.Count > 0)
{
builder.Append(' ');
builder.AppendJoin(",", Parameters.Select(p => $"{EscapeProperty(p.Key)}={EscapeProperty(p.Value)}"));
}
builder.Append("::");
builder.Append(EscapeData(Message));
return builder.ToString().TrimEnd();
}
private static string EscapeData(string? value)
{
return value == null
? ""
: value
.Replace("%", "%25")
.Replace("\r", "%0D")
.Replace("\n", "%0A");
}
private static string EscapeProperty(string? value)
{
return value == null
? ""
: value
.Replace("%", "%25")
.Replace("\r", "%0D")
.Replace("\n", "%0A")
.Replace(":", "%3A")
.Replace(",", "%2C");
}
}
}