Initial commit
[yaffs-website] / node_modules / debug / node.js
1
2 /**
3  * Module dependencies.
4  */
5
6 var tty = require('tty');
7 var util = require('util');
8
9 /**
10  * This is the Node.js implementation of `debug()`.
11  *
12  * Expose `debug()` as the module.
13  */
14
15 exports = module.exports = require('./debug');
16 exports.log = log;
17 exports.formatArgs = formatArgs;
18 exports.save = save;
19 exports.load = load;
20 exports.useColors = useColors;
21
22 /**
23  * Colors.
24  */
25
26 exports.colors = [6, 2, 3, 4, 5, 1];
27
28 /**
29  * The file descriptor to write the `debug()` calls to.
30  * Set the `DEBUG_FD` env variable to override with another value. i.e.:
31  *
32  *   $ DEBUG_FD=3 node script.js 3>debug.log
33  */
34
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);
39
40 /**
41  * Is stdout a TTY? Colored output is enabled when `true`.
42  */
43
44 function useColors() {
45   var debugColors = (process.env.DEBUG_COLORS || '').trim().toLowerCase();
46   if (0 === debugColors.length) {
47     return tty.isatty(fd);
48   } else {
49     return '0' !== debugColors
50         && 'no' !== debugColors
51         && 'false' !== debugColors
52         && 'disabled' !== debugColors;
53   }
54 }
55
56 /**
57  * Map %o to `util.inspect()`, since Node doesn't do that out of the box.
58  */
59
60 var inspect = (4 === util.inspect.length ?
61   // node <= 0.8.x
62   function (v, colors) {
63     return util.inspect(v, void 0, void 0, colors);
64   } :
65   // node > 0.8.x
66   function (v, colors) {
67     return util.inspect(v, { colors: colors });
68   }
69 );
70
71 exports.formatters.o = function(v) {
72   return inspect(v, this.useColors)
73     .replace(/\s*\n\s*/g, ' ');
74 };
75
76 /**
77  * Adds ANSI color escape codes if enabled.
78  *
79  * @api public
80  */
81
82 function formatArgs() {
83   var args = arguments;
84   var useColors = this.useColors;
85   var name = this.namespace;
86
87   if (useColors) {
88     var c = this.color;
89
90     args[0] = '  \u001b[3' + c + ';1m' + name + ' '
91       + '\u001b[0m'
92       + args[0] + '\u001b[3' + c + 'm'
93       + ' +' + exports.humanize(this.diff) + '\u001b[0m';
94   } else {
95     args[0] = new Date().toUTCString()
96       + ' ' + name + ' ' + args[0];
97   }
98   return args;
99 }
100
101 /**
102  * Invokes `console.error()` with the specified arguments.
103  */
104
105 function log() {
106   return stream.write(util.format.apply(this, arguments) + '\n');
107 }
108
109 /**
110  * Save `namespaces`.
111  *
112  * @param {String} namespaces
113  * @api private
114  */
115
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;
121   } else {
122     process.env.DEBUG = namespaces;
123   }
124 }
125
126 /**
127  * Load `namespaces`.
128  *
129  * @return {String} returns the previously persisted debug modes
130  * @api private
131  */
132
133 function load() {
134   return process.env.DEBUG;
135 }
136
137 /**
138  * Copied from `node/src/node.js`.
139  *
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.
142  */
143
144 function createWritableStdioStream (fd) {
145   var stream;
146   var tty_wrap = process.binding('tty_wrap');
147
148   // Note stream._type is used for test-module-load-list.js
149
150   switch (tty_wrap.guessHandleType(fd)) {
151     case 'TTY':
152       stream = new tty.WriteStream(fd);
153       stream._type = 'tty';
154
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();
159       }
160       break;
161
162     case 'FILE':
163       var fs = require('fs');
164       stream = new fs.SyncWriteStream(fd, { autoClose: false });
165       stream._type = 'fs';
166       break;
167
168     case 'PIPE':
169     case 'TCP':
170       var net = require('net');
171       stream = new net.Socket({
172         fd: fd,
173         readable: false,
174         writable: true
175       });
176
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;
182       stream.read = null;
183       stream._type = 'pipe';
184
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();
189       }
190       break;
191
192     default:
193       // Probably an error on in uv_guess_handle()
194       throw new Error('Implement me. Unknown stream file type!');
195   }
196
197   // For supporting legacy API we put the FD here.
198   stream.fd = fd;
199
200   stream._isStdio = true;
201
202   return stream;
203 }
204
205 /**
206  * Enable namespaces listed in `process.env.DEBUG` initially.
207  */
208
209 exports.enable(load());