-
Notifications
You must be signed in to change notification settings - Fork 3
/
Log.cs
executable file
·64 lines (56 loc) · 1.44 KB
/
Log.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
using System;
using System.Linq;
using System.Collections.Generic;
namespace Wolf
{
internal enum LogType
{
Info,
Warning,
Error
}
internal class Log
{
private Config _config;
private List<Tuple<LogType, string>> _messages;
internal Log(Config config)
{
_config = config;
_messages = new List<Tuple<LogType, string>>();
}
internal IEnumerable<string> GetMessagesOfType(LogType type)
{
return _messages.Where(m => m.Item1 == type).Select(m => m.Item2).ToList();
}
internal void Error(string msg)
{
if (_config.ThrowOnError)
{
throw new InvalidOperationException(msg);
}
_messages.Add(Tuple.Create(LogType.Error, msg));
PrintToConsole("ERROR: ", msg, ConsoleColor.Red);
}
internal void Warning(string msg)
{
_messages.Add(Tuple.Create(LogType.Warning, msg));
PrintToConsole("WARNING: ", msg, ConsoleColor.Yellow);
}
internal void Info(string msg)
{
_messages.Add(Tuple.Create(LogType.Info, msg));
PrintToConsole(null, msg, ConsoleColor.Cyan);
}
private static void PrintToConsole(string prefix, string msg, ConsoleColor color)
{
var saveColor = Console.ForegroundColor;
Console.ForegroundColor = color;
if (!string.IsNullOrEmpty(prefix))
{
Console.Write(prefix);
}
Console.WriteLine(msg);
Console.ForegroundColor = saveColor;
}
}
}