# HG changeset patch # User Alain Mazy # Date 1552586675 -3600 # Node ID 548eed46f535a740a230a828c3be96c9127ab2b3 # Parent 9e241cef32a4c02c8613ee550ca5691effc81b01 introduced a Logger class that displays timing and source (C++/JS) diff -r 9e241cef32a4 -r 548eed46f535 Platforms/Wasm/logger.ts --- /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(); diff -r 9e241cef32a4 -r 548eed46f535 Platforms/Wasm/stone-framework-loader.ts --- 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'); // ( 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 9e241cef32a4 -r 548eed46f535 Platforms/Wasm/tsconfig-stone.json --- 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" ] diff -r 9e241cef32a4 -r 548eed46f535 Platforms/Wasm/wasm-application-runner.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 = ( 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 9e241cef32a4 -r 548eed46f535 Platforms/Wasm/wasm-viewport.ts --- 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);