-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathLogCatWindow.cs
339 lines (288 loc) · 10.7 KB
/
LogCatWindow.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
332
333
334
335
336
337
338
339
#if PLATFORM_ANDROID
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.Android;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
#if UNITY_2017_3_OR_NEWER
using UnityEditor.Compilation;
#endif
public class LogCatWindow : EditorWindow
{
// How many log entries to store in memory. Keep it low for better performance.
private const int memoryLimit = 2000;
// How many log entries to show in unity3D editor. Keep it low for better performance.
private const int showLimit = 200;
// Filters
private bool prefilterOnlyUnity = true;
private bool filterOnlyError = false;
private bool filterOnlyWarning = false;
private bool filterOnlyDebug = false;
private bool filterOnlyInfo = false;
private bool filterOnlyVerbose = false;
private string filterByString = String.Empty;
// Android adb logcat process
private Process logCatProcess;
// Log entries
private List<LogCatLog> logsList = new List<LogCatLog>();
private List<LogCatLog> filteredList = new List<LogCatLog>(memoryLimit);
private const string LogcatPattern = @"([0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]{3}) ([WIEDV])/(.*)";
private static readonly Regex LogcatRegex = new Regex(LogcatPattern, RegexOptions.Compiled);
// Filtered GUI list scroll position
private Vector2 scrollPosition = new Vector2(0, 0);
// Add menu item named "LogCat" to the Window menu
[MenuItem("Window/LogCat - Android Logger")]
public static void ShowWindow()
{
// Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(LogCatWindow), false, "Logcat");
}
void Update()
{
if (logsList.Count == 0)
return;
lock (logsList)
{
// Filter
filteredList = logsList.Where(log => (filterByString.Length <= 2 || log.Message.ToLower().Contains(filterByString.ToLower())) &&
((!filterOnlyError && !filterOnlyWarning && !filterOnlyDebug && !filterOnlyInfo && !filterOnlyVerbose)
|| filterOnlyError && log.Type == 'E'
|| filterOnlyWarning && log.Type == 'W'
|| filterOnlyDebug && log.Type == 'D'
|| filterOnlyInfo && log.Type == 'I'
|| filterOnlyVerbose && log.Type == 'V')).ToList();
}
if (logCatProcess != null)
{
Repaint();
}
}
void OnGUI()
{
GUILayout.BeginHorizontal();
// Enable pre-filter if process is not started
GUI.enabled = logCatProcess == null;
prefilterOnlyUnity = GUILayout.Toggle(prefilterOnlyUnity, "Only Unity", "Button", GUILayout.Width(80));
// Enable button if process is not started
GUI.enabled = logCatProcess == null;
if (GUILayout.Button("Start", GUILayout.Width(60)))
{
string adbPath = GetAdbPath();
// Start `adb logcat -c` to clear the log buffer
ProcessStartInfo clearProcessInfo = new ProcessStartInfo();
clearProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
clearProcessInfo.CreateNoWindow = true;
clearProcessInfo.UseShellExecute = false;
clearProcessInfo.FileName = adbPath;
clearProcessInfo.Arguments = @"logcat -c";
Process.Start(clearProcessInfo);
// Start `adb logcat` (with additional optional arguments) process for filtering
ProcessStartInfo logProcessInfo = new ProcessStartInfo();
logProcessInfo.CreateNoWindow = true;
logProcessInfo.UseShellExecute = false;
logProcessInfo.RedirectStandardOutput = true;
logProcessInfo.RedirectStandardError = true;
logProcessInfo.StandardOutputEncoding = Encoding.UTF8;
logProcessInfo.FileName = adbPath;
logProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
// Add additional -s argument for filtering by Unity tag.
logProcessInfo.Arguments = "logcat -v time"+(prefilterOnlyUnity ? " -s \"Unity\"": "");
logCatProcess = Process.Start(logProcessInfo);
logCatProcess.ErrorDataReceived += (sender, errorLine) => {
if (errorLine.Data != null && errorLine.Data.Length > 2)
AddLog(new LogCatLog(errorLine.Data));
};
logCatProcess.OutputDataReceived += (sender, outputLine) => {
if (outputLine.Data != null && outputLine.Data.Length > 2)
AddLog(new LogCatLog(outputLine.Data));
};
logCatProcess.BeginErrorReadLine();
logCatProcess.BeginOutputReadLine();
}
// Disable button if process is already started
GUI.enabled = logCatProcess != null;
if (GUILayout.Button("Stop", GUILayout.Width(60)))
{
StopLogCatProcess();
}
GUI.enabled = true;
if (GUILayout.Button("Clear", GUILayout.Width(60)))
{
lock (logsList)
{
logsList.Clear();
filteredList.Clear();
}
}
GUILayout.Label(filteredList.Count + " matching logs", GUILayout.Height(20));
// Create filters
filterByString = GUILayout.TextField(filterByString, GUILayout.Height(20));
GUI.color = new Color(0.75f, 0.5f, 0.5f, 1f);
filterOnlyError = GUILayout.Toggle(filterOnlyError, "Error", "Button", GUILayout.Width(80));
GUI.color = new Color(0.95f, 0.95f, 0.3f, 1f);
filterOnlyWarning = GUILayout.Toggle(filterOnlyWarning, "Warning", "Button", GUILayout.Width(80));
GUI.color = new Color(0.5f, 0.5f, 0.75f, 1f);
filterOnlyDebug = GUILayout.Toggle(filterOnlyDebug, "Debug", "Button", GUILayout.Width(80));
GUI.color = new Color(0.5f, 0.75f, 0.5f, 1f);
filterOnlyInfo = GUILayout.Toggle(filterOnlyInfo, "Info", "Button", GUILayout.Width(80));
GUI.color = Color.white;
filterOnlyVerbose = GUILayout.Toggle(filterOnlyVerbose, "Verbose", "Button", GUILayout.Width(80));
GUILayout.EndHorizontal();
GUIStyle lineStyle = new GUIStyle();
lineStyle.normal.background = MakeTexture(600, 1, new Color(1.0f, 1.0f, 1.0f, 0.1f));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(Screen.height - 45));
// Show only top `showingLimit` log entries
int fromIndex = filteredList.Count - showLimit;
if (fromIndex < 0)
fromIndex = 0;
for (int i = fromIndex; i < filteredList.Count; i++)
{
LogCatLog log = filteredList[i];
GUI.backgroundColor = log.GetBgColor();
GUILayout.BeginHorizontal(lineStyle);
GUILayout.Label(log.CreationDate + " | " + log.Message);
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
private Texture2D MakeTexture(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; i++)
pix [i] = col;
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
private void AddLog(LogCatLog log)
{
lock (logsList)
{
if (logsList.Count > memoryLimit + 1)
logsList.RemoveRange(0, logsList.Count - memoryLimit + 1);
logsList.Add(log);
}
}
void OnEnable()
{
#if UNITY_2017_3_OR_NEWER
CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted;
#endif
}
void OnDisable()
{
#if UNITY_2017_3_OR_NEWER
CompilationPipeline.assemblyCompilationStarted -= OnAssemblyCompilationStarted;
#endif
}
void OnDestroy()
{
StopLogCatProcess();
}
private void StopLogCatProcess()
{
if (logCatProcess == null)
{
return;
}
try
{
if (!logCatProcess.HasExited)
{
logCatProcess.Kill();
}
}
catch(InvalidOperationException)
{
// Just ignore it.
}
finally
{
logCatProcess.Dispose();
logCatProcess = null;
}
}
private void OnAssemblyCompilationStarted(string _)
{
StopLogCatProcess();
}
private class LogCatLog
{
public LogCatLog(string data)
{
// First char indicates error type:
// W - warning
// E - error
// D - debug
// I - info
// V - verbose
Match match = LogcatRegex.Match(data);
if (match.Success)
{
Type = match.Groups[2].Value[0];
Message = match.Groups[3].Value;
CreationDate = match.Groups[1].Value;
}
else
{
Type = 'V';
Message = data;
CreationDate = DateTime.Now.ToString("MM-dd HH:mm:ss.fff");
}
}
public string CreationDate
{
get;
set;
}
public char Type
{
get;
set;
}
public string Message
{
get;
set;
}
public Color GetBgColor()
{
switch (Type)
{
case 'W':
return Color.yellow;
case 'I':
return Color.green;
case 'E':
return Color.red;
case 'D':
return Color.blue;
case 'V':
default:
return Color.grey;
}
}
}
private static string GetAdbPath()
{
#if UNITY_2019_1_OR_NEWER
ADB adb = ADB.GetInstance();
return adb == null ? string.Empty : adb.GetADBPath();
#else
string androidSdkRoot = EditorPrefs.GetString("AndroidSdkRoot");
if (string.IsNullOrEmpty(androidSdkRoot))
{
return string.Empty;
}
return Path.Combine(androidSdkRoot, Path.Combine("platform-tools", "adb"));
#endif
}
}
#endif