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;
23 exports.colors = [6, 2, 3, 4, 5, 1];
26 // Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
27 // eslint-disable-next-line import/no-extraneous-dependencies
28 const supportsColor = require('supports-color');
30 if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
111 // Swallow - we only care if `supports-color` is available; it doesn't have to be.
115 * Build up the default `inspectOpts` object from the environment variables.
117 * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
120 exports.inspectOpts = Object.keys(process.env).filter(key => {
121 return /^debug_/i.test(key);
122 }).reduce((obj, key) => {
127 .replace(/_([a-z])/g, (_, k) => {
128 return k.toUpperCase();
131 // Coerce string value into JS value
132 let val = process.env[key];
133 if (/^(yes|on|true|enabled)$/i.test(val)) {
135 } else if (/^(no|off|false|disabled)$/i.test(val)) {
137 } else if (val === 'null') {
148 * Is stdout a TTY? Colored output is enabled when `true`.
151 function useColors() {
152 return 'colors' in exports.inspectOpts ?
153 Boolean(exports.inspectOpts.colors) :
154 tty.isatty(process.stderr.fd);
158 * Adds ANSI color escape codes if enabled.
163 function formatArgs(args) {
164 const {namespace: name, useColors} = this;
167 const c = this.color;
168 const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
169 const prefix = ` ${colorCode};1m${name} \u001B[0m`;
171 args[0] = prefix + args[0].split('\n').join('\n' + prefix);
172 args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
174 args[0] = getDate() + name + ' ' + args[0];
179 if (exports.inspectOpts.hideDate) {
182 return new Date().toISOString() + ' ';
186 * Invokes `util.format()` with the specified arguments and writes to stderr.
189 function log(...args) {
190 return process.stderr.write(util.format(...args) + '\n');
196 * @param {String} namespaces
199 function save(namespaces) {
201 process.env.DEBUG = namespaces;
203 // If you set a process.env field to null or undefined, it gets cast to the
204 // string 'null' or 'undefined'. Just delete instead.
205 delete process.env.DEBUG;
212 * @return {String} returns the previously persisted debug modes
217 return process.env.DEBUG;
221 * Init logic for `debug` instances.
223 * Create a new `inspectOpts` object in case `useColors` is set
224 * differently for a particular `debug` instance.
227 function init(debug) {
228 debug.inspectOpts = {};
230 const keys = Object.keys(exports.inspectOpts);
231 for (let i = 0; i < keys.length; i++) {
232 debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
236 module.exports = require('./common')(exports);
238 const {formatters} = module.exports;
241 * Map %o to `util.inspect()`, all on a single line.
244 formatters.o = function (v) {
245 this.inspectOpts.colors = this.useColors;
246 return util.inspect(v, this.inspectOpts)
247 .replace(/\s*\n\s*/g, ' ');
251 * Map %O to `util.inspect()`, allowing multiple lines if needed.
254 formatters.O = function (v) {
255 this.inspectOpts.colors = this.useColors;
256 return util.inspect(v, this.inspectOpts);