Skip to content

Commit

Permalink
improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Aug 3, 2016
1 parent 8da170f commit d4d64ed
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/body/Composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/constraint/MouseConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
57 changes: 43 additions & 14 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
24 changes: 11 additions & 13 deletions src/core/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/factory/Bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/render/RenderPixi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d4d64ed

Please sign in to comment.