|
|
|
|
|
|
|
|
|
var tty = require('tty'); |
|
var util = require('util'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports = module.exports = require('./debug'); |
|
exports.init = init; |
|
exports.log = log; |
|
exports.formatArgs = formatArgs; |
|
exports.save = save; |
|
exports.load = load; |
|
exports.useColors = useColors; |
|
|
|
|
|
|
|
|
|
|
|
exports.colors = [6, 2, 3, 4, 5, 1]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.inspectOpts = Object.keys(process.env).filter(function (key) { |
|
return /^debug_/i.test(key); |
|
}).reduce(function (obj, key) { |
|
|
|
var prop = key |
|
.substring(6) |
|
.toLowerCase() |
|
.replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() }); |
|
|
|
|
|
var val = process.env[key]; |
|
if (/^(yes|on|true|enabled)$/i.test(val)) val = true; |
|
else if (/^(no|off|false|disabled)$/i.test(val)) val = false; |
|
else if (val === 'null') val = null; |
|
else val = Number(val); |
|
|
|
obj[prop] = val; |
|
return obj; |
|
}, {}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fd = parseInt(process.env.DEBUG_FD, 10) || 2; |
|
|
|
if (1 !== fd && 2 !== fd) { |
|
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)')() |
|
} |
|
|
|
var stream = 1 === fd ? process.stdout : |
|
2 === fd ? process.stderr : |
|
createWritableStdioStream(fd); |
|
|
|
|
|
|
|
|
|
|
|
function useColors() { |
|
return 'colors' in exports.inspectOpts |
|
? Boolean(exports.inspectOpts.colors) |
|
: tty.isatty(fd); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
exports.formatters.o = function(v) { |
|
this.inspectOpts.colors = this.useColors; |
|
return util.inspect(v, this.inspectOpts) |
|
.split('\n').map(function(str) { |
|
return str.trim() |
|
}).join(' '); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.formatters.O = function(v) { |
|
this.inspectOpts.colors = this.useColors; |
|
return util.inspect(v, this.inspectOpts); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function formatArgs(args) { |
|
var name = this.namespace; |
|
var useColors = this.useColors; |
|
|
|
if (useColors) { |
|
var c = this.color; |
|
var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m'; |
|
|
|
args[0] = prefix + args[0].split('\n').join('\n' + prefix); |
|
args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m'); |
|
} else { |
|
args[0] = new Date().toUTCString() |
|
+ ' ' + name + ' ' + args[0]; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function log() { |
|
return stream.write(util.format.apply(util, arguments) + '\n'); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function save(namespaces) { |
|
if (null == namespaces) { |
|
|
|
|
|
delete process.env.DEBUG; |
|
} else { |
|
process.env.DEBUG = namespaces; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function load() { |
|
return process.env.DEBUG; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createWritableStdioStream (fd) { |
|
var stream; |
|
var tty_wrap = process.binding('tty_wrap'); |
|
|
|
|
|
|
|
switch (tty_wrap.guessHandleType(fd)) { |
|
case 'TTY': |
|
stream = new tty.WriteStream(fd); |
|
stream._type = 'tty'; |
|
|
|
|
|
|
|
if (stream._handle && stream._handle.unref) { |
|
stream._handle.unref(); |
|
} |
|
break; |
|
|
|
case 'FILE': |
|
var fs = require('fs'); |
|
stream = new fs.SyncWriteStream(fd, { autoClose: false }); |
|
stream._type = 'fs'; |
|
break; |
|
|
|
case 'PIPE': |
|
case 'TCP': |
|
var net = require('net'); |
|
stream = new net.Socket({ |
|
fd: fd, |
|
readable: false, |
|
writable: true |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
stream.readable = false; |
|
stream.read = null; |
|
stream._type = 'pipe'; |
|
|
|
|
|
|
|
if (stream._handle && stream._handle.unref) { |
|
stream._handle.unref(); |
|
} |
|
break; |
|
|
|
default: |
|
|
|
throw new Error('Implement me. Unknown stream file type!'); |
|
} |
|
|
|
|
|
stream.fd = fd; |
|
|
|
stream._isStdio = true; |
|
|
|
return stream; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function init (debug) { |
|
debug.inspectOpts = {}; |
|
|
|
var keys = Object.keys(exports.inspectOpts); |
|
for (var i = 0; i < keys.length; i++) { |
|
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]]; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
exports.enable(load()); |
|
|