ConsoleTab.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. module INSPECTOR {
  2. /**
  3. * The console tab will have two features :
  4. * - hook all console.log call and display them in this panel (and in the browser console as well)
  5. * - display all Babylon logs (called with Tools.Log...)
  6. */
  7. export class ConsoleTab extends Tab {
  8. private _inspector : Inspector;
  9. private _consolePanelContent : HTMLElement;
  10. private _bjsPanelContent : HTMLElement;
  11. private _oldConsoleLog : any;
  12. private _oldConsoleWarn : any;
  13. private _oldConsoleError : any;
  14. constructor(tabbar:TabBar, insp:Inspector) {
  15. super(tabbar, 'Console');
  16. this._inspector = insp;
  17. // Build the shaders panel : a div that will contains the shaders tree and both shaders panels
  18. this._panel = Helpers.CreateDiv('tab-panel') as HTMLDivElement;
  19. let consolePanel = Helpers.CreateDiv('console-panel') as HTMLDivElement;
  20. let bjsPanel = Helpers.CreateDiv('console-panel') as HTMLDivElement;
  21. this._panel.appendChild(consolePanel);
  22. this._panel.appendChild(bjsPanel);
  23. Split([consolePanel, bjsPanel], {
  24. blockDrag : this._inspector.popupMode,
  25. sizes:[50, 50],
  26. direction:'vertical'}
  27. );
  28. // Titles
  29. let title = Helpers.CreateDiv('console-panel-title', consolePanel);
  30. title.textContent = 'Console logs';
  31. title = Helpers.CreateDiv('console-panel-title', bjsPanel);
  32. title.textContent = 'Babylon.js logs';
  33. // Contents
  34. this._consolePanelContent = Helpers.CreateDiv('console-panel-content', consolePanel) as HTMLDivElement;
  35. this._bjsPanelContent = Helpers.CreateDiv('console-panel-content', bjsPanel) as HTMLDivElement;
  36. // save old console.log
  37. this._oldConsoleLog = console.log;
  38. this._oldConsoleWarn = console.warn;
  39. this._oldConsoleError = console.error;
  40. console.log = this._addConsoleLog.bind(this);
  41. console.warn = this._addConsoleWarn.bind(this);
  42. console.error = this._addConsoleError.bind(this);
  43. // Bjs logs
  44. this._bjsPanelContent.innerHTML = BABYLON.Tools.LogCache;
  45. BABYLON.Tools.OnNewCacheEntry = (entry: string) => {
  46. this._bjsPanelContent.innerHTML += entry;
  47. this._bjsPanelContent.scrollTop = this._bjsPanelContent.scrollHeight;
  48. };
  49. // Testing
  50. //console.log("This is a console.log message");
  51. // console.log("That's right, console.log calls are hooked to be written in this window");
  52. // console.log("Object are also stringify-ed", {width:10, height:30, shape:'rectangular'});
  53. // console.warn("This is a console.warn message");
  54. // console.error("This is a console.error message");
  55. // BABYLON.Tools.Log("This is a message");
  56. // BABYLON.Tools.Warn("This is a warning");
  57. // BABYLON.Tools.Error("This is a error");
  58. }
  59. /** Overrides super.dispose */
  60. public dispose() {
  61. console.log = this._oldConsoleLog;
  62. console.warn = this._oldConsoleWarn;
  63. console.error = this._oldConsoleError;
  64. }
  65. private _message(type:string, message:any, caller:string) {
  66. let callerLine = Helpers.CreateDiv('caller', this._consolePanelContent);
  67. callerLine.textContent = caller;
  68. let line = Helpers.CreateDiv(type, this._consolePanelContent);
  69. line.textContent += message ;
  70. this._consolePanelContent.scrollTop = this._consolePanelContent.scrollHeight;
  71. }
  72. private _addConsoleLog(...params : any[]) {
  73. // Get caller name if not null
  74. let callerFunc = this._addConsoleLog.caller as Function;
  75. let caller = callerFunc==null? "Window" : "Function "+callerFunc['name'] + ": ";
  76. for (var i = 0; i < params.length; i++) {
  77. this._message('log', params[i], caller);
  78. // Write again in console does not work on edge, as the console object
  79. // is not instantiate if debugger tools is not open
  80. if (!Helpers.IsBrowserEdge()) {
  81. this._oldConsoleLog(params[i]);
  82. }
  83. }
  84. }
  85. private _addConsoleWarn(...params : any[]) {
  86. // Get caller name if not null
  87. let callerFunc = this._addConsoleLog.caller as Function;
  88. let caller = callerFunc==null? "Window" : callerFunc['name'];
  89. for (var i = 0; i < params.length; i++) {
  90. this._message('warn', params[i], caller);
  91. // Write again in console does not work on edge, as the console object
  92. // is not instantiate if debugger tools is not open
  93. if (!Helpers.IsBrowserEdge()) {
  94. this._oldConsoleWarn(params[i]);
  95. }
  96. }
  97. }
  98. private _addConsoleError(...params : any[]) {
  99. // Get caller name if not null
  100. let callerFunc = this._addConsoleLog.caller as Function;
  101. let caller = callerFunc==null? "Window" : callerFunc['name'];
  102. for (var i = 0; i < params.length; i++) {
  103. this._message('error', params[i], caller);
  104. // Write again in console does not work on edge, as the console object
  105. // is not instantiate if debugger tools is not open
  106. if (!Helpers.IsBrowserEdge()) {
  107. this._oldConsoleError(params[i]);
  108. }
  109. }
  110. }
  111. }
  112. }