-
Notifications
You must be signed in to change notification settings - Fork 14
/
Logger.cs
331 lines (295 loc) · 9.44 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#define UNITY_DIALOGS // Comment out to disable dialogs for fatal errors
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR && UNITY_DIALOGS
using UnityEditor;
#endif
///////////////////////////
// Types
///////////////////////////
[System.Flags]
public enum Channel : uint
{
/// <summary>
/// Logs from C# to do with our Lua api
/// </summary>
Lua = 1 << 0,
/// <summary>
/// Logs directly from the Lua VM
/// </summary>
LuaNative = 1 << 1,
/// <summary>
/// Logs to do with AI/GOAP/
/// </summary>
AI = 1 << 2,
/// <summary>
/// Logs to do with graphics/rendering
/// </summary>
Rendering = 1 << 3,
/// <summary>
/// Logs to do with the physics system
/// </summary>
Physics = 1 << 4,
/// <summary>
/// Logs to do with our UI system
/// </summary>
UI = 1 << 5,
/// <summary>
/// Logs about NetDevices and networks
/// </summary>
NetDevice = 1 << 6,
/// <summary>
/// Logs to do with sound and Wwise
/// </summary>
Audio = 1 << 7,
/// <summary>
/// Logs to do with level loading
/// </summary>
Loading = 1 << 8,
/// <summary>
/// Logs to do with localisation
/// </summary>
Localisation = 1 << 9,
/// <summary>
/// Logs to do with platform services
/// </summary>
Platform = 1 << 10,
/// <summary>
/// Logs asserts
/// </summary>
Assert = 1 << 11,
/// <summary>
/// Build/Content generation logs
/// </summary>
Build = 1 << 12,
/// <summary>
/// Analytics logs
/// </summary>
Analytics = 1 << 13,
/// <summary>
/// Animation logs
/// </summary>
Animation = 1 << 14,
}
/// <summary>
/// The priority of the log
/// </summary>
public enum Priority
{
// Default, simple output about game
Info,
// Warnings that things might not be as expected
Warning,
// Things have already failed, alert the dev
Error,
// Things will not recover, bring up pop up dialog
FatalError,
}
public class Logger
{
public const Channel kAllChannels = (Channel) ~0u;
///////////////////////////
// Singleton set up
///////////////////////////
private static Logger instance;
private static Logger Instance
{
get
{
return instance ?? (instance = new Logger());
}
}
private Logger()
{
m_Channels = kAllChannels;
}
///////////////////////////
// Members
///////////////////////////
private Channel m_Channels;
public delegate void OnLogFunc(Channel channel, Priority priority, string message);
public static event OnLogFunc OnLog;
///////////////////////////
// Channel Control
///////////////////////////
public static void ResetChannels()
{
Instance.m_Channels = kAllChannels;
}
public static void AddChannel(Channel channelToAdd)
{
Instance.m_Channels |= channelToAdd;
}
public static void RemoveChannel(Channel channelToRemove)
{
Instance.m_Channels &= ~channelToRemove;
}
public static void ToggleChannel(Channel channelToToggle)
{
Instance.m_Channels ^= channelToToggle;
}
public static bool IsChannelActive(Channel channelToCheck)
{
return (Instance.m_Channels & channelToCheck) == channelToCheck;
}
public static void SetChannels(Channel channelsToSet)
{
Instance.m_Channels = channelsToSet;
}
///////////////////////////
///////////////////////////
// Logging functions
///////////////////////////
/// <summary>
/// Standard logging function, priority will default to info level
/// </summary>
/// <param name="logChannel"></param>
/// <param name="message"></param>
public static void Log(Channel logChannel, string message)
{
FinalLog(logChannel, Priority.Info, message);
}
/// <summary>
/// Standard logging function with specified priority
/// </summary>
/// <param name="logChannel"></param>
/// <param name="priority"></param>
/// <param name="message"></param>
public static void Log(Channel logChannel, Priority priority, string message)
{
FinalLog(logChannel, priority, message);
}
/// <summary>
/// Log with format args, priority will default to info level
/// </summary>
/// <param name="logChannel"></param>
/// <param name="message"></param>
/// <param name="args"></param>
public static void Log(Channel logChannel, string message, params object[] args)
{
FinalLog(logChannel, Priority.Info, string.Format(message, args));
}
/// <summary>
/// Log with format args and specified priority
/// </summary>
/// <param name="logChannel"></param>
/// <param name="priority"></param>
/// <param name="message"></param>
/// <param name="args"></param>
public static void Log(Channel logChannel, Priority priority, string message, params object[] args)
{
FinalLog(logChannel, priority, string.Format(message, args));
}
/// <summary>
/// Assert that the passed in condition is true, otherwise log a fatal error
/// </summary>
/// <param name="condition">The condition to test</param>
/// <param name="message">A user provided message that will be logged</param>
public static void Assert(bool condition, string message)
{
if (!condition)
{
FinalLog(Channel.Assert, Priority.FatalError, string.Format("Assert Failed: {0}", message));
}
}
/// <summary>
/// This function controls where the final string goes
/// </summary>
/// <param name="logChannel"></param>
/// <param name="priority"></param>
/// <param name="message"></param>
private static void FinalLog(Channel logChannel, Priority priority, string message)
{
if (IsChannelActive(logChannel))
{
// Dialog boxes can't support rich text mark up, do we won't colour the final string
string finalMessage = ContructFinalString(logChannel, priority, message, (priority != Priority.FatalError));
#if UNITY_EDITOR && UNITY_DIALOGS
// Fatal errors will create a pop up when in the editor
if (priority == Priority.FatalError)
{
bool ignore = EditorUtility.DisplayDialog("Fatal error", finalMessage, "Ignore", "Break");
if (!ignore)
{
Debug.Break();
}
}
#endif
// Call the correct unity logging function depending on the type of error
switch (priority)
{
case Priority.FatalError:
case Priority.Error:
Debug.LogError(finalMessage);
break;
case Priority.Warning:
Debug.LogWarning(finalMessage);
break;
case Priority.Info:
Debug.Log(finalMessage);
break;
}
if(OnLog != null)
{
OnLog.Invoke(logChannel, priority, finalMessage);
}
}
}
/// <summary>
/// Creates the final string with colouration based on channel and priority
/// </summary>
/// <param name="logChannel"></param>
/// <param name="priority"></param>
/// <param name="message"></param>
/// <param name="shouldColour"></param>
/// <returns></returns>
private static string ContructFinalString(Channel logChannel, Priority priority, string message, bool shouldColour)
{
string channelColour = null;
string priortiyColour = priorityToColour[priority];
if(!channelToColour.TryGetValue(logChannel, out channelColour))
{
channelColour = "black";
Debug.LogErrorFormat("Please add colour for channel {0}", logChannel);
}
if(shouldColour)
{
return string.Format("<b><color={0}>[{1}] </color></b> <color={2}>{3}</color>", channelColour, logChannel, priortiyColour, message);
}
return string.Format("[{0}] {1}", logChannel, message);
}
/// <summary>
/// Map a channel to a colour, using Unity's rich text system
/// </summary>
private static readonly Dictionary<Channel, string> channelToColour = new Dictionary<Channel, string>
{
{ Channel.Lua, "cyan" },
{ Channel.LuaNative, "lightblue" },
{ Channel.AI, "blue" },
{ Channel.Rendering, "green" },
{ Channel.Physics, "yellow" },
{ Channel.UI, "purple" },
{ Channel.NetDevice, "orange" },
{ Channel.Audio, "teal" },
{ Channel.Loading, "olive" },
{ Channel.Localisation, "brown" },
{ Channel.Platform, "red" },
{ Channel.Assert, "red" },
{ Channel.Build, "navy" },
{ Channel.Analytics, "maroon" },
};
/// <summary>
/// Map a priority to a colour, using Unity's rich text system
/// </summary>
private static readonly Dictionary<Priority, string> priorityToColour = new Dictionary<Priority, string>
{
#if UNITY_PRO_LICENSE
{ Priority.Info, "white" },
#else
{ Priority.Info, "black" },
#endif
{ Priority.Warning, "orange" },
{ Priority.Error, "red" },
{ Priority.FatalError, "red" },
};
}