add debug(err) support. Closes #46

This commit is contained in:
TJ Holowaychuk 2013-04-16 06:54:38 -07:00
parent b5c893d308
commit 97a362f7d0
3 changed files with 29 additions and 1 deletions

View File

@ -17,6 +17,8 @@ function debug(name) {
if (!debug.enabled(name)) return function(){};
return function(fmt){
fmt = coerce(fmt);
var curr = new Date;
var ms = curr - (debug[name] || curr);
debug[name] = curr;
@ -119,6 +121,15 @@ debug.enabled = function(name) {
return false;
};
/**
* Coerce `val`.
*/
function coerce(val) {
if (val instanceof Error) return val.stack;
return val;
}
// persist
if (window.localStorage) debug.enable(localStorage.debug);

View File

@ -19,4 +19,8 @@ function workb() {
setTimeout(workb, Math.random() * 2000);
}
workb();
workb();
setTimeout(function(){
b(new Error('fail'));
}, 5000);

View File

@ -108,6 +108,8 @@ function debug(name) {
var c = color();
function colored(fmt) {
fmt = coerce(fmt);
var curr = new Date;
var ms = curr - (prev[name] || curr);
prev[name] = curr;
@ -121,6 +123,8 @@ function debug(name) {
}
function plain(fmt) {
fmt = coerce(fmt);
fmt = new Date().toUTCString()
+ ' ' + name + ' ' + fmt;
console.error.apply(this, arguments);
@ -132,3 +136,12 @@ function debug(name) {
? colored
: plain;
}
/**
* Coerce `val`.
*/
function coerce(val) {
if (val instanceof Error) return val.stack;
return val;
}