changeset 526:548eed46f535 dev

introduced a Logger class that displays timing and source (C++/JS)
author Alain Mazy <alain@mazy.be>
date Thu, 14 Mar 2019 19:04:35 +0100
parents 9e241cef32a4
children 32dc5af8ab99 1c5104a6f7e4
files Platforms/Wasm/logger.ts Platforms/Wasm/stone-framework-loader.ts Platforms/Wasm/tsconfig-stone.json Platforms/Wasm/wasm-application-runner.ts Platforms/Wasm/wasm-viewport.ts
diffstat 5 files changed, 107 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platforms/Wasm/logger.ts	Thu Mar 14 19:04:35 2019 +0100
@@ -0,0 +1,90 @@
+export enum LogSource {
+  Cpp,
+  Typescript
+}
+
+export class StandardConsoleLogger {
+  public showSource: boolean = true;
+
+  public debug(...args: any[]): void {
+    this._debug(LogSource.Typescript, ...args);
+  }
+
+  public info(...args: any[]): void {
+    this._info(LogSource.Typescript, ...args);
+  }
+
+  public infoFromCpp(message: string): void {
+    this._info(LogSource.Cpp, message);
+  }
+
+  public warning(...args: any[]): void {
+    this._warning(LogSource.Typescript, ...args);
+  }
+
+  public error(...args: any[]): void {
+    this._error(LogSource.Typescript, ...args);
+  }
+
+  public errorFromCpp(message: string): void {
+    this._error(LogSource.Cpp, message);
+  }
+
+  public _debug(source: LogSource, ...args: any[]): void {
+    var output = this.getOutput(source, args);
+    console.debug(...output);
+  }
+
+  private _info(source: LogSource, ...args: any[]): void {
+    var output = this.getOutput(source, args);
+    console.info(...output);
+  }
+
+  public _warning(source: LogSource, ...args: any[]): void {
+    var output = this.getOutput(source, args);
+    console.warn(...output);
+  }
+
+  public _error(source: LogSource, ...args: any[]): void {
+    var output = this.getOutput(source, args);
+    console.error(...output);
+  }
+
+
+  private getOutput(source: LogSource, args: any[]): any[] {
+    var prefix = this.getPrefix();
+    var prefixAndSource = [];
+
+    if (prefix != null) {
+      prefixAndSource = [prefix];
+    } 
+
+    if (this.showSource) {
+      if (source == LogSource.Typescript) {
+        prefixAndSource = [...prefixAndSource, "TS "];
+      } else if (source == LogSource.Cpp) {
+        prefixAndSource = [...prefixAndSource, "C++"];
+      }
+    }
+
+    if (prefixAndSource.length > 0) {
+      prefixAndSource = [...prefixAndSource, "|"];
+    }
+
+    return [...prefixAndSource, ...args];
+  }
+
+  protected getPrefix(): string {
+    return null;
+  }
+}
+
+export class TimeConsoleLogger extends StandardConsoleLogger {
+  protected getPrefix(): string {
+    let now = new Date();
+    let timeString = now.getHours().toString().padStart(2, "0") + ":" + now.getMinutes().toString().padStart(2, "0") + ":" + now.getSeconds().toString().padStart(2, "0") + "." + now.getMilliseconds().toString().padStart(3, "0");
+    return timeString;
+  }
+}
+
+export var defaultLogger: StandardConsoleLogger = new TimeConsoleLogger();
--- a/Platforms/Wasm/stone-framework-loader.ts	Tue Mar 12 18:28:25 2019 +0100
+++ b/Platforms/Wasm/stone-framework-loader.ts	Thu Mar 14 19:04:35 2019 +0100
@@ -2,6 +2,7 @@
  * This file contains primitives to interface with WebAssembly and
  * with the Stone framework.
  **/
+import * as Logger from './logger'
 
 export declare type InitializationCallback = () => void;
 
@@ -56,13 +57,13 @@
   public static Initialize( verbose: boolean,
                             callback: InitializationCallback)
   {
-    console.log('Initializing WebAssembly Module');
+    Logger.defaultLogger.debug('Initializing WebAssembly Module');
 
     // (<any> window).
     (<any> window).StoneFrameworkModule = {
       preRun: [ 
         function() {
-          console.log('Loading the Stone Framework using WebAssembly');
+          Logger.defaultLogger.debug('Loading the Stone Framework using WebAssembly');
         }
       ],
       postRun: [ 
@@ -70,16 +71,16 @@
           // This function is called by ".js" wrapper once the ".wasm"
           // WebAssembly module has been loaded and compiled by the
           // browser
-          console.log('WebAssembly is ready');
+          Logger.defaultLogger.debug('WebAssembly is ready');
           Framework.singleton_ = new Framework(verbose);
           callback();
         }
       ],
       print: function(text : string) {
-        console.log(text);
+        Logger.defaultLogger.infoFromCpp(text);
       },
       printErr: function(text : string) {
-        console.error(text);
+        Logger.defaultLogger.errorFromCpp(text);
       },
       totalDependencies: 0
     };
--- a/Platforms/Wasm/tsconfig-stone.json	Tue Mar 12 18:28:25 2019 +0100
+++ b/Platforms/Wasm/tsconfig-stone.json	Thu Mar 14 19:04:35 2019 +0100
@@ -1,6 +1,7 @@
 {
     "include" : [
         "stone-framework-loader.ts",
+        "logger.ts",
         "wasm-application-runner.ts",
         "wasm-viewport.ts"
     ]
--- a/Platforms/Wasm/wasm-application-runner.ts	Tue Mar 12 18:28:25 2019 +0100
+++ b/Platforms/Wasm/wasm-application-runner.ts	Thu Mar 14 19:04:35 2019 +0100
@@ -1,5 +1,6 @@
 import Stone = require('./stone-framework-loader');
 import StoneViewport = require('./wasm-viewport');
+import * as Logger from './logger'
 
 if (!('WebAssembly' in window)) {
   alert('Sorry, your browser does not support WebAssembly :(');
@@ -85,7 +86,7 @@
   // the WebAssembly environment) and then, create and initialize the Wasm application
   Stone.Framework.Initialize(true, function () {
 
-    console.log("Connecting C++ methods to JS methods");
+    Logger.defaultLogger.debug("Connecting C++ methods to JS methods");
     
     SetStartupParameter = (<any> window).StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']);
     CreateWasmApplication = (<any> window).StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']);
@@ -103,7 +104,7 @@
     SendSerializedMessageToStoneApplication = (<any> window).StoneFrameworkModule.cwrap('SendSerializedMessageToStoneApplication', 'string', ['string']);
     SendCommandToStoneApplication = (<any> window).StoneFrameworkModule.cwrap('SendCommandToStoneApplication', 'string', ['string']);
 
-    console.log("Connecting C++ methods to JS methods - done");
+    Logger.defaultLogger.debug("Connecting C++ methods to JS methods - done");
 
     // Prevent scrolling
     document.body.addEventListener('touchmove', function (event) {
--- a/Platforms/Wasm/wasm-viewport.ts	Tue Mar 12 18:28:25 2019 +0100
+++ b/Platforms/Wasm/wasm-viewport.ts	Thu Mar 14 19:04:35 2019 +0100
@@ -1,5 +1,6 @@
 import wasmApplicationRunner = require('./wasm-application-runner');
 //import stoneFrameworkLoader = require('./stone-framework-loader');
+import * as Logger from './logger'
 
 var isPendingRedraw = false;
 
@@ -7,7 +8,7 @@
 {
   if (!isPendingRedraw) {
     isPendingRedraw = true;
-    console.log('Scheduling a refresh of the viewport, as its content changed');
+    Logger.defaultLogger.debug('Scheduling a refresh of the viewport, as its content changed');
     window.requestAnimationFrame(function() {
       isPendingRedraw = false;
       WasmViewport.GetFromCppViewport(cppViewportHandle).Redraw();
@@ -81,7 +82,7 @@
       this.canvasId_ = canvasId;
       this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement;
       if (this.htmlCanvas_ == null) {
-        console.log("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'");
+        Logger.defaultLogger.error("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'");
       }
       this.context_ = this.htmlCanvas_.getContext('2d');
 
@@ -107,7 +108,7 @@
       if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) {
         return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle];
       }
-      console.log("WasmViewport not found !");
+      Logger.defaultLogger.error("WasmViewport not found !");
       return undefined;
     }
 
@@ -115,7 +116,7 @@
       if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) {
         return WasmViewport.viewportsMapByCanvasId_[canvasId];
       }
-      console.log("WasmViewport not found !");
+      Logger.defaultLogger.error("WasmViewport not found !");
       return undefined;
     }
 
@@ -132,7 +133,7 @@
                          this.imageData_.width,
                          this.imageData_.height,
                          this.renderingBuffer_) == 0) {
-        console.log('The rendering has failed');
+        Logger.defaultLogger.error('The rendering has failed');
       } else {
         // Create an accessor to the rendering buffer (i.e. create a
         // "window" above the heap of the WASM module), then copy it to
@@ -157,7 +158,7 @@
       this.htmlCanvas_.width = this.htmlCanvas_.parentElement.offsetWidth;  
       this.htmlCanvas_.height = this.htmlCanvas_.parentElement.offsetHeight;  
 
-      console.log("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height);
+      Logger.defaultLogger.debug("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height);
 
       if (this.imageData_ === null) {
         this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height);