Skip to content

Commit 00f3046

Browse files
TooTallNatethebigredgeek
authored andcommitted
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: <Buffer 01 00 00 00 01 00 00 00 c0 82>, c: 3.141592653589793 } +0ms ``` Now: ``` foo { foo: 'bar', foo b: <Buffer 01 00 00 00 01 00 00 00 c0 82>, 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
1 parent bd9faa1 commit 00f3046

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

Readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ Then, run the program to be debugged as usual.
8787

8888
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:".
8989

90+
## Formatters
91+
92+
93+
Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting. Below are the officially supported formatters:
94+
95+
| Formatter | Representation |
96+
|-----------|----------------|
97+
| `%O` | Pretty-print an Object on multiple lines. |
98+
| `%o` | Pretty-print an Object all on a single line. |
99+
| `%s` | String. |
100+
| `%d` | Number (both integer and float). |
101+
| `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
102+
| `%%` | Single percent sign ('%'). This does not consume an argument. |
103+
104+
### Custom formatters
105+
106+
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:
107+
108+
```js
109+
const createDebug = require('debug')
110+
createDebug.formatters.h = (v) => {
111+
return v.toString('hex')
112+
}
113+
114+
// …elsewhere
115+
const debug = createDebug('foo')
116+
debug('this is hex: %h', new Buffer('hello world'))
117+
// foo this is hex: 68656c6c6f20776f726c6421 +0ms
118+
```
119+
90120
## Browser support
91121

92122
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`:

debug.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ function debug(namespace) {
9898
args[0] = exports.coerce(args[0]);
9999

100100
if ('string' !== typeof args[0]) {
101-
// anything else let's inspect with %o
102-
args = ['%o'].concat(args);
101+
// anything else let's inspect with %O
102+
args.unshift('%O');
103103
}
104104

105105
// apply any `formatters` transformations
106106
var index = 0;
107-
args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
107+
args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
108108
// if we encounter an escaped % then don't increase the array index
109109
if (match === '%%') return match;
110110
index++;

node.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ var inspect = (4 === util.inspect.length ?
6868
}
6969
);
7070

71-
exports.formatters.o = exports.formatters.O = function(v) {
71+
exports.formatters.o = function(v) {
7272
return inspect(v, this.useColors)
7373
.replace(/\s*\n\s*/g, ' ');
7474
};
7575

76+
exports.formatters.O = function(v) {
77+
return inspect(v, this.useColors);
78+
};
79+
7680
/**
7781
* Adds ANSI color escape codes if enabled.
7882
*
@@ -90,10 +94,9 @@ function formatArgs() {
9094

9195
if (useColors) {
9296
var c = this.color;
97+
var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
9398

94-
args[0] = ' \u001b[3' + c + ';1m' + name + ' '
95-
+ '\u001b[0m'
96-
+ args[0];
99+
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
97100
args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
98101
} else {
99102
args[0] = new Date().toUTCString()

0 commit comments

Comments
 (0)