-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathLogViewerWindow.cs
268 lines (235 loc) · 11.3 KB
/
LogViewerWindow.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
using System;
using System.Collections.Generic;
using System.Linq;
using BepInEx.Logging;
using RuntimeUnityEditor.Core;
using RuntimeUnityEditor.Core.Inspector;
using RuntimeUnityEditor.Core.Inspector.Entries;
using RuntimeUnityEditor.Core.Utils;
using RuntimeUnityEditor.Core.Utils.Abstractions;
using UnityEngine;
using ContextMenu = RuntimeUnityEditor.Core.ContextMenu;
using LogLevel = BepInEx.Logging.LogLevel;
namespace RuntimeUnityEditor.Bepin5.LogViewer
{
/// <summary>
/// A way to display and filter BepInEx log messages.
/// </summary>
public sealed class LogViewerWindow : Window<LogViewerWindow>
{
private InitSettings.Setting<bool> _captureOnStartup;
/// <summary>
/// Enable on game startup
/// </summary>
public bool CaptureOnStartup
{
get => _captureOnStartup.Value;
set => _captureOnStartup.Value = value;
}
private bool _capture;
private LogViewerListener _listener;
/// <summary>
/// Enable capturing log messages
/// </summary>
public bool Capture
{
get => _capture;
set
{
if (_capture != value)
{
_capture = value;
if (_capture)
BepInEx.Logging.Logger.Listeners.Add(_listener);
else
BepInEx.Logging.Logger.Listeners.Remove(_listener);
}
}
}
private InitSettings.Setting<LogLevel> _logLevelFilter;
/// <summary>
/// Which log levels to capture
/// </summary>
public LogLevel LogLevelFilter
{
get => _logLevelFilter.Value;
set => _logLevelFilter.Value = value;
}
private string _searchString;
/// <summary>
/// Filter log messages by this string
/// </summary>
public string SearchString
{
get => _searchString;
set
{
if (_searchString != value)
{
_searchString = value;
UpdateFilteredLogEntries();
}
}
}
private void UpdateFilteredLogEntries()
{
_filteredLogEntries.Clear();
_filteredLogEntries.AddRange(_logEntries.Where(entry => entry.IsMatch(SearchString, LogLevelFilter)));
}
private readonly List<LogViewerEntry> _logEntries = new List<LogViewerEntry>();
private readonly List<LogViewerEntry> _filteredLogEntries = new List<LogViewerEntry>();
/// <inheritdoc />
protected override void Initialize(InitSettings initSettings)
{
Enabled = false;
DefaultScreenPosition = ScreenPartition.CenterLower;
DisplayName = "Logger";
Title = "BepInEx Log Viewer";
MinimumSize = new Vector2(640, 200);
_listener = new LogViewerListener(this);
_captureOnStartup = initSettings.RegisterSetting("Log Viewer", "Enable capture on startup", false, "Start capturing log messages as soon as possible after game starts.");
if (_captureOnStartup.Value)
Capture = true;
_logLevelFilter = initSettings.RegisterSetting("Log Viewer", "Log level filter", LogLevel.All, "Filter captured log messages by their log level.");
_logLevelFilter.ValueChanged += _ => UpdateFilteredLogEntries();
string GetTrimmedTypeName(ILogSource obj)
{
var fullName = obj?.GetType().GetSourceCodeRepresentation() ?? "NULL";
if (fullName.StartsWith("BepInEx.Logging.", StringComparison.Ordinal))
fullName = fullName.Substring("BepInEx.Logging.".Length);
return fullName;
}
ToStringConverter.AddConverter<ILogSource>(obj => $"{obj.SourceName} ({GetTrimmedTypeName(obj)})");
//ToStringConverter.AddConverter<ILogListener>(obj => $"{obj.GetType().FullName}");
}
private Vector2 _scrollPosition;
private int _itemHeight;
/// <inheritdoc />
protected override void DrawContents()
{
GUILayout.BeginHorizontal();
{
GUILayout.BeginHorizontal(GUI.skin.box, IMGUIUtils.LayoutOptionsExpandWidthTrue);
{
GUI.changed = false;
var searchString = SearchString;
var isEmpty = string.IsNullOrEmpty(searchString) && GUI.GetNameOfFocusedControl() != "sbox";
if (isEmpty) GUI.color = Color.gray;
GUI.SetNextControlName("sbox");
var newString = GUILayout.TextField(isEmpty ? "Search log text and stack traces..." : searchString, IMGUIUtils.LayoutOptionsExpandWidthTrue);
if (GUI.changed) SearchString = newString;
GUI.color = Color.white;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(GUI.skin.box, IMGUIUtils.LayoutOptionsExpandWidthFalse);
{
if (!Capture) GUI.color = Color.red;
Capture = GUILayout.Toggle(Capture, new GUIContent("Enable log capture", "Note: This can hurt performance, especially if there is log spam."));
GUI.color = Color.white;
CaptureOnStartup = GUILayout.Toggle(CaptureOnStartup, new GUIContent("Enable on game startup", "Warning: This can hurt performance, especially after running for a while!"));
if (GUILayout.Button("Clear the list"))
{
_logEntries.Clear();
_filteredLogEntries.Clear();
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.BeginHorizontal(GUI.skin.box);
{
GUILayout.Label(new GUIContent("Captured log levels:", "Only new log messages with these levels will be captured, therefore enabling levels will not show past log messages!"));
var filter = LogLevelFilter;
foreach (var logLevel in new[] { LogLevel.Debug, LogLevel.Info, LogLevel.Message, LogLevel.Warning, LogLevel.Error, LogLevel.Fatal, LogLevel.All })
{
GUI.changed = false;
var result = GUILayout.Toggle((filter & logLevel) != 0, logLevel.ToString());
if (GUI.changed)
LogLevelFilter = result ? filter | logLevel : filter & ~logLevel;
}
}
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal(GUI.skin.box);
{
if (GUILayout.Button("Sources")) Inspector.Instance.Push(new InstanceStackEntry(BepInEx.Logging.Logger.Sources, "Log Sources"), true);
if (GUILayout.Button("Listeners")) Inspector.Instance.Push(new InstanceStackEntry(BepInEx.Logging.Logger.Listeners, "Log Listeners"), true);
}
GUILayout.EndHorizontal();
}
GUILayout.EndHorizontal();
var heightMeasured = _itemHeight != 0;
_scrollPosition = GUILayout.BeginScrollView(_scrollPosition, false, true);
{
var logEntries = _filteredLogEntries;
var skipped = heightMeasured ? (int)(_scrollPosition.y / _itemHeight) : 0;
var visible = heightMeasured ? Mathf.Min((int)WindowRect.height / _itemHeight, logEntries.Count - skipped) : logEntries.Count;
var skippedAfter = logEntries.Count - skipped - visible + 3;
GUILayout.Space(skipped * _itemHeight);
for (var i = skipped; i < skipped + visible; i++)
{
var entry = logEntries[logEntries.Count - 1 - i];
GUILayout.BeginHorizontal(GUI.skin.box);
{
if (entry.DrawEntry())
{
if (IMGUIUtils.IsMouseRightClick() && ContextMenu.Initialized)
{
// todo better right click menu
if (entry.Sender != null)
ContextMenu.Instance.Show(entry.Sender);
else
RuntimeUnityEditorCore.Logger.Log(Core.Utils.Abstractions.LogLevel.Warning, $"[{nameof(LogViewerWindow)}] Sender is null, cannot inspect");
}
else
{
try
{
UnityFeatureHelper.systemCopyBuffer = entry.GetClipboardString();
RuntimeUnityEditorCore.Logger.Log(Core.Utils.Abstractions.LogLevel.Message, $"[{nameof(LogViewerWindow)}] Copied to clipboard");
}
catch (Exception e)
{
RuntimeUnityEditorCore.Logger.Log(Core.Utils.Abstractions.LogLevel.Message | Core.Utils.Abstractions.LogLevel.Error, $"[{nameof(LogViewerWindow)}] Failed to copy to clipboard: " + e.Message);
}
}
}
if (entry.Sender != null && GUILayout.Button("Inspect", IMGUIUtils.LayoutOptionsExpandWidthFalse))
Inspector.Instance.Push(new InstanceStackEntry(entry, entry.LogEventArgs.Source.SourceName + " -> Log entry"), true);
DnSpyHelper.DrawDnSpyButtonIfAvailable(entry.Method, new GUIContent("^", $"In dnSpy, attempt to navigate to the method that produced this log message:\n\n{entry.Method.GetFancyDescription()}"));
}
GUILayout.EndHorizontal();
if (!heightMeasured && Event.current.type == EventType.Repaint)
{
var newHeight = (int)GUILayoutUtility.GetLastRect().height;
if (_itemHeight == 0 || _itemHeight > newHeight) // handle long log messages that span multiple lines, we want to use the height of a single line
_itemHeight = newHeight;
}
}
GUILayout.Space(skippedAfter * _itemHeight);
}
GUILayout.EndScrollView();
TooltipWidth = Mathf.Min(777, Mathf.Max(300, (int)WindowRect.width - 100));
}
internal void OnLogEvent(object sender, LogEventArgs eventArgs)
{
try
{
var entry = LogViewerEntry.CreateFromLogEventArgs(sender, eventArgs);
AddEntry(entry);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private void AddEntry(LogViewerEntry entry)
{
_logEntries.Add(entry);
if (entry.IsMatch(SearchString, LogLevelFilter))
_filteredLogEntries.Add(entry);
}
}
}