Create log files for both server and headless clients on Linux

This commit is contained in:
Björn Dahlgren 2020-12-13 14:48:58 +01:00
parent 977f24c451
commit 9465508637
3 changed files with 51 additions and 59 deletions

View File

@ -20,12 +20,12 @@ var Logs = function (config) {
}
}
Logs.generateLogFileName = function () {
Logs.generateLogFileName = function (suffix) {
var dateStr = new Date().toISOString()
.replace(/:/g, '-') // Replace time dividers with dash
.replace(/T/, '_') // Remove date and time divider
.replace(/\..+/, '') // Remove milliseconds
return 'arma3server_' + dateStr + '.log'
return 'arma3server_' + dateStr + '_' + suffix + '.log'
}
Logs.prototype.delete = function (filename, callback) {
@ -46,8 +46,8 @@ Logs.prototype.delete = function (filename, callback) {
})
}
Logs.prototype.generateLogFilePath = function () {
return path.join(this.logsPath(), Logs.generateLogFileName())
Logs.prototype.generateLogFilePath = function (suffix) {
return path.join(this.logsPath(), Logs.generateLogFileName(suffix))
}
Logs.prototype.logsPath = function () {
@ -131,4 +131,47 @@ Logs.prototype.readLogFile = function (filename, callback) {
fs.readFile(filename, callback)
}
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)
}
})
}
module.exports = Logs

View File

@ -1,6 +1,5 @@
var _ = require('lodash')
var events = require('events')
var fs = require('fs')
var Gamedig = require('gamedig')
var slugify = require('slugify')
@ -166,30 +165,7 @@ Server.prototype.start = function () {
var instance = server.start()
var self = this
var logStream = null
if (this.config.type === 'linux') {
logStream = fs.createWriteStream(this.logs.generateLogFilePath(), {
flags: 'a'
})
}
instance.stdout.on('data', function (data) {
if (logStream) {
logStream.write(data)
}
})
instance.stderr.on('data', function (data) {
if (logStream) {
logStream.write(data)
}
})
instance.on('close', function (code) {
if (logStream) {
logStream.end()
}
clearInterval(self.queryStatusInterval)
self.state = null
self.pid = null
@ -200,10 +176,6 @@ Server.prototype.start = function () {
self.emit('state')
})
instance.on('error', function (err) {
console.log(err)
})
this.pid = instance.pid
this.instance = instance
this.queryStatusInterval = setInterval(function () {
@ -212,6 +184,8 @@ Server.prototype.start = function () {
this.startHeadlessClients()
this.logs.logServerProcesses(this.instance, this.headlessClientInstances)
this.emit('state')
return this
@ -233,31 +207,6 @@ Server.prototype.startHeadlessClients = function () {
port: self.port
})
var headlessInstance = headless.start()
var name = 'HC_' + i
var logPrefix = self.id + ' ' + name
console.log(logPrefix + ' starting')
headlessInstance.stdout.on('data', function (data) {
console.log(logPrefix + ' stdout: ' + data)
})
headlessInstance.stderr.on('data', function (data) {
console.log(logPrefix + ' stderr: ' + data)
})
headlessInstance.on('close', function (code) {
console.log(logPrefix + ' exited: ' + code)
var elementIndex = headlessClientInstances.indexOf(headlessInstance)
if (elementIndex !== -1) {
headlessClientInstances.splice(elementIndex, 1)
}
})
headlessInstance.on('error', function (err) {
console.log(logPrefix + ' error: ' + err)
})
return headlessInstance
})

View File

@ -19,13 +19,13 @@ describe('Logs', function () {
describe('generateLogFileName()', function () {
it('should generate valid file name', function () {
Logs.generateLogFileName().should.eql('arma3server_2015-10-21_19-28-32.log')
Logs.generateLogFileName('test').should.eql('arma3server_2015-10-21_19-28-32_test.log')
})
})
describe('generateLogFilePath()', function () {
it('should generate valid file path', function () {
logs.generateLogFilePath().should.eql(path.join('/tmp', 'logs', 'arma3server_2015-10-21_19-28-32.log'))
logs.generateLogFilePath('test').should.eql(path.join('/tmp', 'logs', 'arma3server_2015-10-21_19-28-32_test.log'))
})
})
})