mirror of
https://github.com/debug-js/debug.git
synced 2026-01-18 16:12:38 +00:00
add debug(err) support. Closes #46
This commit is contained in:
parent
b5c893d308
commit
97a362f7d0
11
debug.js
11
debug.js
@ -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);
|
||||
|
||||
@ -19,4 +19,8 @@ function workb() {
|
||||
setTimeout(workb, Math.random() * 2000);
|
||||
}
|
||||
|
||||
workb();
|
||||
workb();
|
||||
|
||||
setTimeout(function(){
|
||||
b(new Error('fail'));
|
||||
}, 5000);
|
||||
|
||||
13
lib/debug.js
13
lib/debug.js
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user