From 34d6aa1c12d255ad67d5d379ee3e49e1d5b69a4a Mon Sep 17 00:00:00 2001 From: Simo Kinnunen Date: Sat, 15 Jun 2013 23:45:05 +0900 Subject: [PATCH 001/248] Minor README code fix. Fixed what I believe to be a small typo in one of the code examples. One "worker" was not being used at all. --- Readme.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 15ee5019..085b7747 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,3 @@ - # debug tiny node.js debugging utility modelled after node core's debugging technique. @@ -85,7 +84,7 @@ setInterval(function(){ }, 1000); setInterval(function(){ - a('doing some work'); + b('doing some work'); }, 1200); ``` @@ -112,4 +111,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From d62596e7d93d5f2b86d016bdb51e37f50ab356f2 Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Thu, 29 Aug 2013 13:21:59 -0600 Subject: [PATCH 002/248] typo --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index c5a34e8b..4ae010ca 100644 --- a/Readme.md +++ b/Readme.md @@ -70,7 +70,7 @@ 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:". + 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 From aca9f15e60dae379e58b8f96971ca2e74bddcda8 Mon Sep 17 00:00:00 2001 From: Hayko Koryun Date: Mon, 23 Dec 2013 03:59:36 -0800 Subject: [PATCH 003/248] updated .npmignore updated `.npmignore` to additionally ignore the new `example` folder (looks like you renamed it from examples though). --- .npmignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.npmignore b/.npmignore index f1250e58..fc10b6f7 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,5 @@ support test examples +example *.sock From a1cf28776451b58f322519ede9f9c0076cac1345 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Thu, 6 Feb 2014 15:12:17 -0800 Subject: [PATCH 004/248] remove unnecessary index.js file --- History.md | 29 +++++++++++++++++------------ Makefile | 2 +- component.json | 3 ++- index.js | 5 ----- package.json | 9 +-------- 5 files changed, 21 insertions(+), 27 deletions(-) delete mode 100644 index.js diff --git a/History.md b/History.md index c8d390ef..63969e01 100644 --- a/History.md +++ b/History.md @@ -1,24 +1,29 @@ -0.7.4 / 2013-11-13 +HEAD +================== + + * remove unnecessary index.js + +0.7.4 / 2013-11-13 ================== * remove "browserify" key from package.json (fixes something in browserify) -0.7.3 / 2013-10-30 +0.7.3 / 2013-10-30 ================== * fix: catch localStorage security error when cookies are blocked (Chrome) * add debug(err) support. Closes #46 * add .browser prop to package.json. Closes #42 -0.7.2 / 2013-02-06 +0.7.2 / 2013-02-06 ================== * fix package.json * fix: Mobile Safari (private mode) is broken with debug * fix: Use unicode to send escape character to shell instead of octal to work with strict mode javascript -0.7.1 / 2013-02-05 +0.7.1 / 2013-02-05 ================== * add repository URL to package.json @@ -26,19 +31,19 @@ * add browserify support * fix component. Closes #24 -0.7.0 / 2012-05-04 +0.7.0 / 2012-05-04 ================== * Added .component to package.json * Added debug.component.js build -0.6.0 / 2012-03-16 +0.6.0 / 2012-03-16 ================== * Added support for "-" prefix in DEBUG [Vinay Pulim] - * Added `.enabled` flag to the node version [TooTallNate] + * Added `.enabled` flag to the node version [TooTallNate] -0.5.0 / 2012-02-02 +0.5.0 / 2012-02-02 ================== * Added: humanize diffs. Closes #8 @@ -46,24 +51,24 @@ * Removed padding. Closes #10 * Fixed: persist client-side variant again. Closes #9 -0.4.0 / 2012-02-01 +0.4.0 / 2012-02-01 ================== * Added browser variant support for older browsers [TooTallNate] * Added `debug.enable('project:*')` to browser variant [TooTallNate] * Added padding to diff (moved it to the right) -0.3.0 / 2012-01-26 +0.3.0 / 2012-01-26 ================== * Added millisecond diff when isatty, otherwise UTC string -0.2.0 / 2012-01-22 +0.2.0 / 2012-01-22 ================== * Added wildcard support -0.1.0 / 2011-12-02 +0.1.0 / 2011-12-02 ================== * Added: remove colors unless stderr isatty [TooTallNate] diff --git a/Makefile b/Makefile index 93fe6b60..9c37f697 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,3 @@ -build: debug.js index.js +build: debug.js component build diff --git a/component.json b/component.json index 491168c2..cb76981b 100644 --- a/component.json +++ b/component.json @@ -4,6 +4,7 @@ "description": "small debugging utility", "version": "0.7.4", "keywords": ["debug", "log", "debugger"], - "scripts": ["index.js", "debug.js"], + "main": "debug.js", + "scripts": ["debug.js"], "dependencies": {} } diff --git a/index.js b/index.js deleted file mode 100644 index e02c13b7..00000000 --- a/index.js +++ /dev/null @@ -1,5 +0,0 @@ -if ('undefined' == typeof window) { - module.exports = require('./lib/debug'); -} else { - module.exports = require('./debug'); -} diff --git a/package.json b/package.json index 523ee8e6..451bb1f8 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,6 @@ , "engines": { "node": "*" } , "files": [ "lib/debug.js", - "debug.js", - "index.js" + "debug.js" ] - , "component": { - "scripts": { - "debug/index.js": "index.js", - "debug/debug.js": "debug.js" - } - } } From 2e992a0cdaaa8bd5b49249a9d96deb696f1a0b46 Mon Sep 17 00:00:00 2001 From: Boris Okunskiy Date: Fri, 14 Mar 2014 11:57:16 +0400 Subject: [PATCH 005/248] Exposing enable() method for Node.js to address #27 --- lib/debug.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 3b0a9183..dda10508 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -17,17 +17,6 @@ module.exports = debug; var names = [] , skips = []; -(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 + '$')); - } - }); - /** * Colors. */ @@ -145,3 +134,23 @@ function coerce(val) { if (val instanceof Error) return val.stack || val.message; return val; } + +/** + * Enable specified `namespaces` for debugging. + */ +debug.enable = function(namespaces) { + namespaces.split(/[\s,]+/) + .forEach(function(name){ + name = name.replace('*', '.*?'); + if (name[0] === '-') { + skips.push(new RegExp('^' + name.substr(1) + '$')); + } else { + names.push(new RegExp('^' + name + '$')); + } + }) +}; + +/** + * Enable namespaces listed in `process.env.DEBUG` initially. + */ +debug.enable(process.env.DEBUG || ''); \ No newline at end of file From 06830e88dacb45d9e13ccb1932fe345cdf3e8a14 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 21 Mar 2014 13:02:02 +0100 Subject: [PATCH 006/248] Readme.md: fix inadvertent markdown italicization Escape a * that made GitHub's markdown parser italicize the paragraph. --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index c5a34e8b..6bf05712 100644 --- a/Readme.md +++ b/Readme.md @@ -68,7 +68,7 @@ setInterval(function(){ ## Wildcards - 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=*`. + 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:". From db73b661fddf9fa4dc035d97207360b047fcb684 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sun, 30 Mar 2014 08:56:39 -0700 Subject: [PATCH 007/248] refactor --- lib/debug.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index dda10508..dca4f34b 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -138,19 +138,21 @@ function coerce(val) { /** * Enable specified `namespaces` for debugging. */ + debug.enable = function(namespaces) { namespaces.split(/[\s,]+/) - .forEach(function(name){ - name = name.replace('*', '.*?'); - if (name[0] === '-') { - skips.push(new RegExp('^' + name.substr(1) + '$')); - } else { - names.push(new RegExp('^' + name + '$')); - } - }) + .forEach(function(name){ + name = name.replace('*', '.*?'); + if (name[0] == '-') { + skips.push(new RegExp('^' + name.substr(1) + '$')); + } else { + names.push(new RegExp('^' + name + '$')); + } + }); }; /** * Enable namespaces listed in `process.env.DEBUG` initially. */ -debug.enable(process.env.DEBUG || ''); \ No newline at end of file + +debug.enable(process.env.DEBUG || ''); From 491eea3b33cb52da73a78b4f65a58b4f778ee197 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sun, 30 Mar 2014 08:59:20 -0700 Subject: [PATCH 008/248] change from stderr to stdout --- Readme.md | 15 +++++++-------- lib/debug.js | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Readme.md b/Readme.md index 6bf05712..8981f8ab 100644 --- a/Readme.md +++ b/Readme.md @@ -11,7 +11,7 @@ $ npm install debug ## Usage With `debug` you simply invoke the exported function to generate your debug function, passing it a name which will determine if a noop function is returned, or a decorated `console.error`, so all of the `console` format string goodies you're used to work fine. A unique color is selected per-function for visibility. - + Example _app.js_: ```js @@ -57,14 +57,13 @@ setInterval(function(){ ![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png) - When stderr is not a TTY, `Date#toUTCString()` is used, making it more useful for logging the debug information as shown below: - _(NOTE: Debug now uses stderr instead of stdout, so the correct shell command for this example is actually `DEBUG=* node example/worker 2> out &`)_ - + When stdout is not a TTY, `Date#toUTCString()` is used, making it more useful for logging the debug information as shown below: + ![](http://f.cl.ly/items/112H3i0e0o0P0a2Q2r11/Screenshot.png) - + ## Conventions - If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". + If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". ## Wildcards @@ -74,7 +73,7 @@ setInterval(function(){ ## 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()`. + 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()`. ```js a = debug('worker:a'); @@ -89,7 +88,7 @@ setInterval(function(){ }, 1200); ``` -## License +## License (The MIT License) diff --git a/lib/debug.js b/lib/debug.js index dca4f34b..e7422e68 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -39,7 +39,7 @@ var prevColor = 0; * Is stdout a TTY? Colored output is disabled when `true`. */ -var isatty = tty.isatty(2); +var isatty = tty.isatty(1); /** * Select a color. @@ -108,7 +108,7 @@ function debug(name) { + fmt + '\u001b[3' + c + 'm' + ' +' + humanize(ms) + '\u001b[0m'; - console.error.apply(this, arguments); + console.log.apply(this, arguments); } function plain(fmt) { @@ -116,7 +116,7 @@ function debug(name) { fmt = new Date().toUTCString() + ' ' + name + ' ' + fmt; - console.error.apply(this, arguments); + console.log.apply(this, arguments); } colored.enabled = plain.enabled = true; From 2109329b3992859090d64058713402252100a781 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sun, 30 Mar 2014 09:00:15 -0700 Subject: [PATCH 009/248] Release 0.8.0 --- History.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 63969e01..835f20a9 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,11 @@ +0.8.0 / 2014-03-30 +================== + + * add `enable()` method for nodejs. Closes #27 + * change from stderr to stdout + * remove unnecessary index.js file + HEAD ================== diff --git a/package.json b/package.json index 451bb1f8..e1d3117f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "debug" - , "version": "0.7.4" + , "version": "0.8.0" , "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" } , "description": "small debugging utility" , "keywords": ["debug", "log", "debugger"] From e289c89b21d8b82a0d01c6abf85b401b2986ea46 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 14 Apr 2014 18:00:26 -0700 Subject: [PATCH 010/248] package: re-add the "component" section Was inadvertently removed in a1cf28776451b58f322519ede9f9c0076cac1345. The "component" section is only used by legacy build systems at this point, but removing this broke our Cloudup build when we tried to update to v0.8.0 of `debug`. Adding it back for v0.8.1 so that we can upgrade again. --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index e1d3117f..f979727d 100644 --- a/package.json +++ b/package.json @@ -14,4 +14,9 @@ "lib/debug.js", "debug.js" ] + , "component": { + "scripts": { + "debug/index.js": "debug.js" + } + } } From 09a93468b0f6289f8e7d83cd59737c9e4a0bbeb8 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Mon, 14 Apr 2014 19:04:38 -0700 Subject: [PATCH 011/248] Release 0.8.1 --- History.md | 10 +++++----- package.json | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/History.md b/History.md index 835f20a9..0d9a49a3 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,9 @@ +0.8.1 / 2014-04-14 +================== + + * package: re-add the "component" section + 0.8.0 / 2014-03-30 ================== @@ -6,11 +11,6 @@ * change from stderr to stdout * remove unnecessary index.js file -HEAD -================== - - * remove unnecessary index.js - 0.7.4 / 2013-11-13 ================== diff --git a/package.json b/package.json index f979727d..7051d22c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "debug" - , "version": "0.8.0" + , "version": "0.8.1" , "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" } , "description": "small debugging utility" , "keywords": ["debug", "log", "debugger"] From 0269707d06e8f317302833300ffd8ad0c061344e Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 28 May 2014 21:40:31 -0700 Subject: [PATCH 012/248] component: fix mismatching version number The `component.json` file is at v0.8.1 currently. --- component.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/component.json b/component.json index cb76981b..4a934083 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "debug", "repo": "visionmedia/debug", "description": "small debugging utility", - "version": "0.7.4", + "version": "0.8.1", "keywords": ["debug", "log", "debugger"], "main": "debug.js", "scripts": ["debug.js"], From 45010b65d0dda9ec10e7ea4f26b6040ca9f69c48 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:07:15 -0700 Subject: [PATCH 013/248] add .jshintrc file --- .jshintrc | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 00000000..299877f2 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + "laxbreak": true +} From 2cffe24185a9aa3a25f1ab490bfdae0509b535f2 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:10:10 -0700 Subject: [PATCH 014/248] rewrite: abstract common logic for Node & browsers --- browser.js | 69 +++++++++++++++++++++ component.json | 7 ++- debug.js | 127 ++++++++++++++++++++------------------- lib/debug.js | 158 ------------------------------------------------- node.js | 104 ++++++++++++++++++++++++++++++++ package.json | 12 ++-- 6 files changed, 247 insertions(+), 230 deletions(-) create mode 100644 browser.js delete mode 100644 lib/debug.js create mode 100644 node.js diff --git a/browser.js b/browser.js new file mode 100644 index 00000000..5a93453c --- /dev/null +++ b/browser.js @@ -0,0 +1,69 @@ + +/** + * This is the web browser implementation of `debug()`. + * + * Expose `debug()` as the module. + */ + +exports = module.exports = require('./debug'); +exports.log = log; +exports.save = save; +exports.load = load; + +/** + * Invokes `console.log()` when available. + * No-op when `consoel.log` is not a "function". + * + * @api public + */ + +function log(fmt) { + var curr = new Date(); + var ms = curr - (this.prev || curr); + this.prev = curr; + + fmt = name + + ' ' + + fmt + + ' +' + exports.humanize(ms); + + // This hackery is required for IE8, + // where the `console.log` function doesn't have 'apply' + return 'object' == typeof console + && 'function' == typeof console.log + && Function.prototype.apply.call(console.log, console, arguments); +} + +/** + * Save `namespaces`, + * + * @param {String} namespaces + * @api private + */ + +function save(namespaces) { + try { + localStorage.debug = namespaces; + } catch(e) {} +} + +/** + * Load `namespaces`, + * + * @return {String} returns the previously persisted debug modes + * @api private + */ + +function load() { + var r = ''; + try { + r = localStorage.debug || ''; + } catch(e) {} + return r; +} + +/** + * Enable namespaces listed in `localStorage.debug` initially. + */ + +exports.enable(load()); diff --git a/component.json b/component.json index 4a934083..16315748 100644 --- a/component.json +++ b/component.json @@ -4,7 +4,10 @@ "description": "small debugging utility", "version": "0.8.1", "keywords": ["debug", "log", "debugger"], - "main": "debug.js", - "scripts": ["debug.js"], + "main": "browser.js", + "scripts": [ + "browser.js", + "debug.js" + ], "dependencies": {} } diff --git a/debug.js b/debug.js index 509dc0de..e207f5c3 100644 --- a/debug.js +++ b/debug.js @@ -1,74 +1,78 @@ /** + * This is the common logic for both the Node.js and web browser + * implementations of `debug()`. + * * Expose `debug()` as the module. */ -module.exports = debug; +exports = module.exports = debug; +exports.coerce = coerce; +exports.disable = disable; +exports.enable = enable; +exports.enabled = enabled; +exports.humanize = humanize; /** - * Create a debugger with the given `name`. - * - * @param {String} name - * @return {Type} - * @api public + * The currently active debug mode names, and names to skip. */ -function debug(name) { - if (!debug.enabled(name)) return function(){}; +exports.names = []; +exports.skips = []; - return function(fmt){ - fmt = coerce(fmt); +/** + * Create a debugger with the given `namespace`. + * + * @param {String} namespace + * @return {Function} + * @api public + */ - var curr = new Date; - var ms = curr - (debug[name] || curr); - debug[name] = curr; +function debug(namespace) { - fmt = name - + ' ' - + fmt - + ' +' + debug.humanize(ms); + // define the `disabled` version + function disabled() { + } + disabled.enabled = false; - // This hackery is required for IE8 - // where `console.log` doesn't have 'apply' - window.console - && console.log - && Function.prototype.apply.call(console.log, console, arguments); + // define the `enabled` version + function enabled(fmt) { + fmt = exports.coerce(fmt); + exports.log.apply(enabled, arguments); } -} + enabled.enabled = true; -/** - * The currently active debug mode names. - */ + var fn = exports.enabled(namespace) ? enabled : disabled; -debug.names = []; -debug.skips = []; + fn.namespace = namespace; + + return fn; +} /** - * Enables a debug mode by name. This can include modes + * Enables a debug mode by namespaces. This can include modes * separated by a colon and wildcards. * - * @param {String} name + * @param {String} namespaces * @api public */ -debug.enable = function(name) { - try { - localStorage.debug = name; - } catch(e){} - - var split = (name || '').split(/[\s,]+/) - , len = split.length; +function enable(namespaces) { + var split = (namespaces || '').split(/[\s,]+/); + var len = split.length; for (var i = 0; i < len; i++) { - name = split[i].replace('*', '.*?'); - if (name[0] === '-') { - debug.skips.push(new RegExp('^' + name.substr(1) + '$')); + namespaces = split[i].replace('*', '.*?'); + if (namespaces[0] === '-') { + exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); } else { - debug.names.push(new RegExp('^' + name + '$')); + exports.names.push(new RegExp('^' + namespaces + '$')); } } -}; + + exports.save(namespaces); +} /** * Disable debug output. @@ -76,9 +80,9 @@ debug.enable = function(name) { * @api public */ -debug.disable = function(){ - debug.enable(''); -}; +function disable() { + exports.enable(''); +} /** * Humanize the given `ms`. @@ -88,16 +92,16 @@ debug.disable = function(){ * @api private */ -debug.humanize = function(ms) { - var sec = 1000 - , min = 60 * 1000 - , hour = 60 * min; +function humanize(ms) { + var sec = 1000; + var min = 60 * 1000; + var hour = 60 * min; if (ms >= hour) return (ms / hour).toFixed(1) + 'h'; if (ms >= min) return (ms / min).toFixed(1) + 'm'; if (ms >= sec) return (ms / sec | 0) + 's'; return ms + 'ms'; -}; +} /** * Returns true if the given mode name is enabled, false otherwise. @@ -107,31 +111,30 @@ debug.humanize = function(ms) { * @api public */ -debug.enabled = function(name) { - for (var i = 0, len = debug.skips.length; i < len; i++) { - if (debug.skips[i].test(name)) { +function enabled(name) { + var i, len; + for (i = 0, len = exports.skips.length; i < len; i++) { + if (exports.skips[i].test(name)) { return false; } } - for (var i = 0, len = debug.names.length; i < len; i++) { - if (debug.names[i].test(name)) { + for (i = 0, len = exports.names.length; i < len; i++) { + if (exports.names[i].test(name)) { return true; } } return false; -}; +} /** * Coerce `val`. + * + * @param {Mixed} val + * @return {Mixed} + * @api private */ function coerce(val) { if (val instanceof Error) return val.stack || val.message; return val; } - -// persist - -try { - if (window.localStorage) debug.enable(localStorage.debug); -} catch(e){} diff --git a/lib/debug.js b/lib/debug.js deleted file mode 100644 index e7422e68..00000000 --- a/lib/debug.js +++ /dev/null @@ -1,158 +0,0 @@ -/** - * Module dependencies. - */ - -var tty = require('tty'); - -/** - * Expose `debug()` as the module. - */ - -module.exports = debug; - -/** - * Enabled debuggers. - */ - -var names = [] - , skips = []; - -/** - * Colors. - */ - -var colors = [6, 2, 3, 4, 5, 1]; - -/** - * Previous debug() call. - */ - -var prev = {}; - -/** - * Previously assigned color. - */ - -var prevColor = 0; - -/** - * Is stdout a TTY? Colored output is disabled when `true`. - */ - -var isatty = tty.isatty(1); - -/** - * Select a color. - * - * @return {Number} - * @api private - */ - -function color() { - return colors[prevColor++ % colors.length]; -} - -/** - * Humanize the given `ms`. - * - * @param {Number} m - * @return {String} - * @api private - */ - -function humanize(ms) { - var sec = 1000 - , min = 60 * 1000 - , hour = 60 * min; - - if (ms >= hour) return (ms / hour).toFixed(1) + 'h'; - if (ms >= min) return (ms / min).toFixed(1) + 'm'; - if (ms >= sec) return (ms / sec | 0) + 's'; - return ms + 'ms'; -} - -/** - * Create a debugger with the given `name`. - * - * @param {String} name - * @return {Type} - * @api public - */ - -function debug(name) { - function disabled(){} - disabled.enabled = false; - - var match = skips.some(function(re){ - return re.test(name); - }); - - if (match) return disabled; - - match = names.some(function(re){ - return re.test(name); - }); - - if (!match) return disabled; - var c = color(); - - function colored(fmt) { - fmt = coerce(fmt); - - var curr = new Date; - var ms = curr - (prev[name] || curr); - prev[name] = curr; - - fmt = ' \u001b[9' + c + 'm' + name + ' ' - + '\u001b[3' + c + 'm\u001b[90m' - + fmt + '\u001b[3' + c + 'm' - + ' +' + humanize(ms) + '\u001b[0m'; - - console.log.apply(this, arguments); - } - - function plain(fmt) { - fmt = coerce(fmt); - - fmt = new Date().toUTCString() - + ' ' + name + ' ' + fmt; - console.log.apply(this, arguments); - } - - colored.enabled = plain.enabled = true; - - return isatty || process.env.DEBUG_COLORS - ? colored - : plain; -} - -/** - * Coerce `val`. - */ - -function coerce(val) { - if (val instanceof Error) return val.stack || val.message; - return val; -} - -/** - * Enable specified `namespaces` for debugging. - */ - -debug.enable = function(namespaces) { - namespaces.split(/[\s,]+/) - .forEach(function(name){ - name = name.replace('*', '.*?'); - if (name[0] == '-') { - skips.push(new RegExp('^' + name.substr(1) + '$')); - } else { - names.push(new RegExp('^' + name + '$')); - } - }); -}; - -/** - * Enable namespaces listed in `process.env.DEBUG` initially. - */ - -debug.enable(process.env.DEBUG || ''); diff --git a/node.js b/node.js new file mode 100644 index 00000000..41638470 --- /dev/null +++ b/node.js @@ -0,0 +1,104 @@ + +/** + * Module dependencies. + */ + +var tty = require('tty'); + +/** + * This is the Node.js implementation of `debug()`. + * + * Expose `debug()` as the module. + */ + +exports = module.exports = require('./debug'); +exports.log = log; +exports.save = save; +exports.load = load; + +/** + * Colors. + */ + +var colors = [6, 2, 3, 4, 5, 1]; + +/** + * Previously assigned color. + */ + +var prevColor = 0; + +/** + * Is stdout a TTY? Colored output is disabled when `true`. + */ + +var useColors = tty.isatty(1) || process.env.DEBUG_COLORS; + +/** + * Select a color. + * + * @return {Number} + * @api private + */ + +function selectColor() { + return colors[prevColor++ % colors.length]; +} + +/** + * Invokes `console.log()` with the specified arguments, + * after adding ANSI color escape codes if enabled. + * + * @api public + */ + +function log(fmt) { + var name = this.namespace; + + if (useColors) { + if (null == this.c) this.c = selectColor(); + + var c = this.c; + var curr = new Date(); + var ms = curr - (this.prev || curr); + this.prev = curr; + + fmt = ' \u001b[9' + c + 'm' + name + ' ' + + '\u001b[3' + c + 'm\u001b[90m' + + fmt + '\u001b[3' + c + 'm' + + ' +' + exports.humanize(ms) + '\u001b[0m'; + } else { + fmt = new Date().toUTCString() + + ' ' + name + ' ' + fmt; + } + + console.log.apply(this, arguments); +} + +/** + * Save `namespaces`, + * + * @param {String} namespaces + * @api private + */ + +function save(namespaces) { + process.env.DEBUG = namespaces; +} + +/** + * Load `namespaces`, + * + * @return {String} returns the previously persisted debug modes + * @api private + */ + +function load() { + return process.env.DEBUG || ''; +} + +/** + * Enable namespaces listed in `process.env.DEBUG` initially. + */ + +exports.enable(load()); diff --git a/package.json b/package.json index 7051d22c..c875bd28 100644 --- a/package.json +++ b/package.json @@ -7,16 +7,12 @@ , "author": "TJ Holowaychuk " , "dependencies": {} , "devDependencies": { "mocha": "*" } - , "main": "lib/debug.js" - , "browser": "./debug.js" - , "engines": { "node": "*" } - , "files": [ - "lib/debug.js", - "debug.js" - ] + , "main": "./node.js" + , "browser": "./browser.js" , "component": { "scripts": { - "debug/index.js": "debug.js" + "debug/index.js": "browser.js" + , "debug/debug.js": "debug.js" } } } From 0c6d45229259a1b2d9f616a796fe465f6321c993 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:15:29 -0700 Subject: [PATCH 015/248] node: apply log to `console` Not necessary, just seems more "proper" --- node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node.js b/node.js index 41638470..df7fbffd 100644 --- a/node.js +++ b/node.js @@ -72,7 +72,7 @@ function log(fmt) { + ' ' + name + ' ' + fmt; } - console.log.apply(this, arguments); + console.log.apply(console, arguments); } /** From d05b468ee2e910f010fecdccbdbdeda8bf3865d2 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:18:08 -0700 Subject: [PATCH 016/248] browser: , -> . --- browser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser.js b/browser.js index 5a93453c..4255ccf4 100644 --- a/browser.js +++ b/browser.js @@ -35,7 +35,7 @@ function log(fmt) { } /** - * Save `namespaces`, + * Save `namespaces`. * * @param {String} namespaces * @api private @@ -48,7 +48,7 @@ function save(namespaces) { } /** - * Load `namespaces`, + * Load `namespaces`. * * @return {String} returns the previously persisted debug modes * @api private From fbbf2ca88487fec11707e4a47403af7100367885 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:18:17 -0700 Subject: [PATCH 017/248] node: , -> . --- node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/node.js b/node.js index df7fbffd..4fa71a9e 100644 --- a/node.js +++ b/node.js @@ -76,7 +76,7 @@ function log(fmt) { } /** - * Save `namespaces`, + * Save `namespaces * * @param {String} namespaces * @api private @@ -87,7 +87,7 @@ function save(namespaces) { } /** - * Load `namespaces`, + * Load `namespaces`. * * @return {String} returns the previously persisted debug modes * @api private From 3eebad470aafd4447cfa8afb6f37fe168d09dc86 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:23:56 -0700 Subject: [PATCH 018/248] browser: fix comment typo --- browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser.js b/browser.js index 4255ccf4..57fd60a8 100644 --- a/browser.js +++ b/browser.js @@ -12,7 +12,7 @@ exports.load = load; /** * Invokes `console.log()` when available. - * No-op when `consoel.log` is not a "function". + * No-op when `console.log` is not a "function". * * @api public */ From bea76f587b659320a181d568b9c7ad5c0a5a424c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 00:29:14 -0700 Subject: [PATCH 019/248] browser: fix undefined `name` variable --- browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser.js b/browser.js index 57fd60a8..69c04151 100644 --- a/browser.js +++ b/browser.js @@ -22,7 +22,7 @@ function log(fmt) { var ms = curr - (this.prev || curr); this.prev = curr; - fmt = name + fmt = this.namespace + ' ' + fmt + ' +' + exports.humanize(ms); From 3314c7761c304af6bfeb01e0691c44ce362c357d Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 22:13:23 -0700 Subject: [PATCH 020/248] browser: don't default save() to empty string --- browser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser.js b/browser.js index 69c04151..be1d476d 100644 --- a/browser.js +++ b/browser.js @@ -55,9 +55,9 @@ function save(namespaces) { */ function load() { - var r = ''; + var r; try { - r = localStorage.debug || ''; + r = localStorage.debug; } catch(e) {} return r; } From f3eb1a539613ee46f4da562634e34aded0e419d6 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 22:13:46 -0700 Subject: [PATCH 021/248] node: don't default save() to an empty string --- node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node.js b/node.js index 4fa71a9e..3751ed67 100644 --- a/node.js +++ b/node.js @@ -94,7 +94,7 @@ function save(namespaces) { */ function load() { - return process.env.DEBUG || ''; + return process.env.DEBUG; } /** From 83bae0234362b047dd8d1828da96758f5453a3d4 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 22:14:10 -0700 Subject: [PATCH 022/248] debug: invoke save() at the beginning of enable() We modify the `namespaces` argument directly for some reason, which ends up saving input that ends up being invalid RegExp code upon the next `load()`+`enable()` call combo. Avoid that completely by just saving the input directly first thing. Fixes browsers since they actually use the "saved" value. --- debug.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/debug.js b/debug.js index e207f5c3..851af35c 100644 --- a/debug.js +++ b/debug.js @@ -58,6 +58,8 @@ function debug(namespace) { */ function enable(namespaces) { + exports.save(namespaces); + var split = (namespaces || '').split(/[\s,]+/); var len = split.length; @@ -65,13 +67,10 @@ function enable(namespaces) { namespaces = split[i].replace('*', '.*?'); if (namespaces[0] === '-') { exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); - } - else { + } else { exports.names.push(new RegExp('^' + namespaces + '$')); } } - - exports.save(namespaces); } /** From 79b75ad626efe5f5c05084e1087884ac14fcff45 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 22:48:44 -0700 Subject: [PATCH 023/248] browser: add colors for WebKit browsers Based off of the implemention from #67, except this one matches the Node.js colored output exactly, including the trailing `diff` value. --- browser.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/browser.js b/browser.js index be1d476d..4ce55cbd 100644 --- a/browser.js +++ b/browser.js @@ -10,6 +10,43 @@ exports.log = log; exports.save = save; exports.load = load; +/** + * Colors. + */ + +var colors = [ + 'cyan', + 'green', + 'goldenrod', // "yellow" is just too bright on a white background... + 'blue', + 'purple', + 'red' +]; + +/** + * Previously assigned color. + */ + +var prevColor = 0; + +/** + * Currently only WebKit-based Web Inspectors are known + * to support "%c" CSS customizations. + */ + +var useColors = 'WebkitAppearance' in document.documentElement.style; + +/** + * Select a color. + * + * @return {Number} + * @api private + */ + +function selectColor() { + return colors[prevColor++ % colors.length]; +} + /** * Invokes `console.log()` when available. * No-op when `console.log` is not a "function". @@ -18,20 +55,30 @@ exports.load = load; */ function log(fmt) { + var args = arguments; var curr = new Date(); var ms = curr - (this.prev || curr); this.prev = curr; - fmt = this.namespace - + ' ' + fmt = (useColors ? '%c' : '') + + this.namespace + + (useColors ? '%c ' : ' ') + fmt - + ' +' + exports.humanize(ms); + + (useColors ? '%c ' : ' ') + + '+' + exports.humanize(ms); + + if (useColors) { + if (null == this.c) this.c = selectColor(); + var c = 'color: ' + this.c; + args = [args[0], c, ''].concat(Array.prototype.slice.call(arguments, 1)); + args.push(c); + } // This hackery is required for IE8, // where the `console.log` function doesn't have 'apply' return 'object' == typeof console && 'function' == typeof console.log - && Function.prototype.apply.call(console.log, console, arguments); + && Function.prototype.apply.call(console.log, console, args); } /** From 8bed009c849aec970b92566936e9dccdd8c93181 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 11:16:12 -0700 Subject: [PATCH 024/248] browser: add Firebug color support --- browser.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/browser.js b/browser.js index 4ce55cbd..96a29850 100644 --- a/browser.js +++ b/browser.js @@ -30,11 +30,19 @@ var colors = [ var prevColor = 0; /** - * Currently only WebKit-based Web Inspectors are known - * to support "%c" CSS customizations. + * Currently only WebKit-based Web Inspectors and the Firebug + * extension (*not* the built-in Firefox web inpector) are + * known to support "%c" CSS customizations. + * + * TODO: add a `localStorage` variable to explicitly enable/disable colors */ -var useColors = 'WebkitAppearance' in document.documentElement.style; +var useColors = + // is webkit? http://stackoverflow.com/a/16459606/376773 + ('WebkitAppearance' in document.documentElement.style) || + // is firebug? http://stackoverflow.com/a/398120/376773 + (window.console && (console.firebug || (console.exception && console.table))); + /** * Select a color. From 7af914c2c067d445957db3bd781e7522138b1741 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 11:55:37 -0700 Subject: [PATCH 025/248] Readme: add "bash" syntax highlighting --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index b4c88a0e..8884188c 100644 --- a/Readme.md +++ b/Readme.md @@ -4,7 +4,7 @@ ## Installation -``` +```bash $ npm install debug ``` From 007123fa111213d1aa2cb50fe8bdb3948c991600 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 12:22:05 -0700 Subject: [PATCH 026/248] node: fix colors in strict mode Before the `fmt` variable was not being updated. Closes #70. --- node.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/node.js b/node.js index 3751ed67..87a94c2f 100644 --- a/node.js +++ b/node.js @@ -52,7 +52,8 @@ function selectColor() { * @api public */ -function log(fmt) { +function log() { + var args = arguments; var name = this.namespace; if (useColors) { @@ -63,16 +64,16 @@ function log(fmt) { var ms = curr - (this.prev || curr); this.prev = curr; - fmt = ' \u001b[9' + c + 'm' + name + ' ' + args[0] = ' \u001b[9' + c + 'm' + name + ' ' + '\u001b[3' + c + 'm\u001b[90m' - + fmt + '\u001b[3' + c + 'm' + + args[0] + '\u001b[3' + c + 'm' + ' +' + exports.humanize(ms) + '\u001b[0m'; } else { - fmt = new Date().toUTCString() - + ' ' + name + ' ' + fmt; + args[0] = new Date().toUTCString() + + ' ' + name + ' ' + args[0]; } - console.log.apply(console, arguments); + console.log.apply(console, args); } /** From eb80377e34dde7bcf2aa323c80bf0c3f47333b15 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 12:25:14 -0700 Subject: [PATCH 027/248] browser: fix colors in strict mode --- browser.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/browser.js b/browser.js index 96a29850..ba81cd90 100644 --- a/browser.js +++ b/browser.js @@ -62,23 +62,23 @@ function selectColor() { * @api public */ -function log(fmt) { +function log() { var args = arguments; var curr = new Date(); var ms = curr - (this.prev || curr); this.prev = curr; - fmt = (useColors ? '%c' : '') + args[0] = (useColors ? '%c' : '') + this.namespace + (useColors ? '%c ' : ' ') - + fmt + + args[0] + (useColors ? '%c ' : ' ') + '+' + exports.humanize(ms); if (useColors) { if (null == this.c) this.c = selectColor(); var c = 'color: ' + this.c; - args = [args[0], c, ''].concat(Array.prototype.slice.call(arguments, 1)); + args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); args.push(c); } From a4aacbd785ded0022091cef8e9a274045e6b0ba5 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 12:55:05 -0700 Subject: [PATCH 028/248] browser: fix the case of passing additional arguments after the "format" Before the "%c" CSS color value was potentially being inserted into the wrong place in the arguments array, which would throw the formatting off in the Console. --- browser.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/browser.js b/browser.js index ba81cd90..1ab3ab8c 100644 --- a/browser.js +++ b/browser.js @@ -79,7 +79,19 @@ function log() { if (null == this.c) this.c = selectColor(); var c = 'color: ' + this.c; args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); - args.push(c); + + // the final "%c" is somewhat tricky, because there could be other + // arguments passed either before or after the %c, so we need to + // figure out the correct index to insert the CSS into + var index = 0; + args[0].replace(/%[a-z%]/g, function(match) { + index++; + if (index < 3) return; // skip the first 2 %c's since that's handled already + if ('%c' === match) { + args.splice(index, 0, c); + } + return match; + }); } // This hackery is required for IE8, From 61394bf18efffbe359b63d7c2473d2bc9f99dfc3 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 13:24:29 -0700 Subject: [PATCH 029/248] debug: use "ms" module for humanizing the diff This would be the first external dependency for `debug`, so it's slightly controversial. --- component.json | 4 +++- debug.js | 21 +-------------------- package.json | 4 +++- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/component.json b/component.json index 16315748..f9badfec 100644 --- a/component.json +++ b/component.json @@ -9,5 +9,7 @@ "browser.js", "debug.js" ], - "dependencies": {} + "dependencies": { + "guille/ms.js": "0.6.1" + } } diff --git a/debug.js b/debug.js index 851af35c..1c8be82e 100644 --- a/debug.js +++ b/debug.js @@ -11,7 +11,7 @@ exports.coerce = coerce; exports.disable = disable; exports.enable = enable; exports.enabled = enabled; -exports.humanize = humanize; +exports.humanize = require('ms'); /** * The currently active debug mode names, and names to skip. @@ -83,25 +83,6 @@ function disable() { exports.enable(''); } -/** - * Humanize the given `ms`. - * - * @param {Number} m - * @return {String} - * @api private - */ - -function humanize(ms) { - var sec = 1000; - var min = 60 * 1000; - var hour = 60 * min; - - if (ms >= hour) return (ms / hour).toFixed(1) + 'h'; - if (ms >= min) return (ms / min).toFixed(1) + 'm'; - if (ms >= sec) return (ms / sec | 0) + 's'; - return ms + 'ms'; -} - /** * Returns true if the given mode name is enabled, false otherwise. * diff --git a/package.json b/package.json index c875bd28..c952f85b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ , "description": "small debugging utility" , "keywords": ["debug", "log", "debugger"] , "author": "TJ Holowaychuk " - , "dependencies": {} + , "dependencies": { + "ms": "0.6.2" + } , "devDependencies": { "mocha": "*" } , "main": "./node.js" , "browser": "./browser.js" From 0f61fca93f917abd9a804648ddea4f52ff846ebc Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 14:17:21 -0700 Subject: [PATCH 030/248] browser: don't let "%%" throw off the color insertion index --- browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser.js b/browser.js index 1ab3ab8c..67ed0b04 100644 --- a/browser.js +++ b/browser.js @@ -85,12 +85,12 @@ function log() { // figure out the correct index to insert the CSS into var index = 0; args[0].replace(/%[a-z%]/g, function(match) { + if ('%%' === match) return; index++; if (index < 3) return; // skip the first 2 %c's since that's handled already if ('%c' === match) { args.splice(index, 0, c); } - return match; }); } From 7a3f318cb6f0456e1ae009a296f382e937cb2059 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 14:23:18 -0700 Subject: [PATCH 031/248] debug: add custom "formatters" So that the browser, node, and possibly the end users can define their own "%n" formatters for the "format" argument while using debug. --- debug.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/debug.js b/debug.js index 851af35c..fc2ddb01 100644 --- a/debug.js +++ b/debug.js @@ -20,6 +20,14 @@ exports.humanize = humanize; exports.names = []; exports.skips = []; +/** + * Map of special "%n" handling functions, for the debug "format" argument. + * + * Valid key names are a single, lowercased letter, i.e. "n". + */ + +exports.formatters = {}; + /** * Create a debugger with the given `namespace`. * @@ -36,9 +44,35 @@ function debug(namespace) { disabled.enabled = false; // define the `enabled` version - function enabled(fmt) { - fmt = exports.coerce(fmt); - exports.log.apply(enabled, arguments); + function enabled() { + var args = Array.prototype.slice.call(arguments); + if ('string' === typeof args[0]) { + args[0] = exports.coerce(args[0]); + } else { + // anything else let's inspect with %o + args = ['%o'].concat(args); + } + + // apply any `formatters` transformations + var index = 0; + args[0] = args[0].replace(/%([a-z%])/g, function(match, format) { + // replace "%%" with a placeholder value for now. + // we restore it after doing the formatters parsing. + if (match === '%%') return '68ddd49c85cb46fc8c'; + index++; + var formatter = exports.formatters[format]; + if ('function' === typeof formatter) { + var val = args[index]; + match = formatter.call(enabled, val); + + // now we need to remove `args[index]` since it's inlined in the `format` + args.splice(index, 1); + index--; + } + return match; + }).replace(/68ddd49c85cb46fc8c/g, '%%'); + + exports.log.apply(enabled, args); } enabled.enabled = true; From 84951941a8dd894220f48243e8f8753e05500989 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 14:24:43 -0700 Subject: [PATCH 032/248] browser: map "%j" to `JSON.stringify()` --- browser.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/browser.js b/browser.js index 67ed0b04..1b60c235 100644 --- a/browser.js +++ b/browser.js @@ -43,6 +43,13 @@ var useColors = // is firebug? http://stackoverflow.com/a/398120/376773 (window.console && (console.firebug || (console.exception && console.table))); +/** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ + +exports.formatters.j = function(v) { + return JSON.stringify(v); +}; /** * Select a color. From b38496e03bcc7937bcf79f2d7f97cd17bfeaec23 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 14:24:59 -0700 Subject: [PATCH 033/248] node: map "%o" to `util.inspect()` --- node.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/node.js b/node.js index 87a94c2f..6111755d 100644 --- a/node.js +++ b/node.js @@ -4,6 +4,7 @@ */ var tty = require('tty'); +var util = require('util'); /** * This is the Node.js implementation of `debug()`. @@ -34,6 +35,16 @@ var prevColor = 0; var useColors = tty.isatty(1) || process.env.DEBUG_COLORS; +/** + * Map %o to `util.inspect()`, since Node doesn't do that out of the box. + */ + +exports.formatters.o = function(v) { + var str = (useColors ? '\u001b[0m' : '') + + util.inspect(v, { colors: useColors }).replace(/\s*\n\s*/g, ' '); + return str; +}; + /** * Select a color. * From 652feca774e855d40c1caa051c8e8dd72cdd1d2a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 18:57:30 -0700 Subject: [PATCH 034/248] debug: remove hacky placeholder logic @visionmedia pointed out that it is not necessary, which is good because it was very hacky. --- debug.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/debug.js b/debug.js index fc2ddb01..42831cff 100644 --- a/debug.js +++ b/debug.js @@ -56,9 +56,8 @@ function debug(namespace) { // apply any `formatters` transformations var index = 0; args[0] = args[0].replace(/%([a-z%])/g, function(match, format) { - // replace "%%" with a placeholder value for now. - // we restore it after doing the formatters parsing. - if (match === '%%') return '68ddd49c85cb46fc8c'; + // if we encounter an escaped % then don't increase the array index + if (match === '%%') return match; index++; var formatter = exports.formatters[format]; if ('function' === typeof formatter) { @@ -70,7 +69,7 @@ function debug(namespace) { index--; } return match; - }).replace(/68ddd49c85cb46fc8c/g, '%%'); + }); exports.log.apply(enabled, args); } From 32b0bf41eb39e6f5bcb70c0e9201039f40756f29 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 19:18:41 -0700 Subject: [PATCH 035/248] browser, node: rename `c` to `color` More clear for the user to override it this way. See: #95 --- browser.js | 4 ++-- node.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/browser.js b/browser.js index 1b60c235..38f83ea3 100644 --- a/browser.js +++ b/browser.js @@ -83,8 +83,8 @@ function log() { + '+' + exports.humanize(ms); if (useColors) { - if (null == this.c) this.c = selectColor(); - var c = 'color: ' + this.c; + if (null == this.color) this.color = selectColor(); + var c = 'color: ' + this.color; args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); // the final "%c" is somewhat tricky, because there could be other diff --git a/node.js b/node.js index 6111755d..57e33fe7 100644 --- a/node.js +++ b/node.js @@ -68,9 +68,9 @@ function log() { var name = this.namespace; if (useColors) { - if (null == this.c) this.c = selectColor(); + if (null == this.color) this.color = selectColor(); - var c = this.c; + var c = this.color; var curr = new Date(); var ms = curr - (this.prev || curr); this.prev = curr; From 9e4e0fff23d6a5899cf5fd827a003ba1c5f3033f Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 19:21:53 -0700 Subject: [PATCH 036/248] node: reset terminal color for the debug content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a controversial change as well, but it seems to be wanted by the community, and usability is more important to me than looks, so why not… Closes #47. Closes #52. Closes #87. --- node.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/node.js b/node.js index 57e33fe7..b1de2c1f 100644 --- a/node.js +++ b/node.js @@ -40,8 +40,7 @@ var useColors = tty.isatty(1) || process.env.DEBUG_COLORS; */ exports.formatters.o = function(v) { - var str = (useColors ? '\u001b[0m' : '') - + util.inspect(v, { colors: useColors }).replace(/\s*\n\s*/g, ' '); + var str = util.inspect(v, { colors: useColors }).replace(/\s*\n\s*/g, ' '); return str; }; @@ -76,7 +75,7 @@ function log() { this.prev = curr; args[0] = ' \u001b[9' + c + 'm' + name + ' ' - + '\u001b[3' + c + 'm\u001b[90m' + + '\u001b[0m' + args[0] + '\u001b[3' + c + 'm' + ' +' + exports.humanize(ms) + '\u001b[0m'; } else { From 182505d3657b4a4ab27432438a1c583b366e0354 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 19:58:08 -0700 Subject: [PATCH 037/248] Readme: add Web Inspector Colors section --- Readme.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 8884188c..9d76727c 100644 --- a/Readme.md +++ b/Readme.md @@ -73,7 +73,7 @@ setInterval(function(){ ## 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()`. + 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()`. ```js a = debug('worker:a'); @@ -88,6 +88,14 @@ setInterval(function(){ }, 1200); ``` +#### Web Inspector Colors + + Colors are also enabled on "Web Inspectors" that understand the `%c` formatting + option. These are WebKit web inspectors, and the Firebug plugin for Firefox. + Colored output looks something like: + + ![](https://cloud.githubusercontent.com/assets/71256/3139768/b98c5fd8-e8ef-11e3-862a-f7253b6f47c6.png) + ## License (The MIT License) From 533406869cabae60a070408fd17f4ebff34376f1 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 20:11:38 -0700 Subject: [PATCH 038/248] debug: fix passing an `Error` object in directly --- debug.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/debug.js b/debug.js index 10a5e86a..060e841a 100644 --- a/debug.js +++ b/debug.js @@ -46,9 +46,10 @@ function debug(namespace) { // define the `enabled` version function enabled() { var args = Array.prototype.slice.call(arguments); - if ('string' === typeof args[0]) { - args[0] = exports.coerce(args[0]); - } else { + + args[0] = exports.coerce(args[0]); + + if ('string' !== typeof args[0]) { // anything else let's inspect with %o args = ['%o'].concat(args); } From 44e6af18d35a27c1711201f84291719e54856abe Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 19:42:37 -0700 Subject: [PATCH 039/248] package: add "browserify" as a dev dependency --- package.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c952f85b..3bded461 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,12 @@ , "keywords": ["debug", "log", "debugger"] , "author": "TJ Holowaychuk " , "dependencies": { - "ms": "0.6.2" + "ms": "0.6.2" + } + , "devDependencies": { + "browserify": "4.1.6" + , "mocha": "*" } - , "devDependencies": { "mocha": "*" } , "main": "./node.js" , "browser": "./browser.js" , "component": { From 0c1a90a2784d903457645d4980298dcc1fa9c62f Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 19:42:58 -0700 Subject: [PATCH 040/248] Makefile: refactor to use browserify Uses browserify to build a `dist/debug.js` file for standalone browser-side consumption. --- Makefile | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 9c37f697..b4896d0a 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,33 @@ -build: debug.js - component build +# get Makefile directory name: http://stackoverflow.com/a/5982798/376773 +THIS_MAKEFILE_PATH:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) +THIS_DIR:=$(shell cd $(dir $(THIS_MAKEFILE_PATH));pwd) + +# BIN directory +BIN := $(THIS_DIR)/node_modules/.bin + +# applications +NODE ?= node +NPM ?= $(NODE) $(shell which npm) +BROWSERIFY ?= $(NODE) $(BIN)/browserify + +all: dist/debug.js + +install: node_modules + +clean: + @rm -rf node_modules dist + +dist: + @mkdir -p $@ + +dist/debug.js: node_modules browser.js debug.js dist + @$(BROWSERIFY) \ + --standalone debug \ + . > $@ + +node_modules: package.json + @NODE_ENV= $(NPM) install + @touch node_modules + +.PHONY: all install clean From 43b0d51c21134e86014f0ba007dfdabc6b21a35e Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 19:44:38 -0700 Subject: [PATCH 041/248] npmignore: don't publish the `dist` dir Not used in node/browserify, so not needed. --- .npmignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.npmignore b/.npmignore index fc10b6f7..7e6163db 100644 --- a/.npmignore +++ b/.npmignore @@ -3,3 +3,4 @@ test examples example *.sock +dist From 808c5865b9049b8b754198badfd1169b6e9c2875 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 20:06:10 -0700 Subject: [PATCH 042/248] dist: recompile --- dist/debug.js | 416 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 416 insertions(+) create mode 100644 dist/debug.js diff --git a/dist/debug.js b/dist/debug.js new file mode 100644 index 00000000..a4098a16 --- /dev/null +++ b/dist/debug.js @@ -0,0 +1,416 @@ +!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.debug=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= d) return Math.round(ms / d) + 'd'; + if (ms >= h) return Math.round(ms / h) + 'h'; + if (ms >= m) return Math.round(ms / m) + 'm'; + if (ms >= s) return Math.round(ms / s) + 's'; + return ms + 'ms'; +} + +/** + * Long format for `ms`. + * + * @param {Number} ms + * @return {String} + * @api private + */ + +function long(ms) { + return plural(ms, d, 'day') + || plural(ms, h, 'hour') + || plural(ms, m, 'minute') + || plural(ms, s, 'second') + || ms + ' ms'; +} + +/** + * Pluralization helper. + */ + +function plural(ms, n, name) { + if (ms < n) return; + if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name; + return Math.ceil(ms / n) + ' ' + name + 's'; +} + +},{}]},{},[1]) +(1) +}); \ No newline at end of file From 9b5e9f7ef28d19b2816c68053c110f9dfdf5116e Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 10:26:58 -0700 Subject: [PATCH 043/248] browser: even more robust "%c" handling Now we support the user passing in their own "%c" fields in `debug()` calls. --- browser.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/browser.js b/browser.js index 38f83ea3..cef8ea57 100644 --- a/browser.js +++ b/browser.js @@ -91,14 +91,18 @@ function log() { // arguments passed either before or after the %c, so we need to // figure out the correct index to insert the CSS into var index = 0; + var lastC = 0; args[0].replace(/%[a-z%]/g, function(match) { if ('%%' === match) return; index++; - if (index < 3) return; // skip the first 2 %c's since that's handled already if ('%c' === match) { - args.splice(index, 0, c); + // we only are interested in the *last* %c + // (the user may have provided their own) + lastC = index; } }); + + args.splice(lastC, 0, c); } // This hackery is required for IE8, From e502a7df95e1438b07d49275998ba32bcd9b40e3 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 10:30:49 -0700 Subject: [PATCH 044/248] *: export the `colors` array DRYs up some of the logic in the process, since the "core" color handling code gets moved to `debug.js` Closes #20. --- browser.js | 31 ++++++++----------------------- debug.js | 27 +++++++++++++++++++++++++-- node.js | 31 ++++++++----------------------- 3 files changed, 41 insertions(+), 48 deletions(-) diff --git a/browser.js b/browser.js index cef8ea57..1d7d93e3 100644 --- a/browser.js +++ b/browser.js @@ -9,12 +9,13 @@ exports = module.exports = require('./debug'); exports.log = log; exports.save = save; exports.load = load; +exports.useColors = useColors; /** * Colors. */ -var colors = [ +exports.colors = [ 'cyan', 'green', 'goldenrod', // "yellow" is just too bright on a white background... @@ -23,12 +24,6 @@ var colors = [ 'red' ]; -/** - * Previously assigned color. - */ - -var prevColor = 0; - /** * Currently only WebKit-based Web Inspectors and the Firebug * extension (*not* the built-in Firefox web inpector) are @@ -37,11 +32,12 @@ var prevColor = 0; * TODO: add a `localStorage` variable to explicitly enable/disable colors */ -var useColors = +function useColors() { // is webkit? http://stackoverflow.com/a/16459606/376773 - ('WebkitAppearance' in document.documentElement.style) || - // is firebug? http://stackoverflow.com/a/398120/376773 - (window.console && (console.firebug || (console.exception && console.table))); + return ('WebkitAppearance' in document.documentElement.style) || + // is firebug? http://stackoverflow.com/a/398120/376773 + (window.console && (console.firebug || (console.exception && console.table))); +} /** * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. @@ -51,17 +47,6 @@ exports.formatters.j = function(v) { return JSON.stringify(v); }; -/** - * Select a color. - * - * @return {Number} - * @api private - */ - -function selectColor() { - return colors[prevColor++ % colors.length]; -} - /** * Invokes `console.log()` when available. * No-op when `console.log` is not a "function". @@ -71,6 +56,7 @@ function selectColor() { function log() { var args = arguments; + var useColors = this.useColors; var curr = new Date(); var ms = curr - (this.prev || curr); this.prev = curr; @@ -83,7 +69,6 @@ function log() { + '+' + exports.humanize(ms); if (useColors) { - if (null == this.color) this.color = selectColor(); var c = 'color: ' + this.color; args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); diff --git a/debug.js b/debug.js index 060e841a..52aecb08 100644 --- a/debug.js +++ b/debug.js @@ -28,6 +28,23 @@ exports.skips = []; exports.formatters = {}; +/** + * Previously assigned color. + */ + +var prevColor = 0; + +/** + * Select a color. + * + * @return {Number} + * @api private + */ + +function selectColor() { + return exports.colors[prevColor++ % exports.colors.length]; +} + /** * Create a debugger with the given `namespace`. * @@ -45,6 +62,12 @@ function debug(namespace) { // define the `enabled` version function enabled() { + var self = enabled; + + // add the `color` if not set + if (null == self.useColors) self.useColors = exports.useColors(); + if (null == self.color && self.useColors) self.color = selectColor(); + var args = Array.prototype.slice.call(arguments); args[0] = exports.coerce(args[0]); @@ -63,7 +86,7 @@ function debug(namespace) { var formatter = exports.formatters[format]; if ('function' === typeof formatter) { var val = args[index]; - match = formatter.call(enabled, val); + match = formatter.call(self, val); // now we need to remove `args[index]` since it's inlined in the `format` args.splice(index, 1); @@ -72,7 +95,7 @@ function debug(namespace) { return match; }); - exports.log.apply(enabled, args); + exports.log.apply(self, args); } enabled.enabled = true; diff --git a/node.js b/node.js index b1de2c1f..721db96a 100644 --- a/node.js +++ b/node.js @@ -16,45 +16,31 @@ exports = module.exports = require('./debug'); exports.log = log; exports.save = save; exports.load = load; +exports.useColors = useColors; /** * Colors. */ -var colors = [6, 2, 3, 4, 5, 1]; - -/** - * Previously assigned color. - */ - -var prevColor = 0; +exports.colors = [6, 2, 3, 4, 5, 1]; /** * Is stdout a TTY? Colored output is disabled when `true`. */ -var useColors = tty.isatty(1) || process.env.DEBUG_COLORS; +function useColors() { + return tty.isatty(1) || process.env.DEBUG_COLORS; +} /** * Map %o to `util.inspect()`, since Node doesn't do that out of the box. */ exports.formatters.o = function(v) { - var str = util.inspect(v, { colors: useColors }).replace(/\s*\n\s*/g, ' '); - return str; + return util.inspect(v, { colors: this.useColors }) + .replace(/\s*\n\s*/g, ' '); }; -/** - * Select a color. - * - * @return {Number} - * @api private - */ - -function selectColor() { - return colors[prevColor++ % colors.length]; -} - /** * Invokes `console.log()` with the specified arguments, * after adding ANSI color escape codes if enabled. @@ -64,11 +50,10 @@ function selectColor() { function log() { var args = arguments; + var useColors = this.useColors; var name = this.namespace; if (useColors) { - if (null == this.color) this.color = selectColor(); - var c = this.color; var curr = new Date(); var ms = curr - (this.prev || curr); From 4501edeac60d351eac3cc782894b91e75934d564 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 10:39:37 -0700 Subject: [PATCH 045/248] dist: recompile --- dist/debug.js | 73 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/dist/debug.js b/dist/debug.js index a4098a16..996cc05d 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -10,12 +10,13 @@ exports = module.exports = _dereq_('./debug'); exports.log = log; exports.save = save; exports.load = load; +exports.useColors = useColors; /** * Colors. */ -var colors = [ +exports.colors = [ 'cyan', 'green', 'goldenrod', // "yellow" is just too bright on a white background... @@ -24,12 +25,6 @@ var colors = [ 'red' ]; -/** - * Previously assigned color. - */ - -var prevColor = 0; - /** * Currently only WebKit-based Web Inspectors and the Firebug * extension (*not* the built-in Firefox web inpector) are @@ -38,11 +33,12 @@ var prevColor = 0; * TODO: add a `localStorage` variable to explicitly enable/disable colors */ -var useColors = +function useColors() { // is webkit? http://stackoverflow.com/a/16459606/376773 - ('WebkitAppearance' in document.documentElement.style) || - // is firebug? http://stackoverflow.com/a/398120/376773 - (window.console && (console.firebug || (console.exception && console.table))); + return ('WebkitAppearance' in document.documentElement.style) || + // is firebug? http://stackoverflow.com/a/398120/376773 + (window.console && (console.firebug || (console.exception && console.table))); +} /** * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. @@ -52,17 +48,6 @@ exports.formatters.j = function(v) { return JSON.stringify(v); }; -/** - * Select a color. - * - * @return {Number} - * @api private - */ - -function selectColor() { - return colors[prevColor++ % colors.length]; -} - /** * Invokes `console.log()` when available. * No-op when `console.log` is not a "function". @@ -72,6 +57,7 @@ function selectColor() { function log() { var args = arguments; + var useColors = this.useColors; var curr = new Date(); var ms = curr - (this.prev || curr); this.prev = curr; @@ -84,7 +70,6 @@ function log() { + '+' + exports.humanize(ms); if (useColors) { - if (null == this.color) this.color = selectColor(); var c = 'color: ' + this.color; args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); @@ -92,14 +77,18 @@ function log() { // arguments passed either before or after the %c, so we need to // figure out the correct index to insert the CSS into var index = 0; + var lastC = 0; args[0].replace(/%[a-z%]/g, function(match) { if ('%%' === match) return; index++; - if (index < 3) return; // skip the first 2 %c's since that's handled already if ('%c' === match) { - args.splice(index, 0, c); + // we only are interested in the *last* %c + // (the user may have provided their own) + lastC = index; } }); + + args.splice(lastC, 0, c); } // This hackery is required for IE8, @@ -174,6 +163,23 @@ exports.skips = []; exports.formatters = {}; +/** + * Previously assigned color. + */ + +var prevColor = 0; + +/** + * Select a color. + * + * @return {Number} + * @api private + */ + +function selectColor() { + return exports.colors[prevColor++ % exports.colors.length]; +} + /** * Create a debugger with the given `namespace`. * @@ -191,10 +197,17 @@ function debug(namespace) { // define the `enabled` version function enabled() { + var self = enabled; + + // add the `color` if not set + if (null == self.useColors) self.useColors = exports.useColors(); + if (null == self.color && self.useColors) self.color = selectColor(); + var args = Array.prototype.slice.call(arguments); - if ('string' === typeof args[0]) { - args[0] = exports.coerce(args[0]); - } else { + + args[0] = exports.coerce(args[0]); + + if ('string' !== typeof args[0]) { // anything else let's inspect with %o args = ['%o'].concat(args); } @@ -208,7 +221,7 @@ function debug(namespace) { var formatter = exports.formatters[format]; if ('function' === typeof formatter) { var val = args[index]; - match = formatter.call(enabled, val); + match = formatter.call(self, val); // now we need to remove `args[index]` since it's inlined in the `format` args.splice(index, 1); @@ -217,7 +230,7 @@ function debug(namespace) { return match; }); - exports.log.apply(enabled, args); + exports.log.apply(self, args); } enabled.enabled = true; From 7df6b473ca1cc3a7a96df81cc294dadc908b30c9 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 10:41:09 -0700 Subject: [PATCH 046/248] example: fix "browser.html" example To use new browserify standalone build --- example/browser.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example/browser.html b/example/browser.html index 9baa91f8..46829328 100644 --- a/example/browser.html +++ b/example/browser.html @@ -1,21 +1,21 @@ + debug() - + From e0b6b07cb63f6fb637aac4b45b1f38927f7ee5ca Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 11:09:55 -0700 Subject: [PATCH 047/248] node: make DEBUG_COLORS able to disable coloring Fixes #59. --- node.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/node.js b/node.js index 721db96a..49ec6924 100644 --- a/node.js +++ b/node.js @@ -25,11 +25,20 @@ exports.useColors = useColors; exports.colors = [6, 2, 3, 4, 5, 1]; /** - * Is stdout a TTY? Colored output is disabled when `true`. + * Is stdout a TTY? Colored output is enabled when `true`. */ function useColors() { - return tty.isatty(1) || process.env.DEBUG_COLORS; + var debugColors = process.env.DEBUG_COLORS; + if (null == debugColors || 0 === debugColors.trim().length) { + return tty.isatty(1); + } else { + debugColors = debugColors.trim().toLowerCase(); + return '0' !== debugColors + && 'no' !== debugColors + && 'false' !== debugCoors + && 'disabled' !== debugColors; + } } /** From 48114629b442e61373e53f9a39000fefcfd089c8 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 14:21:26 -0700 Subject: [PATCH 048/248] node: fix typo and optimize `useColors()` logic --- node.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/node.js b/node.js index 49ec6924..6d9bb617 100644 --- a/node.js +++ b/node.js @@ -29,14 +29,13 @@ exports.colors = [6, 2, 3, 4, 5, 1]; */ function useColors() { - var debugColors = process.env.DEBUG_COLORS; - if (null == debugColors || 0 === debugColors.trim().length) { + var debugColors = (process.env.DEBUG_COLORS || '').trim().toLowerCase(); + if (0 === debugColors.length) { return tty.isatty(1); } else { - debugColors = debugColors.trim().toLowerCase(); return '0' !== debugColors && 'no' !== debugColors - && 'false' !== debugCoors + && 'false' !== debugColors && 'disabled' !== debugColors; } } From 4ddbb61abdff858bafc753d0e22ee45f526ca91a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 1 Jun 2014 14:26:47 -0700 Subject: [PATCH 049/248] debug: ignore empty strings in enable() Stops the unnecessary /^$/ regexp from being "enabled". --- debug.js | 1 + 1 file changed, 1 insertion(+) diff --git a/debug.js b/debug.js index 52aecb08..509f8664 100644 --- a/debug.js +++ b/debug.js @@ -121,6 +121,7 @@ function enable(namespaces) { var len = split.length; for (var i = 0; i < len; i++) { + if (!split[i]) continue; // ignore empty strings namespaces = split[i].replace('*', '.*?'); if (namespaces[0] === '-') { exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); From 872115d10aeb702a17ba99bfa2b5bf9f15a039b1 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 2 Jun 2014 11:36:49 -0700 Subject: [PATCH 050/248] make ms diff be global, not be scope Closes #88. Closes #90. --- browser.js | 5 +---- debug.js | 15 +++++++++++++++ node.js | 5 +---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/browser.js b/browser.js index 1d7d93e3..344ecaba 100644 --- a/browser.js +++ b/browser.js @@ -57,16 +57,13 @@ exports.formatters.j = function(v) { function log() { var args = arguments; var useColors = this.useColors; - var curr = new Date(); - var ms = curr - (this.prev || curr); - this.prev = curr; args[0] = (useColors ? '%c' : '') + this.namespace + (useColors ? '%c ' : ' ') + args[0] + (useColors ? '%c ' : ' ') - + '+' + exports.humanize(ms); + + '+' + exports.humanize(this.diff); if (useColors) { var c = 'color: ' + this.color; diff --git a/debug.js b/debug.js index 509f8664..1f771db4 100644 --- a/debug.js +++ b/debug.js @@ -34,6 +34,12 @@ exports.formatters = {}; var prevColor = 0; +/** + * Previous log timestamp. + */ + +var prevTime; + /** * Select a color. * @@ -62,8 +68,17 @@ function debug(namespace) { // define the `enabled` version function enabled() { + var self = enabled; + // set `diff` timestamp + var curr = +new Date(); + var ms = curr - (prevTime || curr); + self.diff = ms; + self.prev = prevTime; + self.curr = curr; + prevTime = curr; + // add the `color` if not set if (null == self.useColors) self.useColors = exports.useColors(); if (null == self.color && self.useColors) self.color = selectColor(); diff --git a/node.js b/node.js index 6d9bb617..f003f9c1 100644 --- a/node.js +++ b/node.js @@ -63,14 +63,11 @@ function log() { if (useColors) { var c = this.color; - var curr = new Date(); - var ms = curr - (this.prev || curr); - this.prev = curr; args[0] = ' \u001b[9' + c + 'm' + name + ' ' + '\u001b[0m' + args[0] + '\u001b[3' + c + 'm' - + ' +' + exports.humanize(ms) + '\u001b[0m'; + + ' +' + exports.humanize(this.diff) + '\u001b[0m'; } else { args[0] = new Date().toUTCString() + ' ' + name + ' ' + args[0]; From 91daf2860d8a9f7c65ac67bda4a0cbb587f7c61d Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 2 Jun 2014 11:41:49 -0700 Subject: [PATCH 051/248] dist: recompile --- dist/debug.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/dist/debug.js b/dist/debug.js index 996cc05d..b469a932 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -58,16 +58,13 @@ exports.formatters.j = function(v) { function log() { var args = arguments; var useColors = this.useColors; - var curr = new Date(); - var ms = curr - (this.prev || curr); - this.prev = curr; args[0] = (useColors ? '%c' : '') + this.namespace + (useColors ? '%c ' : ' ') + args[0] + (useColors ? '%c ' : ' ') - + '+' + exports.humanize(ms); + + '+' + exports.humanize(this.diff); if (useColors) { var c = 'color: ' + this.color; @@ -169,6 +166,12 @@ exports.formatters = {}; var prevColor = 0; +/** + * Previous log timestamp. + */ + +var prevTime; + /** * Select a color. * @@ -197,8 +200,17 @@ function debug(namespace) { // define the `enabled` version function enabled() { + var self = enabled; + // set `diff` timestamp + var curr = +new Date(); + var ms = curr - (prevTime || curr); + self.diff = ms; + self.prev = prevTime; + self.curr = curr; + prevTime = curr; + // add the `color` if not set if (null == self.useColors) self.useColors = exports.useColors(); if (null == self.color && self.useColors) self.color = selectColor(); @@ -256,6 +268,7 @@ function enable(namespaces) { var len = split.length; for (var i = 0; i < len; i++) { + if (!split[i]) continue; // ignore empty strings namespaces = split[i].replace('*', '.*?'); if (namespaces[0] === '-') { exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); From 908062b7b2503030890953c10168250e0251885c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 4 Jun 2014 20:56:43 -0700 Subject: [PATCH 052/248] Release 1.0.0 --- History.md | 23 +++++++++++++++++++++++ component.json | 8 ++++++-- package.json | 47 +++++++++++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/History.md b/History.md index 0d9a49a3..fe7418b8 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,27 @@ +1.0.0 / 2014-06-04 +================== + + * make ms diff be global, not be scope + * debug: ignore empty strings in enable() + * node: make DEBUG_COLORS able to disable coloring + * *: export the `colors` array + * npmignore: don't publish the `dist` dir + * Makefile: refactor to use browserify + * package: add "browserify" as a dev dependency + * Readme: add Web Inspector Colors section + * node: reset terminal color for the debug content + * node: map "%o" to `util.inspect()` + * browser: map "%j" to `JSON.stringify()` + * debug: add custom "formatters" + * debug: use "ms" module for humanizing the diff + * Readme: add "bash" syntax highlighting + * browser: add Firebug color support + * browser: add colors for WebKit browsers + * node: apply log to `console` + * rewrite: abstract common logic for Node & browsers + * add .jshintrc file + 0.8.1 / 2014-04-14 ================== diff --git a/component.json b/component.json index f9badfec..b1c7882d 100644 --- a/component.json +++ b/component.json @@ -2,8 +2,12 @@ "name": "debug", "repo": "visionmedia/debug", "description": "small debugging utility", - "version": "0.8.1", - "keywords": ["debug", "log", "debugger"], + "version": "1.0.0", + "keywords": [ + "debug", + "log", + "debugger" + ], "main": "browser.js", "scripts": [ "browser.js", diff --git a/package.json b/package.json index 3bded461..012ff296 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,30 @@ { - "name": "debug" - , "version": "0.8.1" - , "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" } - , "description": "small debugging utility" - , "keywords": ["debug", "log", "debugger"] - , "author": "TJ Holowaychuk " - , "dependencies": { - "ms": "0.6.2" - } - , "devDependencies": { - "browserify": "4.1.6" - , "mocha": "*" - } - , "main": "./node.js" - , "browser": "./browser.js" - , "component": { - "scripts": { - "debug/index.js": "browser.js" - , "debug/debug.js": "debug.js" - } + "name": "debug", + "version": "1.0.0", + "repository": { + "type": "git", + "url": "git://github.com/visionmedia/debug.git" + }, + "description": "small debugging utility", + "keywords": [ + "debug", + "log", + "debugger" + ], + "author": "TJ Holowaychuk ", + "dependencies": { + "ms": "0.6.2" + }, + "devDependencies": { + "browserify": "4.1.6", + "mocha": "*" + }, + "main": "./node.js", + "browser": "./browser.js", + "component": { + "scripts": { + "debug/index.js": "browser.js", + "debug/debug.js": "debug.js" } + } } From 4a78aa6c21500ab6cfece18282644dbf4ed7356d Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Wed, 4 Jun 2014 21:02:49 -0700 Subject: [PATCH 053/248] authors --- Readme.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 9d76727c..7e031bcf 100644 --- a/Readme.md +++ b/Readme.md @@ -96,11 +96,16 @@ setInterval(function(){ ![](https://cloud.githubusercontent.com/assets/71256/3139768/b98c5fd8-e8ef-11e3-862a-f7253b6f47c6.png) +## Authors + + - TJ Holowaychuk + - Nathan Rajlich + ## License (The MIT License) -Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca> +Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the From 9fdd3313be7bb7484c4c9a730f64e6e98f48a574 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 5 Jun 2014 00:26:01 -0700 Subject: [PATCH 054/248] node: type comment typo --- node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node.js b/node.js index f003f9c1..f54bcc0b 100644 --- a/node.js +++ b/node.js @@ -77,7 +77,7 @@ function log() { } /** - * Save `namespaces + * Save `namespaces`. * * @param {String} namespaces * @api private From a4de5389c0a5cb2b016aab586b1c9a0330f91e76 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 5 Jun 2014 00:36:36 -0700 Subject: [PATCH 055/248] package: add "contributors" section Feel free to add yourself in a PR if you've contributed a patch in the past. --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 012ff296..315afeaf 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,9 @@ "debugger" ], "author": "TJ Holowaychuk ", + "contributors": [ + "Nathan Rajlich (http://n8.io)" + ], "dependencies": { "ms": "0.6.2" }, From 3aa365ae93fa506fce68401f03fd7d63bf5af145 Mon Sep 17 00:00:00 2001 From: "David M. Lee" Date: Fri, 6 Jun 2014 14:49:26 -0500 Subject: [PATCH 056/248] Don't set DEBUG if namespaces is undefined. On Node.js, if you set a process.env field to either null or undefined, it gets cast to string 'null' or 'undefined'. For debug, this means that if you don't have DEBUG set, it will get set to the string 'undefined'. There are other modules (like node-rest-client) which use the DEBUG environment variable that will start spewing debug output when you don't want it to. --- browser.js | 6 +++++- node.js | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/browser.js b/browser.js index 344ecaba..e0a1b17c 100644 --- a/browser.js +++ b/browser.js @@ -103,7 +103,11 @@ function log() { function save(namespaces) { try { - localStorage.debug = namespaces; + if (null == namespaces) { + delete localStorage.debug; + } else { + localStorage.debug = namespaces; + } } catch(e) {} } diff --git a/node.js b/node.js index f54bcc0b..772bb420 100644 --- a/node.js +++ b/node.js @@ -84,7 +84,13 @@ function log() { */ function save(namespaces) { - process.env.DEBUG = namespaces; + if (null == namespaces) { + // If you set a process.env field to null or undefined, it gets cast to the + // string 'null' or 'undefined'. Just delete instead. + delete process.env.DEBUG; + } else { + process.env.DEBUG = namespaces; + } } /** From c1b0c6e9ccf1adefeb424216afc8ffab3ae691e4 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 6 Jun 2014 13:08:59 -0700 Subject: [PATCH 057/248] browser: use `removeItem()` to clear localStorage --- browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser.js b/browser.js index e0a1b17c..da1e3e56 100644 --- a/browser.js +++ b/browser.js @@ -104,7 +104,7 @@ function log() { function save(namespaces) { try { if (null == namespaces) { - delete localStorage.debug; + localStorage.removeItem('debug'); } else { localStorage.debug = namespaces; } From febc3fe5f5da0ddd95e21652fe8b97571962452c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 6 Jun 2014 13:22:45 -0700 Subject: [PATCH 058/248] Release 1.0.1 --- History.md | 9 +++++++++ component.json | 2 +- package.json | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index fe7418b8..73ff62ac 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,13 @@ +1.0.1 / 2014-06-06 +================== + + * browser: use `removeItem()` to clear localStorage + * browser, node: don't set DEBUG if namespaces is undefined (#107, @leedm777) + * package: add "contributors" section + * node: fix comment typo + * README: list authors + 1.0.0 / 2014-06-04 ================== diff --git a/component.json b/component.json index b1c7882d..1018f978 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "debug", "repo": "visionmedia/debug", "description": "small debugging utility", - "version": "1.0.0", + "version": "1.0.1", "keywords": [ "debug", "log", diff --git a/package.json b/package.json index 315afeaf..a5963959 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "debug", - "version": "1.0.0", + "version": "1.0.1", "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" From 613065a9b4580b6f77efb5435dabbf9e7af67a43 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 6 Jun 2014 13:34:35 -0700 Subject: [PATCH 059/248] dist: recompile --- dist/debug.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dist/debug.js b/dist/debug.js index b469a932..03169640 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -104,7 +104,11 @@ function log() { function save(namespaces) { try { - localStorage.debug = namespaces; + if (null == namespaces) { + localStorage.removeItem('debug'); + } else { + localStorage.debug = namespaces; + } } catch(e) {} } From 72793beb3222ac7045c858514f62ab0e38ad60d2 Mon Sep 17 00:00:00 2001 From: Tim Oxley Date: Sat, 7 Jun 2014 22:21:43 -0700 Subject: [PATCH 060/248] Find node path using shell/which. Without this patch, Make would fail with the following error: ``` > make /bin/sh: node: command not found make: *** [node_modules] Error 127 ``` This may be something strange about my computer's configuration, but seems like a harmless change. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b4896d0a..b0bde6e6 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ THIS_DIR:=$(shell cd $(dir $(THIS_MAKEFILE_PATH));pwd) BIN := $(THIS_DIR)/node_modules/.bin # applications -NODE ?= node +NODE ?= $(shell which node) NPM ?= $(NODE) $(shell which npm) BROWSERIFY ?= $(NODE) $(BIN)/browserify From 83f38e56acc0b885e64690c27599fc522390a782 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 7 Jun 2014 23:15:10 -0700 Subject: [PATCH 061/248] node: fix %o colors on old node <= 0.8.x --- node.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/node.js b/node.js index 772bb420..0e2feb11 100644 --- a/node.js +++ b/node.js @@ -44,8 +44,19 @@ function useColors() { * Map %o to `util.inspect()`, since Node doesn't do that out of the box. */ +var inspect = (4 === util.inspect.length ? + // node <= 0.8.x + function (v, colors) { + return util.inspect(v, void 0, void 0, colors); + } : + // node > 0.8.x + function (v, colors) { + return util.inspect(v, { colors: colors }); + } +); + exports.formatters.o = function(v) { - return util.inspect(v, { colors: this.useColors }) + return inspect(v, this.useColors) .replace(/\s*\n\s*/g, ' '); }; From dd32a4c902b8072eb6b1a36b498d73dada677359 Mon Sep 17 00:00:00 2001 From: Tim Oxley Date: Sat, 7 Jun 2014 23:25:28 -0700 Subject: [PATCH 062/248] Make console logging function configurable. Split log() into 2 methods: formatArgs() and log(), allowing log to be overridden on a per-namespace or global level. Global log settings trump per-namespace settings. --- Readme.md | 26 ++++++++++++++++++ browser.js | 61 ++++++++++++++++++++++++------------------ debug.js | 6 ++++- dist/debug.js | 63 +++++++++++++++++++++++++++----------------- example/browser.html | 4 +++ example/stderr.js | 17 ++++++++++++ node.js | 15 ++++++++--- 7 files changed, 138 insertions(+), 54 deletions(-) create mode 100644 example/stderr.js diff --git a/Readme.md b/Readme.md index 7e031bcf..e3607dbf 100644 --- a/Readme.md +++ b/Readme.md @@ -96,6 +96,32 @@ setInterval(function(){ ![](https://cloud.githubusercontent.com/assets/71256/3139768/b98c5fd8-e8ef-11e3-862a-f7253b6f47c6.png) +### stderr vs stdout + +You can set an alternative logging method per-namespace by overriding the `log` method on a per-namespace or globally: + +Example _stderr.js_: + +```js +var debug = require('../'); +var log = debug('app:log'); + +// by default console.log is used +log('goes to stdout!'); + +var error = debug('app:error'); +// set this namespace to log via console.error +error.log = console.error.bind(console); // don't forget to bind to console! +error('goes to stderr'); +log('still goes to stdout!'); + +// set all output to go via console.warn +// overrides all per-namespace log settings +debug.log = console.warn.bind(console); +log('now goes to stderr via console.warn'); +error('still goes to stderr, but via console.warn now'); +``` + ## Authors - TJ Holowaychuk diff --git a/browser.js b/browser.js index da1e3e56..70602899 100644 --- a/browser.js +++ b/browser.js @@ -7,6 +7,7 @@ exports = module.exports = require('./debug'); exports.log = log; +exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; @@ -47,14 +48,14 @@ exports.formatters.j = function(v) { return JSON.stringify(v); }; + /** - * Invokes `console.log()` when available. - * No-op when `console.log` is not a "function". + * Colorize log arguments if enabled. * * @api public */ -function log() { +function formatArgs() { var args = arguments; var useColors = this.useColors; @@ -65,33 +66,43 @@ function log() { + (useColors ? '%c ' : ' ') + '+' + exports.humanize(this.diff); - if (useColors) { - var c = 'color: ' + this.color; - args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); - - // the final "%c" is somewhat tricky, because there could be other - // arguments passed either before or after the %c, so we need to - // figure out the correct index to insert the CSS into - var index = 0; - var lastC = 0; - args[0].replace(/%[a-z%]/g, function(match) { - if ('%%' === match) return; - index++; - if ('%c' === match) { - // we only are interested in the *last* %c - // (the user may have provided their own) - lastC = index; - } - }); - - args.splice(lastC, 0, c); - } + if (!useColors) return args + + var c = 'color: ' + this.color; + args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); + + // the final "%c" is somewhat tricky, because there could be other + // arguments passed either before or after the %c, so we need to + // figure out the correct index to insert the CSS into + var index = 0; + var lastC = 0; + args[0].replace(/%[a-z%]/g, function(match) { + if ('%%' === match) return; + index++; + if ('%c' === match) { + // we only are interested in the *last* %c + // (the user may have provided their own) + lastC = index; + } + }); + + args.splice(lastC, 0, c); + return args; +} +/** + * Invokes `console.log()` when available. + * No-op when `console.log` is not a "function". + * + * @api public + */ + +function log() { // This hackery is required for IE8, // where the `console.log` function doesn't have 'apply' return 'object' == typeof console && 'function' == typeof console.log - && Function.prototype.apply.call(console.log, console, args); + && Function.prototype.apply.call(console.log, console, arguments); } /** diff --git a/debug.js b/debug.js index 1f771db4..c514fb76 100644 --- a/debug.js +++ b/debug.js @@ -110,7 +110,11 @@ function debug(namespace) { return match; }); - exports.log.apply(self, args); + if ('function' === typeof exports.formatArgs) { + args = exports.formatArgs.apply(self, args); + } + var logFn = exports.log || enabled.log || console.log.bind(console); + logFn.apply(self, args); } enabled.enabled = true; diff --git a/dist/debug.js b/dist/debug.js index 03169640..04441f71 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -8,6 +8,7 @@ exports = module.exports = _dereq_('./debug'); exports.log = log; +exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; @@ -48,14 +49,14 @@ exports.formatters.j = function(v) { return JSON.stringify(v); }; + /** - * Invokes `console.log()` when available. - * No-op when `console.log` is not a "function". + * Colorize log arguments if enabled. * * @api public */ -function log() { +function formatArgs() { var args = arguments; var useColors = this.useColors; @@ -66,33 +67,43 @@ function log() { + (useColors ? '%c ' : ' ') + '+' + exports.humanize(this.diff); - if (useColors) { - var c = 'color: ' + this.color; - args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); + if (!useColors) return args + + var c = 'color: ' + this.color; + args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); + + // the final "%c" is somewhat tricky, because there could be other + // arguments passed either before or after the %c, so we need to + // figure out the correct index to insert the CSS into + var index = 0; + var lastC = 0; + args[0].replace(/%[a-z%]/g, function(match) { + if ('%%' === match) return; + index++; + if ('%c' === match) { + // we only are interested in the *last* %c + // (the user may have provided their own) + lastC = index; + } + }); - // the final "%c" is somewhat tricky, because there could be other - // arguments passed either before or after the %c, so we need to - // figure out the correct index to insert the CSS into - var index = 0; - var lastC = 0; - args[0].replace(/%[a-z%]/g, function(match) { - if ('%%' === match) return; - index++; - if ('%c' === match) { - // we only are interested in the *last* %c - // (the user may have provided their own) - lastC = index; - } - }); + args.splice(lastC, 0, c); + return args; +} - args.splice(lastC, 0, c); - } +/** + * Invokes `console.log()` when available. + * No-op when `console.log` is not a "function". + * + * @api public + */ +function log() { // This hackery is required for IE8, // where the `console.log` function doesn't have 'apply' return 'object' == typeof console && 'function' == typeof console.log - && Function.prototype.apply.call(console.log, console, args); + && Function.prototype.apply.call(console.log, console, arguments); } /** @@ -246,7 +257,11 @@ function debug(namespace) { return match; }); - exports.log.apply(self, args); + if ('function' === typeof exports.formatArgs) { + args = exports.formatArgs.apply(self, args); + } + var logFn = exports.log || enabled.log || console.log.bind(console); + logFn.apply(self, args); } enabled.enabled = true; diff --git a/example/browser.html b/example/browser.html index 46829328..c0b534a4 100644 --- a/example/browser.html +++ b/example/browser.html @@ -10,6 +10,10 @@ var a = debug('worker:a'); var b = debug('worker:b'); + // set all output to go via console.info + // instead of console.log + debug.log = console.info.bind(console); + setInterval(function(){ a('doing some work'); }, 1000); diff --git a/example/stderr.js b/example/stderr.js new file mode 100644 index 00000000..35aa5fcb --- /dev/null +++ b/example/stderr.js @@ -0,0 +1,17 @@ +var debug = require('../'); +var log = debug('app:log'); + +// by default console.log is used +log('goes to stdout!'); + +var error = debug('app:error'); +// set this namespace to log via console.error +error.log = console.error.bind(console); // don't forget to bind to console! +error('goes to stderr'); +log('still goes to stdout!'); + +// set all output to go via console.warn +// overrides all per-namespace log settings +debug.log = console.warn.bind(console); +log('now goes to stderr via console.warn'); +error('still goes to stderr, but via console.warn now'); diff --git a/node.js b/node.js index 772bb420..0b1590c5 100644 --- a/node.js +++ b/node.js @@ -14,6 +14,7 @@ var util = require('util'); exports = module.exports = require('./debug'); exports.log = log; +exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; @@ -50,13 +51,12 @@ exports.formatters.o = function(v) { }; /** - * Invokes `console.log()` with the specified arguments, - * after adding ANSI color escape codes if enabled. + * Adds ANSI color escape codes if enabled. * * @api public */ -function log() { +function formatArgs() { var args = arguments; var useColors = this.useColors; var name = this.namespace; @@ -72,8 +72,15 @@ function log() { args[0] = new Date().toUTCString() + ' ' + name + ' ' + args[0]; } + return args; +} + +/** + * Invokes `console.log()` with the specified arguments. + */ - console.log.apply(console, args); +function log() { + return console.log.apply(console, arguments); } /** From 57f1cf2f303263c58097ab12d1e242cf3afece72 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 9 Jun 2014 11:26:47 -0700 Subject: [PATCH 063/248] History: consistent spacing --- History.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/History.md b/History.md index 73ff62ac..d5604533 100644 --- a/History.md +++ b/History.md @@ -34,26 +34,26 @@ 0.8.1 / 2014-04-14 ================== - * package: re-add the "component" section + * package: re-add the "component" section 0.8.0 / 2014-03-30 ================== - * add `enable()` method for nodejs. Closes #27 - * change from stderr to stdout - * remove unnecessary index.js file + * add `enable()` method for nodejs. Closes #27 + * change from stderr to stdout + * remove unnecessary index.js file 0.7.4 / 2013-11-13 ================== - * remove "browserify" key from package.json (fixes something in browserify) + * remove "browserify" key from package.json (fixes something in browserify) 0.7.3 / 2013-10-30 ================== - * fix: catch localStorage security error when cookies are blocked (Chrome) - * add debug(err) support. Closes #46 - * add .browser prop to package.json. Closes #42 + * fix: catch localStorage security error when cookies are blocked (Chrome) + * add debug(err) support. Closes #46 + * add .browser prop to package.json. Closes #42 0.7.2 / 2013-02-06 ================== From 5f54356b6d803a749b202d4ec563c5af856bb71d Mon Sep 17 00:00:00 2001 From: G Scott Olson Date: Tue, 10 Jun 2014 16:57:20 -0400 Subject: [PATCH 064/248] Update color palette. --- browser.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/browser.js b/browser.js index 70602899..302d8ee4 100644 --- a/browser.js +++ b/browser.js @@ -17,12 +17,12 @@ exports.useColors = useColors; */ exports.colors = [ - 'cyan', - 'green', - 'goldenrod', // "yellow" is just too bright on a white background... - 'blue', - 'purple', - 'red' + 'dodgerblue', + 'forestgreen', + 'goldenrod', + 'crimson', + 'darkorchid', + 'lightseagreen' ]; /** From d738362777d5e91cb3f485edaadfe86d55543e2e Mon Sep 17 00:00:00 2001 From: G Scott Olson Date: Tue, 10 Jun 2014 17:17:41 -0400 Subject: [PATCH 065/248] Restore color order. --- browser.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/browser.js b/browser.js index 302d8ee4..41d957c0 100644 --- a/browser.js +++ b/browser.js @@ -17,12 +17,12 @@ exports.useColors = useColors; */ exports.colors = [ - 'dodgerblue', + 'lightseagreen', 'forestgreen', 'goldenrod', - 'crimson', + 'dodgerblue', 'darkorchid', - 'lightseagreen' + 'crimson' ]; /** From 558f604f0788b7632e052e16a021e11b7201025b Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 10 Jun 2014 15:03:40 -0700 Subject: [PATCH 066/248] dist: recompile --- dist/debug.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dist/debug.js b/dist/debug.js index 04441f71..3fa8ae07 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -18,12 +18,12 @@ exports.useColors = useColors; */ exports.colors = [ - 'cyan', - 'green', - 'goldenrod', // "yellow" is just too bright on a white background... - 'blue', - 'purple', - 'red' + 'lightseagreen', + 'forestgreen', + 'goldenrod', + 'dodgerblue', + 'darkorchid', + 'crimson' ]; /** From 46939bcc1a67ed3a211a2ea2093bbce85473280c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 10 Jun 2014 17:50:47 -0700 Subject: [PATCH 067/248] Release 1.0.2 --- History.md | 8 ++++++++ component.json | 2 +- package.json | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index d5604533..42139a88 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,12 @@ +1.0.2 / 2014-06-10 +================== + + * browser: update color palette (#113, @gscottolson) + * common: make console logging function configurable (#108, @timoxley) + * node: fix %o colors on old node <= 0.8.x + * Makefile: find node path using shell/which (#109, @timoxley) + 1.0.1 / 2014-06-06 ================== diff --git a/component.json b/component.json index 1018f978..dfb0311a 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "debug", "repo": "visionmedia/debug", "description": "small debugging utility", - "version": "1.0.1", + "version": "1.0.2", "keywords": [ "debug", "log", diff --git a/package.json b/package.json index a5963959..2ca9eac6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "debug", - "version": "1.0.1", + "version": "1.0.2", "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" From aad769a8872fa7a31ccf712ec19014e682e11938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Gall=C3=A9-Tessonneau?= Date: Thu, 12 Jun 2014 10:30:19 +0200 Subject: [PATCH 068/248] update documentation about styled console logs Firefox now support styled console logs since version 31 (actually on aurora) : https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/ --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index e3607dbf..1ef8ad2a 100644 --- a/Readme.md +++ b/Readme.md @@ -91,7 +91,7 @@ setInterval(function(){ #### Web Inspector Colors Colors are also enabled on "Web Inspectors" that understand the `%c` formatting - option. These are WebKit web inspectors, and the Firebug plugin for Firefox. + option. These are WebKit web inspectors, Firefox since version 31 and the Firebug plugin for Firefox. Colored output looks something like: ![](https://cloud.githubusercontent.com/assets/71256/3139768/b98c5fd8-e8ef-11e3-862a-f7253b6f47c6.png) From 2f70264aad8d2403559421e7c49093ed1797a3c7 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 16 Jun 2014 17:24:54 -0700 Subject: [PATCH 069/248] browser: fix lint --- browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser.js b/browser.js index 41d957c0..d72eebfd 100644 --- a/browser.js +++ b/browser.js @@ -66,7 +66,7 @@ function formatArgs() { + (useColors ? '%c ' : ' ') + '+' + exports.humanize(this.diff); - if (!useColors) return args + if (!useColors) return args; var c = 'color: ' + this.color; args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); From d92fe0e6e0b97cc9a1ac8fe7c5b4e654acbd7de1 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 8 Jul 2014 12:02:38 +0100 Subject: [PATCH 070/248] Add support for multiple wildcards in namespaces --- debug.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug.js b/debug.js index c514fb76..21dc60a5 100644 --- a/debug.js +++ b/debug.js @@ -141,7 +141,7 @@ function enable(namespaces) { for (var i = 0; i < len; i++) { if (!split[i]) continue; // ignore empty strings - namespaces = split[i].replace('*', '.*?'); + namespaces = split[i].replace(/\*/g, '.*?'); if (namespaces[0] === '-') { exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); } else { From 93c759961d53ad7f06a1892a8dd0bf4be4a7b9df Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 9 Jul 2014 09:17:18 -0700 Subject: [PATCH 071/248] Release 1.0.3 --- History.md | 6 ++++++ component.json | 2 +- package.json | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 42139a88..3417b27d 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,10 @@ +1.0.3 / 2014-07-09 +================== + + * Add support for multiple wildcards in namespaces (#122, @seegno) + * browser: fix lint + 1.0.2 / 2014-06-10 ================== diff --git a/component.json b/component.json index dfb0311a..d42b49e9 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "debug", "repo": "visionmedia/debug", "description": "small debugging utility", - "version": "1.0.2", + "version": "1.0.3", "keywords": [ "debug", "log", diff --git a/package.json b/package.json index 2ca9eac6..cdaa7d4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "debug", - "version": "1.0.2", + "version": "1.0.3", "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" From 3add08ea0c0be11a4107ae2fd0c4edf34793ed1a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 15 Jul 2014 15:08:23 -0700 Subject: [PATCH 072/248] dist: recompile --- dist/debug.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/debug.js b/dist/debug.js index 3fa8ae07..17a14443 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -67,7 +67,7 @@ function formatArgs() { + (useColors ? '%c ' : ' ') + '+' + exports.humanize(this.diff); - if (!useColors) return args + if (!useColors) return args; var c = 'color: ' + this.color; args = [args[0], c, ''].concat(Array.prototype.slice.call(args, 1)); @@ -288,7 +288,7 @@ function enable(namespaces) { for (var i = 0; i < len; i++) { if (!split[i]) continue; // ignore empty strings - namespaces = split[i].replace('*', '.*?'); + namespaces = split[i].replace(/\*/g, '.*?'); if (namespaces[0] === '-') { exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); } else { From e5a4ef2c943a60d8658c0b48c7dcab50876c1c45 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 15 Jul 2014 15:09:24 -0700 Subject: [PATCH 073/248] example: add "Content-Type" UTF-8 header to browser example --- example/browser.html | 1 + 1 file changed, 1 insertion(+) diff --git a/example/browser.html b/example/browser.html index c0b534a4..be39bb4f 100644 --- a/example/browser.html +++ b/example/browser.html @@ -2,6 +2,7 @@ debug() +