-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWPLogger.cs
260 lines (229 loc) · 5.95 KB
/
WPLogger.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
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using UnityEngine;
/// <summary>
/// Custom log system that allow to choose when to display message
/// Optimized for runtime and builds.
/// </summary>
public static class WPLogger
{
/// <summary>
/// String containing all logs
/// </summary>
public static StringBuilder LogHistory { get; private set; } = new StringBuilder();
/// <summary>
/// List of active tags (tag can be any string)
/// </summary>
public static List<string> activeTags { get; private set; } = new List<string>();
/// <summary>
/// Write log to Debug unity class
/// </summary>
public static bool LogToUnity = true;
/// <summary>
/// Write log to local global log
/// </summary>
public static bool LogToHistory = true;
/// <summary>
/// Diplay tag on front of log
/// </summary>
public static bool LogTagHeader = true;
/// <summary>
/// Display time on front of log
/// </summary>
public static bool LogTime = false;
public delegate void WPLoggerEvent(string logText);
/// <summary>
/// Called when a new log is processed
/// </summary>
public static WPLoggerEvent OnLogged;
/// <summary>
/// Called when a new error is processed
/// </summary>
public static WPLoggerEvent OnErrorLogged;
[UnityEngine.RuntimeInitializeOnLoadMethod]
static void Init()
{
#if UNITY_EDITOR
// on editor don't apply settings if some are already existing
if (activeTags != null && activeTags.Count > 0) return;
#endif
ApplySettings(WPLoggerData.GetCurrentSettings());
}
public static void ApplySettings(WPLoggerData.Settings settings)
{
LogToUnity = settings.logToUnity;
LogToHistory = settings.logToHistory;
LogTagHeader = settings.logTagHeader;
LogTime = settings.logTime;
activeTags = new List<string>(settings.defaultActiveTags);
// Make sure Force tag is present
if (!activeTags.Contains(WPMainTag.FORCE))
{
activeTags.Insert(0, WPMainTag.FORCE);
}
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
public static void Show(params object[] objs)
{
if (objs == null) return;
StringBuilder str = new StringBuilder();
object o;
for (int i = 0; i < objs.Length; i++)
{
o = objs[i];
if (o == null) str.Append("[" + i + ": null]");
else str.Append("[" + i + ": " + o.GetType() + " - " + o.ToString() + "]");
}
LogHistory.Append(str);
UnityEngine.Debug.Log(str);
OnLogged?.Invoke(str.ToString());
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
/// <summary>
/// Fast log only wrap default Unity logger and history
/// </summary>
public static void LogFast(string message)
{
LogHistory.AppendLine(message);
UnityEngine.Debug.Log(message);
OnLogged?.Invoke(message);
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
/// <summary>
/// Fast log only wrap default Unity logger
/// </summary>
/// <param name="message"></param>
public static void LogFast(string message, UnityEngine.Object context)
{
LogHistory.AppendLine(message);
UnityEngine.Debug.Log(message, context);
OnLogged?.Invoke(message);
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
public static void Log(string message, params string[] tags)
{
if (tags != null && tags.Length > 0)
{
if (!HasActiveTag(tags)) return;
if (LogTagHeader)
message = "[" + System.String.Join(',', tags) + "] " + message;
}
if (LogTime)
{
message = "(" + GetTime() + ") " + message;
}
if (LogToHistory)
{
LogHistory.AppendLine(message);
}
if (LogToUnity)
{
UnityEngine.Debug.Log(message);
}
OnLogged?.Invoke(message);
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
public static void Log(object obj, params string[] tags)
{
Log(obj.ToString());
}
static bool HasActiveTag(params string[] tags)
{
foreach (var t in tags)
{
if (activeTags.Contains(t))
{
return true;
}
}
return false;
}
/// <summary>
/// Logging Error always display them, even when using tags
/// </summary>
/// <param name="message"></param>
/// <param name="tags"></param>
public static void LogError(string message, params string[] tags)
{
if (tags != null && tags.Length > 0)
{
if (LogTagHeader)
message = "[" + System.String.Join(',', tags) + "] " + message;
}
if (LogTime)
{
message = "(" + GetTime() + ") " + message;
}
if (LogToHistory)
{
LogHistory.AppendLine(message);
}
UnityEngine.Debug.LogError(message);
OnErrorLogged?.Invoke(message);
}
static string GetTime()
{
return System.DateTime.Now.ToString("HH:mm:ss");
}
public static void Clear()
{
LogHistory.Clear();
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
public static void SetTagActive(string tag)
{
if (!string.IsNullOrWhiteSpace(tag) && !activeTags.Contains(tag))
{
activeTags.Add(tag);
}
}
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
[Conditional("WPLOG")]
public static void SetTagDisabled(string tag)
{
if (!string.IsNullOrWhiteSpace(tag) && tag != "F") // F tag can't be disabled
{
activeTags.Remove(tag);
}
}
public static bool IsTagActive(string tag)
{
if (string.IsNullOrWhiteSpace(tag)) return false;
return activeTags.Contains(tag);
}
public static string[] GetTags()
{
return activeTags.ToArray();
}
}
/// <summary>
/// This static class only contain some possible tags.
/// You can use any other string as tag.
/// Tips: Make your own static class that contain const string for your own tags
/// </summary>
public class WPMainTag
{
// Special tag that always display a log
public const string FORCE = "F";
// Examples of tags
public const string INFO = "INFO";
public const string WARNING = "WARN";
public const string IMPORTANT = "IMP";
public const string ANALYTIC = "ANALYTIC";
public const string PLAYER = "PLAYER";
public const string UI = "UI";
}