UNPKG

4.18 kBJavaScriptView Raw
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 .replace(/\s*\n\s*/g, ' ');
87};
88
89/**
90 * Map %o to `util.inspect()`, allowing multiple lines if needed.
91 */
92
93exports.formatters.O = function(v) {
94 this.inspectOpts.colors = this.useColors;
95 return util.inspect(v, this.inspectOpts);
96};
97
98/**
99 * Adds ANSI color escape codes if enabled.
100 *
101 * @api public
102 */
103
104function formatArgs(args) {
105 var name = this.namespace;
106 var useColors = this.useColors;
107
108 if (useColors) {
109 var c = this.color;
110 var colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
111 var prefix = ' ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
112
113 args[0] = prefix + args[0].split('\n').join('\n' + prefix);
114 args.push(colorCode + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
115 } else {
116 args[0] = new Date().toISOString()
117 + ' ' + name + ' ' + args[0];
118 }
119}
120
121/**
122 * Invokes `util.format()` with the specified arguments and writes to stderr.
123 */
124
125function log() {
126 return process.stderr.write(util.format.apply(util, arguments) + '\n');
127}
128
129/**
130 * Save `namespaces`.
131 *
132 * @param {String} namespaces
133 * @api private
134 */
135
136function save(namespaces) {
137 if (null == namespaces) {
138 // If you set a process.env field to null or undefined, it gets cast to the
139 // string 'null' or 'undefined'. Just delete instead.
140 delete process.env.DEBUG;
141 } else {
142 process.env.DEBUG = namespaces;
143 }
144}
145
146/**
147 * Load `namespaces`.
148 *
149 * @return {String} returns the previously persisted debug modes
150 * @api private
151 */
152
153function load() {
154 return process.env.DEBUG;
155}
156
157/**
158 * Init logic for `debug` instances.
159 *
160 * Create a new `inspectOpts` object in case `useColors` is set
161 * differently for a particular `debug` instance.
162 */
163
164function init (debug) {
165 debug.inspectOpts = {};
166
167 var keys = Object.keys(exports.inspectOpts);
168 for (var i = 0; i < keys.length; i++) {
169 debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
170 }
171}
172
173/**
174 * Enable namespaces listed in `process.env.DEBUG` initially.
175 */
176
177exports.enable(load());