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 * Build up the default `inspectOpts` object from the environment variables.
31 * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
34 exports.inspectOpts = Object.keys(process.env).filter(function (key) {
35 return /^debug_/i.test(key);
36 }).reduce(function (obj, key) {
41 .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
43 // coerce string value into JS value
44 var val = process.env[key];
45 if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
46 else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
47 else if (val === 'null') val = null;
48 else val = Number(val);
55 * The file descriptor to write the `debug()` calls to.
56 * Set the `DEBUG_FD` env variable to override with another value. i.e.:
58 * $ DEBUG_FD=3 node script.js 3>debug.log
61 var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
63 if (1 !== fd && 2 !== fd) {
64 util.deprecate(function(){}, 'except for stderr(2) and stdout(1), any other usage of DEBUG_FD is deprecated. Override debug.log if you want to use a different log function (https://git.io/debug_fd)')()
67 var stream = 1 === fd ? process.stdout :
68 2 === fd ? process.stderr :
69 createWritableStdioStream(fd);
72 * Is stdout a TTY? Colored output is enabled when `true`.
75 function useColors() {
76 return 'colors' in exports.inspectOpts
77 ? Boolean(exports.inspectOpts.colors)
82 * Map %o to `util.inspect()`, all on a single line.
85 exports.formatters.o = function(v) {
86 this.inspectOpts.colors = this.useColors;
87 return util.inspect(v, this.inspectOpts)
88 .split('\n').map(function(str) {
94 * Map %o to `util.inspect()`, allowing multiple lines if needed.
97 exports.formatters.O = function(v) {
98 this.inspectOpts.colors = this.useColors;
99 return util.inspect(v, this.inspectOpts);
103 * Adds ANSI color escape codes if enabled.
108 function formatArgs(args) {
109 var name = this.namespace;
110 var useColors = this.useColors;
114 var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
116 args[0] = prefix + args[0].split('\n').join('\n' + prefix);
117 args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
119 args[0] = new Date().toUTCString()
120 + ' ' + name + ' ' + args[0];
125 * Invokes `util.format()` with the specified arguments and writes to `stream`.
129 return stream.write(util.format.apply(util, arguments) + '\n');
135 * @param {String} namespaces
139 function save(namespaces) {
140 if (null == namespaces) {
141 // If you set a process.env field to null or undefined, it gets cast to the
142 // string 'null' or 'undefined'. Just delete instead.
143 delete process.env.DEBUG;
145 process.env.DEBUG = namespaces;
152 * @return {String} returns the previously persisted debug modes
157 return process.env.DEBUG;
161 * Copied from `node/src/node.js`.
163 * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
164 * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
167 function createWritableStdioStream (fd) {
169 var tty_wrap = process.binding('tty_wrap');
171 // Note stream._type is used for test-module-load-list.js
173 switch (tty_wrap.guessHandleType(fd)) {
175 stream = new tty.WriteStream(fd);
176 stream._type = 'tty';
178 // Hack to have stream not keep the event loop alive.
179 // See https://github.com/joyent/node/issues/1726
180 if (stream._handle && stream._handle.unref) {
181 stream._handle.unref();
186 var fs = require('fs');
187 stream = new fs.SyncWriteStream(fd, { autoClose: false });
193 var net = require('net');
194 stream = new net.Socket({
200 // FIXME Should probably have an option in net.Socket to create a
201 // stream from an existing fd which is writable only. But for now
202 // we'll just add this hack and set the `readable` member to false.
203 // Test: ./node test/fixtures/echo.js < /etc/passwd
204 stream.readable = false;
206 stream._type = 'pipe';
208 // FIXME Hack to have stream not keep the event loop alive.
209 // See https://github.com/joyent/node/issues/1726
210 if (stream._handle && stream._handle.unref) {
211 stream._handle.unref();
216 // Probably an error on in uv_guess_handle()
217 throw new Error('Implement me. Unknown stream file type!');
220 // For supporting legacy API we put the FD here.
223 stream._isStdio = true;
229 * Init logic for `debug` instances.
231 * Create a new `inspectOpts` object in case `useColors` is set
232 * differently for a particular `debug` instance.
235 function init (debug) {
236 debug.inspectOpts = {};
238 var keys = Object.keys(exports.inspectOpts);
239 for (var i = 0; i < keys.length; i++) {
240 debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
245 * Enable namespaces listed in `process.env.DEBUG` initially.
248 exports.enable(load());