From 394a01fdef516c2c33e351f782818048d70af373 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 6 Oct 2014 18:19:33 -0400 Subject: [PATCH] closes #2188 --- src/controllers/admin.js | 6 +++--- src/events.js | 32 +++++++++++++++++++++++++---- src/socket.io/admin.js | 8 ++++++++ src/views/admin/advanced/events.tpl | 25 ++++++++++++++++++++-- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/controllers/admin.js b/src/controllers/admin.js index 8f007c7783..f8c31856bc 100644 --- a/src/controllers/admin.js +++ b/src/controllers/admin.js @@ -161,14 +161,14 @@ adminController.database.get = function(req, res, next) { }; adminController.events.get = function(req, res, next) { - events.getLog(function(err, data) { + events.getLog(-1, 5000, function(err, data) { if(err || !data) { return next(err); } - data = data.toString().split('\n').reverse().join('\n'); res.render('admin/advanced/events', { - eventdata: data + eventdata: data.data, + next: data.next }); }); }; diff --git a/src/events.js b/src/events.js index 3bb4c1c78a..01f8833cbb 100644 --- a/src/events.js +++ b/src/events.js @@ -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)); diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 108356465b..46252c1ae0 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -241,4 +241,12 @@ SocketAdmin.clearLog = function(socket, data, callback) { fs.truncate(logPath, callback); }; +SocketAdmin.getMoreEvents = function(socket, next, callback) { + if (parseInt(next, 10) < 0) { + return callback(null, {data: [], next: next}); + } + events.getLog(next, 5000, callback); +}; + + module.exports = SocketAdmin; diff --git a/src/views/admin/advanced/events.tpl b/src/views/admin/advanced/events.tpl index 691ec39c3b..e37bec912d 100644 --- a/src/views/admin/advanced/events.tpl +++ b/src/views/admin/advanced/events.tpl @@ -2,9 +2,30 @@
Events
-
+
{eventdata}
-
\ No newline at end of file + + + +