forked from bbbscarter/UberLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUberLoggerEditorWindow.cs
More file actions
executable file
·972 lines (826 loc) · 33 KB
/
UberLoggerEditorWindow.cs
File metadata and controls
executable file
·972 lines (826 loc) · 33 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System;
using UberLogger;
using System.Text.RegularExpressions;
/// <summary>
/// The console logging frontend.
/// Pulls data from the UberLoggerEditor backend
/// </summary>
public class UberLoggerEditorWindow : EditorWindow, UberLoggerEditor.ILoggerWindow
{
[MenuItem("Window/Show Uber Console")]
static public void ShowLogWindow()
{
Init();
}
static public void Init()
{
var window = ScriptableObject.CreateInstance<UberLoggerEditorWindow>();
window.Show();
window.position = new Rect(200,200,400,300);
window.CurrentTopPaneHeight = window.position.height/2;
}
public void OnLogChange(LogInfo logInfo)
{
Dirty = true;
// Repaint();
}
void OnInspectorUpdate()
{
// Debug.Log("Update");
if(Dirty)
{
Repaint();
}
}
void OnEnable()
{
// Connect to or create the backend
if(!EditorLogger)
{
EditorLogger = UberLogger.Logger.GetLogger<UberLoggerEditor>();
if(!EditorLogger)
{
EditorLogger = UberLoggerEditor.Create();
}
}
// UberLogger doesn't allow for duplicate loggers, so this is safe
// And, due to Unity serialisation stuff, necessary to do to it here.
UberLogger.Logger.AddLogger(EditorLogger);
EditorLogger.AddWindow(this);
// _OR_NEWER only became available from 5.3
#if UNITY_5 || UNITY_5_3_OR_NEWER
titleContent.text = "Uber Console";
#else
title = "Uber Console";
#endif
ClearSelectedMessage();
SmallErrorIcon = EditorGUIUtility.FindTexture( "d_console.erroricon.sml" ) ;
SmallWarningIcon = EditorGUIUtility.FindTexture( "d_console.warnicon.sml" ) ;
SmallMessageIcon = EditorGUIUtility.FindTexture( "d_console.infoicon.sml" ) ;
ErrorIcon = SmallErrorIcon;
WarningIcon = SmallWarningIcon;
MessageIcon = SmallMessageIcon;
Dirty = true;
Repaint();
}
/// <summary>
/// Converts the entire message log to a multiline string
/// </summary>
public string ExtractLogListToString()
{
string result = "";
foreach (CountedLog log in RenderLogs)
{
UberLogger.LogInfo logInfo = log.Log;
result += logInfo.GetRelativeTimeStampAsString() + ": " + logInfo.Severity + ": " + logInfo.Message + "\n";
}
return result;
}
/// <summary>
/// Converts the currently-displayed stack to a multiline string
/// </summary>
public string ExtractLogDetailsToString()
{
string result = "";
if (RenderLogs.Count > 0 && SelectedRenderLog >= 0)
{
var countedLog = RenderLogs[SelectedRenderLog];
var log = countedLog.Log;
for (int c1 = 0; c1 < log.Callstack.Count; c1++)
{
var frame = log.Callstack[c1];
var methodName = frame.GetFormattedMethodName();
result += methodName + "\n";
}
}
return result;
}
/// <summary>
/// Handle "Copy" command; copies log & stacktrace contents to clipboard
/// </summary>
public void HandleCopyToClipboard()
{
const string copyCommandName = "Copy";
Event e = Event.current;
if (e.type == EventType.ValidateCommand && e.commandName == copyCommandName)
{
// Respond to "Copy" command
// Confirm that we will consume the command; this will result in the command being re-issued with type == EventType.ExecuteCommand
e.Use();
}
else if (e.type == EventType.ExecuteCommand && e.commandName == copyCommandName)
{
// Copy current message log and current stack to the clipboard
// Convert all messages to a single long string
// It would be preferable to only copy one of the two, but that requires UberLogger to have focus handling
// between the message log and stack views
string result = ExtractLogListToString();
result += "\n";
// Convert current callstack to a single long string
result += ExtractLogDetailsToString();
GUIUtility.systemCopyBuffer = result;
}
}
Vector2 DrawPos;
public void OnGUI()
{
//Set up the basic style, based on the Unity defaults
//A bit hacky, but means we don't have to ship an editor guistyle and can fit in to pro and free skins
Color defaultLineColor = GUI.backgroundColor;
GUIStyle unityLogLineEven = null;
GUIStyle unityLogLineOdd = null;
GUIStyle unitySmallLogLine = null;
foreach(var style in GUI.skin.customStyles)
{
if (style.name=="CN EntryBackEven") unityLogLineEven = style;
else if(style.name=="CN EntryBackOdd") unityLogLineOdd = style;
else if(style.name=="CN StatusInfo") unitySmallLogLine = style;
}
EntryStyleBackEven = new GUIStyle(unitySmallLogLine);
EntryStyleBackEven.normal = unityLogLineEven.normal;
EntryStyleBackEven.margin = new RectOffset(0,0,0,0);
EntryStyleBackEven.border = new RectOffset(0,0,0,0);
EntryStyleBackEven.fixedHeight = 0;
EntryStyleBackOdd = new GUIStyle(EntryStyleBackEven);
EntryStyleBackOdd.normal = unityLogLineOdd.normal;
// EntryStyleBackOdd = new GUIStyle(unityLogLine);
SizerLineColour = new Color(defaultLineColor.r*0.5f, defaultLineColor.g*0.5f, defaultLineColor.b*0.5f);
// GUILayout.BeginVertical(GUILayout.Height(topPanelHeaderHeight), GUILayout.MinHeight(topPanelHeaderHeight));
ResizeTopPane();
DrawPos = Vector2.zero;
DrawToolbar();
DrawFilter();
DrawChannels();
float logPanelHeight = CurrentTopPaneHeight-DrawPos.y;
if(Dirty)
{
CurrentLogList = EditorLogger.CopyLogInfo();
}
DrawLogList(logPanelHeight);
DrawPos.y += DividerHeight;
DrawLogDetails();
HandleCopyToClipboard();
//If we're dirty, do a repaint
Dirty = false;
if(MakeDirty)
{
Dirty = true;
MakeDirty = false;
Repaint();
}
}
//Some helper functions to draw buttons that are only as big as their text
bool ButtonClamped(string text, GUIStyle style, out Vector2 size)
{
var content = new GUIContent(text);
size = style.CalcSize(content);
var rect = new Rect(DrawPos, size);
return GUI.Button(rect, text, style);
}
bool ToggleClamped(bool state, string text, GUIStyle style, out Vector2 size)
{
var content = new GUIContent(text);
return ToggleClamped(state, content, style, out size);
}
bool ToggleClamped(bool state, GUIContent content, GUIStyle style, out Vector2 size)
{
size = style.CalcSize(content);
Rect drawRect = new Rect(DrawPos, size);
return GUI.Toggle(drawRect, state, content, style);
}
void LabelClamped(string text, GUIStyle style, out Vector2 size)
{
var content = new GUIContent(text);
size = style.CalcSize(content);
Rect drawRect = new Rect(DrawPos, size);
GUI.Label(drawRect, text, style);
}
/// <summary>
/// Draws the thin, Unity-style toolbar showing error counts and toggle buttons
/// </summary>
void DrawToolbar()
{
var toolbarStyle = EditorStyles.toolbarButton;
Vector2 elementSize;
if(ButtonClamped("Clear", EditorStyles.toolbarButton, out elementSize))
{
EditorLogger.Clear();
}
DrawPos.x += elementSize.x;
EditorLogger.ClearOnPlay = ToggleClamped(EditorLogger.ClearOnPlay, "Clear On Play", EditorStyles.toolbarButton, out elementSize);
DrawPos.x += elementSize.x;
EditorLogger.PauseOnError = ToggleClamped(EditorLogger.PauseOnError, "Error Pause", EditorStyles.toolbarButton, out elementSize);
DrawPos.x += elementSize.x;
var showTimes = ToggleClamped(ShowTimes, "Times", EditorStyles.toolbarButton, out elementSize);
if(showTimes!=ShowTimes)
{
MakeDirty = true;
ShowTimes = showTimes;
}
DrawPos.x += elementSize.x;
var showChannels = ToggleClamped(ShowChannels, "Channels", EditorStyles.toolbarButton, out elementSize);
if (showChannels != ShowChannels)
{
MakeDirty = true;
ShowChannels = showChannels;
}
DrawPos.x += elementSize.x;
var collapse = ToggleClamped(Collapse, "Collapse", EditorStyles.toolbarButton, out elementSize);
if(collapse!=Collapse)
{
MakeDirty = true;
Collapse = collapse;
SelectedRenderLog = -1;
}
DrawPos.x += elementSize.x;
ScrollFollowMessages = ToggleClamped(ScrollFollowMessages, "Follow", EditorStyles.toolbarButton, out elementSize);
DrawPos.x += elementSize.x;
var errorToggleContent = new GUIContent(EditorLogger.NoErrors.ToString(), SmallErrorIcon);
var warningToggleContent = new GUIContent(EditorLogger.NoWarnings.ToString(), SmallWarningIcon);
var messageToggleContent = new GUIContent(EditorLogger.NoMessages.ToString(), SmallMessageIcon);
float totalErrorButtonWidth = toolbarStyle.CalcSize(errorToggleContent).x + toolbarStyle.CalcSize(warningToggleContent).x + toolbarStyle.CalcSize(messageToggleContent).x;
float errorIconX = position.width-totalErrorButtonWidth;
if(errorIconX > DrawPos.x)
{
DrawPos.x = errorIconX;
}
var showErrors = ToggleClamped(ShowErrors, errorToggleContent, toolbarStyle, out elementSize);
DrawPos.x += elementSize.x;
var showWarnings = ToggleClamped(ShowWarnings, warningToggleContent, toolbarStyle, out elementSize);
DrawPos.x += elementSize.x;
var showMessages = ToggleClamped(ShowMessages, messageToggleContent, toolbarStyle, out elementSize);
DrawPos.x += elementSize.x;
DrawPos.y += elementSize.y;
DrawPos.x = 0;
//If the errors/warning to show has changed, clear the selected message
if(showErrors!=ShowErrors || showWarnings!=ShowWarnings || showMessages!=ShowMessages)
{
ClearSelectedMessage();
MakeDirty = true;
}
ShowWarnings = showWarnings;
ShowMessages = showMessages;
ShowErrors = showErrors;
}
/// <summary>
/// Draws the channel selector
/// </summary>
void DrawChannels()
{
var channels = GetChannels();
int currentChannelIndex = 0;
for(int c1=0; c1<channels.Count; c1++)
{
if(channels[c1]==CurrentChannel)
{
currentChannelIndex = c1;
break;
}
}
var content = new GUIContent("S");
var size = GUI.skin.button.CalcSize(content);
var drawRect = new Rect(DrawPos, new Vector2(position.width, size.y));
currentChannelIndex = GUI.SelectionGrid(drawRect, currentChannelIndex, channels.ToArray(), channels.Count);
if(CurrentChannel!=channels[currentChannelIndex])
{
CurrentChannel = channels[currentChannelIndex];
ClearSelectedMessage();
MakeDirty = true;
}
DrawPos.y+=size.y;
}
/// <summary>
/// Based on filter and channel selections, should this log be shown?
/// </summary>
bool ShouldShowLog(System.Text.RegularExpressions.Regex regex, LogInfo log)
{
if(log.Channel==CurrentChannel || CurrentChannel=="All" || (CurrentChannel=="No Channel" && String.IsNullOrEmpty(log.Channel)))
{
if((log.Severity==LogSeverity.Message && ShowMessages)
|| (log.Severity==LogSeverity.Warning && ShowWarnings)
|| (log.Severity==LogSeverity.Error && ShowErrors))
{
if(regex==null || regex.IsMatch(log.Message))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Converts a given log element into a piece of gui content to be displayed
/// </summary>
GUIContent GetLogLineGUIContent(UberLogger.LogInfo log, bool showTimes, bool showChannels)
{
var showMessage = log.Message;
//Make all messages single line
showMessage = showMessage.Replace(UberLogger.Logger.UnityInternalNewLine, " ");
showMessage = string.Format("{0}{1}{2}{3}{4}",
showChannels ? "[" + log.Channel + "]" : "",
showTimes && showChannels ? " " : "",
showTimes ? log.GetRelativeTimeStampAsString() : "",
showChannels || showTimes ? " : " : "",
showMessage
);
var content = new GUIContent(showMessage, GetIconForLog(log));
return content;
}
/// <summary>
/// Draws the main log panel
/// </summary>
public void DrawLogList(float height)
{
var oldColor = GUI.backgroundColor;
float buttonY = 0;
System.Text.RegularExpressions.Regex filterRegex = null;
if(!String.IsNullOrEmpty(FilterRegex))
{
filterRegex = new Regex(FilterRegex);
}
var collapseBadgeStyle = EditorStyles.miniButton;
var logLineStyle = EntryStyleBackEven;
// If we've been marked dirty, we need to recalculate the elements to be displayed
if(Dirty)
{
LogListMaxWidth = 0;
LogListLineHeight = 0;
CollapseBadgeMaxWidth = 0;
RenderLogs.Clear();
//When collapsed, count up the unique elements and use those to display
if(Collapse)
{
var collapsedLines = new Dictionary<string, CountedLog>();
var collapsedLinesList = new List<CountedLog>();
foreach(var log in CurrentLogList)
{
if(ShouldShowLog(filterRegex, log))
{
var matchString = log.Message + "!$" + log.Severity + "!$" + log.Channel;
CountedLog countedLog;
if(collapsedLines.TryGetValue(matchString, out countedLog))
{
countedLog.Count++;
}
else
{
countedLog = new CountedLog(log, 1);
collapsedLines.Add(matchString, countedLog);
collapsedLinesList.Add(countedLog);
}
}
}
foreach(var countedLog in collapsedLinesList)
{
var content = GetLogLineGUIContent(countedLog.Log, ShowTimes, ShowChannels);
RenderLogs.Add(countedLog);
var logLineSize = logLineStyle.CalcSize(content);
LogListMaxWidth = Mathf.Max(LogListMaxWidth, logLineSize.x);
LogListLineHeight = Mathf.Max(LogListLineHeight, logLineSize.y);
var collapseBadgeContent = new GUIContent(countedLog.Count.ToString());
var collapseBadgeSize = collapseBadgeStyle.CalcSize(collapseBadgeContent);
CollapseBadgeMaxWidth = Mathf.Max(CollapseBadgeMaxWidth, collapseBadgeSize.x);
}
}
//If we're not collapsed, display everything in order
else
{
foreach(var log in CurrentLogList)
{
if(ShouldShowLog(filterRegex, log))
{
var content = GetLogLineGUIContent(log, ShowTimes, ShowChannels);
RenderLogs.Add(new CountedLog(log, 1));
var logLineSize = logLineStyle.CalcSize(content);
LogListMaxWidth = Mathf.Max(LogListMaxWidth, logLineSize.x);
LogListLineHeight = Mathf.Max(LogListLineHeight, logLineSize.y);
}
}
}
LogListMaxWidth += CollapseBadgeMaxWidth;
}
var scrollRect = new Rect(DrawPos, new Vector2(position.width, height));
float lineWidth = Mathf.Max(LogListMaxWidth, scrollRect.width);
var contentRect = new Rect(0, 0, lineWidth, RenderLogs.Count*LogListLineHeight);
Vector2 lastScrollPosition = LogListScrollPosition;
LogListScrollPosition = GUI.BeginScrollView(scrollRect, LogListScrollPosition, contentRect);
//If we're following the messages but the user has moved, cancel following
if(ScrollFollowMessages)
{
if(lastScrollPosition.y - LogListScrollPosition.y > LogListLineHeight)
{
UberDebug.UnityLog(String.Format("{0} {1}", lastScrollPosition.y, LogListScrollPosition.y));
ScrollFollowMessages = false;
}
}
float logLineX = CollapseBadgeMaxWidth;
//Render all the elements
int firstRenderLogIndex = (int) (LogListScrollPosition.y/LogListLineHeight);
int lastRenderLogIndex = firstRenderLogIndex + (int) (height/LogListLineHeight);
firstRenderLogIndex = Mathf.Clamp(firstRenderLogIndex, 0, RenderLogs.Count);
lastRenderLogIndex = Mathf.Clamp(lastRenderLogIndex, 0, RenderLogs.Count);
buttonY = firstRenderLogIndex*LogListLineHeight;
for(int renderLogIndex=firstRenderLogIndex; renderLogIndex<lastRenderLogIndex; renderLogIndex++)
{
var countedLog = RenderLogs[renderLogIndex];
var log = countedLog.Log;
logLineStyle = (renderLogIndex%2==0) ? EntryStyleBackEven : EntryStyleBackOdd;
if(renderLogIndex==SelectedRenderLog)
{
GUI.backgroundColor = new Color(0.5f, 0.5f, 1);
}
else
{
GUI.backgroundColor = Color.white;
}
//Make all messages single line
var content = GetLogLineGUIContent(log, ShowTimes, ShowChannels);
var drawRect = new Rect(logLineX, buttonY, contentRect.width, LogListLineHeight);
if(GUI.Button(drawRect, content, logLineStyle))
{
//Select a message, or jump to source if it's double-clicked
if(renderLogIndex==SelectedRenderLog)
{
if(EditorApplication.timeSinceStartup-LastMessageClickTime<DoubleClickInterval)
{
LastMessageClickTime = 0;
// Attempt to display source code associated with messages. Search through all stackframes,
// until we find a stackframe that can be displayed in source code view
for (int frame = 0; frame < log.Callstack.Count; frame++)
{
if (JumpToSource(log.Callstack[frame]))
break;
}
}
else
{
LastMessageClickTime = EditorApplication.timeSinceStartup;
}
}
else
{
SelectedRenderLog = renderLogIndex;
SelectedCallstackFrame = -1;
LastMessageClickTime = EditorApplication.timeSinceStartup;
}
//Always select the game object that is the source of this message
var go = log.Source as GameObject;
if(go!=null)
{
Selection.activeGameObject = go;
}
}
if(Collapse)
{
var collapseBadgeContent = new GUIContent(countedLog.Count.ToString());
var collapseBadgeSize = collapseBadgeStyle.CalcSize(collapseBadgeContent);
var collapseBadgeRect = new Rect(0, buttonY, collapseBadgeSize.x, collapseBadgeSize.y);
GUI.Button(collapseBadgeRect, collapseBadgeContent, collapseBadgeStyle);
}
buttonY += LogListLineHeight;
}
//If we're following the log, move to the end
if(ScrollFollowMessages && RenderLogs.Count>0)
{
LogListScrollPosition.y = ((RenderLogs.Count+1)*LogListLineHeight)-scrollRect.height;
}
GUI.EndScrollView();
DrawPos.y += height;
DrawPos.x = 0;
GUI.backgroundColor = oldColor;
}
/// <summary>
/// The bottom of the panel - details of the selected log
/// </summary>
public void DrawLogDetails()
{
var oldColor = GUI.backgroundColor;
SelectedRenderLog = Mathf.Clamp(SelectedRenderLog, 0, CurrentLogList.Count);
if(RenderLogs.Count>0 && SelectedRenderLog>=0)
{
var countedLog = RenderLogs[SelectedRenderLog];
var log = countedLog.Log;
var logLineStyle = EntryStyleBackEven;
var sourceStyle = new GUIStyle(GUI.skin.textArea);
sourceStyle.richText = true;
var drawRect = new Rect(DrawPos, new Vector2(position.width-DrawPos.x, position.height-DrawPos.y));
//Work out the content we need to show, and the sizes
var detailLines = new List<GUIContent>();
float contentHeight = 0;
float contentWidth = 0;
float lineHeight = 0;
for(int c1=0; c1<log.Callstack.Count; c1++)
{
var frame = log.Callstack[c1];
var methodName = frame.GetFormattedMethodNameWithFileName();
if(!String.IsNullOrEmpty(methodName))
{
var content = new GUIContent(methodName);
detailLines.Add(content);
var contentSize = logLineStyle.CalcSize(content);
contentHeight += contentSize.y;
lineHeight = Mathf.Max(lineHeight, contentSize.y);
contentWidth = Mathf.Max(contentSize.x, contentWidth);
if(ShowFrameSource && c1==SelectedCallstackFrame)
{
var sourceContent = GetFrameSourceGUIContent(frame);
if(sourceContent!=null)
{
var sourceSize = sourceStyle.CalcSize(sourceContent);
contentHeight += sourceSize.y;
contentWidth = Mathf.Max(sourceSize.x, contentWidth);
}
}
}
}
//Render the content
var contentRect = new Rect(0, 0, Mathf.Max(contentWidth, drawRect.width), contentHeight);
LogDetailsScrollPosition = GUI.BeginScrollView(drawRect, LogDetailsScrollPosition, contentRect);
float lineY = 0;
for(int c1=0; c1<detailLines.Count; c1++)
{
var lineContent = detailLines[c1];
if(lineContent!=null)
{
logLineStyle = (c1%2==0) ? EntryStyleBackEven : EntryStyleBackOdd;
if(c1==SelectedCallstackFrame)
{
GUI.backgroundColor = new Color(0.5f, 0.5f, 1);
}
else
{
GUI.backgroundColor = Color.white;
}
var frame = log.Callstack[c1];
var lineRect = new Rect(0, lineY, contentRect.width, lineHeight);
// Handle clicks on the stack frame
if(GUI.Button(lineRect, lineContent, logLineStyle))
{
if(c1==SelectedCallstackFrame)
{
if(Event.current.button==1)
{
ToggleShowSource(frame);
Repaint();
}
else
{
if(EditorApplication.timeSinceStartup-LastFrameClickTime<DoubleClickInterval)
{
LastFrameClickTime = 0;
JumpToSource(frame);
}
else
{
LastFrameClickTime = EditorApplication.timeSinceStartup;
}
}
}
else
{
SelectedCallstackFrame = c1;
LastFrameClickTime = EditorApplication.timeSinceStartup;
}
}
lineY += lineHeight;
//Show the source code if needed
if(ShowFrameSource && c1==SelectedCallstackFrame)
{
GUI.backgroundColor = Color.white;
var sourceContent = GetFrameSourceGUIContent(frame);
if(sourceContent!=null)
{
var sourceSize = sourceStyle.CalcSize(sourceContent);
var sourceRect = new Rect(0, lineY, contentRect.width, sourceSize.y);
GUI.Label(sourceRect, sourceContent, sourceStyle);
lineY += sourceSize.y;
}
}
}
}
GUI.EndScrollView();
}
GUI.backgroundColor = oldColor;
}
Texture2D GetIconForLog(LogInfo log)
{
if(log.Severity==LogSeverity.Error)
{
return ErrorIcon;
}
if(log.Severity==LogSeverity.Warning)
{
return WarningIcon;
}
return MessageIcon;
}
void ToggleShowSource(LogStackFrame frame)
{
ShowFrameSource = !ShowFrameSource;
}
bool JumpToSource(LogStackFrame frame)
{
if (frame.FileName != null)
{
var osFileName = UberLogger.Logger.ConvertDirectorySeparatorsFromUnityToOS(frame.FileName);
var filename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), osFileName);
if (System.IO.File.Exists(filename))
{
if (UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(filename, frame.LineNumber))
return true;
}
}
return false;
}
GUIContent GetFrameSourceGUIContent(LogStackFrame frame)
{
var source = GetSourceForFrame(frame);
if(!String.IsNullOrEmpty(source))
{
var content = new GUIContent(source);
return content;
}
return null;
}
void DrawFilter()
{
Vector2 size;
LabelClamped("Filter Regex", GUI.skin.label, out size);
DrawPos.x += size.x;
string filterRegex = null;
bool clearFilter = false;
if(ButtonClamped("Clear", GUI.skin.button, out size))
{
clearFilter = true;
GUIUtility.keyboardControl = 0;
GUIUtility.hotControl = 0;
}
DrawPos.x += size.x;
var drawRect = new Rect(DrawPos, new Vector2(position.width-DrawPos.x, size.y));
filterRegex = EditorGUI.TextArea(drawRect, FilterRegex);
if(clearFilter)
{
filterRegex = null;
}
//If the filter has changed, invalidate our currently selected message
if(filterRegex!=FilterRegex)
{
ClearSelectedMessage();
FilterRegex = filterRegex;
MakeDirty = true;
}
DrawPos.y += size.y;
DrawPos.x = 0;
}
List<string> GetChannels()
{
if(Dirty)
{
CurrentChannels = EditorLogger.CopyChannels();
}
var categories = CurrentChannels;
var channelList = new List<string>();
channelList.Add("All");
channelList.Add("No Channel");
channelList.AddRange(categories);
return channelList;
}
/// <summary>
/// Handles the split window stuff, somewhat bodgily
/// </summary>
private void ResizeTopPane()
{
//Set up the resize collision rect
CursorChangeRect = new Rect(0, CurrentTopPaneHeight, position.width, DividerHeight);
var oldColor = GUI.color;
GUI.color = SizerLineColour;
GUI.DrawTexture(CursorChangeRect, EditorGUIUtility.whiteTexture);
GUI.color = oldColor;
EditorGUIUtility.AddCursorRect(CursorChangeRect,MouseCursor.ResizeVertical);
if( Event.current.type == EventType.MouseDown && CursorChangeRect.Contains(Event.current.mousePosition))
{
Resize = true;
}
//If we've resized, store the new size and force a repaint
if(Resize)
{
CurrentTopPaneHeight = Event.current.mousePosition.y;
CursorChangeRect.Set(CursorChangeRect.x,CurrentTopPaneHeight,CursorChangeRect.width,CursorChangeRect.height);
Repaint();
}
if(Event.current.type == EventType.MouseUp)
Resize = false;
CurrentTopPaneHeight = Mathf.Clamp(CurrentTopPaneHeight, 100, position.height-100);
}
//Cache for GetSourceForFrame
string SourceLines;
LogStackFrame SourceLinesFrame;
/// <summary>
///If the frame has a valid filename, get the source string for the code around the frame
///This is cached, so we don't keep getting it.
/// </summary>
string GetSourceForFrame(LogStackFrame frame)
{
if(SourceLinesFrame==frame)
{
return SourceLines;
}
if(frame.FileName==null)
{
return "";
}
var osFileName = UberLogger.Logger.ConvertDirectorySeparatorsFromUnityToOS(frame.FileName);
var filename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), osFileName);
if (!System.IO.File.Exists(filename))
{
return "";
}
int lineNumber = frame.LineNumber-1;
int linesAround = 3;
var lines = System.IO.File.ReadAllLines(filename);
var firstLine = Mathf.Max(lineNumber-linesAround, 0);
var lastLine = Mathf.Min(lineNumber+linesAround+1, lines.Count());
SourceLines = "";
if(firstLine!=0)
{
SourceLines += "...\n";
}
for(int c1=firstLine; c1<lastLine; c1++)
{
string str = lines[c1] + "\n";
if(c1==lineNumber)
{
str = "<color=#ff0000ff>"+str+"</color>";
}
SourceLines += str;
}
if(lastLine!=lines.Count())
{
SourceLines += "...\n";
}
SourceLinesFrame = frame;
return SourceLines;
}
void ClearSelectedMessage()
{
SelectedRenderLog = -1;
SelectedCallstackFrame = -1;
ShowFrameSource = false;
}
Vector2 LogListScrollPosition;
Vector2 LogDetailsScrollPosition;
Texture2D ErrorIcon;
Texture2D WarningIcon;
Texture2D MessageIcon;
Texture2D SmallErrorIcon;
Texture2D SmallWarningIcon;
Texture2D SmallMessageIcon;
bool ShowChannels = true;
bool ShowTimes = true;
bool Collapse = false;
bool ScrollFollowMessages = false;
float CurrentTopPaneHeight = 200;
bool Resize = false;
Rect CursorChangeRect;
int SelectedRenderLog = -1;
bool Dirty=false;
bool MakeDirty=false;
float DividerHeight = 5;
double LastMessageClickTime = 0;
double LastFrameClickTime = 0;
const double DoubleClickInterval = 0.3f;
//Serialise the logger field so that Unity doesn't forget about the logger when you hit Play
[UnityEngine.SerializeField]
UberLoggerEditor EditorLogger;
List<UberLogger.LogInfo> CurrentLogList = new List<UberLogger.LogInfo>();
HashSet<string> CurrentChannels = new HashSet<string>();
//Standard unity pro colours
Color SizerLineColour;
GUIStyle EntryStyleBackEven;
GUIStyle EntryStyleBackOdd;
string CurrentChannel=null;
string FilterRegex = null;
bool ShowErrors = true;
bool ShowWarnings = true;
bool ShowMessages = true;
int SelectedCallstackFrame = 0;
bool ShowFrameSource = false;
class CountedLog
{
public UberLogger.LogInfo Log = null;
public Int32 Count=1;
public CountedLog(UberLogger.LogInfo log, Int32 count)
{
Log = log;
Count = count;
}
}
List<CountedLog> RenderLogs = new List<CountedLog>();
float LogListMaxWidth = 0;
float LogListLineHeight = 0;
float CollapseBadgeMaxWidth = 0;
}