1 |
|
2 |
|
3 |
|
4 |
|
5 | var tty = require('tty');
|
6 | var util = require('util');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports = module.exports = require('./debug');
|
15 | exports.init = init;
|
16 | exports.log = log;
|
17 | exports.formatArgs = formatArgs;
|
18 | exports.save = save;
|
19 | exports.load = load;
|
20 | exports.useColors = useColors;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | exports.colors = [ 6, 2, 3, 4, 5, 1 ];
|
27 |
|
28 | try {
|
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 |
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | exports.inspectOpts = Object.keys(process.env).filter(function (key) {
|
50 | return /^debug_/i.test(key);
|
51 | }).reduce(function (obj, key) {
|
52 |
|
53 | var prop = key
|
54 | .substring(6)
|
55 | .toLowerCase()
|
56 | .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
|
57 |
|
58 |
|
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 |
|
71 |
|
72 |
|
73 | function useColors() {
|
74 | return 'colors' in exports.inspectOpts
|
75 | ? Boolean(exports.inspectOpts.colors)
|
76 | : tty.isatty(process.stderr.fd);
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | exports.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 |
|
91 |
|
92 |
|
93 | exports.formatters.O = function(v) {
|
94 | this.inspectOpts.colors = this.useColors;
|
95 | return util.inspect(v, this.inspectOpts);
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | function 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 |
|
123 |
|
124 |
|
125 | function log() {
|
126 | return process.stderr.write(util.format.apply(util, arguments) + '\n');
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | function save(namespaces) {
|
137 | if (null == namespaces) {
|
138 |
|
139 |
|
140 | delete process.env.DEBUG;
|
141 | } else {
|
142 | process.env.DEBUG = namespaces;
|
143 | }
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | function load() {
|
154 | return process.env.DEBUG;
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | function 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 |
|
175 |
|
176 |
|
177 | exports.enable(load());
|