5 var tty = require('tty');
6 var util = require('util');
9 * This is the Node.js implementation of `debug()`.
11 * Expose `debug()` as the module.
14 exports = module.exports = require('./debug');
17 exports.formatArgs = formatArgs;
20 exports.useColors = useColors;
26 exports.colors = [ 6, 2, 3, 4, 5, 1 ];
29 var supportsColor = require('supports-color');
30 if (supportsColor && supportsColor.level >= 2) {
32 20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68,
33 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134,
34 135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
35 172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204,
36 205, 206, 207, 208, 209, 214, 215, 220, 221
40 // swallow - we only care if `supports-color` is available; it doesn't have to be.
44 * Build up the default `inspectOpts` object from the environment variables.
46 * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
49 exports.inspectOpts = Object.keys(process.env).filter(function (key) {
50 return /^debug_/i.test(key);
51 }).reduce(function (obj, key) {
56 .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
58 // coerce string value into JS value
59 var val = process.env[key];
60 if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
61 else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
62 else if (val === 'null') val = null;
63 else val = Number(val);
70 * Is stdout a TTY? Colored output is enabled when `true`.
73 function useColors() {
74 return 'colors' in exports.inspectOpts
75 ? Boolean(exports.inspectOpts.colors)
76 : tty.isatty(process.stderr.fd);
80 * Map %o to `util.inspect()`, all on a single line.
83 exports.formatters.o = function(v) {
84 this.inspectOpts.colors = this.useColors;
85 return util.inspect(v, this.inspectOpts)
86 .split('\n').map(function(str) {
92 * Map %o to `util.inspect()`, allowing multiple lines if needed.
95 exports.formatters.O = function(v) {
96 this.inspectOpts.colors = this.useColors;
97 return util.inspect(v, this.inspectOpts);
101 * Adds ANSI color escape codes if enabled.
106 function formatArgs(args) {
107 var name = this.namespace;
108 var useColors = this.useColors;
112 var colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
113 var prefix = ' ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
115 args[0] = prefix + args[0].split('\n').join('\n' + prefix);
116 args.push(colorCode + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
118 args[0] = getDate() + name + ' ' + args[0];
123 if (exports.inspectOpts.hideDate) {
126 return new Date().toISOString() + ' ';
131 * Invokes `util.format()` with the specified arguments and writes to stderr.
135 return process.stderr.write(util.format.apply(util, arguments) + '\n');
141 * @param {String} namespaces
145 function save(namespaces) {
146 if (null == namespaces) {
147 // If you set a process.env field to null or undefined, it gets cast to the
148 // string 'null' or 'undefined'. Just delete instead.
149 delete process.env.DEBUG;
151 process.env.DEBUG = namespaces;
158 * @return {String} returns the previously persisted debug modes
163 return process.env.DEBUG;
167 * Init logic for `debug` instances.
169 * Create a new `inspectOpts` object in case `useColors` is set
170 * differently for a particular `debug` instance.
173 function init (debug) {
174 debug.inspectOpts = {};
176 var keys = Object.keys(exports.inspectOpts);
177 for (var i = 0; i < keys.length; i++) {
178 debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
183 * Enable namespaces listed in `process.env.DEBUG` initially.
186 exports.enable(load());