Skip to content
Browse files

Merge pull request #13 from milewise/master

Added support for "-" prefix in DEBUG variable
  • Loading branch information...
2 parents 119e661 + d656d2f commit 3d03d65ea6250df50bda3fa0c9375b67809800f9 @tj tj committed Mar 8, 2012
Showing with 32 additions and 5 deletions.
  1. +2 −0 Readme.md
  2. +12 −1 debug.js
  3. +18 −4 lib/debug.js
View
2 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()`.
View
13 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;
View
22 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);
});

0 comments on commit 3d03d65

Please sign in to comment.
Something went wrong with that request. Please try again.