Merge pull request #13 from milewise/master

Added support for "-" prefix in DEBUG variable
This commit is contained in:
TJ Holowaychuk 2012-03-08 08:20:05 -08:00
commit 3d03d65ea6
3 changed files with 32 additions and 5 deletions

View File

@ -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()`.

View File

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

View File

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