Navigation Menu

Skip to content

Commit

Permalink
awesome update
Browse files Browse the repository at this point in the history
  • Loading branch information
Franz SEO authored and Franz SEO committed Apr 5, 2011
1 parent 748af54 commit 7570298
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
test.log
16 changes: 16 additions & 0 deletions nolog-test.js
@@ -0,0 +1,16 @@
#!/usr/bin/env node
var util = require('util');
var nolog = require("./nolog.js");
var logfile = "./test.log";

var myjob = nolog.job(logfile,'nl', function(d){console.log("NL");});
console.log(util.inspect(myjob.pids));
var myjob2 = nolog.job(logfile,'sv', function(d){console.log("sv");});
console.log(util.inspect(myjob2.pids));
process.on("exit", function() {
console.log("NologServer Main loop exit")
});

setTimeout(function(){console.log("send kill for myjob");myjob.kill();}, 2000);
setTimeout(function(){console.log("send kill for myjob2");myjob2.kill();}, 6000);

197 changes: 106 additions & 91 deletions nolog.js
@@ -1,97 +1,112 @@
#!/usr/bin/env node

var util = require('util');
var spawn = require('child_process').spawn;

var touchHolder = {};
var grepHolder = [];


var touchf = function(file)
{
return spawn('tail', ['-f', file]);
}

var grep = function(pattern)
{
return spawn('grep', [pattern]);
}

var job = function(file, pattern, callback, errcallback)
{
return function(file, pattern, callback, errcallback)
{
var callback = callback || function(data){ console.log(data); return data; };
var errcallback = errcallback || function(data){ console.log('stderr: '+data); return data; };
var pattern = pattern || undefined;
var file = file || undefined;
var myTouchf, myGrep;

if(file == undefined)
{
throw new Error('critical error in nolog - file must not be undefined');
}
//if file is not in fileA, add file to fileA
//get touchf subprocess of the file
if(touchHolder[file])
{
myTouchf=touchHolder[file];
}
else
{
myTouchf=touchf(file);
touchHolder[file]=myTouchf;
}
//start the new grep
myGrep = spawn('grep', 'pattern');
grepHolder.push(myGrep);
//send the data to the new grep process
myTouchf.stdout.on('data', function(data){
myGrep.stdin.write(data);
});
myTouchf.stderr.on('data', errcallback(data));
myTouchf.on('exit', function(code) {
if(code !== 0)
{
console.log('touch process exited with code '+code);
}
myGrep.stdin.end();
});

myGrep.stdout.on('data', callback(data));
myGrep.stderr.on('data', errcallback(data));

myGrep.on('exit', function(code) {
if(code !== 0)
{
console.log('grep process exited with code '+code);
}
myTouchf.stdin.end();
});

var kill = function(signal)
{
var signal = signal || 'SIGHUP';
myGrep.kill(signal);
myTouchf.kill(signal);
var util = require("util");
var spawn = require("child_process").spawn;
var tailHolder = {};
var stumpA = [];
var isRegExp = function(obj) {
return!!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false))
};
var tailf = function(file) {
//console.log("tail created with: " + file);
return spawn("tail", ["-f", file])
};
var job = function(file, pattern, callback, errcallback, exitcallback) {
//console.log("ajob");
var that = this;
var callback = callback || function(data) {
//console.log(data);
return data
};
var errcallback = errcallback || function(data) {
console.log("stderr: " + data);
throw new Error("data");return data
};

var exitcallback = function(code) {
console.log("tail process exited with code " + code)
}
var pattern = pattern || undefined;
if(pattern && !isRegExp(pattern)) {
pattern = new RegExp(pattern.replace("\\", "\\\\"))
}
var file = file || undefined;
var mytailf;
if(file == undefined) {
throw new Error("critical error in nolog - file must not be undefined");
}
if(tailHolder[file]) {
mytailf = tailHolder[file].process;
}else {
mytailf = tailf(file);
tailHolder[file]={};
tailHolder[file].process = mytailf;
tailHolder[file].listenerA = [];
}
mytailf.stdin.setEncoding("utf8");
mytailf.stdout.setEncoding("utf8");
mytailf.stderr.setEncoding("utf8");
var listener = function(data) {
//console.log("mytailf.stdout.on data");
var dataA = data.split("\n");
for(var i = 0;i < dataA.length;i++) {
//is last line complete (end with \n)
if(i == dataA.length - 1 && data.charCodeAt(dataA[0].lenght - 1) != 10) {
stumpA[0] = dataA[i]
}else {
var line;
if(i == 0 && stumpA[0]) {
stumpA[1] = dataA[i];
line = stumpA.join("");
stumpA.length = 0
}else {
line = dataA[i]
}
var match = line.match(pattern);
if(match) {
callback(match)
}
}

var pids = {'grep':myGrep.pid, 'touch':myTouchf.pid}
//expose to the outside
this.kill = kill;
this.pids = pids;
}
}


//if this process exits, then we exit the sub thingies, too.
process.on('exit', function () {
grepHolder.forEach(function(g){ g.kill(); });
for(var t in touchHolder)
};

mytailf.stdout.on("data", listener);
tailHolder[file].listenerA.push(listener);
mytailf.stderr.on("data", function(data) {
errcallback(data)
});
mytailf.on("exit", exitcallback);
var kill = function()
{
touchHolder[t].kill();
mytailf.stdout.removeListener('data', listener);
var li = tailHolder[file].listenerA.indexOf(listener)
tailHolder[file].listenerA[li]=undefined;
var tempA = [];
tailHolder[file].listenerA.forEach(function(element){ if(element){tempA.push(element);} });
tailHolder[file].listenerA=tempA;
if(!tailHolder[file].listenerA.length)
{
killAll();
}
}
console.log('Main loop exit');
});



var killAll = function()
{
//kills the mytailf
console.log('killAll');
mytailf.kill("SIGHUP");
}
var pid = {"tail":mytailf.pid};
return{"pid":pid, 'pattern':pattern, 'file':file, 'kill':kill}
};
if(!exports) {
var exports = this;
}
exports.job = job;
exports.tailList = tailHolder;
process.on("exit", function() {
for(var t in tailHolder) {
tailHolder[t].process.kill("SIGHUP")
}
console.log("Nolog.js Main loop exit")
});
2 changes: 0 additions & 2 deletions nologserver.js

This file was deleted.

0 comments on commit 7570298

Please sign in to comment.