1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 02:09:32 +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 util = require('util')
var events = require('events')
var colors = require('colors') var colors = require('colors')
function Log(tag, stream) { function Log(tag, stream) {
@ -20,6 +22,18 @@ function Log(tag, stream) {
, FTL: 'red' , FTL: 'red'
} }
this.localIdentifier = null 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) { Log.prototype.setLocalIdentifier = function(identifier) {
@ -27,45 +41,56 @@ Log.prototype.setLocalIdentifier = function(identifier) {
} }
Log.prototype.debug = function() { Log.prototype.debug = function() {
this._write(this._format(this.levels.DEBUG, arguments)) this._write(this._entry(this.levels.DEBUG, arguments))
} }
Log.prototype.verbose = function() { Log.prototype.verbose = function() {
this._write(this._format(this.levels.VERBOSE, arguments)) this._write(this._entry(this.levels.VERBOSE, arguments))
} }
Log.prototype.info = function() { Log.prototype.info = function() {
this._write(this._format(this.levels.INFO, arguments)) this._write(this._entry(this.levels.INFO, arguments))
} }
Log.prototype.warn = function() { Log.prototype.warn = function() {
this._write(this._format(this.levels.WARNING, arguments)) this._write(this._entry(this.levels.WARNING, arguments))
} }
Log.prototype.error = function() { Log.prototype.error = function() {
this._write(this._format(this.levels.ERROR, arguments)) this._write(this._entry(this.levels.ERROR, arguments))
} }
Log.prototype.fatal = function() { 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) { Log.prototype._color = function(priority) {
return priority[this.colors[priority]] return priority[this.colors[priority]]
} }
Log.prototype._format = function(priority, args) { Log.prototype._entry = function(priority, args) {
return util.format('%s/%s %d [%s] %s' return new Log.Entry(
, this._color(priority) priority
, this.tag , this.tag
, process.pid , process.pid
, this.localIdentifier || Log.globalIdentifier , this.localIdentifier || Log.globalIdentifier
, util.format.apply(util, args) , util.format.apply(util, args)
) )
} }
Log.prototype._write = function(out) { Log.prototype._format = function(entry) {
console.error(out) 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 = '*' Log.globalIdentifier = '*'