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 = 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() {
84 var useColors = this.useColors;
85 var name = this.namespace;
90 args[0] = ' \u001b[3' + c + ';1m' + name + ' '
92 + args[0] + '\u001b[3' + c + 'm'
93 + ' +' + exports.humanize(this.diff) + '\u001b[0m';
95 args[0] = new Date().toUTCString()
96 + ' ' + name + ' ' + args[0];
102 * Invokes `console.error()` with the specified arguments.
106 return stream.write(util.format.apply(this, arguments) + '\n');
112 * @param {String} namespaces
116 function save(namespaces) {
117 if (null == namespaces) {
118 // If you set a process.env field to null or undefined, it gets cast to the
119 // string 'null' or 'undefined'. Just delete instead.
120 delete process.env.DEBUG;
122 process.env.DEBUG = namespaces;
129 * @return {String} returns the previously persisted debug modes
134 return process.env.DEBUG;
138 * Copied from `node/src/node.js`.
140 * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
141 * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
144 function createWritableStdioStream (fd) {
146 var tty_wrap = process.binding('tty_wrap');
148 // Note stream._type is used for test-module-load-list.js
150 switch (tty_wrap.guessHandleType(fd)) {
152 stream = new tty.WriteStream(fd);
153 stream._type = 'tty';
155 // Hack to have stream not keep the event loop alive.
156 // See https://github.com/joyent/node/issues/1726
157 if (stream._handle && stream._handle.unref) {
158 stream._handle.unref();
163 var fs = require('fs');
164 stream = new fs.SyncWriteStream(fd, { autoClose: false });
170 var net = require('net');
171 stream = new net.Socket({
177 // FIXME Should probably have an option in net.Socket to create a
178 // stream from an existing fd which is writable only. But for now
179 // we'll just add this hack and set the `readable` member to false.
180 // Test: ./node test/fixtures/echo.js < /etc/passwd
181 stream.readable = false;
183 stream._type = 'pipe';
185 // FIXME Hack to have stream not keep the event loop alive.
186 // See https://github.com/joyent/node/issues/1726
187 if (stream._handle && stream._handle.unref) {
188 stream._handle.unref();
193 // Probably an error on in uv_guess_handle()
194 throw new Error('Implement me. Unknown stream file type!');
197 // For supporting legacy API we put the FD here.
200 stream._isStdio = true;
206 * Enable namespaces listed in `process.env.DEBUG` initially.
209 exports.enable(load());