From a33c866644187e22de1eeb88729c0aa4a4b58706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sun, 28 Feb 2021 14:41:12 +0100 Subject: [PATCH 1/3] Filter log files to rpt --- lib/logs.js | 6 ++++-- test/lib/logs.js | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/logs.js b/lib/logs.js index 69ee870..dbea09e 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -25,7 +25,7 @@ Logs.generateLogFileName = function (suffix) { .replace(/:/g, '-') // Replace time dividers with dash .replace(/T/, '_') // Remove date and time divider .replace(/\..+/, '') // Remove milliseconds - return 'arma3server_' + dateStr + '_' + suffix + '.log' + return 'arma3server_' + dateStr + '_' + suffix + '.rpt' } Logs.prototype.delete = function (filename, callback) { @@ -86,7 +86,9 @@ Logs.prototype.logFiles = function (callback) { return } - files = files.map(function (file) { + files = files.filter(function (file) { + return file.endsWith('.rpt') + }).map(function (file) { return { name: file, path: path.join(directory, file) diff --git a/test/lib/logs.js b/test/lib/logs.js index 3f8c88d..501df5a 100644 --- a/test/lib/logs.js +++ b/test/lib/logs.js @@ -19,13 +19,13 @@ describe('Logs', function () { describe('generateLogFileName()', function () { it('should generate valid file name', function () { - Logs.generateLogFileName('test').should.eql('arma3server_2015-10-21_19-28-32_test.log') + Logs.generateLogFileName('test').should.eql('arma3server_2015-10-21_19-28-32_test.rpt') }) }) describe('generateLogFilePath()', function () { it('should generate valid file path', function () { - logs.generateLogFilePath('test').should.eql(path.join('/tmp', 'logs', 'arma3server_2015-10-21_19-28-32_test.log')) + logs.generateLogFilePath('test').should.eql(path.join('/tmp', 'logs', 'arma3server_2015-10-21_19-28-32_test.rpt')) }) }) }) From 0d9b6134b7392a279277a8bf7adbe7df815132cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sun, 28 Feb 2021 14:30:16 +0100 Subject: [PATCH 2/3] Sort logs by creation date in descending order --- lib/logs.js | 4 +++- public/js/app/collections/logs.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/logs.js b/lib/logs.js index dbea09e..5f1b48a 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -97,13 +97,15 @@ Logs.prototype.logFiles = function (callback) { async.filter(files, function (file, cb) { fs.stat(file.path, function (err, stat) { + file.created = stat.birthtime.toISOString() + file.modified = stat.mtime.toISOString() file.formattedSize = filesize(stat.size) file.size = stat.size cb(!err && stat.isFile()) }) }, function (files) { files.sort(function (a, b) { - return a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + return b.created.localeCompare(a.created) // Descending order }) callback(null, files) diff --git a/public/js/app/collections/logs.js b/public/js/app/collections/logs.js index c63a56e..a5b5170 100644 --- a/public/js/app/collections/logs.js +++ b/public/js/app/collections/logs.js @@ -3,7 +3,9 @@ var Backbone = require('backbone') var Log = require('app/models/log') module.exports = Backbone.Collection.extend({ - comparator: 'name', + comparator: function (a, b) { + return b.get('created').localeCompare(a.get('created')) // Descending order + }, model: Log, url: '/api/logs/' }) From 1ebd0afe86f951efdda1bd3adbca29ca158dccfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sun, 28 Feb 2021 14:47:48 +0100 Subject: [PATCH 3/3] Cleanup old log files on Linux --- lib/logs.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/logs.js b/lib/logs.js index 5f1b48a..fd98e5a 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -12,6 +12,8 @@ var gamesLogFolder = { arma3_x64: 'Arma 3' } +var numberOfLogsToKeep = 20 + var Logs = function (config) { this.config = config @@ -29,18 +31,16 @@ Logs.generateLogFileName = function (suffix) { } Logs.prototype.delete = function (filename, callback) { + callback = callback || function () {} + this.getLogFile(filename, function (err, logFile) { if (err) { - if (callback) { - return callback(err) - } + return callback(err) } else { if (logFile && logFile.path) { fs.unlink(logFile.path, callback) } else { - if (callback) { - return callback(new Error('File not found')) - } + return callback(new Error('File not found')) } } }) @@ -141,6 +141,10 @@ Logs.prototype.logServerProcesses = function (serverProcess, headlessClientProce headlessClientProcesses.forEach(function (headlessClientProcess, idx) { self.logServerProcess(headlessClientProcess, 'hc_' + (idx + 1)) }) + + if (this.config.type === 'linux') { + this.cleanupOldLogFiles() + } } Logs.prototype.logServerProcess = function (serverProcess, suffix) { @@ -178,4 +182,19 @@ Logs.prototype.logServerProcess = function (serverProcess, suffix) { }) } +Logs.prototype.cleanupOldLogFiles = function () { + var self = this + + self.logFiles(function (err, files) { + if (err) { + return + } + + var oldLogFiles = files.slice(numberOfLogsToKeep) + oldLogFiles.forEach(function (logFile) { + self.delete(logFile.name) + }) + }) +} + module.exports = Logs