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) this.gauge.setWriteTo(stream, stream)
|
17 | },
|
18 | get: function () {
|
19 | return stream
|
20 | }
|
21 | })
|
22 |
|
23 |
|
24 | var colorEnabled
|
25 | log.useColor = function () {
|
26 | return colorEnabled != null ? colorEnabled : stream.isTTY
|
27 | }
|
28 |
|
29 | log.enableColor = function () {
|
30 | colorEnabled = true
|
31 | this.gauge.setTheme({hasColor: colorEnabled, hasUnicode: unicodeEnabled})
|
32 | }
|
33 | log.disableColor = function () {
|
34 | colorEnabled = false
|
35 | this.gauge.setTheme({hasColor: colorEnabled, hasUnicode: unicodeEnabled})
|
36 | }
|
37 |
|
38 |
|
39 | log.level = 'info'
|
40 |
|
41 | log.gauge = new Gauge(stream, {
|
42 | enabled: false,
|
43 | theme: {hasColor: log.useColor()},
|
44 | template: [
|
45 | {type: 'progressbar', length: 20},
|
46 | {type: 'activityIndicator', kerning: 1, length: 1},
|
47 | {type: 'section', default: ''},
|
48 | ':',
|
49 | {type: 'logline', kerning: 1, default: ''}
|
50 | ]
|
51 | })
|
52 |
|
53 | log.tracker = new Progress.TrackerGroup()
|
54 |
|
55 |
|
56 |
|
57 | log.progressEnabled = log.gauge.isEnabled()
|
58 |
|
59 | var unicodeEnabled
|
60 |
|
61 | log.enableUnicode = function () {
|
62 | unicodeEnabled = true
|
63 | this.gauge.setTheme({hasColor: this.useColor(), hasUnicode: unicodeEnabled})
|
64 | }
|
65 |
|
66 | log.disableUnicode = function () {
|
67 | unicodeEnabled = false
|
68 | this.gauge.setTheme({hasColor: this.useColor(), hasUnicode: unicodeEnabled})
|
69 | }
|
70 |
|
71 | log.setGaugeThemeset = function (themes) {
|
72 | this.gauge.setThemeset(themes)
|
73 | }
|
74 |
|
75 | log.setGaugeTemplate = function (template) {
|
76 | this.gauge.setTemplate(template)
|
77 | }
|
78 |
|
79 | log.enableProgress = function () {
|
80 | if (this.progressEnabled) return
|
81 | this.progressEnabled = true
|
82 | this.tracker.on('change', this.showProgress)
|
83 | if (this._pause) return
|
84 | this.gauge.enable()
|
85 | }
|
86 |
|
87 | log.disableProgress = function () {
|
88 | if (!this.progressEnabled) return
|
89 | this.progressEnabled = false
|
90 | this.tracker.removeListener('change', this.showProgress)
|
91 | this.gauge.disable()
|
92 | }
|
93 |
|
94 | var trackerConstructors = ['newGroup', 'newItem', 'newStream']
|
95 |
|
96 | var mixinLog = function (tracker) {
|
97 |
|
98 |
|
99 | Object.keys(log).forEach(function (P) {
|
100 | if (P[0] === '_') return
|
101 | if (trackerConstructors.filter(function (C) { return C === P }).length) return
|
102 | if (tracker[P]) return
|
103 | if (typeof log[P] !== 'function') return
|
104 | var func = log[P]
|
105 | tracker[P] = function () {
|
106 | return func.apply(log, arguments)
|
107 | }
|
108 | })
|
109 |
|
110 |
|
111 | if (tracker instanceof Progress.TrackerGroup) {
|
112 | trackerConstructors.forEach(function (C) {
|
113 | var func = tracker[C]
|
114 | tracker[C] = function () { return mixinLog(func.apply(tracker, arguments)) }
|
115 | })
|
116 | }
|
117 | return tracker
|
118 | }
|
119 |
|
120 |
|
121 | trackerConstructors.forEach(function (C) {
|
122 | log[C] = function () { return mixinLog(this.tracker[C].apply(this.tracker, arguments)) }
|
123 | })
|
124 |
|
125 | log.clearProgress = function (cb) {
|
126 | if (!this.progressEnabled) return cb && process.nextTick(cb)
|
127 | this.gauge.hide(cb)
|
128 | }
|
129 |
|
130 | log.showProgress = function (name, completed) {
|
131 | if (!this.progressEnabled) return
|
132 | var values = {}
|
133 | if (name) values.section = name
|
134 | var last = log.record[log.record.length - 1]
|
135 | if (last) {
|
136 | values.subsection = last.prefix
|
137 | var disp = log.disp[last.level] || last.level
|
138 | var logline = this._format(disp, log.style[last.level])
|
139 | if (last.prefix) logline += ' ' + this._format(last.prefix, this.prefixStyle)
|
140 | logline += ' ' + last.message.split(/\r?\n/)[0]
|
141 | values.logline = logline
|
142 | }
|
143 | values.completed = completed || this.tracker.completed()
|
144 | this.gauge.show(values)
|
145 | }.bind(log)
|
146 |
|
147 |
|
148 | log.pause = function () {
|
149 | this._paused = true
|
150 | if (this.progressEnabled) this.gauge.disable()
|
151 | }
|
152 |
|
153 | log.resume = function () {
|
154 | if (!this._paused) return
|
155 | this._paused = false
|
156 |
|
157 | var b = this._buffer
|
158 | this._buffer = []
|
159 | b.forEach(function (m) {
|
160 | this.emitLog(m)
|
161 | }, this)
|
162 | if (this.progressEnabled) this.gauge.enable()
|
163 | }
|
164 |
|
165 | log._buffer = []
|
166 |
|
167 | var id = 0
|
168 | log.record = []
|
169 | log.maxRecordSize = 10000
|
170 | log.log = function (lvl, prefix, message) {
|
171 | var l = this.levels[lvl]
|
172 | if (l === undefined) {
|
173 | return this.emit('error', new Error(util.format(
|
174 | 'Undefined log level: %j', lvl)))
|
175 | }
|
176 |
|
177 | var a = new Array(arguments.length - 2)
|
178 | var stack = null
|
179 | for (var i = 2; i < arguments.length; i++) {
|
180 | var arg = a[i - 2] = arguments[i]
|
181 |
|
182 |
|
183 | if (typeof arg === 'object' && arg &&
|
184 | (arg instanceof Error) && arg.stack) {
|
185 | arg.stack = stack = arg.stack + ''
|
186 | }
|
187 | }
|
188 | if (stack) a.unshift(stack + '\n')
|
189 | message = util.format.apply(util, a)
|
190 |
|
191 | var m = { id: id++,
|
192 | level: lvl,
|
193 | prefix: String(prefix || ''),
|
194 | message: message,
|
195 | messageRaw: a }
|
196 |
|
197 | this.emit('log', m)
|
198 | this.emit('log.' + lvl, m)
|
199 | if (m.prefix) this.emit(m.prefix, m)
|
200 |
|
201 | this.record.push(m)
|
202 | var mrs = this.maxRecordSize
|
203 | var n = this.record.length - mrs
|
204 | if (n > mrs / 10) {
|
205 | var newSize = Math.floor(mrs * 0.9)
|
206 | this.record = this.record.slice(-1 * newSize)
|
207 | }
|
208 |
|
209 | this.emitLog(m)
|
210 | }.bind(log)
|
211 |
|
212 | log.emitLog = function (m) {
|
213 | if (this._paused) {
|
214 | this._buffer.push(m)
|
215 | return
|
216 | }
|
217 | if (this.progressEnabled) this.gauge.pulse(m.prefix)
|
218 | var l = this.levels[m.level]
|
219 | if (l === undefined) return
|
220 | if (l < this.levels[this.level]) return
|
221 | if (l > 0 && !isFinite(l)) return
|
222 |
|
223 |
|
224 |
|
225 | var disp = log.disp[m.level] != null ? log.disp[m.level] : m.level
|
226 | this.clearProgress()
|
227 | m.message.split(/\r?\n/).forEach(function (line) {
|
228 | if (this.heading) {
|
229 | this.write(this.heading, this.headingStyle)
|
230 | this.write(' ')
|
231 | }
|
232 | this.write(disp, log.style[m.level])
|
233 | var p = m.prefix || ''
|
234 | if (p) this.write(' ')
|
235 | this.write(p, this.prefixStyle)
|
236 | this.write(' ' + line + '\n')
|
237 | }, this)
|
238 | this.showProgress()
|
239 | }
|
240 |
|
241 | log._format = function (msg, style) {
|
242 | if (!stream) return
|
243 |
|
244 | var output = ''
|
245 | if (this.useColor()) {
|
246 | style = style || {}
|
247 | var settings = []
|
248 | if (style.fg) settings.push(style.fg)
|
249 | if (style.bg) settings.push('bg' + style.bg[0].toUpperCase() + style.bg.slice(1))
|
250 | if (style.bold) settings.push('bold')
|
251 | if (style.underline) settings.push('underline')
|
252 | if (style.inverse) settings.push('inverse')
|
253 | if (settings.length) output += consoleControl.color(settings)
|
254 | if (style.beep) output += consoleControl.beep()
|
255 | }
|
256 | output += msg
|
257 | if (this.useColor()) {
|
258 | output += consoleControl.color('reset')
|
259 | }
|
260 | return output
|
261 | }
|
262 |
|
263 | log.write = function (msg, style) {
|
264 | if (!stream) return
|
265 |
|
266 | stream.write(this._format(msg, style))
|
267 | }
|
268 |
|
269 | log.addLevel = function (lvl, n, style, disp) {
|
270 |
|
271 | if (disp == null) disp = lvl
|
272 | this.levels[lvl] = n
|
273 | this.style[lvl] = style
|
274 | if (!this[lvl]) {
|
275 | this[lvl] = function () {
|
276 | var a = new Array(arguments.length + 1)
|
277 | a[0] = lvl
|
278 | for (var i = 0; i < arguments.length; i++) {
|
279 | a[i + 1] = arguments[i]
|
280 | }
|
281 | return this.log.apply(this, a)
|
282 | }.bind(this)
|
283 | }
|
284 | this.disp[lvl] = disp
|
285 | }
|
286 |
|
287 | log.prefixStyle = { fg: 'magenta' }
|
288 | log.headingStyle = { fg: 'white', bg: 'black' }
|
289 |
|
290 | log.style = {}
|
291 | log.levels = {}
|
292 | log.disp = {}
|
293 | log.addLevel('silly', -Infinity, { inverse: true }, 'sill')
|
294 | log.addLevel('verbose', 1000, { fg: 'blue', bg: 'black' }, 'verb')
|
295 | log.addLevel('info', 2000, { fg: 'green' })
|
296 | log.addLevel('timing', 2500, { fg: 'green', bg: 'black' })
|
297 | log.addLevel('http', 3000, { fg: 'green', bg: 'black' })
|
298 | log.addLevel('notice', 3500, { fg: 'blue', bg: 'black' })
|
299 | log.addLevel('warn', 4000, { fg: 'black', bg: 'yellow' }, 'WARN')
|
300 | log.addLevel('error', 5000, { fg: 'red', bg: 'black' }, 'ERR!')
|
301 | log.addLevel('silent', Infinity)
|
302 |
|
303 |
|
304 | log.on('error', function () {})
|