From 946550863761062b90f9bc192a59d7f4e480780b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Sun, 13 Dec 2020 14:48:58 +0100 Subject: [PATCH] Create log files for both server and headless clients on Linux --- lib/logs.js | 51 ++++++++++++++++++++++++++++++++++++++++---- lib/server.js | 55 ++---------------------------------------------- test/lib/logs.js | 4 ++-- 3 files changed, 51 insertions(+), 59 deletions(-) diff --git a/lib/logs.js b/lib/logs.js index 85a97a1..69ee870 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -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 diff --git a/lib/server.js b/lib/server.js index 65f3916..6fd3ae7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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 }) diff --git a/test/lib/logs.js b/test/lib/logs.js index d25f1ff..3f8c88d 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().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')) }) }) })