LogScope.cs
6.88 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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.TestTools.Logging
{
internal class LogScope : IDisposable
{
private bool m_Disposed;
private readonly object _lock = new object();
public Queue<LogMatch> ExpectedLogs { get; set; }
public List<LogEvent> AllLogs { get; }
public List<LogEvent> FailingLogs { get; }
public bool IgnoreFailingMessages { get; set; }
public bool IsNUnitException { get; private set; }
public bool IsNUnitSuccessException { get; private set; }
public bool IsNUnitInconclusiveException { get; private set; }
public bool IsNUnitIgnoreException { get; private set; }
public string NUnitExceptionMessage { get; private set; }
private bool m_NeedToProcessLogs;
private static List<LogScope> s_ActiveScopes = new List<LogScope>();
internal static LogScope Current
{
get
{
if (s_ActiveScopes.Count == 0)
throw new InvalidOperationException("No log scope is available");
return s_ActiveScopes[0];
}
}
internal static bool HasCurrentLogScope()
{
return s_ActiveScopes.Count > 0;
}
public LogScope()
{
AllLogs = new List<LogEvent>();
FailingLogs = new List<LogEvent>();
ExpectedLogs = new Queue<LogMatch>();
IgnoreFailingMessages = false;
Activate();
}
private void Activate()
{
s_ActiveScopes.Insert(0, this);
RegisterScope(this);
Application.logMessageReceivedThreaded -= AddLog;
Application.logMessageReceivedThreaded += AddLog;
}
private void Deactivate()
{
Application.logMessageReceivedThreaded -= AddLog;
s_ActiveScopes.Remove(this);
UnregisterScope(this);
}
private static void RegisterScope(LogScope logScope)
{
Application.logMessageReceivedThreaded += logScope.AddLog;
}
private static void UnregisterScope(LogScope logScope)
{
Application.logMessageReceivedThreaded -= logScope.AddLog;
}
public void AddLog(string message, string stacktrace, LogType type)
{
lock (_lock)
{
m_NeedToProcessLogs = true;
var log = new LogEvent
{
LogType = type,
Message = message,
StackTrace = stacktrace,
};
AllLogs.Add(log);
if (IsNUnitResultStateException(stacktrace, type))
{
if (message.StartsWith("SuccessException"))
{
IsNUnitException = true;
IsNUnitSuccessException = true;
if (message.StartsWith("SuccessException: "))
{
NUnitExceptionMessage = message.Substring("SuccessException: ".Length);
return;
}
}
else if (message.StartsWith("InconclusiveException"))
{
IsNUnitException = true;
IsNUnitInconclusiveException = true;
if (message.StartsWith("InconclusiveException: "))
{
NUnitExceptionMessage = message.Substring("InconclusiveException: ".Length);
return;
}
}
else if (message.StartsWith("IgnoreException"))
{
IsNUnitException = true;
IsNUnitIgnoreException = true;
if (message.StartsWith("IgnoreException: "))
{
NUnitExceptionMessage = message.Substring("IgnoreException: ".Length);
return;
}
}
}
if (IsFailingLog(type) && !IgnoreFailingMessages)
{
FailingLogs.Add(log);
}
}
}
public bool IsAllLogsHandled()
{
lock (_lock)
{
return AllLogs.All(x => x.IsHandled);
}
}
public LogEvent GetUnhandledLog()
{
lock (_lock)
{
return AllLogs.First(x => !x.IsHandled);
}
}
private static bool IsNUnitResultStateException(string stacktrace, LogType logType)
{
if (logType != LogType.Exception)
return false;
return string.IsNullOrEmpty(stacktrace) || stacktrace.StartsWith("NUnit.Framework.Assert.");
}
private static bool IsFailingLog(LogType type)
{
switch (type)
{
case LogType.Assert:
case LogType.Error:
case LogType.Exception:
return true;
default:
return false;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (m_Disposed)
{
return;
}
m_Disposed = true;
if (disposing)
{
Deactivate();
}
}
internal bool AnyFailingLogs()
{
ProcessExpectedLogs();
return FailingLogs.Any();
}
internal void ProcessExpectedLogs()
{
lock (_lock)
{
if (!m_NeedToProcessLogs || !ExpectedLogs.Any())
return;
LogMatch expectedLog = null;
foreach (var logEvent in AllLogs)
{
if (!ExpectedLogs.Any())
break;
if (expectedLog == null && ExpectedLogs.Any())
expectedLog = ExpectedLogs.Peek();
if (expectedLog != null && expectedLog.Matches(logEvent))
{
ExpectedLogs.Dequeue();
logEvent.IsHandled = true;
if (FailingLogs.Any(expectedLog.Matches))
{
var failingLog = FailingLogs.First(expectedLog.Matches);
FailingLogs.Remove(failingLog);
}
expectedLog = null;
}
}
m_NeedToProcessLogs = false;
}
}
}
}