This commit is contained in:
barisusakli
2014-10-06 18:19:33 -04:00
parent c827485de5
commit 394a01fdef
4 changed files with 62 additions and 9 deletions

View File

@@ -101,15 +101,39 @@ var fs = require('fs'),
});
};
events.getLog = function(callback) {
events.getLog = function(end, len, callback) {
var logFile = path.join(nconf.get('base_dir'), logFileName);
fs.readFile(logFile, function(err, res) {
if(err) {
fs.stat(logFile, function(err, stat) {
if (err) {
return callback(null, 'No logs found!');
}
callback(null, res);
var buffer = '';
var size = stat.size;
if (end === -1) {
end = size;
}
end = parseInt(end, 10);
var start = Math.max(0, end - len);
var rs = fs.createReadStream(logFile, {start: start, end: end});
rs.addListener('data', function(lines) {
buffer += lines.toString();
});
rs.addListener('end', function() {
var firstNewline = buffer.indexOf('\n');
if (firstNewline !== -1) {
buffer = buffer.slice(firstNewline);
buffer = buffer.split('\n').reverse().join('\n');
}
callback(null, {data: buffer, next: end - buffer.length});
})
});
};
}(module.exports));