Merge pull request #180 from Dahlgren/feature/logs-filter-order

Improved logs handling
This commit is contained in:
Björn Dahlgren 2021-07-10 13:59:17 +02:00 committed by GitHub
commit ae1de87ed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 12 deletions

View File

@ -12,6 +12,8 @@ var gamesLogFolder = {
arma3_x64: 'Arma 3' arma3_x64: 'Arma 3'
} }
var numberOfLogsToKeep = 20
var Logs = function (config) { var Logs = function (config) {
this.config = config this.config = config
@ -25,22 +27,20 @@ Logs.generateLogFileName = function (suffix) {
.replace(/:/g, '-') // Replace time dividers with dash .replace(/:/g, '-') // Replace time dividers with dash
.replace(/T/, '_') // Remove date and time divider .replace(/T/, '_') // Remove date and time divider
.replace(/\..+/, '') // Remove milliseconds .replace(/\..+/, '') // Remove milliseconds
return 'arma3server_' + dateStr + '_' + suffix + '.log' return 'arma3server_' + dateStr + '_' + suffix + '.rpt'
} }
Logs.prototype.delete = function (filename, callback) { Logs.prototype.delete = function (filename, callback) {
callback = callback || function () {}
this.getLogFile(filename, function (err, logFile) { this.getLogFile(filename, function (err, logFile) {
if (err) { if (err) {
if (callback) { return callback(err)
return callback(err)
}
} else { } else {
if (logFile && logFile.path) { if (logFile && logFile.path) {
fs.unlink(logFile.path, callback) fs.unlink(logFile.path, callback)
} else { } else {
if (callback) { return callback(new Error('File not found'))
return callback(new Error('File not found'))
}
} }
} }
}) })
@ -86,7 +86,9 @@ Logs.prototype.logFiles = function (callback) {
return return
} }
files = files.map(function (file) { files = files.filter(function (file) {
return file.endsWith('.rpt')
}).map(function (file) {
return { return {
name: file, name: file,
path: path.join(directory, file) path: path.join(directory, file)
@ -95,13 +97,15 @@ Logs.prototype.logFiles = function (callback) {
async.filter(files, function (file, cb) { async.filter(files, function (file, cb) {
fs.stat(file.path, function (err, stat) { fs.stat(file.path, function (err, stat) {
file.created = stat.birthtime.toISOString()
file.modified = stat.mtime.toISOString()
file.formattedSize = filesize(stat.size) file.formattedSize = filesize(stat.size)
file.size = stat.size file.size = stat.size
cb(!err && stat.isFile()) cb(!err && stat.isFile())
}) })
}, function (files) { }, function (files) {
files.sort(function (a, b) { files.sort(function (a, b) {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase()) return b.created.localeCompare(a.created) // Descending order
}) })
callback(null, files) callback(null, files)
@ -137,6 +141,10 @@ Logs.prototype.logServerProcesses = function (serverProcess, headlessClientProce
headlessClientProcesses.forEach(function (headlessClientProcess, idx) { headlessClientProcesses.forEach(function (headlessClientProcess, idx) {
self.logServerProcess(headlessClientProcess, 'hc_' + (idx + 1)) self.logServerProcess(headlessClientProcess, 'hc_' + (idx + 1))
}) })
if (this.config.type === 'linux') {
this.cleanupOldLogFiles()
}
} }
Logs.prototype.logServerProcess = function (serverProcess, suffix) { Logs.prototype.logServerProcess = function (serverProcess, suffix) {
@ -174,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 module.exports = Logs

View File

@ -3,7 +3,9 @@ var Backbone = require('backbone')
var Log = require('app/models/log') var Log = require('app/models/log')
module.exports = Backbone.Collection.extend({ module.exports = Backbone.Collection.extend({
comparator: 'name', comparator: function (a, b) {
return b.get('created').localeCompare(a.get('created')) // Descending order
},
model: Log, model: Log,
url: '/api/logs/' url: '/api/logs/'
}) })

View File

@ -19,13 +19,13 @@ describe('Logs', function () {
describe('generateLogFileName()', function () { describe('generateLogFileName()', function () {
it('should generate valid file name', 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 () { describe('generateLogFilePath()', function () {
it('should generate valid file path', 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'))
}) })
}) })
}) })