LogWindow.cs 20 KB

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