changeset 531:32dc5af8ab99 bgo-commands-codegen

Merged dev branch
author Benjamin Golinvaux <bgo@osimis.io>
date Sun, 17 Mar 2019 20:16:45 +0100
parents 64782d018fc4 (current diff) 548eed46f535 (diff)
children d4046c7f284d
files
diffstat 6 files changed, 114 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platforms/Wasm/logger.ts	Sun Mar 17 20:16:45 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	Sun Mar 17 20:16:21 2019 +0100
+++ b/Platforms/Wasm/stone-framework-loader.ts	Sun Mar 17 20:16:45 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	Sun Mar 17 20:16:21 2019 +0100
+++ b/Platforms/Wasm/tsconfig-stone.json	Sun Mar 17 20:16:45 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	Sun Mar 17 20:16:21 2019 +0100
+++ b/Platforms/Wasm/wasm-application-runner.ts	Sun Mar 17 20:16:45 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	Sun Mar 17 20:16:21 2019 +0100
+++ b/Platforms/Wasm/wasm-viewport.ts	Sun Mar 17 20:16:45 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);
--- a/Resources/CodeGeneration/template.in.h.j2	Sun Mar 17 20:16:21 2019 +0100
+++ b/Resources/CodeGeneration/template.in.h.j2	Sun Mar 17 20:16:45 2019 +0100
@@ -86,7 +86,7 @@
     return result;
   }
 
-  inline std::string MakeIndent(int indent)
+  inline std::string MakeIndent(size_t indent)
   {
     char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!!
     for(size_t i = 0; i < indent; ++i)
@@ -99,14 +99,14 @@
 
   // generic dumper
   template<typename T>
-  std::ostream& StoneDumpValue(std::ostream& out, const T& value, int indent)
+  std::ostream& StoneDumpValue(std::ostream& out, const T& value, size_t indent)
   {
     out << MakeIndent(indent) << value;
     return out;
   }
 
   // string dumper
-  inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, size_t indent)
   {
     out << MakeIndent(indent) << "\"" << value  << "\"";
     return out;
@@ -148,7 +148,7 @@
   }
 
   template<typename T>
-  std::ostream& StoneDumpValue(std::ostream& out, const std::map<std::string,T>& value, int indent)
+  std::ostream& StoneDumpValue(std::ostream& out, const std::map<std::string,T>& value, size_t indent)
   {
     out << MakeIndent(indent) << "{\n";
     for (typename std::map<std::string, T>::const_iterator it = value.cbegin();
@@ -188,7 +188,7 @@
   }
 
   template<typename T>
-  std::ostream& StoneDumpValue(std::ostream& out, const std::vector<T>& value, int indent)
+  std::ostream& StoneDumpValue(std::ostream& out, const std::vector<T>& value, size_t indent)
   {
     out << MakeIndent(indent) << "[\n";
     for (size_t i = 0; i < value.size(); ++i)
@@ -273,7 +273,7 @@
     return Json::Value(strValue);
   }
 
-  inline std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, int indent = 0)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const {{enum['name']}}& value, size_t indent = 0)
   {
 {% for key in enum['fields']%}    if( value == {{enum['name']}}_{{key}})
     {
@@ -311,7 +311,7 @@
     return result;
   }
 
-  inline std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, int indent = 0)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const {{struct['name']}}& value, size_t indent = 0)
   {
     out << MakeIndent(indent) << "{\n";
 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}    out << MakeIndent(indent) << "{{key}}:\n";