From efe391725a02134e022ee1c0958aa778bbbfb522 Mon Sep 17 00:00:00 2001 From: khromenokroman Date: Sat, 24 Aug 2024 22:58:11 +0200 Subject: [PATCH] iox-#2330 Refactor environment variable retrieval Extract environment variable retrieval logic into a static method. This refactoring improves code readability and reuse, ensuring cleaner and more maintainable code. --- .../iceoryx_posh/internal/roudi/roudi.hpp | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp b/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp index fe6a2d1bd1..5ac146cc39 100644 --- a/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp +++ b/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp @@ -94,23 +94,18 @@ class SystemdServiceHandler final : public ISystemd m_shutdown.store(true); } - void processNotify() final - { - /* - * We get information about how they are running. If as a unit, then we launch - * watchdog and send a notification about the launch, otherwise we do nothing - */ - iox::string invocationIdStr; - auto const* const ENV_VAR = "INVOCATION_ID"; - invocationIdStr.unsafe_raw_access([&](auto* buffer, auto const info) { + static std::string getEnvironmentVariable(const char* const env_var) { + iox::string str; + + str.unsafe_raw_access([&](auto* buffer, auto const info) { size_t actualSizeWithNull{0}; - auto result = IOX_POSIX_CALL(iox_getenv_s)(&actualSizeWithNull, buffer, info.total_size, ENV_VAR) + auto result = IOX_POSIX_CALL(iox_getenv_s)(&actualSizeWithNull, buffer, info.total_size, env_var) .failureReturnValue(-1) .evaluate(); if (result.has_error() && result.error().errnum == ERANGE) { - IOX_LOG(ERROR, "Invalid value for 'INVOCATION_ID' environment variable!"); + IOX_LOG(ERROR, "Invalid value for '" + std::string(env_var) + "' environment variable!"); } size_t actual_size{0}; @@ -123,6 +118,13 @@ class SystemdServiceHandler final : public ISystemd return actual_size; }); + return std::string(str.c_str()); + } + + void processNotify() final + { + std::string invocationIdStr = getEnvironmentVariable("INVOCATION_ID"); + if (!invocationIdStr.empty()) { IOX_LOG(WARN, "Run APP in unit(systemd)"); @@ -141,11 +143,9 @@ class SystemdServiceHandler final : public ISystemd + resultReady.get_error().getHumanReadableErrnum()); return; } - IOX_LOG(DEBUG, "WatchDog READY=1"); + IOX_LOG(DEBUG, "Notify READY=1 successful"); IOX_LOG(INFO, "Start watchdog"); - IOX_LOG(INFO, "std::condition_variable: " << sizeof(std::condition_variable)); - IOX_LOG(INFO, "std::mutex: " << sizeof(std::mutex)); while (!m_shutdown.load()) { std::unique_lock lock(watchdogMutex);