Comparing version 0.1.4 to 0.2.0
146
lib/dbug.js
@@ -5,3 +5,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
const tty = require('tty'); | ||
const tty = require('tty').isatty(2); | ||
@@ -11,5 +11,5 @@ const colors = [6, 2, 3, 4, 5, 1]; | ||
function envDebugColor() { | ||
function envDebugColor(colorString) { | ||
try { | ||
return !!JSON.parse(process.env.DEBUG_COLORS); | ||
return !!JSON.parse(colorString); | ||
} catch (ex) { | ||
@@ -20,27 +20,29 @@ return false; | ||
function environ() { | ||
var useColor = process.env.DEBUG_COLORS ? envDebugColor() : tty.isatty(2); | ||
function parseDEBUG(debugStr) { | ||
var names = []; | ||
var skips = []; | ||
if (process.env.DEBUG) { | ||
process.env.DEBUG | ||
.split(/[\s,]+/) | ||
.forEach(function(name){ | ||
name = name.replace('*', '.*?'); | ||
if (name[0] === '-') { | ||
skips.push(new RegExp('^' + name.substr(1) + '$')); | ||
} else { | ||
names.push(new RegExp('^' + name + '(:.+)?$')); | ||
} | ||
}); | ||
} | ||
debugStr | ||
.split(/[\s,]+/) | ||
.forEach(function(name){ | ||
name = name.replace('*', '.*?'); | ||
if (name[0] === '-') { | ||
skips.push(new RegExp('^' + name.substr(1) + '$')); | ||
} else { | ||
names.push(new RegExp('^' + name + '(:.+)?$')); | ||
} | ||
}); | ||
return { | ||
names: names, | ||
skips: skips, | ||
useColor: useColor | ||
skips: skips | ||
}; | ||
} | ||
function getter(obj, name, fn) { | ||
Object.defineProperty(obj, name, { | ||
get: fn | ||
}); | ||
} | ||
function color() { | ||
@@ -57,17 +59,29 @@ return colors[prevColor++ % colors.length]; | ||
function disable() { | ||
function disabled() {} | ||
disabled.enabled = false; | ||
disabled.log = | ||
disabled.debug = | ||
disabled.info = | ||
disabled.warn = | ||
disabled.error = | ||
function noop() {}; | ||
return disabled; | ||
function isEnabled(debugStr, name) { | ||
var env = parseDEBUG(debugStr); | ||
var match = env.skips.some(function(re){ | ||
return re.test(name); | ||
}); | ||
if (match) { | ||
return false; | ||
} | ||
match = env.names.some(function(re){ | ||
return re.test(name); | ||
}); | ||
if (!match) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
function enable(name, useColor) { | ||
function isColored() { | ||
var env = process.env.DEBUG_COLORS; | ||
return env ? envDebugColor(env) : tty; | ||
} | ||
function logger(name) { | ||
var c; | ||
var format = useColor ? function colored(level, text) { | ||
function colored(level, text) { | ||
text = coerce(text); | ||
@@ -80,3 +94,5 @@ if (!c) { | ||
+ text; | ||
} : function plain(level, text) { | ||
} | ||
function plain(level, text) { | ||
text = coerce(text); | ||
@@ -86,45 +102,57 @@ | ||
+ ' ' + name + '.' + level + ' ' + text; | ||
} | ||
return function log(level, args) { | ||
var format = isColored() ? colored : plain; | ||
args[0] = format(level, args[0]); | ||
console[level === 'debug' ? 'log' : level].apply(console, args); | ||
}; | ||
} | ||
var loggers = {}; | ||
function log(name, level, args) { | ||
loggers[name](level, args); | ||
} | ||
function dbugger(name) { | ||
function logAt(level) { | ||
return function log(text) { | ||
text = format(level === 'log' ? 'debug' : level, text); | ||
console[level].apply(console, arguments); | ||
return function canLog() { | ||
if (!dbug.enabled) { | ||
return; | ||
} | ||
module.exports.__log(name, level, arguments); | ||
}; | ||
} | ||
function dbug() { | ||
dbug.log.apply(this, arguments); | ||
} | ||
dbug.enabled = true; | ||
dbug[format.name] = true; | ||
dbug.log = logAt('log'); | ||
var dbug = logAt('debug'); | ||
dbug.log = logAt('debug'); | ||
dbug.debug = dbug.log; | ||
dbug.info = logAt('info'); | ||
dbug.warn = logAt('warn'); | ||
dbug.error = logAt('error'); | ||
return dbug; | ||
} | ||
module.exports = function dbug(name) { | ||
var env = environ(); | ||
var match = env.skips.some(function(re){ | ||
return re.test(name); | ||
getter(dbug, 'enabled', function enabled() { | ||
var DEBUG = process.env.DEBUG; | ||
if (dbug.__DEBUG === DEBUG) { | ||
return dbug.__enabled; | ||
} | ||
dbug.__DEBUG = DEBUG; | ||
return dbug.__enabled = isEnabled(DEBUG, name); | ||
}); | ||
if (match) { | ||
return disable(); | ||
} | ||
getter(dbug, 'colored', isColored); | ||
match = env.names.some(function(re){ | ||
return re.test(name); | ||
}); | ||
loggers[name] = logger(name); | ||
if (!match) { | ||
return disable(); | ||
} | ||
return dbug; | ||
} | ||
module.exports = function dbug(name) { | ||
return dbugger(name); | ||
}; | ||
return enable(name, env.useColor); | ||
}; | ||
// woah. this is a private API. don't rely on it. i can blow it up | ||
// any time. kablamo! | ||
module.exports.__log = log; |
{ | ||
"name": "dbug", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "debug improvements", | ||
@@ -10,4 +10,5 @@ "main": "./lib/dbug.js", | ||
"devDependencies": { | ||
"insist": "*", | ||
"mocha": "1.x", | ||
"utcstring": "0.1.x" | ||
"utcstring": "*" | ||
}, | ||
@@ -23,9 +24,4 @@ "readmeFilename": "README.md", | ||
"author": "Sean McArthur <sean.monstar@gmail.com> (http://seanmonstar.com)", | ||
"bugs": "https://github.com/seanmonstar/dbug/issues", | ||
"homepage": "https://github.com/seanmonstar/dbug", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/seanmonstar/dbug.git" | ||
}, | ||
"repo": "seanmonstar/dbug", | ||
"license": "MPLv2.0" | ||
} |
@@ -28,2 +28,4 @@ # dbug | ||
However, `dbug` will now check for chages to the env variable, so a script can enable debugging even after the dbugger has been created. | ||
[debug]: https://npmjs.org/package/debug |
@@ -7,3 +7,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
const assert = require('assert'); | ||
const assert = require('insist'); | ||
@@ -41,3 +41,2 @@ const utc = require('utcstring'); | ||
function contains(haystack, needle) { | ||
@@ -67,5 +66,6 @@ return haystack.indexOf(needle) !== -1; | ||
process.env.DEBUG = 'derp'; | ||
assert(require('../')('derp').enabled); | ||
var derp = require('../')('derp'); | ||
assert(derp.enabled); | ||
process.env.DEBUG = 'foo'; | ||
assert(!require('../')('derp:herp').enabled); | ||
assert(!derp.enabled); | ||
} | ||
@@ -139,7 +139,7 @@ }, | ||
}, | ||
'should look for DEBUG_COLOR to override tty': function() { | ||
'should look for DEBUG_COLORS to override tty': function() { | ||
process.env.DEBUG_COLORS = false; | ||
assert(require('../')('foo').plain); | ||
assert(!require('../')('foo').colored); | ||
}, | ||
'should format with utc': function() { | ||
'should format with utc if plain': function() { | ||
process.env.DEBUG_COLORS = false; | ||
@@ -159,5 +159,35 @@ var plain = require('../')('foo'); | ||
process.stdout.write = out; | ||
}, | ||
'should check process.env every time': function() { | ||
delete process.env.DEBUG_COLORS; | ||
var check = require('../')('check'); | ||
assert(check.colored); | ||
process.env.DEBUG_COLORS = false; | ||
assert(!check.colored); | ||
} | ||
}, | ||
'__log': { | ||
// woah. this is a private API. don't rely on it. i can blow it up | ||
// any time. kablamo! | ||
'should provide new log method': function() { | ||
var d = require('../'); | ||
var counter = 0; | ||
var arg; | ||
d.__log = function(name, level, args) { | ||
counter++; | ||
assert.equal(name, 'foo'); | ||
assert.equal(level, 'debug'); | ||
assert.equal(args[0], arg); | ||
}; | ||
var dbug = d('foo'); | ||
assert.equal(counter, 0); | ||
dbug(arg = 'bar'); | ||
assert.equal(counter, 1); | ||
dbug(arg = 'baz'); | ||
assert.equal(counter, 2); | ||
} | ||
}, | ||
'after': function() { | ||
@@ -164,0 +194,0 @@ //console._stdout = process.stdout; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27470
9
294
31
3
2
10