6 var tty = require('tty');
7 var util = require('util');
10 * This is the Node.js implementation of `debug()`.
12 * Expose `debug()` as the module.
15 exports = module.exports = require('./debug');
17 exports.formatArgs = formatArgs;
20 exports.useColors = useColors;
26 exports.colors = [6, 2, 3, 4, 5, 1];
29 * The file descriptor to write the `debug()` calls to.
30 * Set the `DEBUG_FD` env variable to override with another value. i.e.:
32 * $ DEBUG_FD=3 node script.js 3>debug.log
35 var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
36 var stream = 1 === fd ? process.stdout :
37 2 === fd ? process.stderr :
38 createWritableStdioStream(fd);
41 * Is stdout a TTY? Colored output is enabled when `true`.
44 function useColors() {
45 var debugColors = (process.env.DEBUG_COLORS || '').trim().toLowerCase();
46 if (0 === debugColors.length) {
47 return tty.isatty(fd);
49 return '0' !== debugColors
50 && 'no' !== debugColors
51 && 'false' !== debugColors
52 && 'disabled' !== debugColors;
57 * Map %o to `util.inspect()`, since Node doesn't do that out of the box.
60 var inspect = (4 === util.inspect.length ?
62 function (v, colors) {
63 return util.inspect(v, void 0, void 0, colors);
66 function (v, colors) {
67 return util.inspect(v, { colors: colors });
71 exports.formatters.o = exports.formatters.O = function(v) {
72 return inspect(v, this.useColors)
73 .replace(/\s*\n\s*/g, ' ');
77 * Adds ANSI color escape codes if enabled.
82 function formatArgs() {
83 var len = arguments.length;
84 var args = new Array(len);
85 var useColors = this.useColors;
86 var name = this.namespace;
87 for (var i = 0; i < len; i++) {
88 args[i] = arguments[i];
94 args[0] = ' \u001b[3' + c + ';1m' + name + ' '
97 args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
99 args[0] = new Date().toUTCString()
100 + ' ' + name + ' ' + args[0];
106 * Invokes `console.error()` with the specified arguments.
110 return stream.write(util.format.apply(this, arguments) + '\n');
116 * @param {String} namespaces
120 function save(namespaces) {
121 if (null == namespaces) {
122 // If you set a process.env field to null or undefined, it gets cast to the
123 // string 'null' or 'undefined'. Just delete instead.
124 delete process.env.DEBUG;
126 process.env.DEBUG = namespaces;
133 * @return {String} returns the previously persisted debug modes
138 return process.env.DEBUG;
142 * Copied from `node/src/node.js`.
144 * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
145 * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
148 function createWritableStdioStream (fd) {
150 var tty_wrap = process.binding('tty_wrap');
152 // Note stream._type is used for test-module-load-list.js
154 switch (tty_wrap.guessHandleType(fd)) {
156 stream = new tty.WriteStream(fd);
157 stream._type = 'tty';
159 // Hack to have stream not keep the event loop alive.
160 // See https://github.com/joyent/node/issues/1726
161 if (stream._handle && stream._handle.unref) {
162 stream._handle.unref();
167 var fs = require('fs');
168 stream = new fs.SyncWriteStream(fd, { autoClose: false });
174 var net = require('net');
175 stream = new net.Socket({
181 // FIXME Should probably have an option in net.Socket to create a
182 // stream from an existing fd which is writable only. But for now
183 // we'll just add this hack and set the `readable` member to false.
184 // Test: ./node test/fixtures/echo.js < /etc/passwd
185 stream.readable = false;
187 stream._type = 'pipe';
189 // FIXME Hack to have stream not keep the event loop alive.
190 // See https://github.com/joyent/node/issues/1726
191 if (stream._handle && stream._handle.unref) {
192 stream._handle.unref();
197 // Probably an error on in uv_guess_handle()
198 throw new Error('Implement me. Unknown stream file type!');
201 // For supporting legacy API we put the FD here.
204 stream._isStdio = true;
210 * Enable namespaces listed in `process.env.DEBUG` initially.
213 exports.enable(load());