Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
/**
2
* Module dependencies.
3
*/
4
5
var tty = require('tty');
6
var util = require('util');
7
8
/**
9
* This is the Node.js implementation of `debug()`.
10
*
11
* Expose `debug()` as the module.
12
*/
13
14
exports = module.exports = require('./debug');
15
exports.init = init;
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
* Build up the default `inspectOpts` object from the environment variables.
30
*
31
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
32
*/
33
34
exports.inspectOpts = Object.keys(process.env).filter(function (key) {
35
return /^debug_/i.test(key);
36
}).reduce(function (obj, key) {
37
// camel-case
38
var prop = key
39
.substring(6)
40
.toLowerCase()
41
.replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
42
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);
49
50
obj[prop] = val;
51
return obj;
52
}, {});
53
54
/**
55
* The file descriptor to write the `debug()` calls to.
56
* Set the `DEBUG_FD` env variable to override with another value. i.e.:
57
*
58
* $ DEBUG_FD=3 node script.js 3>debug.log
59
*/
60
61
var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
62
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)')()
65
}
66
67
var stream = 1 === fd ? process.stdout :
68
2 === fd ? process.stderr :
69
createWritableStdioStream(fd);
70
71
/**
72
* Is stdout a TTY? Colored output is enabled when `true`.
73
*/
74
75
function useColors() {
76
return 'colors' in exports.inspectOpts
77
? Boolean(exports.inspectOpts.colors)
78
: tty.isatty(fd);
79
}
80
81
/**
82
* Map %o to `util.inspect()`, all on a single line.
83
*/
84
85
exports.formatters.o = function(v) {
86
this.inspectOpts.colors = this.useColors;
87
return util.inspect(v, this.inspectOpts)
88
.replace(/\s*\n\s*/g, ' ');
89
};
90
91
/**
92
* Map %o to `util.inspect()`, allowing multiple lines if needed.
93
*/
94
95
exports.formatters.O = function(v) {
96
this.inspectOpts.colors = this.useColors;
97
return util.inspect(v, this.inspectOpts);
98
};
99
100
/**
101
* Adds ANSI color escape codes if enabled.
102
*
103
* @api public
104
*/
105
106
function formatArgs(args) {
107
var name = this.namespace;
108
var useColors = this.useColors;
109
110
if (useColors) {
111
var c = this.color;
112
var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
113
114
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
115
args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
116
} else {
117
args[0] = new Date().toUTCString()
118
+ ' ' + name + ' ' + args[0];
119
}
120
}
121
122
/**
123
* Invokes `util.format()` with the specified arguments and writes to `stream`.
124
*/
125
126
function log() {
127
return stream.write(util.format.apply(util, arguments) + '\n');
128
}
129
130
/**
131
* Save `namespaces`.
132
*
133
* @param {String} namespaces
134
* @api private
135
*/
136
137
function save(namespaces) {
138
if (null == namespaces) {
139
// If you set a process.env field to null or undefined, it gets cast to the
140
// string 'null' or 'undefined'. Just delete instead.
141
delete process.env.DEBUG;
142
} else {
143
process.env.DEBUG = namespaces;
144
}
145
}
146
147
/**
148
* Load `namespaces`.
149
*
150
* @return {String} returns the previously persisted debug modes
151
* @api private
152
*/
153
154
function load() {
155
return process.env.DEBUG;
156
}
157
158
/**
159
* Copied from `node/src/node.js`.
160
*
161
* XXX: It's lame that node doesn't expose this API out-of-the-box. It also
162
* relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
163
*/
164
165
function createWritableStdioStream (fd) {
166
var stream;
167
var tty_wrap = process.binding('tty_wrap');
168
169
// Note stream._type is used for test-module-load-list.js
170
171
switch (tty_wrap.guessHandleType(fd)) {
172
case 'TTY':
173
stream = new tty.WriteStream(fd);
174
stream._type = 'tty';
175
176
// Hack to have stream not keep the event loop alive.
177
// See https://github.com/joyent/node/issues/1726
178
if (stream._handle && stream._handle.unref) {
179
stream._handle.unref();
180
}
181
break;
182
183
case 'FILE':
184
var fs = require('fs');
185
stream = new fs.SyncWriteStream(fd, { autoClose: false });
186
stream._type = 'fs';
187
break;
188
189
case 'PIPE':
190
case 'TCP':
191
var net = require('net');
192
stream = new net.Socket({
193
fd: fd,
194
readable: false,
195
writable: true
196
});
197
198
// FIXME Should probably have an option in net.Socket to create a
199
// stream from an existing fd which is writable only. But for now
200
// we'll just add this hack and set the `readable` member to false.
201
// Test: ./node test/fixtures/echo.js < /etc/passwd
202
stream.readable = false;
203
stream.read = null;
204
stream._type = 'pipe';
205
206
// FIXME Hack to have stream not keep the event loop alive.
207
// See https://github.com/joyent/node/issues/1726
208
if (stream._handle && stream._handle.unref) {
209
stream._handle.unref();
210
}
211
break;
212
213
default:
214
// Probably an error on in uv_guess_handle()
215
throw new Error('Implement me. Unknown stream file type!');
216
}
217
218
// For supporting legacy API we put the FD here.
219
stream.fd = fd;
220
221
stream._isStdio = true;
222
223
return stream;
224
}
225
226
/**
227
* Init logic for `debug` instances.
228
*
229
* Create a new `inspectOpts` object in case `useColors` is set
230
* differently for a particular `debug` instance.
231
*/
232
233
function init (debug) {
234
debug.inspectOpts = {};
235
236
var keys = Object.keys(exports.inspectOpts);
237
for (var i = 0; i < keys.length; i++) {
238
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
239
}
240
}
241
242
/**
243
* Enable namespaces listed in `process.env.DEBUG` initially.
244
*/
245
246
exports.enable(load());
247
248