LogWindow.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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].message, existingEntries[i].verbosity, existingEntries[i].category);
  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="message">Message string.</param>
  160. /// <param name="verbosity">Verbosity level defining message importance.</param>
  161. /// <param name="category">Category of the sub-system reporting the message.</param>
  162. private void OnEntryAdded(string message, LogVerbosity verbosity, int category)
  163. {
  164. // Check if compiler message or reported exception, otherwise parse it as a normal log message
  165. ParsedLogEntry logEntry = ScriptCodeManager.ParseCompilerMessage(message);
  166. if (logEntry == null)
  167. logEntry = Debug.ParseExceptionMessage(message);
  168. if (logEntry == null)
  169. logEntry = Debug.ParseLogMessage(message);
  170. ConsoleEntryData newEntry = new ConsoleEntryData();
  171. newEntry.verbosity = verbosity;
  172. newEntry.category = category;
  173. newEntry.callstack = logEntry.callstack;
  174. newEntry.message = logEntry.message;
  175. entries.Add(newEntry);
  176. if (DoesFilterMatch(verbosity))
  177. {
  178. listView.AddEntry(newEntry);
  179. filteredEntries.Add(newEntry);
  180. }
  181. }
  182. /// <summary>
  183. /// Changes the filter that controls what type of messages are displayed in the console.
  184. /// </summary>
  185. /// <param name="filter">Flags that control which type of messages should be displayed.</param>
  186. private void SetFilter(EntryFilter filter)
  187. {
  188. if (this.filter == filter)
  189. return;
  190. this.filter = filter;
  191. listView.Clear();
  192. filteredEntries.Clear();
  193. foreach (var entry in entries)
  194. {
  195. if (DoesFilterMatch(entry.verbosity))
  196. {
  197. listView.AddEntry(entry);
  198. filteredEntries.Add(entry);
  199. }
  200. }
  201. sSelectedElementIdx = -1;
  202. }
  203. /// <summary>
  204. /// Checks if the currently active entry filter matches the provided type (the entry with the type that should be
  205. /// displayed).
  206. /// </summary>
  207. /// <param name="verbosity">Message verbosity defining its importance.</param>
  208. /// <returns>True if the entry with the specified type should be displayed in the console.</returns>
  209. private bool DoesFilterMatch(LogVerbosity verbosity)
  210. {
  211. switch (verbosity)
  212. {
  213. case LogVerbosity.VeryVerbose:
  214. case LogVerbosity.Verbose:
  215. case LogVerbosity.Info:
  216. return filter.HasFlag(EntryFilter.Info);
  217. case LogVerbosity.Warning:
  218. return filter.HasFlag(EntryFilter.Warning);
  219. case LogVerbosity.Error:
  220. case LogVerbosity.Fatal:
  221. return filter.HasFlag(EntryFilter.Error);
  222. }
  223. return false;
  224. }
  225. /// <summary>
  226. /// Removes all entries from the debug log.
  227. /// </summary>
  228. private void ClearLog()
  229. {
  230. Debug.Clear();
  231. ClearEntries();
  232. }
  233. /// <summary>
  234. /// Removes all entries from the console.
  235. /// </summary>
  236. private void ClearEntries()
  237. {
  238. listView.Clear();
  239. entries.Clear();
  240. filteredEntries.Clear();
  241. sSelectedElementIdx = -1;
  242. RefreshDetailsPanel();
  243. }
  244. /// <summary>
  245. /// Shows or hides the details panel.
  246. /// </summary>
  247. /// <param name="show">True to show, false to hide.</param>
  248. private void ToggleDetailsPanel(bool show)
  249. {
  250. detailsArea.Active = show;
  251. detailsSeparator.Active = show;
  252. if (show)
  253. {
  254. int listWidth = Width - (int)(Width * DETAILS_PANE_SIZE_PCT) - SEPARATOR_WIDTH;
  255. listView.SetSize(listWidth, ListHeight);
  256. RefreshDetailsPanel();
  257. }
  258. else
  259. listView.SetSize(Width, ListHeight);
  260. }
  261. /// <summary>
  262. /// Toggles should the console be cleared when the play mode is entered.
  263. /// </summary>
  264. /// <param name="clear">True if the console should be cleared upon entering the play mode.</param>
  265. private void ToggleClearOnPlay(bool clear)
  266. {
  267. EditorSettings.SetBool(CLEAR_ON_PLAY_KEY, clear);
  268. }
  269. /// <summary>
  270. /// Updates the contents of the details panel according to the currently selected element.
  271. /// </summary>
  272. private void RefreshDetailsPanel()
  273. {
  274. detailsArea.Layout.Clear();
  275. if (sSelectedElementIdx != -1)
  276. {
  277. GUILayoutX paddingX = detailsArea.Layout.AddLayoutX();
  278. paddingX.AddSpace(5);
  279. GUILayoutY paddingY = paddingX.AddLayoutY();
  280. paddingX.AddSpace(5);
  281. paddingY.AddSpace(5);
  282. GUILayoutY mainLayout = paddingY.AddLayoutY();
  283. paddingY.AddSpace(5);
  284. ConsoleEntryData entry = filteredEntries[sSelectedElementIdx];
  285. LocString message = new LocEdString(entry.message);
  286. GUILabel messageLabel = new GUILabel(message, EditorStyles.MultiLineLabel, GUIOption.FlexibleHeight());
  287. mainLayout.AddElement(messageLabel);
  288. mainLayout.AddSpace(10);
  289. if (entry.callstack != null)
  290. {
  291. foreach (var call in entry.callstack)
  292. {
  293. string fileName = Path.GetFileName(call.file);
  294. string callMessage;
  295. if (string.IsNullOrEmpty(call.method))
  296. callMessage = "\tin " + fileName + ":" + call.line;
  297. else
  298. callMessage = "\t" + call.method + " in " + fileName + ":" + call.line;
  299. GUIButton callBtn = new GUIButton(new LocEdString(callMessage));
  300. mainLayout.AddElement(callBtn);
  301. CallStackEntry hoistedCall = call;
  302. callBtn.OnClick += () =>
  303. {
  304. CodeEditor.OpenFile(hoistedCall.file, hoistedCall.line);
  305. };
  306. }
  307. }
  308. mainLayout.AddFlexibleSpace();
  309. }
  310. else
  311. {
  312. GUILayoutX centerX = detailsArea.Layout.AddLayoutX();
  313. centerX.AddFlexibleSpace();
  314. GUILayoutY centerY = centerX.AddLayoutY();
  315. centerX.AddFlexibleSpace();
  316. centerY.AddFlexibleSpace();
  317. GUILabel nothingSelectedLbl = new GUILabel(new LocEdString("(No entry selected)"));
  318. centerY.AddElement(nothingSelectedLbl);
  319. centerY.AddFlexibleSpace();
  320. }
  321. }
  322. #endregion
  323. #region Types
  324. /// <summary>
  325. /// Filter type that determines what kind of messages are shown in the console.
  326. /// </summary>
  327. [Flags]
  328. private enum EntryFilter
  329. {
  330. Info = 0x01, Warning = 0x02, Error = 0x04, All = Info | Warning | Error
  331. }
  332. /// <summary>
  333. /// Contains data for a single entry in the console.
  334. /// </summary>
  335. private class ConsoleEntryData : GUIListViewData
  336. {
  337. public LogVerbosity verbosity;
  338. public int category;
  339. public string message;
  340. public CallStackEntry[] callstack;
  341. }
  342. /// <summary>
  343. /// Contains GUI elements used for displaying a single entry in the console.
  344. /// </summary>
  345. private class ConsoleGUIEntry : GUIListViewEntry<ConsoleEntryData>
  346. {
  347. private const int CALLER_LABEL_HEIGHT = 11;
  348. private const int PADDING = 3;
  349. private const int MESSAGE_HEIGHT = ENTRY_HEIGHT - CALLER_LABEL_HEIGHT - PADDING * 2;
  350. private static readonly Color BG_COLOR = Color.DarkGray;
  351. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  352. private GUIPanel overlay;
  353. private GUIPanel main;
  354. private GUIPanel underlay;
  355. private GUITexture icon;
  356. private GUILabel messageLabel;
  357. private GUILabel functionLabel;
  358. private GUITexture background;
  359. private int entryIdx;
  360. private string file;
  361. private int line;
  362. /// <inheritdoc/>
  363. public override void BuildGUI()
  364. {
  365. main = Layout.AddPanel(0, 1, 1, GUIOption.FixedHeight(ENTRY_HEIGHT));
  366. overlay = main.AddPanel(-1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  367. underlay = main.AddPanel(1, 0, 0, GUIOption.FixedHeight(ENTRY_HEIGHT));
  368. GUILayoutX mainLayout = main.AddLayoutX();
  369. GUILayoutY overlayLayout = overlay.AddLayoutY();
  370. GUILayoutY underlayLayout = underlay.AddLayoutY();
  371. icon = new GUITexture(null, GUIOption.FixedWidth(32), GUIOption.FixedHeight(32));
  372. messageLabel = new GUILabel(new LocEdString(""), EditorStyles.MultiLineLabel, GUIOption.FixedHeight(MESSAGE_HEIGHT));
  373. functionLabel = new GUILabel(new LocEdString(""), GUIOption.FixedHeight(CALLER_LABEL_HEIGHT));
  374. mainLayout.AddSpace(PADDING);
  375. GUILayoutY iconLayout = mainLayout.AddLayoutY();
  376. iconLayout.AddFlexibleSpace();
  377. iconLayout.AddElement(icon);
  378. iconLayout.AddFlexibleSpace();
  379. mainLayout.AddSpace(PADDING);
  380. GUILayoutY messageLayout = mainLayout.AddLayoutY();
  381. messageLayout.AddSpace(PADDING);
  382. messageLayout.AddElement(messageLabel);
  383. messageLayout.AddElement(functionLabel);
  384. messageLayout.AddSpace(PADDING);
  385. mainLayout.AddFlexibleSpace();
  386. mainLayout.AddSpace(PADDING);
  387. background = new GUITexture(Builtin.WhiteTexture, GUIOption.FixedHeight(ENTRY_HEIGHT));
  388. underlayLayout.AddElement(background);
  389. GUIButton button = new GUIButton(new LocEdString(""), EditorStyles.Blank, GUIOption.FixedHeight(ENTRY_HEIGHT));
  390. overlayLayout.AddElement(button);
  391. button.OnClick += OnClicked;
  392. button.OnDoubleClick += OnDoubleClicked;
  393. }
  394. /// <inheritdoc/>
  395. public override void UpdateContents(int index, ConsoleEntryData data)
  396. {
  397. if (index != sSelectedElementIdx)
  398. {
  399. if (index%2 != 0)
  400. {
  401. background.Visible = true;
  402. background.SetTint(BG_COLOR);
  403. }
  404. else
  405. {
  406. background.Visible = false;
  407. }
  408. }
  409. else
  410. {
  411. background.Visible = true;
  412. background.SetTint(SELECTION_COLOR);
  413. }
  414. switch (data.verbosity)
  415. {
  416. case LogVerbosity.VeryVerbose:
  417. case LogVerbosity.Verbose:
  418. case LogVerbosity.Info:
  419. icon.SetTexture(EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Info, 32, false));
  420. break;
  421. case LogVerbosity.Warning:
  422. icon.SetTexture(EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Warning, 32, false));
  423. break;
  424. case LogVerbosity.Error:
  425. case LogVerbosity.Fatal:
  426. icon.SetTexture(EditorBuiltin.GetLogMessageIcon(LogMessageIcon.Error, 32, false));
  427. break;
  428. }
  429. messageLabel.SetContent(new LocEdString(data.message));
  430. string method = "";
  431. if (data.callstack != null && data.callstack.Length > 0)
  432. {
  433. string filePath = data.callstack[0].file;
  434. bool isFilePathValid = filePath.IndexOfAny(Path.GetInvalidPathChars()) == -1;
  435. if (isFilePathValid)
  436. file = Path.GetFileName(data.callstack[0].file);
  437. else
  438. file = "<unknown file>";
  439. line = data.callstack[0].line;
  440. if (string.IsNullOrEmpty(data.callstack[0].method))
  441. method = "\tin " + file + ":" + line;
  442. else
  443. method = "\t" + data.callstack[0].method + " in " + file + ":" + line;
  444. }
  445. else
  446. {
  447. file = "";
  448. line = 0;
  449. }
  450. functionLabel.SetContent(new LocEdString(method));
  451. entryIdx = index;
  452. }
  453. /// <summary>
  454. /// Triggered when the entry is selected.
  455. /// </summary>
  456. private void OnClicked()
  457. {
  458. sSelectedElementIdx = entryIdx;
  459. LogWindow window = GetWindow<LogWindow>();
  460. window.RefreshDetailsPanel();
  461. RefreshEntries();
  462. }
  463. /// <summary>
  464. /// Triggered when the entry is double-clicked.
  465. /// </summary>
  466. private void OnDoubleClicked()
  467. {
  468. if(!string.IsNullOrEmpty(file))
  469. CodeEditor.OpenFile(file, line);
  470. }
  471. }
  472. #endregion
  473. }
  474. /** @} */
  475. }