From 67654fdf8540ebfc626c1185c128fce00479429c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 12:38:43 +0100 Subject: [PATCH] Add stripping of instance paths in Factorio log Add option defaulting to true to strip instance directory paths printed into the Factorio log down to be relative to the instance. --- CHANGELOG.md | 1 + docs/configuration.md | 20 ++++++++++++++++ packages/lib/config/definitions.js | 6 +++++ packages/lib/factorio/server.js | 38 +++++++++++++++++++++++++++++- packages/slave/slave.js | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18a79727..049f024a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Version 2.0.0 player data. - Added synchronization of in-game adminlist, banlist and whitelist to Clusterio core. Previously this was handled by the playerManager plugin. +- Added stripping of long paths in the Factorio server log. ### Changes diff --git a/docs/configuration.md b/docs/configuration.md index 71a76b60..0ab53847 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -280,6 +280,26 @@ here overrides the properties there. Defaults to {"tags":["clusterio"],"auto_pause":false}. +### factorio.strip_paths + +Strip down absolute paths in the server log going to files inside the +instance directory such that they are relative to the instance +directory. This improves the signal to noise ratio of the log. + +Sample output when disabled: + + 0.487 Loading map /srv/clusterio/instances/test/saves/world.zip: 2615888 bytes. + 0.614 Checksum for script /srv/clusterio/instances/test/temp/currently-playing/control.lua: 2390553941 + 1.277 Script @/srv/clusterio/instances/test/temp/currently-playing/modules/example/test.lua:7: Example log line. + +Sample output when enabled: + + 0.487 Loading map saves/world.zip: 2615888 bytes. + 0.614 Checksum for script temp/currently-playing/control.lua: 2390553941 + 1.277 Script @modules/example/test.lua:7: Example log line. + +Defaults to true + ### factorio.sync_adminlist Synchronize in-game admin list with admin status of the users in diff --git a/packages/lib/config/definitions.js b/packages/lib/config/definitions.js index c95c0fb3..87709a41 100644 --- a/packages/lib/config/definitions.js +++ b/packages/lib/config/definitions.js @@ -250,6 +250,12 @@ FactorioGroup.define({ "auto_pause": false, }, }); +FactorioGroup.define({ + name: "strip_paths", + description: "Strip down instance paths in the log", + type: "boolean", + initial_value: true, +}); FactorioGroup.define({ name: "sync_adminlist", description: "Synchronize adminlist with master server", diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index 2294b4f4..78b6b226 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -12,6 +12,22 @@ const rconClient = require("rcon-client"); const libErrors = require("@clusterio/lib/errors"); +/** + * Escapes text for inclusion in a RegExp + * + * Adds \ character in front of special meta characters in the passsed in + * text so that it can be embedded into a RegExp and only match the text. + * + * See https://stackoverflow.com/a/9310752 + * + * @param {string} text - Text to escape RegExp meta chars in. + * @returns {string} escaped text. + * @inner + */ +function escapeRegExp(text) { + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); +} + /** * Determines the version of Factorio the datadir is pointing to by * reading the changelog.txt in it. @@ -497,6 +513,7 @@ class FactorioServer extends events.EventEmitter { * @param {number} options.gamePort - UDP port to host game on. * @param {number} options.rconPort - TCP port to use for RCON. * @param {string} options.rconPassword - Password use for RCON. + * @param {boolean} options.stripPaths - Strip paths in the console. */ constructor(factorioDir, writeDir, options) { super(); @@ -523,6 +540,19 @@ class FactorioServer extends events.EventEmitter { this._rconReady = false; this._gameReady = false; + if (options.stripPaths) { + let chars = new Set(this.writePath("temp")); + chars.delete(":"); // Having a colon could lead to matching the line number + chars = [...chars].join(""); + + this._stripRegExp = new RegExp( + `(${escapeRegExp(this.writePath("temp", "currently-playing", path.sep))})|` + + `(${escapeRegExp(this.writePath(path.sep))})|` + + `(\\.\\.\\.[${chars}]*?currently-playing\\${path.sep})`, + "g" + ); + } + // Array of possible causes for an unexpected shutdown this._unexpected = []; @@ -606,7 +636,13 @@ class FactorioServer extends events.EventEmitter { this.emit(source, line); - let output = parseOutput(line.toString("utf-8"), source); + line = line.toString("utf-8"); + if (this._stripRegExp) { + line = line.replace(this._stripRegExp, ""); + } + + + let output = parseOutput(line, source); heuristicLoop: for (let heuristic of outputHeuristics) { for (let [name, expected] of Object.entries(heuristic.filter)) { if (!Object.hasOwnProperty.call(output, name)) { diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 37c13d7b..e97666ec 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -135,6 +135,7 @@ class Instance extends libLink.Link{ rconPort: this.config.get("factorio.rcon_port"), rconPassword: this.config.get("factorio.rcon_password"), enableWhitelist: this.config.get("factorio.enable_whitelist"), + stripPaths: this.config.get("factorio.strip_paths"), }; this._running = false;