From d4d64edaa14b5a2494605b1072d3ce020f90f0a6 Mon Sep 17 00:00:00 2001 From: liabru Date: Thu, 4 Aug 2016 00:14:10 +0100 Subject: [PATCH] improved logging --- src/body/Composite.js | 2 +- src/constraint/MouseConstraint.js | 2 +- src/core/Common.js | 57 +++++++++++++++++++++++-------- src/core/Engine.js | 2 +- src/core/Plugin.js | 24 ++++++------- src/factory/Bodies.js | 2 +- src/render/RenderPixi.js | 4 +-- 7 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/body/Composite.js b/src/body/Composite.js index 397327b..5e5a431 100644 --- a/src/body/Composite.js +++ b/src/body/Composite.js @@ -85,7 +85,7 @@ var Body = require('./Body'); case 'body': // skip adding compound parts if (obj.parent !== obj) { - Common.log('Composite.add: skipped adding a compound body part (you must add its parent instead)', 'warn'); + Common.warn('Composite.add: skipped adding a compound body part (you must add its parent instead)'); break; } diff --git a/src/constraint/MouseConstraint.js b/src/constraint/MouseConstraint.js index e64a685..f6879b5 100644 --- a/src/constraint/MouseConstraint.js +++ b/src/constraint/MouseConstraint.js @@ -42,7 +42,7 @@ var Bounds = require('../geometry/Bounds'); mouse = Mouse.create(options.element); } else { mouse = Mouse.create(); - Common.log('MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected', 'warn'); + Common.warn('MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected'); } } diff --git a/src/core/Common.js b/src/core/Common.js index ae2aad5..bef7025 100644 --- a/src/core/Common.js +++ b/src/core/Common.js @@ -301,24 +301,53 @@ module.exports = Common; }; /** - * A wrapper for console.log, for providing errors and warnings. - * @method log - * @param {string} message - * @param {string} type + * The console logging level to use, where each level includes all levels above and excludes the levels below. + * The default level is 'debug' which shows all console messages. + * Possible level values are: + * - 0 = None + * - 1 = Debug + * - 2 = Info + * - 3 = Warn + * - 4 = Error + * @property Common.logLevel + * @type {Number} + * @default 1 */ - Common.log = function(message, type) { - if (!console || !console.log || !console.warn) - return; + Common.logLevel = 1; - switch (type) { + /** + * Shows a `console.log` message only if the current `Common.logLevel` allows it. + * The message will be prefixed with 'Matter.js' to make it easily identifiable. + * @method log + * @param ...objs {} The objects to log. + */ + Common.log = function() { + if (console && Common.logLevel > 0 && Common.logLevel <= 3) { + console.log.apply(console, [Matter.name + ':'].concat(Array.prototype.slice.call(arguments))); + } + }; - case 'warn': - console.warn('Matter.js:', message); - break; - case 'error': - console.log('Matter.js:', message); - break; + /** + * Shows a `console.info` message only if the current `Common.logLevel` allows it. + * The message will be prefixed with 'Matter.js' to make it easily identifiable. + * @method info + * @param ...objs {} The objects to log. + */ + Common.info = function() { + if (console && Common.logLevel > 0 && Common.logLevel <= 2) { + console.info.apply(console, [Matter.name + ':'].concat(Array.prototype.slice.call(arguments))); + } + }; + /** + * Shows a `console.warn` message only if the current `Common.logLevel` allows it. + * The message will be prefixed with 'Matter.js' to make it easily identifiable. + * @method warn + * @param ...objs {} The objects to log. + */ + Common.warn = function() { + if (console && Common.logLevel > 0 && Common.logLevel <= 3) { + console.warn.apply(console, [Matter.name + ':'].concat(Array.prototype.slice.call(arguments))); } }; diff --git a/src/core/Engine.js b/src/core/Engine.js index cad2911..b838f02 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -42,7 +42,7 @@ var Body = require('../body/Body'); options = options || {}; if (element || options.render) { - Common.log('Engine.create: engine.render is deprecated (see docs)', 'warn'); + Common.warn('Engine.create: engine.render is deprecated (see docs)'); } var defaults = { diff --git a/src/core/Plugin.js b/src/core/Plugin.js index 373eb58..6651d43 100644 --- a/src/core/Plugin.js +++ b/src/core/Plugin.js @@ -16,17 +16,17 @@ var Common = require('./Common'); Plugin.exports = function(plugin) { if (!Plugin.isValid(plugin)) { - Common.log('Plugin.exports: ' + Plugin.toString(plugin) + ' does not implement all required fields.', 'warn'); + Common.warn('Plugin.exports:', Plugin.toString(plugin), 'does not implement all required fields.'); } if (plugin.name in Plugin._registry) { var registered = Plugin._registry[plugin.name]; if (Plugin.versionParse(plugin.version).number >= Plugin.versionParse(registered.version).number) { - Common.log('Plugin.exports: ' + Plugin.toString(registered) + ' was upgraded to ' + Plugin.toString(plugin), 'warn'); + Common.warn('Plugin.exports:', Plugin.toString(registered), 'was upgraded to', Plugin.toString(plugin)); Plugin._registry[plugin.name] = plugin; } else { - Common.log('Plugin.exports: ' + Plugin.toString(registered) + ' can not be downgraded to ' + Plugin.toString(plugin), 'warn'); + Common.warn('Plugin.exports:', Plugin.toString(registered), 'can not be downgraded to', Plugin.toString(plugin)); } } else { Plugin._registry[plugin.name] = plugin; @@ -65,12 +65,12 @@ var Common = require('./Common'); */ Plugin.installDependencies = function(base) { if (!base.uses || base.uses.length === 0) { - Common.log('Plugin.installDependencies: ' + Plugin.toString(base) + ' does not specify any dependencies to install.', 'warn'); + Common.warn('Plugin.installDependencies:', Plugin.toString(base), 'does not specify any dependencies to install.'); return; } if (base.used && base.used.length > 0) { - Common.log('Plugin.installDependencies: ' + Plugin.toString(base) + ' has already installed its dependencies.', 'warn'); + Common.warn('Plugin.installDependencies:', Plugin.toString(base), 'has already installed its dependencies.'); return; } @@ -121,16 +121,14 @@ var Common = require('./Common'); resolved = Plugin.resolve(dependency); if (resolved && !Plugin.versionSatisfies(resolved.version, parsed.range)) { - Common.log( - 'Plugin.trackDependencies: ' + Plugin.toString(resolved) + ' does not satisfy ' - + Plugin.toString(parsed) + ' used by ' + Plugin.toString(parsedBase) + '.', - 'warn' + Common.warn( + 'Plugin.trackDependencies:', Plugin.toString(resolved), 'does not satisfy', + Plugin.toString(parsed), 'used by', Plugin.toString(parsedBase) + '.' ); } else if (!resolved) { - Common.log( - 'Plugin.trackDependencies: ' + dependency + ' used by ' - + Plugin.toString(parsedBase) + ' could not be resolved.', - 'warn' + Common.warn( + 'Plugin.trackDependencies:', dependency, 'used by', + Plugin.toString(parsedBase), 'could not be resolved.' ); } diff --git a/src/factory/Bodies.js b/src/factory/Bodies.js index c5f2588..4248158 100644 --- a/src/factory/Bodies.js +++ b/src/factory/Bodies.js @@ -214,7 +214,7 @@ var Vector = require('../geometry/Vector'); minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 10; if (!window.decomp) { - Common.log('Bodies.fromVertices: poly-decomp.js required. Could not decompose vertices. Fallback to convex hull.', 'warn'); + Common.warn('Bodies.fromVertices: poly-decomp.js required. Could not decompose vertices. Fallback to convex hull.'); } // ensure vertexSets is an array of arrays diff --git a/src/render/RenderPixi.js b/src/render/RenderPixi.js index dabf201..9b2ef50 100644 --- a/src/render/RenderPixi.js +++ b/src/render/RenderPixi.js @@ -39,7 +39,7 @@ var Vector = require('../geometry/Vector'); * @deprecated */ RenderPixi.create = function(options) { - Common.log('RenderPixi.create: Matter.RenderPixi is deprecated (see docs)', 'warn'); + Common.warn('RenderPixi.create: Matter.RenderPixi is deprecated (see docs)'); var defaults = { controller: RenderPixi, @@ -118,7 +118,7 @@ var Vector = require('../geometry/Vector'); if (Common.isElement(render.element)) { render.element.appendChild(render.canvas); } else { - Common.log('No "render.element" passed, "render.canvas" was not inserted into document.', 'warn'); + Common.warn('No "render.element" passed, "render.canvas" was not inserted into document.'); } // prevent menus on canvas