LogWindow.cs 20 KB

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