Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enabled() updates existing debug instances, add destroy() function #440

Merged
merged 2 commits into from Apr 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/debug.js
Expand Up @@ -13,6 +13,11 @@ exports.enable = enable;
exports.enabled = enabled;
exports.humanize = require('ms');

/**
* Active `debug` instances.
*/
exports.instances = [];

/**
* The currently active debug mode names, and names to skip.
*/
Expand Down Expand Up @@ -114,15 +119,23 @@ function createDebug(namespace) {
debug.enabled = exports.enabled(namespace);
debug.useColors = exports.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;

// env-specific initialization logic for debug instances
if ('function' === typeof exports.init) {
exports.init(debug);
}

exports.instances.push(debug);

return debug;
}

function destroy () {
const index = exports.instances.indexOf(this)
exports.instances.splice(index, 1)
}

/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
Expand All @@ -149,6 +162,11 @@ function enable(namespaces) {
exports.names.push(new RegExp('^' + namespaces + '$'));
}
}

for (var i = 0; i < exports.instances.length; i++) {
var instance = exports.instances[i];
instance.enabled = exports.enabled(instance.namespace);
}
}

/**
Expand Down