forked from danarcher/XCom2ModTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.cs
123 lines (105 loc) · 3.73 KB
/
Report.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
using System;
using System.IO;
using System.Security;
using System.Xml.Linq;
namespace XCom2ModTool
{
internal static class Report
{
public static Verbosity Verbosity { get; set; } = Verbosity.Concise;
public static void Error(string message)
{
WriteXmlLine(Console.Error, $"<error>error: {message}</error>");
}
public static void Warning(string message)
{
WriteXmlLine(Console.Error, $"<warning>warning: {message}</warning>");
}
public static void WriteLine() => Console.WriteLine();
public static void WriteLine(string message)
{
WriteXmlLine(Console.Out, $"<info>{message}</info>");
}
public static void Verbose(string message, Verbosity verbosity = Verbosity.Verbose)
{
if (Verbosity >= verbosity)
{
WriteXmlLine(Console.Out, $"<verbose>{message}</verbose>");
}
}
public static void Exception(Exception ex, string message = null)
{
Error(message ?? ex.Message);
var detailed = ex as DetailedException;
if (detailed != null)
{
foreach (var detail in detailed.Details)
{
WriteXmlLine(Console.Error, $" <error>{detail}</error>"); // No "error: " prefix, but indented to it
}
}
if (Verbosity >= Verbosity.Verbose)
{
Error(SecurityElement.Escape(ex.ToString()));
}
}
public static void WriteXmlLine(TextWriter writer, string message)
{
message = message.Replace("\\<", "<");
var doc = XDocument.Parse($"<?xml version=\"1.0\"?><root>{message}</root>", LoadOptions.PreserveWhitespace);
void Expand(XElement element)
{
var previousColor = Console.ForegroundColor;
if (Settings.Default.UseColoredOutput)
{
Console.ForegroundColor = element.Name.LocalName switch
{
"error" => Settings.Default.ErrorColor,
"warning" => Settings.Default.WarningColor,
"verbose" => Settings.Default.VerboseColor,
"info" => Settings.Default.InfoColor,
_ => Enum.TryParse(element.Name.LocalName, ignoreCase: true, out ConsoleColor color) ? color : previousColor
};
}
foreach (var node in element.Nodes())
{
switch (node)
{
case XElement child:
Expand(child);
break;
case XText text:
writer.Write(text.Value);
break;
}
}
Console.ForegroundColor = previousColor;
}
Expand(doc.Root);
writer.WriteLine();
}
public class ColorChange : IDisposable
{
private readonly ConsoleColor previousColor;
public ColorChange(ConsoleColor color)
{
if (Settings.Default.UseColoredOutput)
{
previousColor = Console.ForegroundColor;
Console.ForegroundColor = color;
}
}
public void Dispose()
{
Console.ForegroundColor = previousColor;
}
}
}
internal enum Verbosity
{
Concise = 0,
Verbose = 1,
Loquacious = 2,
Periphrastic = 3,
}
}