• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Module dependencies.
3 */
4
5var tty = require('tty');
6var util = require('util');
7
8/**
9 * This is the Node.js implementation of `debug()`.
10 *
11 * Expose `debug()` as the module.
12 */
13
14exports = module.exports = require('./debug');
15exports.init = init;
16exports.log = log;
17exports.formatArgs = formatArgs;
18exports.save = save;
19exports.load = load;
20exports.useColors = useColors;
21
22/**
23 * Colors.
24 */
25
26exports.colors = [ 6, 2, 3, 4, 5, 1 ];
27
28try {
29  var supportsColor = require('supports-color');
30  if (supportsColor && supportsColor.level >= 2) {
31    exports.colors = [
32      20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68,
33      69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134,
34      135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
35      172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204,
36      205, 206, 207, 208, 209, 214, 215, 220, 221
37    ];
38  }
39} catch (err) {
40  // swallow - we only care if `supports-color` is available; it doesn't have to be.
41}
42
43/**
44 * Build up the default `inspectOpts` object from the environment variables.
45 *
46 *   $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
47 */
48
49exports.inspectOpts = Object.keys(process.env).filter(function (key) {
50  return /^debug_/i.test(key);
51}).reduce(function (obj, key) {
52  // camel-case
53  var prop = key
54    .substring(6)
55    .toLowerCase()
56    .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
57
58  // coerce string value into JS value
59  var val = process.env[key];
60  if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
61  else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
62  else if (val === 'null') val = null;
63  else val = Number(val);
64
65  obj[prop] = val;
66  return obj;
67}, {});
68
69/**
70 * Is stdout a TTY? Colored output is enabled when `true`.
71 */
72
73function useColors() {
74  return 'colors' in exports.inspectOpts
75    ? Boolean(exports.inspectOpts.colors)
76    : tty.isatty(process.stderr.fd);
77}
78
79/**
80 * Map %o to `util.inspect()`, all on a single line.
81 */
82
83exports.formatters.o = function(v) {
84  this.inspectOpts.colors = this.useColors;
85  return util.inspect(v, this.inspectOpts)
86    .split('\n').map(function(str) {
87      return str.trim()
88    }).join(' ');
89};
90
91/**
92 * Map %o to `util.inspect()`, allowing multiple lines if needed.
93 */
94
95exports.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
106function formatArgs(args) {
107  var name = this.namespace;
108  var useColors = this.useColors;
109
110  if (useColors) {
111    var c = this.color;
112    var colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
113    var prefix = '  ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
114
115    args[0] = prefix + args[0].split('\n').join('\n' + prefix);
116    args.push(colorCode + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
117  } else {
118    args[0] = getDate() + name + ' ' + args[0];
119  }
120}
121
122function getDate() {
123  if (exports.inspectOpts.hideDate) {
124    return '';
125  } else {
126    return new Date().toISOString() + ' ';
127  }
128}
129
130/**
131 * Invokes `util.format()` with the specified arguments and writes to stderr.
132 */
133
134function log() {
135  return process.stderr.write(util.format.apply(util, arguments) + '\n');
136}
137
138/**
139 * Save `namespaces`.
140 *
141 * @param {String} namespaces
142 * @api private
143 */
144
145function save(namespaces) {
146  if (null == namespaces) {
147    // If you set a process.env field to null or undefined, it gets cast to the
148    // string 'null' or 'undefined'. Just delete instead.
149    delete process.env.DEBUG;
150  } else {
151    process.env.DEBUG = namespaces;
152  }
153}
154
155/**
156 * Load `namespaces`.
157 *
158 * @return {String} returns the previously persisted debug modes
159 * @api private
160 */
161
162function load() {
163  return process.env.DEBUG;
164}
165
166/**
167 * Init logic for `debug` instances.
168 *
169 * Create a new `inspectOpts` object in case `useColors` is set
170 * differently for a particular `debug` instance.
171 */
172
173function init (debug) {
174  debug.inspectOpts = {};
175
176  var keys = Object.keys(exports.inspectOpts);
177  for (var i = 0; i < keys.length; i++) {
178    debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
179  }
180}
181
182/**
183 * Enable namespaces listed in `process.env.DEBUG` initially.
184 */
185
186exports.enable(load());
187