debug-2.1.0/000077500000000000000000000000001241756661500126155ustar00rootroot00000000000000debug-2.1.0/.gitignore000066400000000000000000000000441241756661500146030ustar00rootroot00000000000000.DS_Store node_modules *.sock build debug-2.1.0/.jshintrc000066400000000000000000000000271241756661500144410ustar00rootroot00000000000000{ "laxbreak": true } debug-2.1.0/.npmignore000066400000000000000000000000521241756661500146110ustar00rootroot00000000000000support test examples example *.sock dist debug-2.1.0/History.md000066400000000000000000000104431241756661500146020ustar00rootroot00000000000000 2.1.0 / 2014-10-15 ================== * node: implement `DEBUG_FD` env variable support * package: update "browserify" to v6.1.0 * package: add "license" field to package.json (#135, @panuhorsmalahti) 2.0.0 / 2014-09-01 ================== * package: update "browserify" to v5.11.0 * node: use stderr rather than stdout for logging (#29, @stephenmathieson) 1.0.4 / 2014-07-15 ================== * dist: recompile * example: remove `console.info()` log usage * example: add "Content-Type" UTF-8 header to browser example * browser: place %c marker after the space character * browser: reset the "content" color via `color: inherit` * browser: add colors support for Firefox >= v31 * debug: prefer an instance `log()` function over the global one (#119) * Readme: update documentation about styled console logs for FF v31 (#116, @wryk) 1.0.3 / 2014-07-09 ================== * Add support for multiple wildcards in namespaces (#122, @seegno) * browser: fix lint 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 ================== * 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 ================== * 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 ================== * 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 0.7.4 / 2013-11-13 ================== * 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 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 ================== * add repository URL to package.json * add DEBUG_COLORED to force colored output * add browserify support * fix component. Closes #24 0.7.0 / 2012-05-04 ================== * Added .component to package.json * Added debug.component.js build 0.6.0 / 2012-03-16 ================== * Added support for "-" prefix in DEBUG [Vinay Pulim] * Added `.enabled` flag to the node version [TooTallNate] 0.5.0 / 2012-02-02 ================== * Added: humanize diffs. Closes #8 * Added `debug.disable()` to the CS variant * Removed padding. Closes #10 * Fixed: persist client-side variant again. Closes #9 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 ================== * Added millisecond diff when isatty, otherwise UTC string 0.2.0 / 2012-01-22 ================== * Added wildcard support 0.1.0 / 2011-12-02 ================== * Added: remove colors unless stderr isatty [TooTallNate] 0.0.1 / 2010-01-03 ================== * Initial release debug-2.1.0/Makefile000066400000000000000000000012461241756661500142600ustar00rootroot00000000000000 # 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 ?= $(shell which 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 debug-2.1.0/Readme.md000066400000000000000000000130401241756661500143320ustar00rootroot00000000000000# debug tiny node.js debugging utility modelled after node core's debugging technique. ## Installation ```bash $ 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 var debug = require('debug')('http') , http = require('http') , name = 'My App'; // fake app debug('booting %s', name); http.createServer(function(req, res){ debug(req.method + ' ' + req.url); res.end('hello\n'); }).listen(3000, function(){ debug('listening'); }); // fake worker of some kind require('./worker'); ``` Example _worker.js_: ```js var debug = require('debug')('worker'); setInterval(function(){ debug('doing some work'); }, 1000); ``` The __DEBUG__ environment variable is then used to enable these based on space or comma-delimited names. Here are some examples: ![debug http and worker](http://f.cl.ly/items/18471z1H402O24072r1J/Screenshot.png) ![debug worker](http://f.cl.ly/items/1X413v1a3M0d3C2c1E0i/Screenshot.png) ## Millisecond diff When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls. ![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png) 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". ## 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=*`. You can also exclude specific debuggers by prefixing them with a "-" character. For example, `DEBUG=*,-connect:*` would include all debuggers except those starting with "connect:". ## Browser support Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`. ```js a = debug('worker:a'); b = debug('worker:b'); setInterval(function(){ a('doing some work'); }, 1000); setInterval(function(){ b('doing some work'); }, 1200); ``` #### Web Inspector Colors Colors are also enabled on "Web Inspectors" that understand the `%c` formatting option. These are WebKit web inspectors, Firefox ([since version 31](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/)) and the Firebug plugin for Firefox (any version). Colored output looks something like: ![](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 - Nathan Rajlich ## License (The MIT License) 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 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 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. debug-2.1.0/browser.js000066400000000000000000000063151241756661500146430ustar00rootroot00000000000000 /** * This is the web browser implementation of `debug()`. * * Expose `debug()` as the module. */ exports = module.exports = require('./debug'); exports.log = log; exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; /** * Colors. */ exports.colors = [ 'lightseagreen', 'forestgreen', 'goldenrod', 'dodgerblue', 'darkorchid', 'crimson' ]; /** * Currently only WebKit-based Web Inspectors, Firefox >= v31, * and the Firebug extension (any Firefox version) are known * to support "%c" CSS customizations. * * TODO: add a `localStorage` variable to explicitly enable/disable colors */ function useColors() { // is webkit? http://stackoverflow.com/a/16459606/376773 return ('WebkitAppearance' in document.documentElement.style) || // is firebug? http://stackoverflow.com/a/398120/376773 (window.console && (console.firebug || (console.exception && console.table))) || // is firefox >= v31? // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31); } /** * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. */ exports.formatters.j = function(v) { return JSON.stringify(v); }; /** * Colorize log arguments if enabled. * * @api public */ function formatArgs() { var args = arguments; var useColors = this.useColors; args[0] = (useColors ? '%c' : '') + this.namespace + (useColors ? ' %c' : ' ') + args[0] + (useColors ? '%c ' : ' ') + '+' + exports.humanize(this.diff); if (!useColors) return args; var c = 'color: ' + this.color; args = [args[0], c, 'color: inherit'].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, arguments); } /** * Save `namespaces`. * * @param {String} namespaces * @api private */ function save(namespaces) { try { if (null == namespaces) { localStorage.removeItem('debug'); } else { 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()); debug-2.1.0/component.json000066400000000000000000000004651241756661500155170ustar00rootroot00000000000000{ "name": "debug", "repo": "visionmedia/debug", "description": "small debugging utility", "version": "2.1.0", "keywords": [ "debug", "log", "debugger" ], "main": "browser.js", "scripts": [ "browser.js", "debug.js" ], "dependencies": { "guille/ms.js": "0.6.1" } } debug-2.1.0/debug.js000066400000000000000000000100001241756661500142300ustar00rootroot00000000000000 /** * This is the common logic for both the Node.js and web browser * implementations of `debug()`. * * Expose `debug()` as the module. */ exports = module.exports = debug; exports.coerce = coerce; exports.disable = disable; exports.enable = enable; exports.enabled = enabled; exports.humanize = require('ms'); /** * The currently active debug mode names, and names to skip. */ 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 = {}; /** * Previously assigned color. */ var prevColor = 0; /** * Previous log timestamp. */ var prevTime; /** * Select a color. * * @return {Number} * @api private */ function selectColor() { return exports.colors[prevColor++ % exports.colors.length]; } /** * Create a debugger with the given `namespace`. * * @param {String} namespace * @return {Function} * @api public */ function debug(namespace) { // define the `disabled` version function disabled() { } disabled.enabled = false; // 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(); var args = Array.prototype.slice.call(arguments); args[0] = exports.coerce(args[0]); if ('string' !== typeof args[0]) { // 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) { // 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) { var val = args[index]; match = formatter.call(self, val); // now we need to remove `args[index]` since it's inlined in the `format` args.splice(index, 1); index--; } return match; }); if ('function' === typeof exports.formatArgs) { args = exports.formatArgs.apply(self, args); } var logFn = enabled.log || exports.log || console.log.bind(console); logFn.apply(self, args); } enabled.enabled = true; var fn = exports.enabled(namespace) ? enabled : disabled; fn.namespace = namespace; return fn; } /** * Enables a debug mode by namespaces. This can include modes * separated by a colon and wildcards. * * @param {String} namespaces * @api public */ function enable(namespaces) { exports.save(namespaces); var split = (namespaces || '').split(/[\s,]+/); var len = split.length; for (var i = 0; i < len; i++) { if (!split[i]) continue; // ignore empty strings namespaces = split[i].replace(/\*/g, '.*?'); if (namespaces[0] === '-') { exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); } else { exports.names.push(new RegExp('^' + namespaces + '$')); } } } /** * Disable debug output. * * @api public */ function disable() { exports.enable(''); } /** * Returns true if the given mode name is enabled, false otherwise. * * @param {String} name * @return {Boolean} * @api public */ 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 (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; } debug-2.1.0/example/000077500000000000000000000000001241756661500142505ustar00rootroot00000000000000debug-2.1.0/example/app.js000066400000000000000000000005071241756661500153700ustar00rootroot00000000000000 var debug = require('../')('http') , http = require('http') , name = 'My App'; // fake app debug('booting %s', name); http.createServer(function(req, res){ debug(req.method + ' ' + req.url); res.end('hello\n'); }).listen(3000, function(){ debug('listening'); }); // fake worker of some kind require('./worker');debug-2.1.0/example/browser.html000066400000000000000000000010251241756661500166170ustar00rootroot00000000000000 debug() debug-2.1.0/example/stderr.js000066400000000000000000000010521241756661500161070ustar00rootroot00000000000000var 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'); debug-2.1.0/example/wildcards.js000066400000000000000000000002551241756661500165640ustar00rootroot00000000000000 var debug = { foo: require('../')('test:foo'), bar: require('../')('test:bar'), baz: require('../')('test:baz') }; debug.foo('foo') debug.bar('bar') debug.baz('baz')debug-2.1.0/example/worker.js000066400000000000000000000007501241756661500161210ustar00rootroot00000000000000 // DEBUG=* node example/worker // DEBUG=worker:* node example/worker // DEBUG=worker:a node example/worker // DEBUG=worker:b node example/worker var a = require('../')('worker:a') , b = require('../')('worker:b'); function work() { a('doing lots of uninteresting work'); setTimeout(work, Math.random() * 1000); } work(); function workb() { b('doing some work'); setTimeout(workb, Math.random() * 2000); } workb(); setTimeout(function(){ b(new Error('fail')); }, 5000); debug-2.1.0/node.js000066400000000000000000000112241241756661500141000ustar00rootroot00000000000000 /** * Module dependencies. */ var tty = require('tty'); var util = require('util'); /** * This is the Node.js implementation of `debug()`. * * Expose `debug()` as the module. */ exports = module.exports = require('./debug'); exports.log = log; exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; /** * Colors. */ exports.colors = [6, 2, 3, 4, 5, 1]; /** * The file descriptor to write the `debug()` calls to. * Set the `DEBUG_FD` env variable to override with another value. i.e.: * * $ DEBUG_FD=3 node script.js 3>debug.log */ var fd = parseInt(process.env.DEBUG_FD, 10) || 2; var stream = 1 === fd ? process.stdout : 2 === fd ? process.stderr : createWritableStdioStream(fd); /** * Is stdout a TTY? Colored output is enabled when `true`. */ function useColors() { var debugColors = (process.env.DEBUG_COLORS || '').trim().toLowerCase(); if (0 === debugColors.length) { return tty.isatty(fd); } else { return '0' !== debugColors && 'no' !== debugColors && 'false' !== debugColors && 'disabled' !== debugColors; } } /** * 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 inspect(v, this.useColors) .replace(/\s*\n\s*/g, ' '); }; /** * Adds ANSI color escape codes if enabled. * * @api public */ function formatArgs() { var args = arguments; var useColors = this.useColors; var name = this.namespace; if (useColors) { var c = this.color; args[0] = ' \u001b[9' + c + 'm' + name + ' ' + '\u001b[0m' + args[0] + '\u001b[3' + c + 'm' + ' +' + exports.humanize(this.diff) + '\u001b[0m'; } else { args[0] = new Date().toUTCString() + ' ' + name + ' ' + args[0]; } return args; } /** * Invokes `console.error()` with the specified arguments. */ function log() { return stream.write(util.format.apply(this, arguments) + '\n'); } /** * Save `namespaces`. * * @param {String} namespaces * @api private */ function save(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; } } /** * Load `namespaces`. * * @return {String} returns the previously persisted debug modes * @api private */ function load() { return process.env.DEBUG; } /** * Copied from `node/src/node.js`. * * XXX: It's lame that node doesn't expose this API out-of-the-box. It also * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame. */ function createWritableStdioStream (fd) { var stream; var tty_wrap = process.binding('tty_wrap'); // Note stream._type is used for test-module-load-list.js switch (tty_wrap.guessHandleType(fd)) { case 'TTY': stream = new tty.WriteStream(fd); stream._type = 'tty'; // Hack to have stream not keep the event loop alive. // See https://github.com/joyent/node/issues/1726 if (stream._handle && stream._handle.unref) { stream._handle.unref(); } break; case 'FILE': var fs = require('fs'); stream = new fs.SyncWriteStream(fd, { autoClose: false }); stream._type = 'fs'; break; case 'PIPE': case 'TCP': var net = require('net'); stream = new net.Socket({ fd: fd, readable: false, writable: true }); // FIXME Should probably have an option in net.Socket to create a // stream from an existing fd which is writable only. But for now // we'll just add this hack and set the `readable` member to false. // Test: ./node test/fixtures/echo.js < /etc/passwd stream.readable = false; stream.read = null; stream._type = 'pipe'; // FIXME Hack to have stream not keep the event loop alive. // See https://github.com/joyent/node/issues/1726 if (stream._handle && stream._handle.unref) { stream._handle.unref(); } break; default: // Probably an error on in uv_guess_handle() throw new Error('Implement me. Unknown stream file type!'); } // For supporting legacy API we put the FD here. stream.fd = fd; stream._isStdio = true; return stream; } /** * Enable namespaces listed in `process.env.DEBUG` initially. */ exports.enable(load()); debug-2.1.0/package.json000066400000000000000000000012541241756661500151050ustar00rootroot00000000000000{ "name": "debug", "version": "2.1.0", "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git" }, "description": "small debugging utility", "keywords": [ "debug", "log", "debugger" ], "author": "TJ Holowaychuk ", "contributors": [ "Nathan Rajlich (http://n8.io)" ], "license": "MIT", "dependencies": { "ms": "0.6.2" }, "devDependencies": { "browserify": "6.1.0", "mocha": "*" }, "main": "./node.js", "browser": "./browser.js", "component": { "scripts": { "debug/index.js": "browser.js", "debug/debug.js": "debug.js" } } }