Skip to content

Instantly share code, notes, and snippets.

@bobpoekert
Created November 12, 2010 01:35
Show Gist options
  • Save bobpoekert/673587 to your computer and use it in GitHub Desktop.
Save bobpoekert/673587 to your computer and use it in GitHub Desktop.
var tail = function(fname, onData, ender) {
fs.open(fname, 'a+', function(err, fd) {
if (err) {
puts('tail error: '+err);
if (ender(fd, 0) !== false) {
setTimeout(function(){
tail(fname, onData);
}, 10);
}
} else {
puts('tailing');
var size = 1024;
var buffer = new Buffer(size);
var pos = 0;
var reader = function() {
fs.read(fd, buffer, 0, size, null, function(err, bytesRead) {
if (bytesRead > 0) {
pos += bytesRead;
var outp = new Buffer(bytesRead);
buffer.copy(outp, 0, 0, bytesRead);
onData(outp, fd);
}
if (ender && ender(fd, pos) === false) {
fs.close(fd);
} else {
if (bytesRead == 0) {
setTimeout(reader, 10);
} else {
reader();
}
}
});
};
reader();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment