Skip to content

Commit

Permalink
node: allow configurable util.inspect() options
Browse files Browse the repository at this point in the history
Via env variables by default. So to get more object depth,
you pass the `DEBUG_DEPTH=10` env var.

For the `showHidden` option, you set `DEBUG_SHOW_HIDDEN=on`.

See the Node.js docs for the complete list of `util.inspect()` options:
https://nodejs.org/api/util.html#util_util_inspect_object_options
  • Loading branch information
TooTallNate committed Nov 10, 2016
1 parent f0e8a97 commit d23b07f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
5 changes: 5 additions & 0 deletions debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ function createDebug(namespace) {
debug.useColors = exports.useColors();
debug.color = selectColor();

// env-specific initialization logic for debug instances
if ('function' === typeof exports.init) {
exports.init(debug);
}

return debug;
}

Expand Down
68 changes: 45 additions & 23 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var util = require('util');
*/

exports = module.exports = require('./debug');
exports.init = init;
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
Expand All @@ -24,6 +25,32 @@ exports.useColors = useColors;

exports.colors = [6, 2, 3, 4, 5, 1];

/**
* Build up the default `inspectOpts` object from the environment variables.
*
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
*/

exports.inspectOpts = Object.keys(process.env).filter(function (key) {
return /^debug_/i.test(key);
}).reduce(function (obj, key) {
// camel-case
var prop = key
.substring(6)
.toLowerCase()
.replace(/_([a-z])/, function (_, k) { return k.toUpperCase() });

// coerce string value into JS value
var val = process.env[key];
if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
else if (val === 'null') val = null;
else val = Number(val);

obj[prop] = val;
return obj;
}, {});

/**
* The file descriptor to write the `debug()` calls to.
* Set the `DEBUG_FD` env variable to override with another value. i.e.:
Expand All @@ -41,34 +68,18 @@ var stream = 1 === fd ? process.stdout :
*/

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;
}
return 'colors' in exports.inspectOpts
? Boolean(exports.inspectOpts.colors)
: tty.isatty(fd);
}

/**
* 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 = exports.formatters.O = function(v) {
return inspect(v, this.useColors)
this.inspectOpts.colors = this.useColors;
return util.inspect(v, this.inspectOpts)
.replace(/\s*\n\s*/g, ' ');
};

Expand Down Expand Up @@ -96,11 +107,11 @@ function formatArgs(args) {
}

/**
* Invokes `console.error()` with the specified arguments.
* Invokes `util.format()` with the specified arguments and writes to `stream`.
*/

function log() {
return stream.write(util.format.apply(this, arguments) + '\n');
return stream.write(util.format.apply(util, arguments) + '\n');
}

/**
Expand Down Expand Up @@ -199,6 +210,17 @@ function createWritableStdioStream (fd) {
return stream;
}

/**
* Init logic for `debug` instances.
*
* Create a new `inspectOpts` object in case `useColors` is set
* differently for a particular `debug` instance.
*/

function init (debug) {
debug.inspectOpts = util._extend({}, exports.inspectOpts);
}

/**
* Enable namespaces listed in `process.env.DEBUG` initially.
*/
Expand Down

0 comments on commit d23b07f

Please sign in to comment.