1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var formatRegExp = /%[sdj%]/g;
23 exports.format = function(f) {
26 for (var i = 0; i < arguments.length; i++) {
27 objects.push(inspect(arguments[i]));
29 return objects.join(' ');
34 var len = args.length;
35 var str = String(f).replace(formatRegExp, function(x) {
36 if (x === '%%') return '%';
37 if (i >= len) return x;
39 case '%s': return String(args[i++]);
40 case '%d': return Number(args[i++]);
41 case '%j': return JSON.stringify(args[i++]);
46 for (var x = args[i]; i < len; x = args[++i]) {
47 if (IS_NULL(x) || !IS_OBJECT(x)) {
50 str += ' ' + inspect(x);
57 // Mark that a method should not be used.
58 // Returns a modified function which warns once by default.
59 // If --no-deprecation is set, then it is a no-op.
60 exports.deprecate = function(fn, msg) {
61 if (process.noDeprecation === true) {
66 function deprecated() {
68 if (process.throwDeprecation) {
70 } else if (process.traceDeprecation) {
77 return fn.apply(this, arguments);
85 var debugEnviron = process.env.NODE_DEBUG || '';
86 exports.debuglog = function(set) {
87 set = set.toUpperCase();
89 if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
90 var pid = process.pid;
91 debugs[set] = function() {
92 var msg = exports.format.apply(exports, arguments);
93 console.error('%s %d: %s', set, pid, msg);
96 debugs[set] = function() {};
104 * Echos the value of a value. Trys to print the value out
105 * in the best way possible given the different types.
107 * @param {Object} obj The object to print out.
108 * @param {Object} opts Optional options object that alters the output.
110 /* legacy: obj, showHidden, depth, colors*/
111 function inspect(obj, opts) {
115 stylize: stylizeNoColor
118 if (arguments.length >= 3) ctx.depth = arguments[2];
119 if (arguments.length >= 4) ctx.colors = arguments[3];
120 if (IS_BOOLEAN(opts)) {
122 ctx.showHidden = opts;
124 // got an "options" object
125 exports._extend(ctx, opts);
127 // set default options
128 if (IS_UNDEFINED(ctx.showHidden)) ctx.showHidden = false;
129 if (IS_UNDEFINED(ctx.depth)) ctx.depth = 2;
130 if (IS_UNDEFINED(ctx.colors)) ctx.colors = false;
131 if (IS_UNDEFINED(ctx.customInspect)) ctx.customInspect = true;
132 if (ctx.colors) ctx.stylize = stylizeWithColor;
133 return formatValue(ctx, obj, ctx.depth);
135 exports.inspect = inspect;
138 // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
142 'underline' : [4, 24],
150 'magenta' : [35, 39],
155 // Don't use 'blue' not visible on cmd.exe
164 // "name": intentionally not styling
169 function stylizeWithColor(str, styleType) {
170 var style = inspect.styles[styleType];
173 return '\u001b[' + inspect.colors[style][0] + 'm' + str +
174 '\u001b[' + inspect.colors[style][1] + 'm';
181 function stylizeNoColor(str, styleType) {
186 function arrayToHash(array) {
189 array.forEach(function(val, idx) {
197 function formatValue(ctx, value, recurseTimes) {
198 // Provide a hook for user-specified inspect functions.
199 // Check that value is an object with an inspect function on it
200 if (ctx.customInspect &&
202 IS_FUNCTION(value.inspect) &&
203 // Filter out the util module, it's inspect function is special
204 value.inspect !== exports.inspect &&
205 // Also filter out any prototype objects using the circular check.
206 !(value.constructor && value.constructor.prototype === value)) {
207 var ret = value.inspect(recurseTimes);
208 if (!IS_STRING(ret)) {
209 ret = formatValue(ctx, ret, recurseTimes);
214 // Primitive types cannot have properties
215 var primitive = formatPrimitive(ctx, value);
220 // Look up the keys of the object.
221 var keys = Object.keys(value);
222 var visibleKeys = arrayToHash(keys);
224 if (ctx.showHidden) {
225 keys = Object.getOwnPropertyNames(value);
228 // Some type of object without properties can be shortcutted.
229 if (keys.length === 0) {
230 if (IS_FUNCTION(value)) {
231 var name = value.name ? ': ' + value.name : '';
232 return ctx.stylize('[Function' + name + ']', 'special');
234 if (isRegExp(value)) {
235 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
238 return ctx.stylize(Date.prototype.toString.call(value), 'date');
240 if (isError(value)) {
241 return formatError(value);
245 var base = '', array = false, braces = ['{', '}'];
247 // Make Array say that they are Array
248 if (isArray(value)) {
253 // Make functions say that they are functions
254 if (IS_FUNCTION(value)) {
255 var n = value.name ? ': ' + value.name : '';
256 base = ' [Function' + n + ']';
259 // Make RegExps say that they are RegExps
260 if (isRegExp(value)) {
261 base = ' ' + RegExp.prototype.toString.call(value);
264 // Make dates with properties first say the date
266 base = ' ' + Date.prototype.toUTCString.call(value);
269 // Make error with message first say the error
270 if (isError(value)) {
271 base = ' ' + formatError(value);
274 if (keys.length === 0 && (!array || value.length == 0)) {
275 return braces[0] + base + braces[1];
278 if (recurseTimes < 0) {
279 if (isRegExp(value)) {
280 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
282 return ctx.stylize('[Object]', 'special');
286 ctx.seen.push(value);
290 output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
292 output = keys.map(function(key) {
293 return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
299 return reduceToSingleString(output, base, braces);
303 function formatPrimitive(ctx, value) {
304 if (IS_UNDEFINED(value))
305 return ctx.stylize('undefined', 'undefined');
306 if (IS_STRING(value)) {
307 var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
308 .replace(/'/g, "\\'")
309 .replace(/\\"/g, '"') + '\'';
310 return ctx.stylize(simple, 'string');
312 if (IS_NUMBER(value))
313 return ctx.stylize('' + value, 'number');
314 if (IS_BOOLEAN(value))
315 return ctx.stylize('' + value, 'boolean');
316 // For some reason typeof null is "object", so special case here.
318 return ctx.stylize('null', 'null');
322 function formatError(value) {
323 return '[' + Error.prototype.toString.call(value) + ']';
327 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
329 for (var i = 0, l = value.length; i < l; ++i) {
330 if (hasOwnProperty(value, String(i))) {
331 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
337 keys.forEach(function(key) {
338 if (!key.match(/^\d+$/)) {
339 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
347 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
349 desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
352 str = ctx.stylize('[Getter/Setter]', 'special');
354 str = ctx.stylize('[Getter]', 'special');
358 str = ctx.stylize('[Setter]', 'special');
361 if (!hasOwnProperty(visibleKeys, key)) {
362 name = '[' + key + ']';
365 if (ctx.seen.indexOf(desc.value) < 0) {
366 if (IS_NULL(recurseTimes)) {
367 str = formatValue(ctx, desc.value, null);
369 str = formatValue(ctx, desc.value, recurseTimes - 1);
371 if (str.indexOf('\n') > -1) {
373 str = str.split('\n').map(function(line) {
375 }).join('\n').substr(2);
377 str = '\n' + str.split('\n').map(function(line) {
383 str = ctx.stylize('[Circular]', 'special');
386 if (IS_UNDEFINED(name)) {
387 if (array && key.match(/^\d+$/)) {
390 name = JSON.stringify('' + key);
391 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
392 name = name.substr(1, name.length - 2);
393 name = ctx.stylize(name, 'name');
395 name = name.replace(/'/g, "\\'")
396 .replace(/\\"/g, '"')
397 .replace(/(^"|"$)/g, "'");
398 name = ctx.stylize(name, 'string');
402 return name + ': ' + str;
406 function reduceToSingleString(output, base, braces) {
408 var length = output.reduce(function(prev, cur) {
410 if (cur.indexOf('\n') >= 0) numLinesEst++;
411 return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
416 (base === '' ? '' : base + '\n ') +
418 output.join(',\n ') +
423 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
427 // NOTE: These type checking functions intentionally don't use `instanceof`
428 // because it is fragile and can be easily faked with `Object.create()`.
429 function isArray(ar) {
432 exports.isArray = isArray;
435 function isRegExp(re) {
436 return IS_OBJECT(re) && objectToString(re) === '[object RegExp]';
438 exports.isRegExp = isRegExp;
442 return IS_OBJECT(d) && objectToString(d) === '[object Date]';
444 exports.isDate = isDate;
447 function isError(e) {
448 return IS_OBJECT(e) && objectToString(e) === '[object Error]';
450 exports.isError = isError;
453 function objectToString(o) {
454 return Object.prototype.toString.call(o);
459 return n < 10 ? '0' + n.toString(10) : n.toString(10);
463 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
464 'Oct', 'Nov', 'Dec'];
467 function timestamp() {
469 var time = [pad(d.getHours()),
471 pad(d.getSeconds())].join(':');
472 return [d.getDate(), months[d.getMonth()], time].join(' ');
476 // log is just a thin wrapper to console.log that prepends a timestamp
477 exports.log = function() {
478 console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
483 * Inherit the prototype methods from one constructor into another.
485 * The Function.prototype.inherits from lang.js rewritten as a standalone
486 * function (not on Function.prototype). NOTE: If this file is to be loaded
487 * during bootstrapping this function needs to be rewritten using some native
488 * functions as prototype setup using normal JavaScript does not work as
489 * expected during bootstrapping (see mirror.js in r114903).
491 * @param {function} ctor Constructor function which needs to inherit the
493 * @param {function} superCtor Constructor function to inherit prototype from.
495 exports.inherits = function(ctor, superCtor) {
496 ctor.super_ = superCtor;
497 ctor.prototype = Object.create(superCtor.prototype, {
507 exports._extend = function(origin, add) {
508 // Don't do anything if add isn't an object
509 if (!add || !IS_OBJECT(add)) return origin;
511 var keys = Object.keys(add);
514 origin[keys[i]] = add[keys[i]];
519 function hasOwnProperty(obj, prop) {
520 return Object.prototype.hasOwnProperty.call(obj, prop);
524 // Deprecated old stuff.
526 exports.p = exports.deprecate(function() {
527 for (var i = 0, len = arguments.length; i < len; ++i) {
528 console.error(exports.inspect(arguments[i]));
530 }, 'util.p: Use console.error() instead');
533 exports.exec = exports.deprecate(function() {
534 return require('child_process').exec.apply(this, arguments);
535 }, 'util.exec is now called `child_process.exec`.');
538 exports.print = exports.deprecate(function() {
539 for (var i = 0, len = arguments.length; i < len; ++i) {
540 process.stdout.write(String(arguments[i]));
542 }, 'util.print: Use console.log instead');
545 exports.puts = exports.deprecate(function() {
546 for (var i = 0, len = arguments.length; i < len; ++i) {
547 process.stdout.write(arguments[i] + '\n');
549 }, 'util.puts: Use console.log instead');
552 exports.debug = exports.deprecate(function(x) {
553 process.stderr.write('DEBUG: ' + x + '\n');
554 }, 'util.debug: Use console.error instead');
557 exports.error = exports.deprecate(function(x) {
558 for (var i = 0, len = arguments.length; i < len; ++i) {
559 process.stderr.write(arguments[i] + '\n');
561 }, 'util.error: Use console.error instead');
564 exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
565 var callbackCalled = false;
567 function call(a, b, c) {
568 if (callback && !callbackCalled) {
570 callbackCalled = true;
574 readStream.addListener('data', function(chunk) {
575 if (writeStream.write(chunk) === false) readStream.pause();
578 writeStream.addListener('drain', function() {
582 readStream.addListener('end', function() {
586 readStream.addListener('close', function() {
590 readStream.addListener('error', function(err) {
595 writeStream.addListener('error', function(err) {
596 readStream.destroy();
599 }, 'util.pump(): Use readableStream.pipe() instead');
603 exports._errnoException = function(err, syscall) {
604 if (IS_UNDEFINED(uv)) uv = process.binding('uv');
605 var errname = uv.errname(err);
606 var e = new Error(syscall + ' ' + errname);