# HG changeset patch # User Benjamin Golinvaux # Date 1552850205 -3600 # Node ID 32dc5af8ab99ba17dbf429bde0ec9a42013d3745 # Parent 64782d018fc456c9fa2d2c164a695471e31a7b2a# Parent 548eed46f535a740a230a828c3be96c9127ab2b3 Merged dev branch diff -r 64782d018fc4 -r 32dc5af8ab99 Platforms/Wasm/logger.ts --- /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(); diff -r 64782d018fc4 -r 32dc5af8ab99 Platforms/Wasm/stone-framework-loader.ts --- 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'); // ( window). ( 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 }; diff -r 64782d018fc4 -r 32dc5af8ab99 Platforms/Wasm/tsconfig-stone.json --- 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" ] diff -r 64782d018fc4 -r 32dc5af8ab99 Platforms/Wasm/wasm-application-runner.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 = ( window).StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']); CreateWasmApplication = ( window).StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']); @@ -103,7 +104,7 @@ SendSerializedMessageToStoneApplication = ( window).StoneFrameworkModule.cwrap('SendSerializedMessageToStoneApplication', 'string', ['string']); SendCommandToStoneApplication = ( 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) { diff -r 64782d018fc4 -r 32dc5af8ab99 Platforms/Wasm/wasm-viewport.ts --- 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); diff -r 64782d018fc4 -r 32dc5af8ab99 Resources/CodeGeneration/template.in.h.j2 --- 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(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!! for(size_t i = 0; i < indent; ++i) @@ -99,14 +99,14 @@ // generic dumper template - 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 - std::ostream& StoneDumpValue(std::ostream& out, const std::map& value, int indent) + std::ostream& StoneDumpValue(std::ostream& out, const std::map& value, size_t indent) { out << MakeIndent(indent) << "{\n"; for (typename std::map::const_iterator it = value.cbegin(); @@ -188,7 +188,7 @@ } template - std::ostream& StoneDumpValue(std::ostream& out, const std::vector& value, int indent) + std::ostream& StoneDumpValue(std::ostream& out, const std::vector& 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";