-
Notifications
You must be signed in to change notification settings - Fork 62
/
UberLoggerEditor.cs
executable file
·165 lines (140 loc) · 3.88 KB
/
UberLoggerEditor.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
#if UNITY_EDITOR
using UnityEngine;
using System.Collections.Generic;
using System;
using UnityEditor;
using UberLogger;
/// <summary>
/// The basic editor logger backend
/// This is seperate from the editor frontend, so we can have multiple frontends active if we wish,
/// and so that we catch errors even without the frontend active.
/// Derived from ScriptableObject so it persists across play sessions.
/// </summary>
[System.Serializable]
public class UberLoggerEditor : ScriptableObject, UberLogger.ILogger
{
List<LogInfo> LogInfo = new List<LogInfo>();
HashSet<string> Channels = new HashSet<string>();
public bool PauseOnError = false;
public bool ClearOnPlay = true;
public bool WasPlaying = false;
public int NoErrors;
public int NoWarnings;
public int NoMessages;
static public UberLoggerEditor Create()
{
var editorDebug = ScriptableObject.FindObjectOfType<UberLoggerEditor>();
if(editorDebug==null)
{
editorDebug = ScriptableObject.CreateInstance<UberLoggerEditor>();
}
editorDebug.NoErrors = 0;
editorDebug.NoWarnings = 0;
editorDebug.NoMessages = 0;
return editorDebug;
}
public void OnEnable()
{
EditorApplication.playModeStateChanged += OnPlaymodeStateChanged;
//Make this scriptable object persist between Play sessions
hideFlags = HideFlags.HideAndDontSave;
}
/// <summary>
/// If we're about to start playing and 'ClearOnPlay' is set, clear the current logs
/// </summary>
public void ProcessOnStartClear()
{
if(!WasPlaying && EditorApplication.isPlayingOrWillChangePlaymode)
{
if(ClearOnPlay)
{
Clear();
}
}
WasPlaying = EditorApplication.isPlayingOrWillChangePlaymode;
}
void OnPlaymodeStateChanged(PlayModeStateChange obj)
{
ProcessOnStartClear();
}
/// <summary>
/// Interface for deriving new logger backends.
/// Add a new logger via Logger.AddLogger()
/// </summary>
public interface ILoggerWindow
{
/// <summary>
/// Logging backend entry point. logInfo contains all the information about the logging request.
/// </summary>
void OnLogChange(LogInfo logInfo);
}
List<ILoggerWindow> Windows = new List<ILoggerWindow>();
public void AddWindow(ILoggerWindow window)
{
if(!Windows.Contains(window))
{
Windows.Add(window);
}
}
public void Log(LogInfo logInfo)
{
lock(this)
{
if(!String.IsNullOrEmpty(logInfo.Channel) && !Channels.Contains(logInfo.Channel))
{
Channels.Add(logInfo.Channel);
}
LogInfo.Add(logInfo);
}
if(logInfo.Severity==LogSeverity.Error)
{
NoErrors++;
}
else if(logInfo.Severity==LogSeverity.Warning)
{
NoWarnings++;
}
else
{
NoMessages++;
}
foreach(var window in Windows)
{
window.OnLogChange(logInfo);
}
if(logInfo.Severity==LogSeverity.Error && PauseOnError)
{
UnityEngine.Debug.Break();
}
}
public void Clear()
{
lock(this)
{
LogInfo.Clear();
Channels.Clear();
NoWarnings = 0;
NoErrors = 0;
NoMessages = 0;
foreach(var window in Windows)
{
window.OnLogChange(null);
}
}
}
public List<LogInfo> CopyLogInfo()
{
lock(this)
{
return new List<LogInfo>(LogInfo);
}
}
public HashSet<string> CopyChannels()
{
lock(this)
{
return new HashSet<string>(Channels);
}
}
}
#endif