zordon
Version:
straightforward distributed logging tool
64 lines (43 loc) • 1.16 kB
JavaScript
var net = require('net')
var fs = require('fs')
var spawn = require('child_process').spawn
var combine = require('stream-combiner')
exports = module.exports
exports.server = function(port, cb) {
if (!port) return cb(new Error('must define port'))
var clients = []
net.createServer(function(con) {
addCon(con)
con.on('close', subCon)
handle(con)
}).listen(port)
function addCon(con) {
clients.push(con.remoteAddress)
console.log(clients)
}
function subCon() {
clients.splice(clients.indexOf(this.remoteAddress), 1)
console.log(clients)
}
function handle(con) {
con.pipe(process.stdout)
}
}
exports.client = function(logfile, url) {
if (!url || !logfile) throw new Error('must supply file and url')
var parts = url.split(':')
var host = parts[0]
var port = parseInt(parts[1])
net.connect(port, 'localhost')
.on('error', function(err) {
console.log(err.code)
})
.on('connect', function() {
tail(logfile).pipe(this)
})
}
function tail(logfile) {
var child = spawn('tail', ['-f', logfile])
child.stderr.pipe(process.stdout)
return child.stdout
}