ConsoleWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. GUILayoutY layout = GUI.AddLayoutY();
  40. listView = new GUIListView<ConsoleGUIEntry, ConsoleEntryData>(Width, Height, ENTRY_HEIGHT, layout);
  41. Debug.OnAdded += OnEntryAdded;
  42. // TODO - Add buttons to filter info/warning/error
  43. // TODO - Add button to clear console
  44. // TODO - Add button that splits the window vertically and displays details about an entry + callstack
  45. // TODO - On entry double-click open VS at that line
  46. // TODO - On callstack entry double-click open VS at that line
  47. }
  48. private void OnEditorUpdate()
  49. {
  50. listView.Update();
  51. }
  52. private void OnDestroy()
  53. {
  54. }
  55. /// <inheritdoc/>
  56. protected override void WindowResized(int width, int height)
  57. {
  58. listView.SetSize(width, height);
  59. base.WindowResized(width, height);
  60. }
  61. /// <summary>
  62. /// Triggered when a new entry is added in the debug log.
  63. /// </summary>
  64. /// <param name="type">Type of the message.</param>
  65. /// <param name="message">Message string.</param>
  66. private void OnEntryAdded(DebugMessageType type, string message)
  67. {
  68. ConsoleEntryData newEntry = new ConsoleEntryData();
  69. newEntry.type = type;
  70. int firstMatchIdx = -1;
  71. Regex regex = new Regex(@"\tat .* in (.*), line (\d*), column .*, namespace .*");
  72. var matches = regex.Matches(message);
  73. newEntry.callstack = new ConsoleEntryData.CallStackEntry[matches.Count];
  74. for(int i = 0; i < matches.Count; i++)
  75. {
  76. ConsoleEntryData.CallStackEntry callstackEntry = new ConsoleEntryData.CallStackEntry();
  77. callstackEntry.file = matches[i].Groups[0].Value;
  78. int.TryParse(matches[i].Groups[1].Value, out callstackEntry.line);
  79. newEntry.callstack[i] = callstackEntry;
  80. if (firstMatchIdx == -1)
  81. firstMatchIdx = matches[i].Index;
  82. }
  83. if (firstMatchIdx != -1)
  84. newEntry.message = message.Substring(0, firstMatchIdx);
  85. else
  86. newEntry.message = message;
  87. entries.Add(newEntry);
  88. if (DoesFilterMatch(type))
  89. listView.AddEntry(newEntry);
  90. }
  91. /// <summary>
  92. /// Changes the filter that controls what type of messages are displayed in the console.
  93. /// </summary>
  94. /// <param name="filter">Flags that control which type of messages should be displayed.</param>
  95. private void SetFilter(EntryFilter filter)
  96. {
  97. if (this.filter == filter)
  98. return;
  99. listView.Clear();
  100. foreach (var entry in entries)
  101. {
  102. if (DoesFilterMatch(entry.type))
  103. listView.AddEntry(entry);
  104. }
  105. this.filter = filter;
  106. }
  107. /// <summary>
  108. /// Checks if the currently active entry filter matches the provided type (i.e. the entry with the type should be
  109. /// displayed).
  110. /// </summary>
  111. /// <param name="type">Type of the entry to check.</param>
  112. /// <returns>True if the entry with the specified type should be displayed in the console.</returns>
  113. private bool DoesFilterMatch(DebugMessageType type)
  114. {
  115. switch (type)
  116. {
  117. case DebugMessageType.Info:
  118. return filter.HasFlag(EntryFilter.Info);
  119. case DebugMessageType.Warning:
  120. return filter.HasFlag(EntryFilter.Warning);
  121. case DebugMessageType.Error:
  122. return filter.HasFlag(EntryFilter.Error);
  123. }
  124. return false;
  125. }
  126. /// <summary>
  127. /// Removes all entries from the console.
  128. /// </summary>
  129. private void Clear()
  130. {
  131. listView.Clear();
  132. entries.Clear();
  133. }
  134. /// <summary>
  135. /// Contains data for a single entry in the console.
  136. /// </summary>
  137. private class ConsoleEntryData : GUIListViewData
  138. {
  139. /// <summary>
  140. /// Contains data for a single entry in a call stack associated with a console entry.
  141. /// </summary>
  142. public class CallStackEntry
  143. {
  144. public string file;
  145. public int line;
  146. }
  147. public DebugMessageType type;
  148. public string message;
  149. public CallStackEntry[] callstack;
  150. }
  151. /// <summary>
  152. /// Contains GUI elements used for displaying a single entry in the console.
  153. /// </summary>
  154. private class ConsoleGUIEntry : GUIListViewEntry<ConsoleEntryData>
  155. {
  156. private GUILabel messageLabel;
  157. /// <inheritdoc/>
  158. public override void BuildGUI()
  159. {
  160. messageLabel = new GUILabel(new LocEdString(""), EditorStyles.MultiLineLabel, GUIOption.FixedHeight(ENTRY_HEIGHT));
  161. Layout.AddElement(messageLabel);
  162. }
  163. /// <inheritdoc/>
  164. public override void UpdateContents(ConsoleEntryData data)
  165. {
  166. messageLabel.SetContent(new LocEdString(data.message));
  167. }
  168. }
  169. }
  170. }