Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions bin/nodestalgia
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

var program = require('commander');
var path = require('path');
var carrier = require('carrier');

var filename = [],
useStdin = false,
spawn,
tail;

program
.version('0.1.2')
Expand All @@ -13,8 +19,9 @@ program
.option('-S, --no-sumarize', 'Do not show the sumarize counters')
.parse(process.argv);

var filename = [];
if (program.args.length > 0) {
if (program.args.length === 1 && program.args[0] === '-') {
useStdin = true;
} else if (program.args.length > 0) {
for (var i = 0; i < program.args.length; i++) {
var f = program.args[i];
if (!path.existsSync(f)) {
Expand All @@ -34,17 +41,18 @@ events = require('events'),
socketio = require('socket.io');

var app = module.exports = express.createServer();
var rootpath = __dirname + '/..';

// Configuration
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('views', rootpath + '/views');
app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(require('stylus').middleware({ src: rootpath + '/public' }));
app.use(app.router);
app.use(express['static'](__dirname + '/public'));
app.use(express['static'](rootpath + '/public'));
});

app.configure('development', function () {
Expand All @@ -68,9 +76,6 @@ app.listen(8081);
var io = socketio.listen(app);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

var spawn = require('child_process').spawn;
var tail = spawn('tail', ['-f'].concat(filename));

// 127.0.0.1 - - [07/Mar/2012:23:21:47 +0100] "GET / HTTP/1.0" 200 454 "-" "ApacheBench/2.3"
var regexp = /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).+\[(.+)\] "(\w+) ([^ ]+) .*" (\w+) (\w+)/;

Expand All @@ -97,8 +102,8 @@ function reverse_addr(addr) {
return e;
}

tail.stdout.on('data', function (data) {
var str = data.toString('utf8')
function parseLine(line) {
var str = line.toString('utf8')
, match = regexp.exec(str)
, robj
, matchdns;
Expand Down Expand Up @@ -135,6 +140,14 @@ tail.stdout.on('data', function (data) {
io.sockets.emit('log', JSON.stringify(robj));
});
}

}
});
}

if (useStdin) {
process.stdin.resume();
carrier.carry(process.stdin, parseLine);
} else {
spawn = require('child_process').spawn;
tail = spawn('tail', ['-f'].concat(filename));
tail.stdout.on('data', parseLine);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
, "jade": ">= 0.0.1"
, "socket.io": ">= 0.8.x"
, "commander": ">= 0.5.x"
, "carrier": ">= 0.1.10"
},
"devDependencies": {},
"optionalDependencies": {},
Expand Down