ConsoleWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using BansheeEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Displays a list of log messages.
  9. /// </summary>
  10. public class ConsoleWindow : EditorWindow
  11. {
  12. /// <summary>
  13. /// Filter type that determines what kind of messages are shown in the console.
  14. /// </summary>
  15. [Flags]
  16. private enum EntryFilter
  17. {
  18. Info = 0x01, Warning = 0x02, Error = 0x04, All = Info | Warning | Error
  19. }
  20. private const int ENTRY_HEIGHT = 60;
  21. private GUIListView<ConsoleGUIEntry, ConsoleEntryData> listView;
  22. private List<ConsoleEntryData> entries = new List<ConsoleEntryData>();
  23. private EntryFilter filter = EntryFilter.All;
  24. /// <summary>
  25. /// Opens the console window.
  26. /// </summary>
  27. [MenuItem("Windows/Console", ButtonModifier.CtrlAlt, ButtonCode.C, 6000)]
  28. private static void OpenConsoleWindow()
  29. {
  30. OpenWindow<ConsoleWindow>();
  31. }
  32. /// <inheritdoc/>
  33. protected override LocString GetDisplayName()
  34. {
  35. return new LocEdString("Console");
  36. }
  37. private void OnInitialize()
  38. {
  39. Width = 300;
  40. GUILayoutY layout = GUI.AddLayoutY();
  41. listView = new GUIListView<ConsoleGUIEntry, ConsoleEntryData>(Width, Height, ENTRY_HEIGHT, layout);
  42. Debug.OnAdded += OnEntryAdded;
  43. // TODO - Add buttons to filter info/warning/error
  44. // TODO - Add button to clear console
  45. // TODO - Add button that splits the window vertically and displays details about an entry + callstack
  46. // TODO - On entry double-click open VS at that line
  47. // TODO - On callstack entry double-click open VS at that line
  48. }
  49. private void OnEditorUpdate()
  50. {
  51. listView.Update();
  52. }
  53. private void OnDestroy()
  54. {
  55. Debug.OnAdded -= OnEntryAdded;
  56. }
  57. /// <inheritdoc/>
  58. protected override void WindowResized(int width, int height)
  59. {
  60. listView.SetSize(width, height);
  61. base.WindowResized(width, height);
  62. }
  63. /// <summary>
  64. /// Triggered when a new entry is added in the debug log.
  65. /// </summary>
  66. /// <param name="type">Type of the message.</param>
  67. /// <param name="message">Message string.</param>
  68. private void OnEntryAdded(DebugMessageType type, string message)
  69. {
  70. ConsoleEntryData newEntry = new ConsoleEntryData();
  71. newEntry.type = type;
  72. int firstMatchIdx = -1;
  73. Regex regex = new Regex(@"\tat .* in (.*), line (\d*), column .*, namespace .*");
  74. var matches = regex.Matches(message);
  75. newEntry.callstack = new ConsoleEntryData.CallStackEntry[matches.Count];
  76. for(int i = 0; i < matches.Count; i++)
  77. {
  78. ConsoleEntryData.CallStackEntry callstackEntry = new ConsoleEntryData.CallStackEntry();
  79. callstackEntry.file = matches[i].Groups[0].Value;
  80. int.TryParse(matches[i].Groups[1].Value, out callstackEntry.line);
  81. newEntry.callstack[i] = callstackEntry;
  82. if (firstMatchIdx == -1)
  83. firstMatchIdx = matches[i].Index;
  84. }
  85. if (firstMatchIdx != -1)
  86. newEntry.message = message.Substring(0, firstMatchIdx);
  87. else
  88. newEntry.message = message;
  89. entries.Add(newEntry);
  90. if (DoesFilterMatch(type))
  91. listView.AddEntry(newEntry);
  92. }
  93. /// <summary>
  94. /// Changes the filter that controls what type of messages are displayed in the console.
  95. /// </summary>
  96. /// <param name="filter">Flags that control which type of messages should be displayed.</param>
  97. private void SetFilter(EntryFilter filter)
  98. {
  99. if (this.filter == filter)
  100. return;
  101. listView.Clear();
  102. foreach (var entry in entries)
  103. {
  104. if (DoesFilterMatch(entry.type))
  105. listView.AddEntry(entry);
  106. }
  107. this.filter = filter;
  108. }
  109. /// <summary>
  110. /// Checks if the currently active entry filter matches the provided type (i.e. the entry with the type should be
  111. /// displayed).
  112. /// </summary>
  113. /// <param name="type">Type of the entry to check.</param>
  114. /// <returns>True if the entry with the specified type should be displayed in the console.</returns>
  115. private bool DoesFilterMatch(DebugMessageType type)
  116. {
  117. switch (type)
  118. {
  119. case DebugMessageType.Info:
  120. return filter.HasFlag(EntryFilter.Info);
  121. case DebugMessageType.Warning:
  122. return filter.HasFlag(EntryFilter.Warning);
  123. case DebugMessageType.Error:
  124. return filter.HasFlag(EntryFilter.Error);
  125. }
  126. return false;
  127. }
  128. /// <summary>
  129. /// Removes all entries from the console.
  130. /// </summary>
  131. private void Clear()
  132. {
  133. listView.Clear();
  134. entries.Clear();
  135. }
  136. /// <summary>
  137. /// Contains data for a single entry in the console.
  138. /// </summary>
  139. private class ConsoleEntryData : GUIListViewData
  140. {
  141. /// <summary>
  142. /// Contains data for a single entry in a call stack associated with a console entry.
  143. /// </summary>
  144. public class CallStackEntry
  145. {
  146. public string file;
  147. public int line;
  148. }
  149. public DebugMessageType type;
  150. public string message;
  151. public CallStackEntry[] callstack;
  152. }
  153. /// <summary>
  154. /// Contains GUI elements used for displaying a single entry in the console.
  155. /// </summary>
  156. private class ConsoleGUIEntry : GUIListViewEntry<ConsoleEntryData>
  157. {
  158. private GUILabel messageLabel;
  159. /// <inheritdoc/>
  160. public override void BuildGUI()
  161. {
  162. messageLabel = new GUILabel(new LocEdString(""), EditorStyles.MultiLineLabel, GUIOption.FixedHeight(ENTRY_HEIGHT));
  163. Layout.AddElement(messageLabel);
  164. }
  165. /// <inheritdoc/>
  166. public override void UpdateContents(ConsoleEntryData data)
  167. {
  168. messageLabel.SetContent(new LocEdString(data.message));
  169. }
  170. }
  171. }
  172. }