1 | 'use strict'
|
2 | var Progress = require('are-we-there-yet')
|
3 | var Gauge = require('gauge')
|
4 | var EE = require('events').EventEmitter
|
5 | var log = exports = module.exports = new EE()
|
6 | var util = require('util')
|
7 |
|
8 | var setBlocking = require('set-blocking')
|
9 | var consoleControl = require('console-control-strings')
|
10 |
|
11 | setBlocking(true)
|
12 | var stream = process.stderr
|
13 | Object.defineProperty(log, 'stream', {
|
14 | set: function (newStream) {
|
15 | stream = newStream
|
16 | if (this.gauge) {
|
17 | this.gauge.setWriteTo(stream, stream)
|
18 | }
|
19 | },
|
20 | get: function () {
|
21 | return stream
|
22 | },
|
23 | })
|
24 |
|
25 |
|
26 | var colorEnabled
|
27 | log.useColor = function () {
|
28 | return colorEnabled != null ? colorEnabled : stream.isTTY
|
29 | }
|
30 |
|
31 | log.enableColor = function () {
|
32 | colorEnabled = true
|
33 | this.gauge.setTheme({ hasColor: colorEnabled, hasUnicode: unicodeEnabled })
|
34 | }
|
35 | log.disableColor = function () {
|
36 | colorEnabled = false
|
37 | this.gauge.setTheme({ hasColor: colorEnabled, hasUnicode: unicodeEnabled })
|
38 | }
|
39 |
|
40 |
|
41 | log.level = 'info'
|
42 |
|
43 | log.gauge = new Gauge(stream, {
|
44 | enabled: false,
|
45 | theme: { hasColor: log.useColor() },
|
46 | template: [
|
47 | { type: 'progressbar', length: 20 },
|
48 | { type: 'activityIndicator', kerning: 1, length: 1 },
|
49 | { type: 'section', default: '' },
|
50 | ':',
|
51 | { type: 'logline', kerning: 1, default: '' },
|
52 | ],
|
53 | })
|
54 |
|
55 | log.tracker = new Progress.TrackerGroup()
|
56 |
|
57 |
|
58 |
|
59 | log.progressEnabled = log.gauge.isEnabled()
|
60 |
|
61 | var unicodeEnabled
|
62 |
|
63 | log.enableUnicode = function () {
|
64 | unicodeEnabled = true
|
65 | this.gauge.setTheme({ hasColor: this.useColor(), hasUnicode: unicodeEnabled })
|
66 | }
|
67 |
|
68 | log.disableUnicode = function () {
|
69 | unicodeEnabled = false
|
70 | this.gauge.setTheme({ hasColor: this.useColor(), hasUnicode: unicodeEnabled })
|
71 | }
|
72 |
|
73 | log.setGaugeThemeset = function (themes) {
|
74 | this.gauge.setThemeset(themes)
|
75 | }
|
76 |
|
77 | log.setGaugeTemplate = function (template) {
|
78 | this.gauge.setTemplate(template)
|
79 | }
|
80 |
|
81 | log.enableProgress = function () {
|
82 | if (this.progressEnabled) {
|
83 | return
|
84 | }
|
85 |
|
86 | this.progressEnabled = true
|
87 | this.tracker.on('change', this.showProgress)
|
88 | if (this._paused) {
|
89 | return
|
90 | }
|
91 |
|
92 | this.gauge.enable()
|
93 | }
|
94 |
|
95 | log.disableProgress = function () {
|
96 | if (!this.progressEnabled) {
|
97 | return
|
98 | }
|
99 | this.progressEnabled = false
|
100 | this.tracker.removeListener('change', this.showProgress)
|
101 | this.gauge.disable()
|
102 | }
|
103 |
|
104 | var trackerConstructors = ['newGroup', 'newItem', 'newStream']
|
105 |
|
106 | var mixinLog = function (tracker) {
|
107 |
|
108 |
|
109 | Object.keys(log).forEach(function (P) {
|
110 | if (P[0] === '_') {
|
111 | return
|
112 | }
|
113 |
|
114 | if (trackerConstructors.filter(function (C) {
|
115 | return C === P
|
116 | }).length) {
|
117 | return
|
118 | }
|
119 |
|
120 | if (tracker[P]) {
|
121 | return
|
122 | }
|
123 |
|
124 | if (typeof log[P] !== 'function') {
|
125 | return
|
126 | }
|
127 |
|
128 | var func = log[P]
|
129 | tracker[P] = function () {
|
130 | return func.apply(log, arguments)
|
131 | }
|
132 | })
|
133 |
|
134 |
|
135 | if (tracker instanceof Progress.TrackerGroup) {
|
136 | trackerConstructors.forEach(function (C) {
|
137 | var func = tracker[C]
|
138 | tracker[C] = function () {
|
139 | return mixinLog(func.apply(tracker, arguments))
|
140 | }
|
141 | })
|
142 | }
|
143 | return tracker
|
144 | }
|
145 |
|
146 |
|
147 | trackerConstructors.forEach(function (C) {
|
148 | log[C] = function () {
|
149 | return mixinLog(this.tracker[C].apply(this.tracker, arguments))
|
150 | }
|
151 | })
|
152 |
|
153 | log.clearProgress = function (cb) {
|
154 | if (!this.progressEnabled) {
|
155 | return cb && process.nextTick(cb)
|
156 | }
|
157 |
|
158 | this.gauge.hide(cb)
|
159 | }
|
160 |
|
161 | log.showProgress = function (name, completed) {
|
162 | if (!this.progressEnabled) {
|
163 | return
|
164 | }
|
165 |
|
166 | var values = {}
|
167 | if (name) {
|
168 | values.section = name
|
169 | }
|
170 |
|
171 | var last = log.record[log.record.length - 1]
|
172 | if (last) {
|
173 | values.subsection = last.prefix
|
174 | var disp = log.disp[last.level] || last.level
|
175 | var logline = this._format(disp, log.style[last.level])
|
176 | if (last.prefix) {
|
177 | logline += ' ' + this._format(last.prefix, this.prefixStyle)
|
178 | }
|
179 |
|
180 | logline += ' ' + last.message.split(/\r?\n/)[0]
|
181 | values.logline = logline
|
182 | }
|
183 | values.completed = completed || this.tracker.completed()
|
184 | this.gauge.show(values)
|
185 | }.bind(log)
|
186 |
|
187 |
|
188 | log.pause = function () {
|
189 | this._paused = true
|
190 | if (this.progressEnabled) {
|
191 | this.gauge.disable()
|
192 | }
|
193 | }
|
194 |
|
195 | log.resume = function () {
|
196 | if (!this._paused) {
|
197 | return
|
198 | }
|
199 |
|
200 | this._paused = false
|
201 |
|
202 | var b = this._buffer
|
203 | this._buffer = []
|
204 | b.forEach(function (m) {
|
205 | this.emitLog(m)
|
206 | }, this)
|
207 | if (this.progressEnabled) {
|
208 | this.gauge.enable()
|
209 | }
|
210 | }
|
211 |
|
212 | log._buffer = []
|
213 |
|
214 | var id = 0
|
215 | log.record = []
|
216 | log.maxRecordSize = 10000
|
217 | log.log = function (lvl, prefix, message) {
|
218 | var l = this.levels[lvl]
|
219 | if (l === undefined) {
|
220 | return this.emit('error', new Error(util.format(
|
221 | 'Undefined log level: %j', lvl)))
|
222 | }
|
223 |
|
224 | var a = new Array(arguments.length - 2)
|
225 | var stack = null
|
226 | for (var i = 2; i < arguments.length; i++) {
|
227 | var arg = a[i - 2] = arguments[i]
|
228 |
|
229 |
|
230 | if (typeof arg === 'object' && arg instanceof Error && arg.stack) {
|
231 | Object.defineProperty(arg, 'stack', {
|
232 | value: stack = arg.stack + '',
|
233 | enumerable: true,
|
234 | writable: true,
|
235 | })
|
236 | }
|
237 | }
|
238 | if (stack) {
|
239 | a.unshift(stack + '\n')
|
240 | }
|
241 | message = util.format.apply(util, a)
|
242 |
|
243 | var m = {
|
244 | id: id++,
|
245 | level: lvl,
|
246 | prefix: String(prefix || ''),
|
247 | message: message,
|
248 | messageRaw: a,
|
249 | }
|
250 |
|
251 | this.emit('log', m)
|
252 | this.emit('log.' + lvl, m)
|
253 | if (m.prefix) {
|
254 | this.emit(m.prefix, m)
|
255 | }
|
256 |
|
257 | this.record.push(m)
|
258 | var mrs = this.maxRecordSize
|
259 | var n = this.record.length - mrs
|
260 | if (n > mrs / 10) {
|
261 | var newSize = Math.floor(mrs * 0.9)
|
262 | this.record = this.record.slice(-1 * newSize)
|
263 | }
|
264 |
|
265 | this.emitLog(m)
|
266 | }.bind(log)
|
267 |
|
268 | log.emitLog = function (m) {
|
269 | if (this._paused) {
|
270 | this._buffer.push(m)
|
271 | return
|
272 | }
|
273 | if (this.progressEnabled) {
|
274 | this.gauge.pulse(m.prefix)
|
275 | }
|
276 |
|
277 | var l = this.levels[m.level]
|
278 | if (l === undefined) {
|
279 | return
|
280 | }
|
281 |
|
282 | if (l < this.levels[this.level]) {
|
283 | return
|
284 | }
|
285 |
|
286 | if (l > 0 && !isFinite(l)) {
|
287 | return
|
288 | }
|
289 |
|
290 |
|
291 |
|
292 | var disp = log.disp[m.level] != null ? log.disp[m.level] : m.level
|
293 | this.clearProgress()
|
294 | m.message.split(/\r?\n/).forEach(function (line) {
|
295 | if (this.heading) {
|
296 | this.write(this.heading, this.headingStyle)
|
297 | this.write(' ')
|
298 | }
|
299 | this.write(disp, log.style[m.level])
|
300 | var p = m.prefix || ''
|
301 | if (p) {
|
302 | this.write(' ')
|
303 | }
|
304 |
|
305 | this.write(p, this.prefixStyle)
|
306 | this.write(' ' + line + '\n')
|
307 | }, this)
|
308 | this.showProgress()
|
309 | }
|
310 |
|
311 | log._format = function (msg, style) {
|
312 | if (!stream) {
|
313 | return
|
314 | }
|
315 |
|
316 | var output = ''
|
317 | if (this.useColor()) {
|
318 | style = style || {}
|
319 | var settings = []
|
320 | if (style.fg) {
|
321 | settings.push(style.fg)
|
322 | }
|
323 |
|
324 | if (style.bg) {
|
325 | settings.push('bg' + style.bg[0].toUpperCase() + style.bg.slice(1))
|
326 | }
|
327 |
|
328 | if (style.bold) {
|
329 | settings.push('bold')
|
330 | }
|
331 |
|
332 | if (style.underline) {
|
333 | settings.push('underline')
|
334 | }
|
335 |
|
336 | if (style.inverse) {
|
337 | settings.push('inverse')
|
338 | }
|
339 |
|
340 | if (settings.length) {
|
341 | output += consoleControl.color(settings)
|
342 | }
|
343 |
|
344 | if (style.beep) {
|
345 | output += consoleControl.beep()
|
346 | }
|
347 | }
|
348 | output += msg
|
349 | if (this.useColor()) {
|
350 | output += consoleControl.color('reset')
|
351 | }
|
352 |
|
353 | return output
|
354 | }
|
355 |
|
356 | log.write = function (msg, style) {
|
357 | if (!stream) {
|
358 | return
|
359 | }
|
360 |
|
361 | stream.write(this._format(msg, style))
|
362 | }
|
363 |
|
364 | log.addLevel = function (lvl, n, style, disp) {
|
365 |
|
366 | if (disp == null) {
|
367 | disp = lvl
|
368 | }
|
369 |
|
370 | this.levels[lvl] = n
|
371 | this.style[lvl] = style
|
372 | if (!this[lvl]) {
|
373 | this[lvl] = function () {
|
374 | var a = new Array(arguments.length + 1)
|
375 | a[0] = lvl
|
376 | for (var i = 0; i < arguments.length; i++) {
|
377 | a[i + 1] = arguments[i]
|
378 | }
|
379 |
|
380 | return this.log.apply(this, a)
|
381 | }.bind(this)
|
382 | }
|
383 | this.disp[lvl] = disp
|
384 | }
|
385 |
|
386 | log.prefixStyle = { fg: 'magenta' }
|
387 | log.headingStyle = { fg: 'white', bg: 'black' }
|
388 |
|
389 | log.style = {}
|
390 | log.levels = {}
|
391 | log.disp = {}
|
392 | log.addLevel('silly', -Infinity, { inverse: true }, 'sill')
|
393 | log.addLevel('verbose', 1000, { fg: 'blue', bg: 'black' }, 'verb')
|
394 | log.addLevel('info', 2000, { fg: 'green' })
|
395 | log.addLevel('timing', 2500, { fg: 'green', bg: 'black' })
|
396 | log.addLevel('http', 3000, { fg: 'green', bg: 'black' })
|
397 | log.addLevel('notice', 3500, { fg: 'blue', bg: 'black' })
|
398 | log.addLevel('warn', 4000, { fg: 'black', bg: 'yellow' }, 'WARN')
|
399 | log.addLevel('error', 5000, { fg: 'red', bg: 'black' }, 'ERR!')
|
400 | log.addLevel('silent', Infinity)
|
401 |
|
402 |
|
403 | log.on('error', function () {})
|