-
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathLoggerBase.cs
More file actions
186 lines (159 loc) · 4.93 KB
/
LoggerBase.cs
File metadata and controls
186 lines (159 loc) · 4.93 KB
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
#if RABBIT_LOGGER_1
using CrashKonijn.Logger;
using System.Linq;
#else
using System;
using UnityEngine;
#endif
using System.Collections.Generic;
using CrashKonijn.Agent.Core;
using ILoggerConfig = CrashKonijn.Agent.Core.ILoggerConfig;
namespace CrashKonijn.Agent.Runtime
{
#if RABBIT_LOGGER_1
public abstract class LoggerBase<TObj> : ILogger<TObj>
where TObj : class
{
protected ILoggerConfig config;
protected TObj source;
public abstract string Name { get; }
public List<string> Logs => this.logger.Logs.Select(x => x.message).ToList();
private IRabbitLogger logger;
public void Initialize(ILoggerConfig config, TObj obj)
{
this.source = obj;
this.config = config;
this.logger = LoggerFactory.Create<TObj>(obj);
this.UnregisterEvents();
this.RegisterEvents();
}
public void Log(string message) => this.logger.Log(message);
public void Warning(string message) => this.logger.Warning(message);
public void Error(string message) => this.logger.Error(message);
public bool ShouldLog() => this.logger.ShouldLog();
protected abstract void RegisterEvents();
protected abstract void UnregisterEvents();
~LoggerBase()
{
this.UnregisterEvents();
}
public void Dispose()
{
this.UnregisterEvents();
this.logger?.Dispose();
}
}
#else
public abstract class LoggerBase<TObj> : ILogger<TObj>
{
protected ILoggerConfig config;
protected TObj source;
public abstract string Name { get; }
public List<string> Logs { get; } = new();
public void Initialize(ILoggerConfig config, TObj source)
{
this.config = config;
this.source = source;
this.UnregisterEvents();
this.RegisterEvents();
}
public void Log(string message)
{
#if UNITY_EDITOR
this.Handle(message, DebugSeverity.Log);
#endif
}
public void Warning(string message)
{
#if UNITY_EDITOR
this.Handle(message, DebugSeverity.Warning);
#endif
}
public void Error(string message)
{
#if UNITY_EDITOR
this.Handle(message, DebugSeverity.Error);
#endif
}
public bool ShouldLog()
{
#if UNITY_EDITOR
return this.config.DebugMode != DebugMode.None;
#else
return false;
#endif
}
public void Dispose()
{
this.UnregisterEvents();
}
private string FormatLog(string message, DebugSeverity severity)
{
var formattedTime = DateTime.Now.ToString("HH:mm:ss");
return $"<color={this.GetColor(severity)}>[{formattedTime}]</color>: {message}";
}
private string GetColor(DebugSeverity severity)
{
switch (severity)
{
case DebugSeverity.Log:
return "white";
case DebugSeverity.Warning:
return "yellow";
case DebugSeverity.Error:
return "red";
default:
return "white";
}
}
private string FormatConsole(string message)
{
return $"{this.Name}: {message}";
}
protected void Handle(string message, DebugSeverity severity)
{
switch (this.config.DebugMode)
{
case DebugMode.None:
break;
case DebugMode.Log:
this.Store(this.FormatLog(message, severity));
break;
case DebugMode.Console:
this.Store(this.FormatLog(message, severity));
this.AddToConsole(this.FormatConsole(message), severity);
break;
}
}
private void AddToConsole(string message, DebugSeverity severity)
{
switch (severity)
{
case DebugSeverity.Log:
Debug.Log(this.FormatConsole(message));
break;
case DebugSeverity.Warning:
Debug.LogWarning(this.FormatConsole(message));
break;
case DebugSeverity.Error:
Debug.LogError(this.FormatConsole(message));
break;
}
}
private void Store(string message)
{
if (this.config.MaxLogSize == 0)
return;
if (this.Logs.Count >= this.config.MaxLogSize)
this.Logs.RemoveAt(0);
this.Logs.Add(message);
}
protected abstract void RegisterEvents();
protected abstract void UnregisterEvents();
~LoggerBase()
{
this.UnregisterEvents();
}
}
#endif
}