diff --git a/MODULE.bazel b/MODULE.bazel index 754f0308..b6cd1023 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -16,6 +16,7 @@ module( ) # Bazel global rules +bazel_dep(name = "bazel_skylib", version = "1.9.0") bazel_dep(name = "rules_pkg", version = "1.1.0") bazel_dep(name = "rules_python", version = "1.8.5") bazel_dep(name = "rules_rust", version = "0.68.1-score") diff --git a/config/BUILD b/config/BUILD index 2057a774..ae6bb3ec 100644 --- a/config/BUILD +++ b/config/BUILD @@ -10,6 +10,8 @@ # # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") + config_setting( name = "x86_64-linux", define_values = { @@ -52,3 +54,17 @@ config_setting( name = "ub_sanitizer_enabled", define_values = {"sanitize": "undefined"}, ) + +bool_flag( + name = "use_cout_log", + build_setting_default = False, + visibility = ["//visibility:public"], +) + +config_setting( + name = "lm_use_cout_log", + flag_values = { + ":use_cout_log": "True", + }, + visibility = ["//visibility:public"], +) diff --git a/src/control_client_lib/BUILD b/src/control_client_lib/BUILD index d119de27..d4e304e0 100644 --- a/src/control_client_lib/BUILD +++ b/src/control_client_lib/BUILD @@ -32,6 +32,5 @@ cc_library( "//src/launch_manager_daemon/common:log", "//src/launch_manager_daemon/common:osal", "@score_baselibs//score/concurrency/future", - "@score_baselibs//score/mw/log", ], ) diff --git a/src/launch_manager_daemon/common/BUILD b/src/launch_manager_daemon/common/BUILD index 6a39acb4..c378f4ad 100644 --- a/src/launch_manager_daemon/common/BUILD +++ b/src/launch_manager_daemon/common/BUILD @@ -51,8 +51,16 @@ cc_library( cc_library( name = "log", hdrs = ["include/score/lcm/internal/log.hpp"], + defines = select({ + "//config:lm_use_cout_log": [], + "//conditions:default": ["LC_LOG_SCORE_MW_LOG"], + }), includes = ["include"], visibility = ["//src:__subpackages__"], + deps = [] + select({ + "//config:lm_use_cout_log": [], + "//conditions:default": ["@score_baselibs//score/mw/log"], + }), ) cc_library( diff --git a/src/launch_manager_daemon/common/include/score/lcm/identifier_hash.hpp b/src/launch_manager_daemon/common/include/score/lcm/identifier_hash.hpp index 8fe24d1f..ffd9d0d5 100644 --- a/src/launch_manager_daemon/common/include/score/lcm/identifier_hash.hpp +++ b/src/launch_manager_daemon/common/include/score/lcm/identifier_hash.hpp @@ -15,16 +15,13 @@ #define IDENTIFIER_HASH_H_ #include -#include #include +#include #include #include #include -namespace score -{ - -namespace lcm +namespace score::lcm { /// @file identifier_hash.hpp @@ -145,23 +142,47 @@ class IdentifierHash final /// @param os The output stream. /// @param id The IdentifierHash object to output. /// @return A reference to the output stream. -inline std::ostream& operator<<(std::ostream& os, const IdentifierHash& id) +inline std::ostream& operator<<(std::ostream& stream, const IdentifierHash& id_hash) noexcept(false) +{ + const auto& reg = IdentifierHash::get_registry(); + const auto it = reg.find(id_hash.data()); + if (it != reg.end()) + { + stream << it->second; + } + else + { + stream << ""; + } + return stream; +} + +} // namespace score::lcm + +#ifdef LC_LOG_SCORE_MW_LOG +#include "score/mw/log/logger.h" + +namespace score::lcm +{ + +inline score::mw::log::LogStream& operator<<(score::mw::log::LogStream& stream, + const IdentifierHash& id_hash) noexcept(false) { const auto& reg = IdentifierHash::get_registry(); - const auto it = reg.find(id.data()); + const auto it = reg.find(id_hash.data()); if (it != reg.end()) { - os << it->second; + stream << it->second; } else { - os << ""; + stream << ""; } - return os; + return stream; } -} // namespace lcm +} // namespace score::lcm -} // namespace score +#endif // LC_LOG_SCORE_MW_LOG #endif // IDENTIFIER_HASH_H_ diff --git a/src/launch_manager_daemon/common/include/score/lcm/internal/log.hpp b/src/launch_manager_daemon/common/include/score/lcm/internal/log.hpp index de02e145..df2f76ac 100644 --- a/src/launch_manager_daemon/common/include/score/lcm/internal/log.hpp +++ b/src/launch_manager_daemon/common/include/score/lcm/internal/log.hpp @@ -11,7 +11,6 @@ * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ - #ifndef LCM_LOG_HPP_INCLUDED #define LCM_LOG_HPP_INCLUDED @@ -23,43 +22,44 @@ #include "score/mw/log/logger.h" -namespace score { +namespace score +{ -namespace lcm { +namespace lcm +{ -namespace internal { +namespace internal +{ /// @brief Function to access global logging context, for Launch Manager. /// Launch Manager (LM) daemon, uses a single global logging context. /// This context is stored as a static variable inside this function and used all over LM daemon implementation. /// Please note that code should not call this function directly, but should use a set of wrapper macros. /// More information can be found in docs/architecture/concepts/logging/logging.rst file. -inline score::mw::log::Logger& _getLmLogger() noexcept { - // RULECHECKER_comment(1, 1, check_static_object_dynamic_initialization, "This is safe because the static is a function local.", true); +inline score::mw::log::Logger& _getLmLogger() noexcept +{ + // RULECHECKER_comment(1, 1, check_static_object_dynamic_initialization, "This is safe because the static is a + // function local.", true); static score::mw::log::Logger& log{score::mw::log::CreateLogger("LM", "Launch Manager logging context")}; return log; } -} // namespace lcm - } // namespace internal +} // namespace lcm + } // namespace score #else // LC_LOG_SCORE_MW_LOG // The only other solution supported is console logging. -#include -#include -#include #include +#include +#include #include -namespace score { - -namespace lcm { - -namespace internal { +namespace score::lcm::internal +{ enum class LogLevel { @@ -71,53 +71,62 @@ enum class LogLevel kVerbose = 5, }; -inline LogLevel GetLevelFromEnv() { - if (const char* levelStr = std::getenv("LC_STDOUT_LOG_LEVEL")) { +inline LogLevel GetLevelFromEnv() +{ + if (const char* levelStr = std::getenv("LC_STDOUT_LOG_LEVEL")) + { std::string_view levelSv{levelStr}; int logLevelTmp; - try { + try + { logLevelTmp = std::stoi(levelSv.data()); - }catch(...) { + } + catch (...) + { return LogLevel::kInfo; - } + } - if(logLevelTmp >= static_cast(LogLevel::kFatal) && logLevelTmp <= static_cast(LogLevel::kVerbose)) { + if (logLevelTmp >= static_cast(LogLevel::kFatal) && logLevelTmp <= static_cast(LogLevel::kVerbose)) + { return LogLevel(logLevelTmp); - } else { + } + else + { return LogLevel::kInfo; - } - - } else { + } + } + else + { return LogLevel::kInfo; } } -static LogLevel GetLevel() { +static LogLevel GetLevel() +{ const static LogLevel logLevel = GetLevelFromEnv(); return logLevel; } -inline std::ostream& operator<<(std::ostream& os, const std::tm* now ) { - std::cout << (now->tm_year + 1900) << '/' - << (now->tm_mon + 1) << '/' - << now->tm_mday << " " - << now->tm_hour << ":" - << now->tm_min << ":" - << now->tm_sec; +inline std::ostream& operator<<(std::ostream& os, const std::tm* now) +{ + std::cout << (now->tm_year + 1900) << '/' << (now->tm_mon + 1) << '/' << now->tm_mday << " " << now->tm_hour << ":" + << now->tm_min << ":" << now->tm_sec; return os; } class Stream { -public: + public: Stream() noexcept = default; - Stream(const Stream &) = delete; - Stream(Stream && other) noexcept { + Stream(const Stream&) = delete; + Stream(Stream&& other) noexcept + { print_ = other.print_; moved_ = true; }; - void SetPrint() { + void SetPrint() + { print_ = true; } @@ -125,7 +134,7 @@ class Stream Stream& operator<<(const T* value) noexcept { - if(print_) + if (print_) std::cout << " " << value; return *this; } @@ -133,17 +142,18 @@ class Stream template Stream& operator<<(const T& value) noexcept { - if(print_) + if (print_) std::cout << " " << value; return *this; } ~Stream() { - if(print_ && moved_) + if (print_ && moved_) std::cout << " ]" << reset_color_ << std::endl; } -private: + + private: bool print_{false}; bool moved_{false}; std::string_view reset_color_{"\033[0m"}; @@ -151,21 +161,22 @@ class Stream class Logger { -public: - Logger(std::string_view f_context, std::string_view f_description) : - ctxId_(f_context), ctxDescription_{f_description} { - + public: + Logger(std::string_view f_context, std::string_view f_description) + : ctxId_(f_context), ctxDescription_{f_description} + { } Stream LogFatal() noexcept { Stream stream; - if(GetLevel() >= LogLevel::kFatal) { + if (GetLevel() >= LogLevel::kFatal) + { stream.SetPrint(); std::time_t t = std::time(0); std::tm now; localtime_r(&t, &now); - stream << check_it_ << text_color_ << &now << appId_ << ctxId_ << "FATAL: ["; + stream << check_it_ << text_color_ << &now << appId_ << ctxId_ << "FATAL: ["; } return std::move(stream); } @@ -173,12 +184,13 @@ class Logger Stream LogError() noexcept { Stream stream; - if(GetLevel() >= LogLevel::kError) { + if (GetLevel() >= LogLevel::kError) + { stream.SetPrint(); std::time_t t = std::time(0); std::tm now; localtime_r(&t, &now); - stream << check_it_ << text_color_ << &now << appId_ << ctxId_ << "ERROR: ["; + stream << check_it_ << text_color_ << &now << appId_ << ctxId_ << "ERROR: ["; } return std::move(stream); @@ -187,12 +199,13 @@ class Logger Stream LogWarn() noexcept { Stream stream; - if(GetLevel() >= LogLevel::kWarn) { + if (GetLevel() >= LogLevel::kWarn) + { stream.SetPrint(); std::time_t t = std::time(0); std::tm now; localtime_r(&t, &now); - stream << check_it_ << text_color_ << &now << appId_ << ctxId_ << "WARNING: ["; + stream << check_it_ << text_color_ << &now << appId_ << ctxId_ << "WARNING: ["; } return std::move(stream); } @@ -200,12 +213,13 @@ class Logger Stream LogInfo() noexcept { Stream stream; - if(GetLevel() >= LogLevel::kInfo) { + if (GetLevel() >= LogLevel::kInfo) + { stream.SetPrint(); std::time_t t = std::time(0); std::tm now; localtime_r(&t, &now); - stream << text_color_ << &now << appId_ << ctxId_ << "INFO: ["; + stream << text_color_ << &now << appId_ << ctxId_ << "INFO: ["; } return std::move(stream); } @@ -213,12 +227,13 @@ class Logger Stream LogDebug() noexcept { Stream stream; - if(GetLevel() >= LogLevel::kDebug) { + if (GetLevel() >= LogLevel::kDebug) + { stream.SetPrint(); std::time_t t = std::time(0); std::tm now; localtime_r(&t, &now); - stream << text_color_ << &now << appId_ << ctxId_ << "DEBUG: ["; + stream << text_color_ << &now << appId_ << ctxId_ << "DEBUG: ["; } return std::move(stream); } @@ -226,16 +241,18 @@ class Logger Stream LogVerbose() noexcept { Stream stream; - if(GetLevel() >= LogLevel::kVerbose) { + if (GetLevel() >= LogLevel::kVerbose) + { stream.SetPrint(); std::time_t t = std::time(0); std::tm now; localtime_r(&t, &now); - stream << text_color_ << &now << appId_ << ctxId_ << "VERBOSE: ["; + stream << text_color_ << &now << appId_ << ctxId_ << "VERBOSE: ["; } return std::move(stream); } -private: + + private: const std::string_view appId_{"LCLM"}; const std::string_view ctxId_{"####"}; const std::string_view ctxDescription_{"####"}; @@ -243,17 +260,17 @@ class Logger const std::string_view check_it_{"\033[101;30m !!! -> \033[0m"}; }; -inline Logger& _getLmLogger() noexcept { - // RULECHECKER_comment(1, 1, check_static_object_dynamic_initialization, "This is safe because the static is a function local.", true); +inline Logger& _getLmLogger() noexcept +{ + // RULECHECKER_comment(1, 1, check_static_object_dynamic_initialization, "This is safe because the static is a + // function local.", true); static Logger log{"LCLM", "Launch Manager logging context"}; return log; } -} // namespace lcm - -} // namespace internal +} // namespace score::lcm::internal -} // namespace score +// namespace internal #endif // LC_LOG_SCORE_MW_LOG diff --git a/src/launch_manager_daemon/common/src/internal/controlclientchannel.cpp b/src/launch_manager_daemon/common/src/internal/controlclientchannel.cpp index 3536aa81..d9536f3e 100644 --- a/src/launch_manager_daemon/common/src/internal/controlclientchannel.cpp +++ b/src/launch_manager_daemon/common/src/internal/controlclientchannel.cpp @@ -171,8 +171,6 @@ ControlClientChannelP ControlClientChannel::initializeControlClientChannel(int f std::next(static_cast(channelMemory), static_cast(sizeof(osal::IpcCommsSync))); result = ControlClientChannelP(static_cast(static_cast(controlClientStartPtr)), [](ControlClientChannel*) {}); - LM_LOG_DEBUG() << "ControlClientChannel mapped (creation path: " << std::boolalpha << (mem_ptr != nullptr) << ")"; - if (result) { std::unique_lock lock(init_mutex_); diff --git a/src/launch_manager_daemon/health_monitor_lib/BUILD b/src/launch_manager_daemon/health_monitor_lib/BUILD index ecafe216..e5ac4dda 100644 --- a/src/launch_manager_daemon/health_monitor_lib/BUILD +++ b/src/launch_manager_daemon/health_monitor_lib/BUILD @@ -42,6 +42,10 @@ cc_library_with_common_opts( name = "phm_logging", srcs = ["src/score/lcm/saf/logging/PhmLogger.cpp"], hdrs = ["src/score/lcm/saf/logging/PhmLogger.hpp"], + defines = select({ + "//config:lm_use_cout_log": [], + "//conditions:default": ["LC_LOG_SCORE_MW_LOG"], + }), includes = [ "src", "src/score/lcm/saf/logging", diff --git a/src/launch_manager_daemon/health_monitor_lib/src/score/lcm/saf/daemon/SwClusterHandler.cpp b/src/launch_manager_daemon/health_monitor_lib/src/score/lcm/saf/daemon/SwClusterHandler.cpp index 8ed7de8e..d1282643 100644 --- a/src/launch_manager_daemon/health_monitor_lib/src/score/lcm/saf/daemon/SwClusterHandler.cpp +++ b/src/launch_manager_daemon/health_monitor_lib/src/score/lcm/saf/daemon/SwClusterHandler.cpp @@ -61,7 +61,7 @@ bool SwClusterHandler::constructWorkers(std::shared_ptr #include "tests/utils/test_helper/test_helper.hpp" -score::lcm::ControlClient client; TEST(Smoke, Daemon) { + + score::lcm::ControlClient client {}; + TEST_STEP("Control daemon report kRunning") { // report kRunning auto result = score::lcm::LifecycleClient{}.ReportExecutionState(score::lcm::ExecutionState::kRunning);