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')
8 var ansi = require('ansi')
9 log.cursor = ansi(process.stderr)
10 log.stream = process.stderr
12 // by default, let ansi decide based on tty-ness.
13 var colorEnabled = undefined
14 log.enableColor = function () {
16 this.cursor.enabled = true
18 log.disableColor = function () {
20 this.cursor.enabled = false
26 log.gauge = new Gauge(log.cursor)
27 log.tracker = new Progress.TrackerGroup()
29 // no progress bars unless asked
30 log.progressEnabled = false
32 var gaugeTheme = undefined
34 log.enableUnicode = function () {
35 gaugeTheme = Gauge.unicode
36 log.gauge.setTheme(gaugeTheme)
39 log.disableUnicode = function () {
40 gaugeTheme = Gauge.ascii
41 log.gauge.setTheme(gaugeTheme)
44 var gaugeTemplate = undefined
45 log.setGaugeTemplate = function (template) {
46 gaugeTemplate = template
47 log.gauge.setTemplate(gaugeTemplate)
50 log.enableProgress = function () {
51 if (this.progressEnabled) return
52 this.progressEnabled = true
53 if (this._pause) return
54 this.tracker.on('change', this.showProgress)
59 log.disableProgress = function () {
60 if (!this.progressEnabled) return
62 this.progressEnabled = false
63 this.tracker.removeListener('change', this.showProgress)
67 var trackerConstructors = ['newGroup', 'newItem', 'newStream']
69 var mixinLog = function (tracker) {
70 // mixin the public methods from log into the tracker
71 // (except: conflicts and one's we handle specially)
72 Object.keys(log).forEach(function (P) {
73 if (P[0] === '_') return
74 if (trackerConstructors.filter(function (C) { return C === P }).length) return
75 if (tracker[P]) return
76 if (typeof log[P] !== 'function') return
78 tracker[P] = function () {
79 return func.apply(log, arguments)
82 // if the new tracker is a group, make sure any subtrackers get
84 if (tracker instanceof Progress.TrackerGroup) {
85 trackerConstructors.forEach(function (C) {
87 tracker[C] = function () { return mixinLog(func.apply(tracker, arguments)) }
93 // Add tracker constructors to the top level log object
94 trackerConstructors.forEach(function (C) {
95 log[C] = function () { return mixinLog(this.tracker[C].apply(this.tracker, arguments)) }
98 log.clearProgress = function () {
99 if (!this.progressEnabled) return
103 log.showProgress = function (name, completed) {
104 if (!this.progressEnabled) return
105 if (completed == null) completed = this.tracker.completed()
106 this.gauge.show(name, completed)
107 }.bind(log) // bind for use in tracker's on-change listener
109 // temporarily stop emitting, but don't drop
110 log.pause = function () {
114 log.resume = function () {
115 if (!this._paused) return
120 b.forEach(function (m) {
123 if (this.progressEnabled) this.enableProgress()
130 log.maxRecordSize = 10000
131 log.log = function (lvl, prefix, message) {
132 var l = this.levels[lvl]
133 if (l === undefined) {
134 return this.emit('error', new Error(util.format(
135 'Undefined log level: %j', lvl)))
138 var a = new Array(arguments.length - 2)
140 for (var i = 2; i < arguments.length; i ++) {
141 var arg = a[i-2] = arguments[i]
143 // resolve stack traces to a plain string.
144 if (typeof arg === 'object' && arg &&
145 (arg instanceof Error) && arg.stack) {
146 arg.stack = stack = arg.stack + ''
149 if (stack) a.unshift(stack + '\n')
150 message = util.format.apply(util, a)
154 prefix: String(prefix || ''),
159 this.emit('log.' + lvl, m)
160 if (m.prefix) this.emit(m.prefix, m)
163 var mrs = this.maxRecordSize
164 var n = this.record.length - mrs
166 var newSize = Math.floor(mrs * 0.9)
167 this.record = this.record.slice(-1 * newSize)
173 log.emitLog = function (m) {
178 if (this.progressEnabled) this.gauge.pulse(m.prefix)
179 var l = this.levels[m.level]
180 if (l === undefined) return
181 if (l < this.levels[this.level]) return
182 if (l > 0 && !isFinite(l)) return
184 var style = log.style[m.level]
185 var disp = log.disp[m.level] || m.level
187 m.message.split(/\r?\n/).forEach(function (line) {
189 this.write(this.heading, this.headingStyle)
192 this.write(disp, log.style[m.level])
193 var p = m.prefix || ''
194 if (p) this.write(' ')
195 this.write(p, this.prefixStyle)
196 this.write(' ' + line + '\n')
201 log.write = function (msg, style) {
202 if (!this.cursor) return
203 if (this.stream !== this.cursor.stream) {
204 this.cursor = ansi(this.stream, { enabled: colorEnabled })
206 if (gaugeTheme != null) options.theme = gaugeTheme
207 if (gaugeTemplate != null) options.template = gaugeTemplate
208 this.gauge = new Gauge(options, this.cursor)
212 if (style.fg) this.cursor.fg[style.fg]()
213 if (style.bg) this.cursor.bg[style.bg]()
214 if (style.bold) this.cursor.bold()
215 if (style.underline) this.cursor.underline()
216 if (style.inverse) this.cursor.inverse()
217 if (style.beep) this.cursor.beep()
218 this.cursor.write(msg).reset()
221 log.addLevel = function (lvl, n, style, disp) {
222 if (!disp) disp = lvl
224 this.style[lvl] = style
225 if (!this[lvl]) this[lvl] = function () {
226 var a = new Array(arguments.length + 1)
228 for (var i = 0; i < arguments.length; i ++) {
229 a[i + 1] = arguments[i]
231 return this.log.apply(this, a)
233 this.disp[lvl] = disp
236 log.prefixStyle = { fg: 'magenta' }
237 log.headingStyle = { fg: 'white', bg: 'black' }
242 log.addLevel('silly', -Infinity, { inverse: true }, 'sill')
243 log.addLevel('verbose', 1000, { fg: 'blue', bg: 'black' }, 'verb')
244 log.addLevel('info', 2000, { fg: 'green' })
245 log.addLevel('http', 3000, { fg: 'green', bg: 'black' })
246 log.addLevel('warn', 4000, { fg: 'black', bg: 'yellow' }, 'WARN')
247 log.addLevel('error', 5000, { fg: 'red', bg: 'black' }, 'ERR!')
248 log.addLevel('silent', Infinity)
250 // allow 'error' prefix
251 log.on('error', function(){})