5 const tty = require('tty');
6 const util = require('util');
9 * This is the Node.js implementation of `debug()`.
14 exports.formatArgs = formatArgs;
17 exports.useColors = useColors;
18 exports.destroy = util.deprecate(
20 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'
27 exports.colors = [6, 2, 3, 4, 5, 1];
30 // Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
31 // eslint-disable-next-line import/no-extraneous-dependencies
32 const supportsColor = require('supports-color');
34 if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
115 // Swallow - we only care if `supports-color` is available; it doesn't have to be.
119 * Build up the default `inspectOpts` object from the environment variables.
121 * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
124 exports.inspectOpts = Object.keys(process.env).filter(key => {
125 return /^debug_/i.test(key);
126 }).reduce((obj, key) => {
131 .replace(/_([a-z])/g, (_, k) => {
132 return k.toUpperCase();
135 // Coerce string value into JS value
136 let val = process.env[key];
137 if (/^(yes|on|true|enabled)$/i.test(val)) {
139 } else if (/^(no|off|false|disabled)$/i.test(val)) {
141 } else if (val === 'null') {
152 * Is stdout a TTY? Colored output is enabled when `true`.
155 function useColors() {
156 return 'colors' in exports.inspectOpts ?
157 Boolean(exports.inspectOpts.colors) :
158 tty.isatty(process.stderr.fd);
162 * Adds ANSI color escape codes if enabled.
167 function formatArgs(args) {
168 const {namespace: name, useColors} = this;
171 const c = this.color;
172 const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
173 const prefix = ` ${colorCode};1m${name} \u001B[0m`;
175 args[0] = prefix + args[0].split('\n').join('\n' + prefix);
176 args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
178 args[0] = getDate() + name + ' ' + args[0];
183 if (exports.inspectOpts.hideDate) {
186 return new Date().toISOString() + ' ';
190 * Invokes `util.format()` with the specified arguments and writes to stderr.
193 function log(...args) {
194 return process.stderr.write(util.format(...args) + '\n');
200 * @param {String} namespaces
203 function save(namespaces) {
205 process.env.DEBUG = namespaces;
207 // If you set a process.env field to null or undefined, it gets cast to the
208 // string 'null' or 'undefined'. Just delete instead.
209 delete process.env.DEBUG;
216 * @return {String} returns the previously persisted debug modes
221 return process.env.DEBUG;
225 * Init logic for `debug` instances.
227 * Create a new `inspectOpts` object in case `useColors` is set
228 * differently for a particular `debug` instance.
231 function init(debug) {
232 debug.inspectOpts = {};
234 const keys = Object.keys(exports.inspectOpts);
235 for (let i = 0; i < keys.length; i++) {
236 debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
240 module.exports = require('./common')(exports);
242 const {formatters} = module.exports;
245 * Map %o to `util.inspect()`, all on a single line.
248 formatters.o = function (v) {
249 this.inspectOpts.colors = this.useColors;
250 return util.inspect(v, this.inspectOpts)
252 .map(str => str.trim())
257 * Map %O to `util.inspect()`, allowing multiple lines if needed.
260 formatters.O = function (v) {
261 this.inspectOpts.colors = this.useColors;
262 return util.inspect(v, this.inspectOpts);