-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
172 lines (149 loc) · 4.83 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
// Copyright (c) Doug Cook. All rights reserved.
// Licensed under the MIT License.
using System;
using CultureInfo = System.Globalization.CultureInfo;
using StringWriter = System.IO.StringWriter;
using TraceLevel = System.Diagnostics.TraceLevel;
namespace GitDiffWrapper
{
internal static class Logger
{
private const string ErrorPrefix = "gd ERROR: ";
private const string WarningPrefix = "gd WARNING: ";
private const string InfoPrefix = "gd Info: ";
private const string VerbosePrefix = "gd verbose: ";
private const string UnknownPrefix = "gd unk";
private static StringWriter writer;
public static TraceLevel Level = TraceLevel.Info;
public static StringWriter Writer
{
get
{
if (writer == null)
{
writer = new StringWriter(CultureInfo.InvariantCulture);
}
return writer;
}
}
public static string LevelPrefix(TraceLevel messageLevel)
{
switch (messageLevel)
{
case TraceLevel.Error: return ErrorPrefix;
case TraceLevel.Warning: return WarningPrefix;
case TraceLevel.Info: return InfoPrefix;
case TraceLevel.Verbose: return VerbosePrefix;
default: return UnknownPrefix + ((int)messageLevel).ToString(CultureInfo.InvariantCulture) + ": ";
}
}
public static void Write(string message)
{
Console.Error.Write(message);
}
public static void Write(string format, params object[] args)
{
Console.Error.Write(format, args);
}
public static void WriteLine(string message)
{
Console.Error.WriteLine(message);
}
public static void WriteLine(string format, params object[] args)
{
Console.Error.WriteLine(format, args);
}
public static void Message(TraceLevel messageLevel, string message)
{
if (Level >= messageLevel)
{
Write(LevelPrefix(messageLevel));
WriteLine(message);
}
}
public static void Message(TraceLevel messageLevel, string format, params object[] args)
{
if (Level >= messageLevel)
{
Write(LevelPrefix(messageLevel));
WriteLine(format, args);
}
}
public static void Error(string message)
{
if (Level >= TraceLevel.Error)
{
Write(ErrorPrefix);
WriteLine(message);
}
var w = Writer;
w.Write(ErrorPrefix);
w.WriteLine(message);
}
public static void Error(string format, params object[] args)
{
if (Level >= TraceLevel.Error)
{
Write(ErrorPrefix);
WriteLine(format, args);
}
var w = Writer;
w.Write(ErrorPrefix);
w.WriteLine(format, args);
}
public static void Warn(string message)
{
if (Level >= TraceLevel.Warning)
{
Write(WarningPrefix);
WriteLine(message);
}
var w = Writer;
w.Write(WarningPrefix);
w.WriteLine(message);
}
public static void Warn(string format, params object[] args)
{
if (Level >= TraceLevel.Warning)
{
Write(WarningPrefix);
WriteLine(format, args);
}
var w = Writer;
w.Write(WarningPrefix);
w.WriteLine(format, args);
}
public static void Info(string message)
{
if (Level >= TraceLevel.Info)
{
Write(InfoPrefix);
WriteLine(message);
}
}
public static void Info(string format, params object[] args)
{
if (Level >= TraceLevel.Info)
{
Write(InfoPrefix);
WriteLine(format, args);
}
}
public static void Verbose(string message)
{
if (Level >= TraceLevel.Verbose)
{
Write(VerbosePrefix);
WriteLine(message);
}
}
public static void Verbose(string format, params object[] args)
{
if (Level >= TraceLevel.Verbose)
{
Write(VerbosePrefix);
WriteLine(format, args);
}
}
}
}