ConsoleTab.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. };
  48. // Testing
  49. //console.log("This is a console.log message");
  50. // console.log("That's right, console.log calls are hooked to be written in this window");
  51. // console.log("Object are also stringify-ed", {width:10, height:30, shape:'rectangular'});
  52. // console.warn("This is a console.warn message");
  53. // console.error("This is a console.error message");
  54. // BABYLON.Tools.Log("This is a message");
  55. // BABYLON.Tools.Warn("This is a warning");
  56. // BABYLON.Tools.Error("This is a error");
  57. }
  58. /** Overrides super.dispose */
  59. public dispose() {
  60. console.log = this._oldConsoleLog;
  61. console.warn = this._oldConsoleWarn;
  62. console.error = this._oldConsoleError;
  63. }
  64. private _message(type:string, message:any, caller:string) {
  65. let callerLine = Helpers.CreateDiv('caller', this._consolePanelContent);
  66. callerLine.textContent = caller;
  67. let line = Helpers.CreateDiv(type, this._consolePanelContent);
  68. if (typeof message === "string") {
  69. line.textContent += message ;
  70. } else {
  71. line.textContent += JSON.stringify(message) ;
  72. line.classList.add('object')
  73. }
  74. }
  75. private _addConsoleLog(...params : any[]) {
  76. // Get caller name if not null
  77. let callerFunc = this._addConsoleLog.caller as Function;
  78. let caller = callerFunc==null? "Window" : "Function "+callerFunc['name'] + ": ";
  79. for (var i = 0; i < params.length; i++) {
  80. this._message('log', params[i], caller);
  81. // Write again in console does not work on edge, as the console object
  82. // is not instantiate if debugger tools is not open
  83. if (!Helpers.IsBrowserEdge()) {
  84. this._oldConsoleLog(params[i]);
  85. }
  86. }
  87. }
  88. private _addConsoleWarn(...params : any[]) {
  89. // Get caller name if not null
  90. let callerFunc = this._addConsoleLog.caller as Function;
  91. let caller = callerFunc==null? "Window" : callerFunc['name'];
  92. for (var i = 0; i < params.length; i++) {
  93. this._message('warn', params[i], caller);
  94. // Write again in console does not work on edge, as the console object
  95. // is not instantiate if debugger tools is not open
  96. if (!Helpers.IsBrowserEdge()) {
  97. this._oldConsoleWarn(params[i]);
  98. }
  99. }
  100. }
  101. private _addConsoleError(...params : any[]) {
  102. // Get caller name if not null
  103. let callerFunc = this._addConsoleLog.caller as Function;
  104. let caller = callerFunc==null? "Window" : callerFunc['name'];
  105. for (var i = 0; i < params.length; i++) {
  106. this._message('error', params[i], caller);
  107. // Write again in console does not work on edge, as the console object
  108. // is not instantiate if debugger tools is not open
  109. if (!Helpers.IsBrowserEdge()) {
  110. this._oldConsoleError(params[i]);
  111. }
  112. }
  113. }
  114. }
  115. }