socketioMEANseed/server/node_modules/socket.io/node_modules/debug/node.js
server/node_modules/socket.io/node_modules/debug/node.jsRaw
1
2/**
3 * Module dependencies.
4 */
5
6var tty = require('tty');
7var util = require('util');
8
9/**
10 * This is the Node.js implementation of `debug()`.
11 *
12 * Expose `debug()` as the module.
13 */
14
15exports = module.exports = require('./debug');
16exports.log = log;
17exports.formatArgs = formatArgs;
18exports.save = save;
19exports.load = load;
20exports.useColors = useColors;
21
22/**
23 * Colors.
24 */
25
26exports.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
35var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
36var 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
44function 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
60var 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
71exports.formatters.o = 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
82function 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];
89 }
90
91 if (useColors) {
92 var c = this.color;
93
94 args[0] = ' \u001b[3' + c + ';1m' + name + ' '
95 + '\u001b[0m'
96 + args[0];
97 args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
98 } else {
99 args[0] = new Date().toUTCString()
100 + ' ' + name + ' ' + args[0];
101 }
102 return args;
103}
104
105/**
106 * Invokes `console.error()` with the specified arguments.
107 */
108
109function log() {
110 return stream.write(util.format.apply(this, arguments) + '\n');
111}
112
113/**
114 * Save `namespaces`.
115 *
116 * @param {String} namespaces
117 * @api private
118 */
119
120function 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;
125 } else {
126 process.env.DEBUG = namespaces;
127 }
128}
129
130/**
131 * Load `namespaces`.
132 *
133 * @return {String} returns the previously persisted debug modes
134 * @api private
135 */
136
137function load() {
138 return process.env.DEBUG;
139}
140
141/**
142 * Copied from `node/src/node.js`.
143 *
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.
146 */
147
148function createWritableStdioStream (fd) {
149 var stream;
150 var tty_wrap = process.binding('tty_wrap');
151
152 // Note stream._type is used for test-module-load-list.js
153
154 switch (tty_wrap.guessHandleType(fd)) {
155 case 'TTY':
156 stream = new tty.WriteStream(fd);
157 stream._type = 'tty';
158
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();
163 }
164 break;
165
166 case 'FILE':
167 var fs = require('fs');
168 stream = new fs.SyncWriteStream(fd, { autoClose: false });
169 stream._type = 'fs';
170 break;
171
172 case 'PIPE':
173 case 'TCP':
174 var net = require('net');
175 stream = new net.Socket({
176 fd: fd,
177 readable: false,
178 writable: true
179 });
180
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;
186 stream.read = null;
187 stream._type = 'pipe';
188
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();
193 }
194 break;
195
196 default:
197 // Probably an error on in uv_guess_handle()
198 throw new Error('Implement me. Unknown stream file type!');
199 }
200
201 // For supporting legacy API we put the FD here.
202 stream.fd = fd;
203
204 stream._isStdio = true;
205
206 return stream;
207}
208
209/**
210 * Enable namespaces listed in `process.env.DEBUG` initially.
211 */
212
213exports.enable(load());