2
0

LogWindow.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace bs.Editor
  8. {
  9. /** @addtogroup Windows
  10. * @{
  11. */
  12. /// <summary>
  13. /// Displays a list of log messages.
  14. /// </summary>
  15. [DefaultSize(600, 300)]
  16. public class LogWindow : EditorWindow
  17. {
  18. #region Constants
  19. public const string CLEAR_ON_PLAY_KEY = "EditorClearLogOnPlay";
  20. private const int TITLE_HEIGHT = 25;
  21. private const int ENTRY_HEIGHT = 39;
  22. private const int SEPARATOR_WIDTH = 3;
  23. private const float DETAILS_PANE_SIZE_PCT = 0.7f;
  24. private static readonly Color SEPARATOR_COLOR = new Color(33.0f / 255.0f, 33.0f / 255.0f, 33.0f / 255.0f);
  25. #endregion
  26. #region Fields
  27. private static int sSelectedElementIdx = -1;
  28. private GUIListView<ConsoleGUIEntry, ConsoleEntryData> listView;
  29. private List<ConsoleEntryData> entries = new List<ConsoleEntryData>();
  30. private List<ConsoleEntryData> filteredEntries = new List<ConsoleEntryData>();
  31. private EntryFilter filter = EntryFilter.All;
  32. private GUITexture detailsSeparator;
  33. private GUIScrollArea detailsArea;
  34. #endregion
  35. #region Private properties
  36. /// <summary>
  37. /// Returns the height of the list area.
  38. /// </summary>
  39. private int ListHeight
  40. {
  41. get { return Height - TITLE_HEIGHT; }
  42. }
  43. #endregion
  44. #region Public methods
  45. /// <summary>
  46. /// Rebuilds the list of all entires in the console.
  47. /// </summary>
  48. public void Refresh()
  49. {
  50. ClearEntries();
  51. LogEntry[] existingEntries = Debug.Messages;
  52. for (int i = 0; i < existingEntries.Length; i++)
  53. OnEntryAdded(existingEntries[i].type, existingEntries[i].message);
  54. }
  55. #endregion
  56. #region Private methods
  57. /// <summary>
  58. /// Opens the console window.
  59. /// </summary>
  60. [MenuItem("Windows/Log", ButtonModifier.CtrlAlt, ButtonCode.M, 6000)]
  61. private static void OpenConsoleWindow()
  62. {
  63. OpenWindow<LogWindow>();
  64. }
  65. /// <inheritdoc/>
  66. protected override LocString GetDisplayName()
  67. {
  68. return new LocEdString("Log");
  69. }
  70. private void OnInitialize()
  71. {
  72. GUILayoutY layout = GUI.AddLayoutY();
  73. GUILayoutX titleLayout = layout.AddLayoutX();
  74. GUIContentImages infoImages = new GUIContentImages(
  75. EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Info, 16, false),
  76. EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Info, 16, true));
  77. GUIContentImages warningImages = new GUIContentImages(
  78. EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Warning, 16, false),
  79. EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Warning, 16, true));
  80. GUIContentImages errorImages = new GUIContentImages(
  81. EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Error, 16, false),
  82. EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Error, 16, true));
  83. GUIToggle infoBtn = new GUIToggle(new GUIContent(infoImages), EditorStyles.Button, GUIOption.FixedHeight(TITLE_HEIGHT));
  84. GUIToggle warningBtn = new GUIToggle(new GUIContent(warningImages), EditorStyles.Button, GUIOption.FixedHeight(TITLE_HEIGHT));
  85. GUIToggle errorBtn = new GUIToggle(new GUIContent(errorImages), EditorStyles.Button, GUIOption.FixedHeight(TITLE_HEIGHT));
  86. GUIToggle detailsBtn = new GUIToggle(new LocEdString("Show details"), EditorStyles.Button, GUIOption.FixedHeight(TITLE_HEIGHT));
  87. GUIButton clearBtn = new GUIButton(new LocEdString("Clear"), GUIOption.FixedHeight(TITLE_HEIGHT));
  88. GUIToggle clearOnPlayBtn = new GUIToggle(new LocEdString("Clear on play"), EditorStyles.Button, GUIOption.FixedHeight(TITLE_HEIGHT));
  89. titleLayout.AddElement(infoBtn);
  90. titleLayout.AddElement(warningBtn);
  91. titleLayout.AddElement(errorBtn);
  92. titleLayout.AddFlexibleSpace();
  93. titleLayout.AddElement(detailsBtn);
  94. titleLayout.AddElement(clearBtn);
  95. titleLayout.AddElement(clearOnPlayBtn);
  96. infoBtn.Value = filter.HasFlag(EntryFilter.Info);
  97. warningBtn.Value = filter.HasFlag(EntryFilter.Warning);
  98. errorBtn.Value = filter.HasFlag(EntryFilter.Error);
  99. clearOnPlayBtn.Value = EditorSettings.GetBool(CLEAR_ON_PLAY_KEY, true);
  100. infoBtn.OnToggled += x =>
  101. {
  102. if (x)
  103. SetFilter(filter | EntryFilter.Info);
  104. else
  105. SetFilter(filter & ~EntryFilter.Info);
  106. };
  107. warningBtn.OnToggled += x =>
  108. {
  109. if (x)
  110. SetFilter(filter | EntryFilter.Warning);
  111. else
  112. SetFilter(filter & ~EntryFilter.Warning);
  113. };
  114. errorBtn.OnToggled += x =>
  115. {
  116. if (x)
  117. SetFilter(filter | EntryFilter.Error);
  118. else
  119. SetFilter(filter & ~EntryFilter.Error);
  120. };
  121. detailsBtn.OnToggled += ToggleDetailsPanel;
  122. clearBtn.OnClick += ClearLog;
  123. clearOnPlayBtn.OnToggled += ToggleClearOnPlay;
  124. GUILayoutX mainLayout = layout.AddLayoutX();
  125. listView = new GUIListView<ConsoleGUIEntry, ConsoleEntryData>(Width, ListHeight, ENTRY_HEIGHT, mainLayout);
  126. detailsSeparator = new GUITexture(Builtin.WhiteTexture, GUIOption.FixedWidth(SEPARATOR_WIDTH));
  127. detailsArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  128. mainLayout.AddElement(detailsSeparator);
  129. mainLayout.AddElement(detailsArea);
  130. detailsSeparator.Active = false;
  131. detailsArea.Active = false;
  132. detailsSeparator.SetTint(SEPARATOR_COLOR);
  133. Refresh();
  134. Debug.OnAdded += OnEntryAdded;
  135. }
  136. private void OnEditorUpdate()
  137. {
  138. listView.Update();
  139. }
  140. private void OnDestroy()
  141. {
  142. Debug.OnAdded -= OnEntryAdded;
  143. }
  144. /// <inheritdoc/>
  145. protected override void WindowResized(int width, int height)
  146. {
  147. if (detailsArea.Active)
  148. {
  149. int listWidth = width - (int)(width * DETAILS_PANE_SIZE_PCT) - SEPARATOR_WIDTH;
  150. listView.SetSize(listWidth, height - TITLE_HEIGHT);
  151. }
  152. else
  153. listView.SetSize(width, height - TITLE_HEIGHT);
  154. base.WindowResized(width, height);
  155. }
  156. /// <summary>
  157. /// Triggered when a new entry is added in the debug log.
  158. /// </summary>
  159. /// <param name="type">Type of the message.</param>
  160. /// <param name="message">Message string.</param>
  161. private void OnEntryAdded(DebugMessageType type, string message)
  162. {
  163. // Check if compiler message or reported exception, otherwise parse it as a normal log message
  164. ParsedLogEntry logEntry = ScriptCodeManager.ParseCompilerMessage(message);
  165. if (logEntry == null)
  166. logEntry = Debug.ParseExceptionMessage(message);
  167. if (logEntry == null)
  168. logEntry = Debug.ParseLogMessage(message);
  169. ConsoleEntryData newEntry = new ConsoleEntryData();
  170. newEntry.type = type;
  171. newEntry.callstack = logEntry.callstack;
  172. newEntry.message = logEntry.message;
  173. entries.Add(newEntry);
  174. if (DoesFilterMatch(type))
  175. {
  176. listView.AddEntry(newEntry);
  177. filteredEntries.Add(newEntry);
  178. }
  179. }
  180. /// <summary>
  181. /// Changes the filter that controls what type of messages are displayed in the console.
  182. /// </summary>
  183. /// <param name="filter">Flags that control which type of messages should be displayed.</param>
  184. private void SetFilter(EntryFilter filter)
  185. {
  186. if (this.filter == filter)
  187. return;
  188. this.filter = filter;
  189. listView.Clear();
  190. filteredEntries.Clear();
  191. foreach (var entry in entries)
  192. {
  193. if (DoesFilterMatch(entry.type))
  194. {
  195. listView.AddEntry(entry);
  196. filteredEntries.Add(entry);
  197. }
  198. }
  199. sSelectedElementIdx = -1;
  200. }
  201. /// <summary>
  202. /// Checks if the currently active entry filter matches the provided type (the entry with the type that should be
  203. /// displayed).
  204. /// </summary>
  205. /// <param name="type">Type of the entry to check.</param>
  206. /// <returns>True if the entry with the specified type should be displayed in the console.</returns>
  207. private bool DoesFilterMatch(DebugMessageType type)
  208. {
  209. switch (type)
  210. {
  211. case DebugMessageType.Info:
  212. return filter.HasFlag(EntryFilter.Info);
  213. case DebugMessageType.Warning:
  214. case DebugMessageType.CompilerWarning:
  215. return filter.HasFlag(EntryFilter.Warning);
  216. case DebugMessageType.Error:
  217. case DebugMessageType.CompilerError:
  218. return filter.HasFlag(EntryFilter.Error);
  219. }
  220. return false;
  221. }
  222. /// <summary>
  223. /// Removes all entries from the debug log.
  224. /// </summary>
  225. private void ClearLog()
  226. {
  227. Debug.Clear();
  228. ClearEntries();
  229. }
  230. /// <summary>
  231. /// Removes all entries from the console.
  232. /// </summary>
  233. private void ClearEntries()
  234. {
  235. listView.Clear();
  236. entries.Clear();
  237. filteredEntries.Clear();
  238. sSelectedElementIdx = -1;
  239. RefreshDetailsPanel();
  240. }
  241. /// <summary>
  242. /// Shows or hides the details panel.
  243. /// </summary>
  244. /// <param name="show">True to show, false to hide.</param>
  245. private void ToggleDetailsPanel(bool show)
  246. {
  247. detailsArea.Active = show;
  248. detailsSeparator.Active = show;
  249. if (show)
  250. {
  251. int listWidth = Width - (int)(Width * DETAILS_PANE_SIZE_PCT) - SEPARATOR_WIDTH;
  252. listView.SetSize(listWidth, ListHeight);
  253. RefreshDetailsPanel();
  254. }
  255. else
  256. listView.SetSize(Width, ListHeight);
  257. }
  258. /// <summary>
  259. /// Toggles should the console be cleared when the play mode is entered.
  260. /// </summary>
  261. /// <param name="clear">True if the console should be cleared upon entering the play mode.</param>
  262. private void ToggleClearOnPlay(bool clear)
  263. {
  264. EditorSettings.SetBool(CLEAR_ON_PLAY_KEY, clear);
  265. }
  266. /// <summary>
  267. /// Updates the contents of the details panel according to the currently selected element.
  268. /// </summary>
  269. private void RefreshDetailsPanel()
  270. {
  271. detailsArea.Layout.Clear();
  272. if (sSelectedElementIdx != -1)
  273. {
  274. GUILayoutX paddingX = detailsArea.Layout.AddLayoutX();
  275. paddingX.AddSpace(5);
  276. GUILayoutY paddingY = paddingX.AddLayoutY();
  277. paddingX.AddSpace(5);
  278. paddingY.AddSpace(5);
  279. GUILayoutY mainLayout = paddingY.AddLayoutY();
  280. paddingY.AddSpace(5);
  281. ConsoleEntryData entry = filteredEntries[sSelectedElementIdx];
  282. LocString message = new LocEdString(entry.message);
  283. GUILabel messageLabel = new GUILabel(message, EditorStyles.MultiLineLabel, GUIOption.FlexibleHeight());
  284. mainLayout.AddElement(messageLabel);
  285. mainLayout.AddSpace(10);
  286. if (entry.callstack != null)
  287. {
  288. foreach (var call in entry.callstack)
  289. {
  290. string fileName = Path.GetFileName(call.file);
  291. string callMessage;
  292. if (string.IsNullOrEmpty(call.method))
  293. callMessage = "\tin " + fileName + ":" + call.line;
  294. else
  295. callMessage = "\t" + call.method + " in " + fileName + ":" + call.line;
  296. GUIButton callBtn = new GUIButton(new LocEdString(callMessage));
  297. mainLayout.AddElement(callBtn);
  298. CallStackEntry hoistedCall = call;
  299. callBtn.OnClick += () =>
  300. {
  301. CodeEditor.OpenFile(hoistedCall.file, hoistedCall.line);
  302. };
  303. }
  304. }
  305. mainLayout.AddFlexibleSpace();
  306. }
  307. else
  308. {
  309. GUILayoutX centerX = detailsArea.Layout.AddLayoutX();
  310. centerX.AddFlexibleSpace();
  311. GUILayoutY centerY = centerX.AddLayoutY();
  312. centerX.AddFlexibleSpace();
  313. centerY.AddFlexibleSpace();
  314. GUILabel nothingSelectedLbl = new GUILabel(new LocEdString("(No entry selected)"));
  315. centerY.AddElement(nothingSelectedLbl);
  316. centerY.AddFlexibleSpace();
  317. }
  318. }
  319. #endregion
  320. #region Types
  321. /// <summary>
  322. /// Filter type that determines what kind of messages are shown in the console.
  323. /// </summary>
  324. [Flags]
  325. private enum EntryFilter
  326. {
  327. Info = 0x01, Warning = 0x02, Error = 0x04, All = Info | Warning | Error
  328. }
  329. /// <summary>
  330. /// Contains data for a single entry in the console.
  331. /// </summary>
  332. private class ConsoleEntryData : GUIListViewData
  333. {
  334. public DebugMessageType type;
  335. public string message;
  336. public CallStackEntry[] callstack;
  337. }
  338. /// <summary>
  339. /// Contains GUI elements used for displaying a single entry in the console.
  340. /// </summary>
  341. private class ConsoleGUIEntry : GUIListViewEntry<ConsoleEntryData>
  342. {
  343. private const int CALLER_LABEL_HEIGHT = 11;
  344. private const int PADDING = 3;
  345. private const int MESSAGE_HEIGHT = ENTRY_HEIGHT - CALLER_LABEL_HEIGHT - PADDING * 2;
  346. private static readonly Color BG_COLOR = Color.DarkGray;
  347. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  348. private GUIPanel overlay;
  349. private GUIPanel main;
  350. private GUIPanel underlay;
  351. private GUITexture icon;
  352. private GUILabel messageLabel;
  353. private GUILabel functionLabel;
  354. private GUITexture background;
  355. private int entryIdx;
  356. private string file;
  357. private int line;
  358. /// <inheritdoc/>
  359. public override void BuildGUI()
  360. {
  361. main = Layout.AddPanel(0, 1, 1, GUIOption.FixedHeight(ENTRY_HEIGHT));
  362. overlay = main.AddPanel(-1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  363. underlay = main.AddPanel(1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  364. GUILayoutX mainLayout = main.AddLayoutX();
  365. GUILayoutY overlayLayout = overlay.AddLayoutY();
  366. GUILayoutY underlayLayout = underlay.AddLayoutY();
  367. icon = new GUITexture(null, GUIOption.FixedWidth(32), GUIOption.FixedHeight(32));
  368. messageLabel = new GUILabel(new LocEdString(""), EditorStyles.MultiLineLabel, GUIOption.FixedHeight(MESSAGE_HEIGHT));
  369. functionLabel = new GUILabel(new LocEdString(""), GUIOption.FixedHeight(CALLER_LABEL_HEIGHT));
  370. mainLayout.AddSpace(PADDING);
  371. GUILayoutY iconLayout = mainLayout.AddLayoutY();
  372. iconLayout.AddFlexibleSpace();
  373. iconLayout.AddElement(icon);
  374. iconLayout.AddFlexibleSpace();
  375. mainLayout.AddSpace(PADDING);
  376. GUILayoutY messageLayout = mainLayout.AddLayoutY();
  377. messageLayout.AddSpace(PADDING);
  378. messageLayout.AddElement(messageLabel);
  379. messageLayout.AddElement(functionLabel);
  380. messageLayout.AddSpace(PADDING);
  381. mainLayout.AddFlexibleSpace();
  382. mainLayout.AddSpace(PADDING);
  383. background = new GUITexture(Builtin.WhiteTexture, GUIOption.FixedHeight(ENTRY_HEIGHT));
  384. underlayLayout.AddElement(background);
  385. GUIButton button = new GUIButton(new LocEdString(""), EditorStyles.Blank, GUIOption.FixedHeight(ENTRY_HEIGHT));
  386. overlayLayout.AddElement(button);
  387. button.OnClick += OnClicked;
  388. button.OnDoubleClick += OnDoubleClicked;
  389. }
  390. /// <inheritdoc/>
  391. public override void UpdateContents(int index, ConsoleEntryData data)
  392. {
  393. if (index != sSelectedElementIdx)
  394. {
  395. if (index%2 != 0)
  396. {
  397. background.Visible = true;
  398. background.SetTint(BG_COLOR);
  399. }
  400. else
  401. {
  402. background.Visible = false;
  403. }
  404. }
  405. else
  406. {
  407. background.Visible = true;
  408. background.SetTint(SELECTION_COLOR);
  409. }
  410. switch (data.type)
  411. {
  412. case DebugMessageType.Info:
  413. icon.SetTexture(EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Info, 32, false));
  414. break;
  415. case DebugMessageType.Warning:
  416. case DebugMessageType.CompilerWarning:
  417. icon.SetTexture(EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Warning, 32, false));
  418. break;
  419. case DebugMessageType.Error:
  420. case DebugMessageType.CompilerError:
  421. icon.SetTexture(EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Error, 32, false));
  422. break;
  423. }
  424. messageLabel.SetContent(new LocEdString(data.message));
  425. string method = "";
  426. if (data.callstack != null && data.callstack.Length > 0)
  427. {
  428. string filePath = data.callstack[0].file;
  429. bool isFilePathValid = filePath.IndexOfAny(Path.GetInvalidPathChars()) == -1;
  430. if (isFilePathValid)
  431. file = Path.GetFileName(data.callstack[0].file);
  432. else
  433. file = "<unknown file>";
  434. line = data.callstack[0].line;
  435. if (string.IsNullOrEmpty(data.callstack[0].method))
  436. method = "\tin " + file + ":" + line;
  437. else
  438. method = "\t" + data.callstack[0].method + " in " + file + ":" + line;
  439. }
  440. else
  441. {
  442. file = "";
  443. line = 0;
  444. }
  445. functionLabel.SetContent(new LocEdString(method));
  446. entryIdx = index;
  447. }
  448. /// <summary>
  449. /// Triggered when the entry is selected.
  450. /// </summary>
  451. private void OnClicked()
  452. {
  453. sSelectedElementIdx = entryIdx;
  454. LogWindow window = GetWindow<LogWindow>();
  455. window.RefreshDetailsPanel();
  456. RefreshEntries();
  457. }
  458. /// <summary>
  459. /// Triggered when the entry is double-clicked.
  460. /// </summary>
  461. private void OnDoubleClicked()
  462. {
  463. if(!string.IsNullOrEmpty(file))
  464. CodeEditor.OpenFile(file, line);
  465. }
  466. }
  467. #endregion
  468. }
  469. /** @} */
  470. }