diff --git a/Readme.md b/Readme.md index 93e2957..419fcdf 100644 --- a/Readme.md +++ b/Readme.md @@ -85,6 +85,8 @@ setInterval(function(){ The "*" character may be used as a wildcard. Suppose for example your library has debuggers named "connect:bodyParser", "connect:compress", "connect:session", instead of listing all three with `DEBUG=connect:bodyParser,connect.compress,connect:session`, you may simply do `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`. + You can also exclude specific debuggers by prefixing them with a "-" character. For example, `DEBUG=* -connect:*` would include all debuggers except those starting with "connect:". + ## Browser support Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`. diff --git a/debug.js b/debug.js index e586b6a..b2f0798 100644 --- a/debug.js +++ b/debug.js @@ -39,6 +39,7 @@ function debug(name) { */ debug.names = []; +debug.skips = []; /** * Enables a debug mode by name. This can include modes @@ -56,7 +57,12 @@ debug.enable = function(name) { for (var i = 0; i < len; i++) { name = split[i].replace('*', '.*?'); - debug.names.push(new RegExp('^' + name + '$')); + if (name[0] === '-') { + debug.skips.push(new RegExp('^' + name.substr(1) + '$')); + } + else { + debug.names.push(new RegExp('^' + name + '$')); + } } }; @@ -98,6 +104,11 @@ debug.humanize = function(ms) { */ debug.enabled = function(name) { + for (var i = 0, len = debug.skips.length; i < len; i++) { + if (debug.skips[i].test(name)) { + return false; + } + } for (var i = 0, len = debug.names.length; i < len; i++) { if (debug.names[i].test(name)) { return true; diff --git a/lib/debug.js b/lib/debug.js index 740ebb8..1be8b66 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -27,11 +27,19 @@ exports.version = '0.5.0'; * Enabled debuggers. */ -var names = (process.env.DEBUG || '') +var names = [], + skips = []; + +(process.env.DEBUG || '') .split(/[\s,]+/) - .map(function(name){ + .forEach(function(name){ name = name.replace('*', '.*?'); - return new RegExp('^' + name + '$'); + if (name[0] === '-') { + skips.push(new RegExp('^' + name.substr(1) + '$')); + } + else { + names.push(new RegExp('^' + name + '$')); + } }); /** @@ -97,7 +105,13 @@ function humanize(ms) { */ function debug(name) { - var match = names.some(function(re){ + var match = skips.some(function(re){ + return re.test(name); + }); + + if (match) return function(){}; + + match = names.some(function(re){ return re.test(name); });