From 00f3046c30eb6462123d1e68e5520d62ef19a996 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 12 Dec 2016 11:13:19 -0800 Subject: [PATCH] Node: `%O` (big O) pretty-prints the object (#322) * %O (big O) pretty-prints the object For example: ```js var debug = require('./')('foo') var o = { foo: 'bar', b: new Buffer(10), c: Math.PI } debug('%O', o) ``` Previously: ``` foo { foo: 'bar', b: , c: 3.141592653589793 } +0ms ``` Now: ``` foo { foo: 'bar', foo b: , foo c: 3.141592653589793 } +0ms ``` This is a breaking change for anybody relying on the old `%O` behavior. Though I don't think `%O` was working previously because the formatters regexp wasn't checking for uppercase formatters (now fixed in this patch). * use %O by default if no formatting string is given * Readme: add Formatters section Fixes #302. * Readme: finish custom formatters example --- Readme.md | 30 ++++++++++++++++++++++++++++++ debug.js | 6 +++--- node.js | 11 +++++++---- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 9e80851f..7ee4d081 100644 --- a/Readme.md +++ b/Readme.md @@ -87,6 +87,36 @@ Then, run the program to be debugged as usual. 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:". +## Formatters + + + Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting. Below are the officially supported formatters: + +| Formatter | Representation | +|-----------|----------------| +| `%O` | Pretty-print an Object on multiple lines. | +| `%o` | Pretty-print an Object all on a single line. | +| `%s` | String. | +| `%d` | Number (both integer and float). | +| `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. | +| `%%` | Single percent sign ('%'). This does not consume an argument. | + +### Custom formatters + + You can add custom formatters by extending the `debug.formatters` object. For example, if you wanted to add support for rendering a Buffer as hex with `%h`, you could do something like: + +```js +const createDebug = require('debug') +createDebug.formatters.h = (v) => { + return v.toString('hex') +} + +// …elsewhere +const debug = createDebug('foo') +debug('this is hex: %h', new Buffer('hello world')) +// foo this is hex: 68656c6c6f20776f726c6421 +0ms +``` + ## Browser support Debug works in the browser as well, currently persisted by `localStorage`. Consider the situation shown below where you have `worker:a` and `worker:b`, and wish to debug both. You can enable this using `localStorage.debug`: diff --git a/debug.js b/debug.js index b7fc5176..6664947c 100644 --- a/debug.js +++ b/debug.js @@ -98,13 +98,13 @@ function debug(namespace) { args[0] = exports.coerce(args[0]); if ('string' !== typeof args[0]) { - // anything else let's inspect with %o - args = ['%o'].concat(args); + // anything else let's inspect with %O + args.unshift('%O'); } // apply any `formatters` transformations var index = 0; - args[0] = args[0].replace(/%([a-z%])/g, function(match, format) { + args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) { // if we encounter an escaped % then don't increase the array index if (match === '%%') return match; index++; diff --git a/node.js b/node.js index 01e9fad5..1f5cd7ed 100644 --- a/node.js +++ b/node.js @@ -68,11 +68,15 @@ var inspect = (4 === util.inspect.length ? } ); -exports.formatters.o = exports.formatters.O = function(v) { +exports.formatters.o = function(v) { return inspect(v, this.useColors) .replace(/\s*\n\s*/g, ' '); }; +exports.formatters.O = function(v) { + return inspect(v, this.useColors); +}; + /** * Adds ANSI color escape codes if enabled. * @@ -90,10 +94,9 @@ function formatArgs() { if (useColors) { var c = this.color; + var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m'; - args[0] = ' \u001b[3' + c + ';1m' + name + ' ' - + '\u001b[0m' - + args[0]; + args[0] = prefix + args[0].split('\n').join('\n' + prefix); args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m'); } else { args[0] = new Date().toUTCString()