"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npmlog/log.js" (8 Mar 2017, 8043 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 // by default, decide based on tty-ness.
   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 // default level
   39 log.level = 'info'
   40 
   41 log.gauge = new Gauge(stream, {
   42   theme: {hasColor: log.useColor()},
   43   template: [
   44     {type: 'progressbar', length: 20},
   45     {type: 'activityIndicator', kerning: 1, length: 1},
   46     {type: 'section', default: ''},
   47     ':',
   48     {type: 'logline', kerning: 1, default: ''}
   49   ]
   50 })
   51 
   52 log.tracker = new Progress.TrackerGroup()
   53 
   54 // no progress bars unless asked
   55 log.progressEnabled = false
   56 
   57 var unicodeEnabled
   58 
   59 log.enableUnicode = function () {
   60   unicodeEnabled = true
   61   this.gauge.setTheme({hasColor: this.useColor(), hasUnicode: unicodeEnabled})
   62 }
   63 
   64 log.disableUnicode = function () {
   65   unicodeEnabled = false
   66   this.gauge.setTheme({hasColor: this.useColor(), hasUnicode: unicodeEnabled})
   67 }
   68 
   69 log.setGaugeThemeset = function (themes) {
   70   this.gauge.setThemeset(themes)
   71 }
   72 
   73 log.setGaugeTemplate = function (template) {
   74   this.gauge.setTemplate(template)
   75 }
   76 
   77 log.enableProgress = function () {
   78   if (this.progressEnabled) return
   79   this.progressEnabled = true
   80   if (this._pause) return
   81   this.tracker.on('change', this.showProgress)
   82   this.gauge.enable()
   83   this.showProgress()
   84 }
   85 
   86 log.disableProgress = function () {
   87   if (!this.progressEnabled) return
   88   this.clearProgress()
   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   // mixin the public methods from log into the tracker
   98   // (except: conflicts and one's we handle specially)
   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   // if the new tracker is a group, make sure any subtrackers get
  110   // mixed in too
  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 // Add tracker constructors to the top level log object
  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) // bind for use in tracker's on-change listener
  146 
  147 // temporarily stop emitting, but don't drop
  148 log.pause = function () {
  149   this._paused = true
  150 }
  151 
  152 log.resume = function () {
  153   if (!this._paused) return
  154   this._paused = false
  155 
  156   var b = this._buffer
  157   this._buffer = []
  158   b.forEach(function (m) {
  159     this.emitLog(m)
  160   }, this)
  161   if (this.progressEnabled) this.enableProgress()
  162 }
  163 
  164 log._buffer = []
  165 
  166 var id = 0
  167 log.record = []
  168 log.maxRecordSize = 10000
  169 log.log = function (lvl, prefix, message) {
  170   var l = this.levels[lvl]
  171   if (l === undefined) {
  172     return this.emit('error', new Error(util.format(
  173       'Undefined log level: %j', lvl)))
  174   }
  175 
  176   var a = new Array(arguments.length - 2)
  177   var stack = null
  178   for (var i = 2; i < arguments.length; i++) {
  179     var arg = a[i - 2] = arguments[i]
  180 
  181     // resolve stack traces to a plain string.
  182     if (typeof arg === 'object' && arg &&
  183         (arg instanceof Error) && arg.stack) {
  184       arg.stack = stack = arg.stack + ''
  185     }
  186   }
  187   if (stack) a.unshift(stack + '\n')
  188   message = util.format.apply(util, a)
  189 
  190   var m = { id: id++,
  191             level: lvl,
  192             prefix: String(prefix || ''),
  193             message: message,
  194             messageRaw: a }
  195 
  196   this.emit('log', m)
  197   this.emit('log.' + lvl, m)
  198   if (m.prefix) this.emit(m.prefix, m)
  199 
  200   this.record.push(m)
  201   var mrs = this.maxRecordSize
  202   var n = this.record.length - mrs
  203   if (n > mrs / 10) {
  204     var newSize = Math.floor(mrs * 0.9)
  205     this.record = this.record.slice(-1 * newSize)
  206   }
  207 
  208   this.emitLog(m)
  209 }.bind(log)
  210 
  211 log.emitLog = function (m) {
  212   if (this._paused) {
  213     this._buffer.push(m)
  214     return
  215   }
  216   if (this.progressEnabled) this.gauge.pulse(m.prefix)
  217   var l = this.levels[m.level]
  218   if (l === undefined) return
  219   if (l < this.levels[this.level]) return
  220   if (l > 0 && !isFinite(l)) return
  221 
  222   var disp = log.disp[m.level] || m.level
  223   this.clearProgress()
  224   m.message.split(/\r?\n/).forEach(function (line) {
  225     if (this.heading) {
  226       this.write(this.heading, this.headingStyle)
  227       this.write(' ')
  228     }
  229     this.write(disp, log.style[m.level])
  230     var p = m.prefix || ''
  231     if (p) this.write(' ')
  232     this.write(p, this.prefixStyle)
  233     this.write(' ' + line + '\n')
  234   }, this)
  235   this.showProgress()
  236 }
  237 
  238 log._format = function (msg, style) {
  239   if (!stream) return
  240 
  241   var output = ''
  242   if (this.useColor()) {
  243     style = style || {}
  244     var settings = []
  245     if (style.fg) settings.push(style.fg)
  246     if (style.bg) settings.push('bg' + style.bg[0].toUpperCase() + style.bg.slice(1))
  247     if (style.bold) settings.push('bold')
  248     if (style.underline) settings.push('underline')
  249     if (style.inverse) settings.push('inverse')
  250     if (settings.length) output += consoleControl.color(settings)
  251     if (style.beep) output += consoleControl.beep()
  252   }
  253   output += msg
  254   if (this.useColor()) {
  255     output += consoleControl.color('reset')
  256   }
  257   return output
  258 }
  259 
  260 log.write = function (msg, style) {
  261   if (!stream) return
  262 
  263   stream.write(this._format(msg, style))
  264 }
  265 
  266 log.addLevel = function (lvl, n, style, disp) {
  267   if (!disp) disp = lvl
  268   this.levels[lvl] = n
  269   this.style[lvl] = style
  270   if (!this[lvl]) {
  271     this[lvl] = function () {
  272       var a = new Array(arguments.length + 1)
  273       a[0] = lvl
  274       for (var i = 0; i < arguments.length; i++) {
  275         a[i + 1] = arguments[i]
  276       }
  277       return this.log.apply(this, a)
  278     }.bind(this)
  279   }
  280   this.disp[lvl] = disp
  281 }
  282 
  283 log.prefixStyle = { fg: 'magenta' }
  284 log.headingStyle = { fg: 'white', bg: 'black' }
  285 
  286 log.style = {}
  287 log.levels = {}
  288 log.disp = {}
  289 log.addLevel('silly', -Infinity, { inverse: true }, 'sill')
  290 log.addLevel('verbose', 1000, { fg: 'blue', bg: 'black' }, 'verb')
  291 log.addLevel('info', 2000, { fg: 'green' })
  292 log.addLevel('http', 3000, { fg: 'green', bg: 'black' })
  293 log.addLevel('warn', 4000, { fg: 'black', bg: 'yellow' }, 'WARN')
  294 log.addLevel('error', 5000, { fg: 'red', bg: 'black' }, 'ERR!')
  295 log.addLevel('silent', Infinity)
  296 
  297 // allow 'error' prefix
  298 log.on('error', function () {})