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'
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-13 13:48:58 +00:00
|
|
|
Logs.generateLogFileName = function (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
|
2020-12-13 13:48:58 +00:00
|
|
|
return 'arma3server_' + dateStr + '_' + suffix + '.log'
|
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) {
|
|
|
|
this.getLogFile(filename, function (err, logFile) {
|
|
|
|
if (err) {
|
|
|
|
if (callback) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (logFile && logFile.path) {
|
|
|
|
fs.unlink(logFile.path, callback)
|
|
|
|
} else {
|
|
|
|
if (callback) {
|
|
|
|
return callback(new Error('File not found'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:48:58 +00:00
|
|
|
Logs.prototype.generateLogFilePath = function (suffix) {
|
|
|
|
return path.join(this.logsPath(), Logs.generateLogFileName(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
|
|
|
}
|
|
|
|
|
|
|
|
files = files.map(function (file) {
|
|
|
|
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) {
|
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
|
|
|
|
cb(!err && stat.isFile())
|
|
|
|
})
|
2015-10-31 14:27:16 +00:00
|
|
|
}, function (files) {
|
|
|
|
files.sort(function (a, b) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return a.name.toLowerCase().localeCompare(b.name.toLowerCase())
|
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:48:58 +00:00
|
|
|
Logs.prototype.logServerProcesses = function (serverProcess, headlessClientProcesses) {
|
|
|
|
var self = this
|
|
|
|
this.logServerProcess(serverProcess, 'server')
|
|
|
|
headlessClientProcesses.forEach(function (headlessClientProcess, idx) {
|
|
|
|
self.logServerProcess(headlessClientProcess, 'hc_' + (idx + 1))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Logs.prototype.logServerProcess = function (serverProcess, suffix) {
|
|
|
|
if (this.config.type !== 'linux') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var logStream = fs.createWriteStream(this.generateLogFilePath(suffix), {
|
|
|
|
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) {
|
|
|
|
if (logStream) {
|
|
|
|
logStream.write(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
module.exports = Logs
|