From efa6e48a377cab32e68dabcee2a1f100d1ccb432 Mon Sep 17 00:00:00 2001 From: Lennart Nachtigall Date: Tue, 10 Oct 2023 16:06:30 +0200 Subject: [PATCH] Wired in syslog into spdlog --- rcl_logging_spdlog/src/rcl_logging_spdlog.cpp | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp b/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp index 9369c5c..7905f67 100644 --- a/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp +++ b/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp @@ -30,6 +30,7 @@ #include "spdlog/spdlog.h" #include "spdlog/sinks/basic_file_sink.h" +#include "spdlog/sinks/syslog_sink.h" #include "rcl_logging_interface/rcl_logging_interface.h" @@ -89,6 +90,40 @@ get_should_use_old_flushing_behavior() } } +RCL_LOGGING_INTERFACE_LOCAL +bool +get_should_use_syslog() +{ + const char * env_var_name = "RCL_LOGGING_SPDLOG_ENABLE_SYSLOG"; + + try { + std::string env_var_value = rcpputils::get_env_var(env_var_name); + + if (env_var_value.empty()) { + // not set + return false; + } + if ("0" == env_var_value) { + // explicitly false + return false; + } + if ("1" == env_var_value) { + // explicitly true + return true; + } + + // unknown value + throw std::runtime_error("unrecognized value: " + env_var_value); + } catch (const std::runtime_error & error) { + throw std::runtime_error( + std::string("failed to get env var '") + env_var_name + "': " + error.what() + ); + } +} + + + + } // namespace rcl_logging_ret_t rcl_logging_external_initialize( @@ -173,9 +208,24 @@ rcl_logging_ret_t rcl_logging_external_initialize( RCUTILS_SET_ERROR_MSG("Failed to create log file name string"); return RCL_LOGGING_RET_ERROR; } + std::vector sinks; + auto sink = std::make_shared(name_buffer, false); + sinks.push_back(sink); - auto sink = std::make_unique(name_buffer, false); - g_root_logger = std::make_shared("root", std::move(sink)); + bool should_use_syslog = false; + try { + should_use_syslog = ::get_should_use_syslog(); + } catch (const std::runtime_error & error) { + RCUTILS_SET_ERROR_MSG(error.what()); + return RCL_LOGGING_RET_ERROR; + } + + if(should_use_syslog) { + auto syslog_sink = std::make_shared("", LOG_PID,LOG_LOCAL1, true); + sinks.push_back(syslog_sink); + } + + g_root_logger = std::make_shared("root", sinks.begin(), sinks.end()); if (!should_use_old_flushing_behavior) { // in this case we should do the new thing (until config files are supported) // which is to configure the logger to flush periodically and on @@ -186,6 +236,9 @@ rcl_logging_ret_t rcl_logging_external_initialize( // the old behavior is to not configure the sink at all, so do nothing } + + + spdlog::register_logger(g_root_logger); g_root_logger->set_pattern("%v");