Created
September 27, 2010 11:55
-
-
Save jfro/598907 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var sys = require('sys'), | |
| fs = require('fs'), | |
| spawn = require('child_process').spawn, | |
| tail = spawn('tail', ['-f', 'server.log']); | |
| // var used for \n scanner | |
| tail.current_line = ""; | |
| // holds user state | |
| var status = { | |
| users: {}, | |
| updateData: function () { | |
| //console.log('Updating... online: ' + sys.inspect(this.online)); | |
| //console.log(JSON.stringify(this.users)); | |
| fs.writeFile('users.json', JSON.stringify(this.users), function (err) { | |
| if (err) throw err; | |
| //console.log('User status file updated'); | |
| }); | |
| } | |
| }; | |
| tail.on('newline', function(line) { | |
| console.log(line); | |
| var matches = null; | |
| if(matches = line.match(/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s\[INFO\]\s(\w+)\s\[(.*)\]\slogged in/)) | |
| { | |
| var date = matches[1], | |
| username = matches[2]; | |
| // update our state to show user is online and not offline | |
| var user = status.users[username]; | |
| if(user) | |
| { | |
| user.date = date; | |
| user.online = true; | |
| } | |
| else | |
| { | |
| status.users[username] = { | |
| date: date, | |
| online: true | |
| }; | |
| } | |
| status.updateData(); | |
| } | |
| if(matches = line.match(/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s\[INFO\]\s(\w+)\slost connection/)) | |
| { | |
| var date = matches[1], | |
| username = matches[2]; | |
| // update user status as offline | |
| var user = status.users[username]; | |
| if(user) | |
| { | |
| user.date = date; | |
| user.online = false; | |
| } | |
| status.updateData(); | |
| } | |
| }); | |
| tail.stdout.on('data', function (data) { | |
| //sys.print('stdout: ' + data); | |
| data = data.toString(); | |
| for(var i = 0; i < data.length; i++) | |
| { | |
| //console.log("Checking '"+data[i]+"'"); | |
| if(data[i] == "\n") | |
| { | |
| //console.log("Got newline"); | |
| tail.emit('newline', tail.current_line); | |
| tail.current_line = ""; | |
| } | |
| else | |
| { | |
| tail.current_line += data[i]; | |
| } | |
| } | |
| //myString.match(/^\d+$/) | |
| //tail.emit('newline', "test"); | |
| }); | |
| tail.stderr.on('data', function (data) { | |
| sys.print('stderr: ' + data); | |
| }); | |
| tail.on('exit', function (code) { | |
| console.log('child process exited with code ' + code); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment