0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-12 16:08:50 -05:00

improved logging

This commit is contained in:
liabru 2016-08-04 00:14:10 +01:00
parent 8da170f382
commit d4d64edaa1
7 changed files with 60 additions and 33 deletions

View file

@ -85,7 +85,7 @@ var Body = require('./Body');
case 'body': case 'body':
// skip adding compound parts // skip adding compound parts
if (obj.parent !== obj) { 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; break;
} }

View file

@ -42,7 +42,7 @@ var Bounds = require('../geometry/Bounds');
mouse = Mouse.create(options.element); mouse = Mouse.create(options.element);
} else { } else {
mouse = Mouse.create(); 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');
} }
} }

View file

@ -301,24 +301,53 @@ module.exports = Common;
}; };
/** /**
* A wrapper for console.log, for providing errors and warnings. * The console logging level to use, where each level includes all levels above and excludes the levels below.
* @method log * The default level is 'debug' which shows all console messages.
* @param {string} message * Possible level values are:
* @param {string} type * - 0 = None
* - 1 = Debug
* - 2 = Info
* - 3 = Warn
* - 4 = Error
* @property Common.logLevel
* @type {Number}
* @default 1
*/ */
Common.log = function(message, type) { Common.logLevel = 1;
if (!console || !console.log || !console.warn)
return;
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); * Shows a `console.info` message only if the current `Common.logLevel` allows it.
break; * The message will be prefixed with 'Matter.js' to make it easily identifiable.
case 'error': * @method info
console.log('Matter.js:', message); * @param ...objs {} The objects to log.
break; */
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)));
} }
}; };

View file

@ -42,7 +42,7 @@ var Body = require('../body/Body');
options = options || {}; options = options || {};
if (element || options.render) { 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 = { var defaults = {

View file

@ -16,17 +16,17 @@ var Common = require('./Common');
Plugin.exports = function(plugin) { Plugin.exports = function(plugin) {
if (!Plugin.isValid(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) { if (plugin.name in Plugin._registry) {
var registered = Plugin._registry[plugin.name]; var registered = Plugin._registry[plugin.name];
if (Plugin.versionParse(plugin.version).number >= Plugin.versionParse(registered.version).number) { 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; Plugin._registry[plugin.name] = plugin;
} else { } 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 { } else {
Plugin._registry[plugin.name] = plugin; Plugin._registry[plugin.name] = plugin;
@ -65,12 +65,12 @@ var Common = require('./Common');
*/ */
Plugin.installDependencies = function(base) { Plugin.installDependencies = function(base) {
if (!base.uses || base.uses.length === 0) { 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; return;
} }
if (base.used && base.used.length > 0) { 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; return;
} }
@ -121,16 +121,14 @@ var Common = require('./Common');
resolved = Plugin.resolve(dependency); resolved = Plugin.resolve(dependency);
if (resolved && !Plugin.versionSatisfies(resolved.version, parsed.range)) { if (resolved && !Plugin.versionSatisfies(resolved.version, parsed.range)) {
Common.log( Common.warn(
'Plugin.trackDependencies: ' + Plugin.toString(resolved) + ' does not satisfy ' 'Plugin.trackDependencies:', Plugin.toString(resolved), 'does not satisfy',
+ Plugin.toString(parsed) + ' used by ' + Plugin.toString(parsedBase) + '.', Plugin.toString(parsed), 'used by', Plugin.toString(parsedBase) + '.'
'warn'
); );
} else if (!resolved) { } else if (!resolved) {
Common.log( Common.warn(
'Plugin.trackDependencies: ' + dependency + ' used by ' 'Plugin.trackDependencies:', dependency, 'used by',
+ Plugin.toString(parsedBase) + ' could not be resolved.', Plugin.toString(parsedBase), 'could not be resolved.'
'warn'
); );
} }

View file

@ -214,7 +214,7 @@ var Vector = require('../geometry/Vector');
minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 10; minimumArea = typeof minimumArea !== 'undefined' ? minimumArea : 10;
if (!window.decomp) { 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 // ensure vertexSets is an array of arrays

View file

@ -39,7 +39,7 @@ var Vector = require('../geometry/Vector');
* @deprecated * @deprecated
*/ */
RenderPixi.create = function(options) { 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 = { var defaults = {
controller: RenderPixi, controller: RenderPixi,
@ -118,7 +118,7 @@ var Vector = require('../geometry/Vector');
if (Common.isElement(render.element)) { if (Common.isElement(render.element)) {
render.element.appendChild(render.canvas); render.element.appendChild(render.canvas);
} else { } 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 // prevent menus on canvas