1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var tty = require('tty');
|
7 | var util = require('util');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports = module.exports = require('./debug');
|
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 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
|
36 | var stream = 1 === fd ? process.stdout :
|
37 | 2 === fd ? process.stderr :
|
38 | createWritableStdioStream(fd);
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function useColors() {
|
45 | var debugColors = (process.env.DEBUG_COLORS || '').trim().toLowerCase();
|
46 | if (0 === debugColors.length) {
|
47 | return tty.isatty(fd);
|
48 | } else {
|
49 | return '0' !== debugColors
|
50 | && 'no' !== debugColors
|
51 | && 'false' !== debugColors
|
52 | && 'disabled' !== debugColors;
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | var inspect = (4 === util.inspect.length ?
|
61 |
|
62 | function (v, colors) {
|
63 | return util.inspect(v, void 0, void 0, colors);
|
64 | } :
|
65 |
|
66 | function (v, colors) {
|
67 | return util.inspect(v, { colors: colors });
|
68 | }
|
69 | );
|
70 |
|
71 | exports.formatters.o = function(v) {
|
72 | return inspect(v, this.useColors)
|
73 | .replace(/\s*\n\s*/g, ' ');
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function formatArgs() {
|
83 | var args = arguments;
|
84 | var useColors = this.useColors;
|
85 | var name = this.namespace;
|
86 |
|
87 | if (useColors) {
|
88 | var c = this.color;
|
89 |
|
90 | args[0] = ' \u001b[9' + c + 'm' + name + ' '
|
91 | + '\u001b[0m'
|
92 | + args[0] + '\u001b[3' + c + 'm'
|
93 | + ' +' + exports.humanize(this.diff) + '\u001b[0m';
|
94 | } else {
|
95 | args[0] = new Date().toUTCString()
|
96 | + ' ' + name + ' ' + args[0];
|
97 | }
|
98 | return args;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function log() {
|
106 | return stream.write(util.format.apply(this, arguments) + '\n');
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | function save(namespaces) {
|
117 | if (null == namespaces) {
|
118 |
|
119 |
|
120 | delete process.env.DEBUG;
|
121 | } else {
|
122 | process.env.DEBUG = namespaces;
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function load() {
|
134 | return process.env.DEBUG;
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function createWritableStdioStream (fd) {
|
145 | var stream;
|
146 | var tty_wrap = process.binding('tty_wrap');
|
147 |
|
148 |
|
149 |
|
150 | switch (tty_wrap.guessHandleType(fd)) {
|
151 | case 'TTY':
|
152 | stream = new tty.WriteStream(fd);
|
153 | stream._type = 'tty';
|
154 |
|
155 |
|
156 |
|
157 | if (stream._handle && stream._handle.unref) {
|
158 | stream._handle.unref();
|
159 | }
|
160 | break;
|
161 |
|
162 | case 'FILE':
|
163 | var fs = require('fs');
|
164 | stream = new fs.SyncWriteStream(fd, { autoClose: false });
|
165 | stream._type = 'fs';
|
166 | break;
|
167 |
|
168 | case 'PIPE':
|
169 | case 'TCP':
|
170 | var net = require('net');
|
171 | stream = new net.Socket({
|
172 | fd: fd,
|
173 | readable: false,
|
174 | writable: true
|
175 | });
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | stream.readable = false;
|
182 | stream.read = null;
|
183 | stream._type = 'pipe';
|
184 |
|
185 |
|
186 |
|
187 | if (stream._handle && stream._handle.unref) {
|
188 | stream._handle.unref();
|
189 | }
|
190 | break;
|
191 |
|
192 | default:
|
193 |
|
194 | throw new Error('Implement me. Unknown stream file type!');
|
195 | }
|
196 |
|
197 |
|
198 | stream.fd = fd;
|
199 |
|
200 | stream._isStdio = true;
|
201 |
|
202 | return stream;
|
203 | }
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | exports.enable(load());
|