-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.cs
174 lines (151 loc) · 3.25 KB
/
Debug.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
using System;
using System.Diagnostics;
using System.IO;
public static class Debug
{
public static string OutputDir = "";
public const bool AddTimestamp = true;
public const string TimestampFormat = "yyyy/MM/dd HH:mm:ss.ffff";
public static void Initialize()
{
string fileName = OutputDir + "output_log.txt";
try
{
if (File.Exists(fileName))
{
string backupFileName = OutputDir + "output_log_backup.txt";
if (File.Exists(backupFileName))
{
File.Delete(backupFileName);
}
File.Move(fileName, backupFileName);
}
}
catch
{
// ignored
}
Trace.Listeners.Clear();
// Logging to console creates issues for the client if it is singleplayer. Plus it is pointless.
ConsoleTraceListener consoleListener = new ConsoleTraceListener(useErrorStream: false)
{
TraceOutputOptions = TraceOptions.None
};
Trace.Listeners.Add(consoleListener);
TextWriterTraceListener writerListener = new TextWriterTraceListener(new StreamWriter(fileName, append: false))
{
TraceOutputOptions = TraceOptions.None
};
Trace.Listeners.Add(writerListener);
Trace.AutoFlush = true;
}
public static void Destroy()
{
Trace.Listeners.Clear();
}
private static void WriteToLog(string message)
{
try
{
Trace.WriteLine($"{(AddTimestamp ? DateTime.UtcNow.ToString(TimestampFormat + " -" ) : "")} {message}");
}
catch
{
// ignored
}
}
private static string GetString(object value)
{
return value != null ? value.ToString() : "NULL";
}
[Conditional("DEBUG")]
[Conditional("SHOW_ALL_LOGS")]
public static void Log(string message)
{
WriteToLog($"[Debug] {message}");
}
[Conditional("DEBUG")]
[Conditional("SHOW_ALL_LOGS")]
public static void Log(params object[] values)
{
if (values.Length == 1)
{
WriteToLog($"[Debug] {GetString(values[0])}");
}
else
{
WriteToLog($"[Debug] {string.Join(" ", values)}");
}
}
[Conditional("DEBUG")]
[Conditional("SHOW_ALL_LOGS")]
public static void LogIf(bool condition, string message)
{
if (condition)
{
WriteToLog($"[Debug] {message}");
}
}
[Conditional("DEBUG")]
[Conditional("SHOW_ALL_LOGS")]
public static void LogIf(bool condition, params object[] values)
{
if (condition)
{
Log(values);
}
}
public static void UnformattedMessage(string message)
{
Trace.WriteLine(message);
}
public static void Info(string message)
{
WriteToLog($"[Info] {message}");
}
public static void Info(params object[] values)
{
if (values.Length == 1)
{
WriteToLog($"[Info] {GetString(values[0])}");
}
else
{
WriteToLog($"[Info] {string.Join(" ", values)}");
}
}
public static void Warning(string message)
{
WriteToLog("[Warn] " + message);
}
public static void Warning(params object[] values)
{
if (values.Length == 1)
{
WriteToLog("[Warn] " + GetString(values[0]));
}
else
{
WriteToLog("[Warn] " + string.Join(" ", values));
}
}
public static void Error(string message)
{
WriteToLog($"[Error] {message}");
}
public static void Error(params object[] values)
{
if (values.Length == 1)
{
WriteToLog("[Error] " + GetString(values[0]));
}
else
{
WriteToLog("[Error] " + string.Join(" ", values));
}
}
public static void Exception(Exception ex)
{
WriteToLog($"[Exception] {ex}");
}
}