0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-24 13:29:01 -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':
// 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;
}

View file

@ -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');
}
}

View file

@ -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)));
}
};

View file

@ -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 = {

View file

@ -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.'
);
}

View file

@ -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

View file

@ -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