2017-08-24 09:15:06 +00:00
|
|
|
var async = require('async')
|
|
|
|
var fs = require('fs.extra')
|
2017-10-01 15:24:06 +00:00
|
|
|
var filesize = require('filesize')
|
2017-08-24 09:15:06 +00:00
|
|
|
var path = require('path')
|
|
|
|
var userhome = require('userhome')
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-10-03 22:50:07 +00:00
|
|
|
var gamesLogFolder = {
|
|
|
|
arma1: 'ArmA',
|
|
|
|
arma2: 'ArmA 2',
|
|
|
|
arma2oa: 'ArmA 2 OA',
|
|
|
|
arma3: 'Arma 3',
|
|
|
|
arma3_x64: 'Arma 3'
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:47:48 +00:00
|
|
|
var numberOfLogsToKeep = 20
|
|
|
|
|
2015-10-31 14:27:16 +00:00
|
|
|
var Logs = function (config) {
|
2017-08-24 09:15:06 +00:00
|
|
|
this.config = config
|
2015-10-31 14:27:16 +00:00
|
|
|
|
|
|
|
if (this.config.type === 'linux') {
|
2017-08-24 09:15:06 +00:00
|
|
|
fs.mkdirp(this.logsPath())
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2021-07-10 12:04:14 +00:00
|
|
|
Logs.generateLogFileName = function (prefix, suffix) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var dateStr = new Date().toISOString()
|
|
|
|
.replace(/:/g, '-') // Replace time dividers with dash
|
|
|
|
.replace(/T/, '_') // Remove date and time divider
|
|
|
|
.replace(/\..+/, '') // Remove milliseconds
|
2021-07-10 12:04:14 +00:00
|
|
|
return prefix + '_' + dateStr + '_' + suffix + '.rpt'
|
2017-08-24 09:15:06 +00:00
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-10-03 23:04:09 +00:00
|
|
|
Logs.prototype.delete = function (filename, callback) {
|
2021-02-28 13:47:48 +00:00
|
|
|
callback = callback || function () {}
|
|
|
|
|
2017-10-03 23:04:09 +00:00
|
|
|
this.getLogFile(filename, function (err, logFile) {
|
|
|
|
if (err) {
|
2021-02-28 13:47:48 +00:00
|
|
|
return callback(err)
|
2017-10-03 23:04:09 +00:00
|
|
|
} else {
|
|
|
|
if (logFile && logFile.path) {
|
|
|
|
fs.unlink(logFile.path, callback)
|
|
|
|
} else {
|
2021-02-28 13:47:48 +00:00
|
|
|
return callback(new Error('File not found'))
|
2017-10-03 23:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-10 12:04:14 +00:00
|
|
|
Logs.prototype.generateLogFilePath = function (prefix, suffix) {
|
|
|
|
return path.join(this.logsPath(), Logs.generateLogFileName(prefix, suffix))
|
2017-08-24 09:15:06 +00:00
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
|
|
|
|
Logs.prototype.logsPath = function () {
|
|
|
|
if (this.config.type === 'linux') {
|
2017-08-24 09:15:06 +00:00
|
|
|
return path.join(this.config.path, 'logs')
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 22:50:07 +00:00
|
|
|
var gameLogFolder = gamesLogFolder[this.config.game]
|
|
|
|
|
|
|
|
if (!gameLogFolder) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:27:16 +00:00
|
|
|
if (this.config.type === 'windows') {
|
2017-10-03 22:50:07 +00:00
|
|
|
return userhome('AppData', 'Local', gameLogFolder)
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.type === 'wine') {
|
2017-08-24 09:15:06 +00:00
|
|
|
var username = process.env.USER
|
2017-10-03 22:50:07 +00:00
|
|
|
return userhome('.wine', 'drive_c', 'users', username, 'Local Settings', 'Application Data', gameLogFolder)
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
return null
|
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
|
|
|
|
Logs.prototype.logFiles = function (callback) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var directory = this.logsPath()
|
2015-10-31 14:27:16 +00:00
|
|
|
|
|
|
|
if (directory === null) {
|
2017-10-03 22:50:07 +00:00
|
|
|
return callback(null, [])
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.readdir(directory, function (err, files) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
callback(err)
|
|
|
|
return
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 13:41:12 +00:00
|
|
|
files = files.filter(function (file) {
|
|
|
|
return file.endsWith('.rpt')
|
|
|
|
}).map(function (file) {
|
2015-10-31 14:27:16 +00:00
|
|
|
return {
|
|
|
|
name: file,
|
2017-08-24 09:15:06 +00:00
|
|
|
path: path.join(directory, file)
|
|
|
|
}
|
|
|
|
})
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
async.filter(files, function (file, cb) {
|
2015-10-31 14:27:16 +00:00
|
|
|
fs.stat(file.path, function (err, stat) {
|
2022-05-30 20:48:43 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:30:16 +00:00
|
|
|
file.created = stat.birthtime.toISOString()
|
|
|
|
file.modified = stat.mtime.toISOString()
|
2017-10-01 15:24:06 +00:00
|
|
|
file.formattedSize = filesize(stat.size)
|
2017-08-24 09:15:06 +00:00
|
|
|
file.size = stat.size
|
2022-05-30 20:48:43 +00:00
|
|
|
cb(null, stat.isFile())
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
2022-05-30 20:48:43 +00:00
|
|
|
}, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:27:16 +00:00
|
|
|
files.sort(function (a, b) {
|
2021-02-28 13:30:16 +00:00
|
|
|
return b.created.localeCompare(a.created) // Descending order
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
callback(null, files)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
|
|
|
|
Logs.prototype.getLogFile = function (filename, callback) {
|
2015-11-21 18:02:46 +00:00
|
|
|
this.logFiles(function (err, files) {
|
2015-10-31 14:27:16 +00:00
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
callback(err)
|
2015-10-31 14:27:16 +00:00
|
|
|
} else {
|
|
|
|
var validLogs = files.filter(function (file) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return file.name === filename
|
|
|
|
})
|
2015-10-31 14:27:16 +00:00
|
|
|
|
|
|
|
if (validLogs.length > 0) {
|
2017-08-24 09:15:06 +00:00
|
|
|
callback(null, validLogs[0])
|
2015-10-31 14:27:16 +00:00
|
|
|
} else {
|
2017-08-24 09:15:06 +00:00
|
|
|
callback(null, null)
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-10-01 15:46:43 +00:00
|
|
|
Logs.prototype.readLogFile = function (filename, callback) {
|
|
|
|
fs.readFile(filename, callback)
|
|
|
|
}
|
|
|
|
|
2021-07-10 12:04:14 +00:00
|
|
|
Logs.prototype.logServerProcess = function (serverProcess, prefix, suffix) {
|
2020-12-13 13:48:58 +00:00
|
|
|
if (this.config.type !== 'linux') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-10 12:04:14 +00:00
|
|
|
var logStream = fs.createWriteStream(this.generateLogFilePath(prefix, suffix), {
|
2020-12-13 13:48:58 +00:00
|
|
|
flags: 'a'
|
|
|
|
})
|
|
|
|
|
|
|
|
serverProcess.stdout.on('data', function (data) {
|
|
|
|
if (logStream) {
|
|
|
|
logStream.write(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
serverProcess.stderr.on('data', function (data) {
|
|
|
|
if (logStream) {
|
|
|
|
logStream.write(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
serverProcess.on('close', function (code) {
|
|
|
|
if (logStream) {
|
|
|
|
logStream.end()
|
|
|
|
logStream = undefined
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
serverProcess.on('error', function (err) {
|
2022-01-23 19:47:57 +00:00
|
|
|
if (logStream && err) {
|
|
|
|
logStream.write(err.toString())
|
2020-12-13 13:48:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:47:48 +00:00
|
|
|
Logs.prototype.cleanupOldLogFiles = function () {
|
2021-07-10 13:10:17 +00:00
|
|
|
if (this.config.type !== 'linux') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:47:48 +00:00
|
|
|
var self = this
|
|
|
|
|
|
|
|
self.logFiles(function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldLogFiles = files.slice(numberOfLogsToKeep)
|
|
|
|
oldLogFiles.forEach(function (logFile) {
|
|
|
|
self.delete(logFile.name)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
module.exports = Logs
|