From 4c02d278f1b3eac7d9f82cefa2744d1afc7f3aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C3=B6rschig?= Date: Fri, 11 Oct 2024 13:30:41 +0200 Subject: [PATCH] SILKIT-1612 log: add participant name to file names (#132) Ensure that filenames are (more) unique, just in case multiple log files are opened in quick succession. --- SilKit/source/core/vasio/VAsioConnection.cpp | 24 ++------------------ SilKit/source/services/logging/Logger.cpp | 4 ++-- SilKit/source/util/StringHelpers.cpp | 20 ++++++++++++++++ SilKit/source/util/StringHelpers.hpp | 2 ++ docs/configuration/logging-configuration.rst | 2 +- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/SilKit/source/core/vasio/VAsioConnection.cpp b/SilKit/source/core/vasio/VAsioConnection.cpp index 74ec20e3e..7e9767fa9 100644 --- a/SilKit/source/core/vasio/VAsioConnection.cpp +++ b/SilKit/source/core/vasio/VAsioConnection.cpp @@ -41,6 +41,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "Uri.hpp" #include "Assert.hpp" #include "TransformAcceptorUris.hpp" +#include "StringHelpers.hpp" #include "ConnectPeer.hpp" #include "util/TracingMacros.hpp" @@ -63,27 +64,6 @@ namespace fs = SilKit::Filesystem; namespace { -auto printableName(const std::string& participantName) -> std::string -{ - std::string safeName; - for (const auto& ch : participantName) - { - // do not use std::isalnum, as it may sensitive to the current locale - const bool isAlphaNumeric{('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') - || (ch == '_' || ch == '-' || ch == '.' || ch == '~')}; - - if (isAlphaNumeric) - { - safeName.push_back(ch); - } - else - { - safeName += fmt::format("{:02X}", static_cast(ch)); - } - } - return safeName; -} - //Debug print of given peer infos auto printUris(const std::vector& uris) @@ -99,7 +79,7 @@ auto makeLocalEndpoint(const std::string& participantName, const SilKit::Core::P { asio::local::stream_protocol::endpoint result; // Ensure the participantName is in a useful encoding - const auto safe_name = printableName(participantName); + const auto safe_name = SilKit::Util::PrintableString(participantName); const auto bounded_name = safe_name.substr(0, std::min(safe_name.size(), 10)); // We hash the participant name, ID and the current working directory diff --git a/SilKit/source/services/logging/Logger.cpp b/SilKit/source/services/logging/Logger.cpp index 5d78d0ea8..ae03835d3 100644 --- a/SilKit/source/services/logging/Logger.cpp +++ b/SilKit/source/services/logging/Logger.cpp @@ -330,7 +330,7 @@ Logger::Logger(const std::string& participantName, Config::Logging config) { if (sink.format == Config::Sink::Format::Json) { - auto filename = fmt::format("{}_{}.jsonl", sink.logName, logFileTimestamp); + auto filename = fmt::format("{}_{}_{}.jsonl", sink.logName, SilKit::Util::PrintableString(participantName), logFileTimestamp); auto fileSink = std::make_shared(filename); using spdlog::details::make_unique; // for pre c++14 auto formatter = make_unique(); @@ -342,7 +342,7 @@ Logger::Logger(const std::string& participantName, Config::Logging config) } else { - auto filename = fmt::format("{}_{}.txt", sink.logName, logFileTimestamp); + auto filename = fmt::format("{}_{}_{}.txt", sink.logName, SilKit::Util::PrintableString(participantName), logFileTimestamp); auto fileSink = std::make_shared(filename); fileSink->set_level(log_level); _loggerSimple->sinks().push_back(fileSink); diff --git a/SilKit/source/util/StringHelpers.cpp b/SilKit/source/util/StringHelpers.cpp index 4e7e68d79..d69042690 100644 --- a/SilKit/source/util/StringHelpers.cpp +++ b/SilKit/source/util/StringHelpers.cpp @@ -119,6 +119,26 @@ auto LowerCase(std::string input) -> std::string return input; } +auto PrintableString(const std::string& participantName) -> std::string +{ + std::string safeName; + for (const auto& ch : participantName) + { + // do not use std::isalnum, as it may sensitive to the current locale + const bool isAlphaNumeric{('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') + || (ch == '_' || ch == '-' || ch == '.' || ch == '~')}; + + if (isAlphaNumeric) + { + safeName.push_back(ch); + } + else + { + safeName += fmt::format("{:02X}", static_cast(ch)); + } + } + return safeName; +} } // namespace Util } // namespace SilKit diff --git a/SilKit/source/util/StringHelpers.hpp b/SilKit/source/util/StringHelpers.hpp index 2a0d6eebf..a54b23cf3 100644 --- a/SilKit/source/util/StringHelpers.hpp +++ b/SilKit/source/util/StringHelpers.hpp @@ -41,6 +41,8 @@ auto CurrentTimestampString() -> std::string; auto LowerCase(std::string input) -> std::string; +auto PrintableString(const std::string& input) -> std::string; + } // namespace Util } // namespace SilKit diff --git a/docs/configuration/logging-configuration.rst b/docs/configuration/logging-configuration.rst index c6b5d3b7d..4a7701fb8 100644 --- a/docs/configuration/logging-configuration.rst +++ b/docs/configuration/logging-configuration.rst @@ -74,4 +74,4 @@ logs to a file, the following configuration could be used: *Error*, *Warn*, *Info*, *Debug*, *Trace*, and *Off*. * - LogName - The filename used by sinks of type *File*. The - resulting filename is ``_.txt``. + resulting filename is ``__.txt``.