]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npmlog/log.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npmlog / log.js
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 ansi = require('ansi')
9 log.cursor = ansi(process.stderr)
10 log.stream = process.stderr
11
12 // by default, let ansi decide based on tty-ness.
13 var colorEnabled = undefined
14 log.enableColor = function () {
15   colorEnabled = true
16   this.cursor.enabled = true
17 }
18 log.disableColor = function () {
19   colorEnabled = false
20   this.cursor.enabled = false
21 }
22
23 // default level
24 log.level = 'info'
25
26 log.gauge = new Gauge(log.cursor)
27 log.tracker = new Progress.TrackerGroup()
28
29 // no progress bars unless asked
30 log.progressEnabled = false
31
32 var gaugeTheme = undefined
33
34 log.enableUnicode = function () {
35   gaugeTheme = Gauge.unicode
36   log.gauge.setTheme(gaugeTheme)
37 }
38
39 log.disableUnicode = function () {
40   gaugeTheme = Gauge.ascii
41   log.gauge.setTheme(gaugeTheme)
42 }
43
44 var gaugeTemplate = undefined
45 log.setGaugeTemplate = function (template) {
46   gaugeTemplate = template
47   log.gauge.setTemplate(gaugeTemplate)
48 }
49
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)
55   this.gauge.enable()
56   this.showProgress()
57 }
58
59 log.disableProgress = function () {
60   if (!this.progressEnabled) return
61   this.clearProgress()
62   this.progressEnabled = false
63   this.tracker.removeListener('change', this.showProgress)
64   this.gauge.disable()
65 }
66
67 var trackerConstructors = ['newGroup', 'newItem', 'newStream']
68
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
77     var func = log[P]
78     tracker[P] = function () {
79       return func.apply(log, arguments)
80     }
81   })
82   // if the new tracker is a group, make sure any subtrackers get
83   // mixed in too
84   if (tracker instanceof Progress.TrackerGroup) {
85     trackerConstructors.forEach(function (C) {
86       var func = tracker[C]
87       tracker[C] = function () { return mixinLog(func.apply(tracker, arguments)) }
88     })
89   }
90   return tracker
91 }
92
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)) }
96 })
97
98 log.clearProgress = function () {
99   if (!this.progressEnabled) return
100   this.gauge.hide()
101 }
102
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
108
109 // temporarily stop emitting, but don't drop
110 log.pause = function () {
111   this._paused = true
112 }
113
114 log.resume = function () {
115   if (!this._paused) return
116   this._paused = false
117
118   var b = this._buffer
119   this._buffer = []
120   b.forEach(function (m) {
121     this.emitLog(m)
122   }, this)
123   if (this.progressEnabled) this.enableProgress()
124 }
125
126 log._buffer = []
127
128 var id = 0
129 log.record = []
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)))
136   }
137
138   var a = new Array(arguments.length - 2)
139   var stack = null
140   for (var i = 2; i < arguments.length; i ++) {
141     var arg = a[i-2] = arguments[i]
142
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 + ''
147     }
148   }
149   if (stack) a.unshift(stack + '\n')
150   message = util.format.apply(util, a)
151
152   var m = { id: id++,
153             level: lvl,
154             prefix: String(prefix || ''),
155             message: message,
156             messageRaw: a }
157
158   this.emit('log', m)
159   this.emit('log.' + lvl, m)
160   if (m.prefix) this.emit(m.prefix, m)
161
162   this.record.push(m)
163   var mrs = this.maxRecordSize
164   var n = this.record.length - mrs
165   if (n > mrs / 10) {
166     var newSize = Math.floor(mrs * 0.9)
167     this.record = this.record.slice(-1 * newSize)
168   }
169
170   this.emitLog(m)
171 }.bind(log)
172
173 log.emitLog = function (m) {
174   if (this._paused) {
175     this._buffer.push(m)
176     return
177   }
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
183
184   var style = log.style[m.level]
185   var disp = log.disp[m.level] || m.level
186   this.clearProgress()
187   m.message.split(/\r?\n/).forEach(function (line) {
188     if (this.heading) {
189       this.write(this.heading, this.headingStyle)
190       this.write(' ')
191     }
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')
197   }, this)
198   this.showProgress()
199 }
200
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 })
205     var options = {}
206     if (gaugeTheme != null) options.theme = gaugeTheme
207     if (gaugeTemplate != null) options.template = gaugeTemplate
208     this.gauge = new Gauge(options, this.cursor)
209   }
210
211   style = style || {}
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()
219 }
220
221 log.addLevel = function (lvl, n, style, disp) {
222   if (!disp) disp = lvl
223   this.levels[lvl] = n
224   this.style[lvl] = style
225   if (!this[lvl]) this[lvl] = function () {
226     var a = new Array(arguments.length + 1)
227     a[0] = lvl
228     for (var i = 0; i < arguments.length; i ++) {
229       a[i + 1] = arguments[i]
230     }
231     return this.log.apply(this, a)
232   }.bind(this)
233   this.disp[lvl] = disp
234 }
235
236 log.prefixStyle = { fg: 'magenta' }
237 log.headingStyle = { fg: 'white', bg: 'black' }
238
239 log.style = {}
240 log.levels = {}
241 log.disp = {}
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)
249
250 // allow 'error' prefix
251 log.on('error', function(){})