1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-03 17:59:28 +02:00

Emit log messages as they occur.

This commit is contained in:
Simo Kinnunen 2014-01-29 18:09:01 +09:00
parent 6fc2791ab2
commit ba56b8d70c

View file

@ -1,4 +1,6 @@
var util = require('util')
var events = require('events')
var colors = require('colors')
function Log(tag, stream) {
@ -20,6 +22,18 @@ function Log(tag, stream) {
, FTL: 'red'
}
this.localIdentifier = null
events.EventEmitter.call(this)
}
util.inherits(Log, events.EventEmitter)
Log.Entry = function(priority, tag, pid, identifier, message) {
this.time = Date.now()
this.priority = priority
this.tag = tag
this.pid = pid
this.identifier = identifier
this.message = message
}
Log.prototype.setLocalIdentifier = function(identifier) {
@ -27,45 +41,56 @@ Log.prototype.setLocalIdentifier = function(identifier) {
}
Log.prototype.debug = function() {
this._write(this._format(this.levels.DEBUG, arguments))
this._write(this._entry(this.levels.DEBUG, arguments))
}
Log.prototype.verbose = function() {
this._write(this._format(this.levels.VERBOSE, arguments))
this._write(this._entry(this.levels.VERBOSE, arguments))
}
Log.prototype.info = function() {
this._write(this._format(this.levels.INFO, arguments))
this._write(this._entry(this.levels.INFO, arguments))
}
Log.prototype.warn = function() {
this._write(this._format(this.levels.WARNING, arguments))
this._write(this._entry(this.levels.WARNING, arguments))
}
Log.prototype.error = function() {
this._write(this._format(this.levels.ERROR, arguments))
this._write(this._entry(this.levels.ERROR, arguments))
}
Log.prototype.fatal = function() {
this._write(this._format(this.levels.FATAL, arguments))
this._write(this._entry(this.levels.FATAL, arguments))
}
Log.prototype._color = function(priority) {
return priority[this.colors[priority]]
}
Log.prototype._format = function(priority, args) {
return util.format('%s/%s %d [%s] %s'
, this._color(priority)
, this.tag
, process.pid
, this.localIdentifier || Log.globalIdentifier
, util.format.apply(util, args)
Log.prototype._entry = function(priority, args) {
return new Log.Entry(
priority
, this.tag
, process.pid
, this.localIdentifier || Log.globalIdentifier
, util.format.apply(util, args)
)
}
Log.prototype._write = function(out) {
console.error(out)
Log.prototype._format = function(entry) {
return util.format('%s/%s %d [%s] %s'
, this._color(entry.priority)
, entry.tag
, entry.pid
, entry.identifier
, entry.message
)
}
Log.prototype._write = function(entry) {
console.error(this._format(entry))
this.emit('entry', entry)
}
Log.globalIdentifier = '*'