3030
3131#include " spdlog/spdlog.h"
3232#include " spdlog/sinks/basic_file_sink.h"
33+ #include " spdlog/sinks/syslog_sink.h"
3334
3435#include " rcl_logging_interface/rcl_logging_interface.h"
3536
@@ -89,6 +90,40 @@ get_should_use_old_flushing_behavior()
8990 }
9091}
9192
93+ RCL_LOGGING_INTERFACE_LOCAL
94+ bool
95+ get_should_use_syslog ()
96+ {
97+ const char * env_var_name = " RCL_LOGGING_SPDLOG_ENABLE_SYSLOG" ;
98+
99+ try {
100+ std::string env_var_value = rcpputils::get_env_var (env_var_name);
101+
102+ if (env_var_value.empty ()) {
103+ // not set
104+ return false ;
105+ }
106+ if (" 0" == env_var_value) {
107+ // explicitly false
108+ return false ;
109+ }
110+ if (" 1" == env_var_value) {
111+ // explicitly true
112+ return true ;
113+ }
114+
115+ // unknown value
116+ throw std::runtime_error (" unrecognized value: " + env_var_value);
117+ } catch (const std::runtime_error & error) {
118+ throw std::runtime_error (
119+ std::string (" failed to get env var '" ) + env_var_name + " ': " + error.what ()
120+ );
121+ }
122+ }
123+
124+
125+
126+
92127} // namespace
93128
94129rcl_logging_ret_t rcl_logging_external_initialize (
@@ -173,9 +208,24 @@ rcl_logging_ret_t rcl_logging_external_initialize(
173208 RCUTILS_SET_ERROR_MSG (" Failed to create log file name string" );
174209 return RCL_LOGGING_RET_ERROR;
175210 }
211+ std::vector<spdlog::sink_ptr> sinks;
212+ auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(name_buffer, false );
213+ sinks.push_back (sink);
176214
177- auto sink = std::make_unique<spdlog::sinks::basic_file_sink_mt>(name_buffer, false );
178- g_root_logger = std::make_shared<spdlog::logger>(" root" , std::move (sink));
215+ bool should_use_syslog = false ;
216+ try {
217+ should_use_syslog = ::get_should_use_syslog ();
218+ } catch (const std::runtime_error & error) {
219+ RCUTILS_SET_ERROR_MSG (error.what ());
220+ return RCL_LOGGING_RET_ERROR;
221+ }
222+
223+ if (should_use_syslog) {
224+ auto syslog_sink = std::make_shared<spdlog::sinks::syslog_sink_mt>(" " , LOG_PID,LOG_LOCAL1, true );
225+ sinks.push_back (syslog_sink);
226+ }
227+
228+ g_root_logger = std::make_shared<spdlog::logger>(" root" , sinks.begin (), sinks.end ());
179229 if (!should_use_old_flushing_behavior) {
180230 // in this case we should do the new thing (until config files are supported)
181231 // which is to configure the logger to flush periodically and on
@@ -186,6 +236,9 @@ rcl_logging_ret_t rcl_logging_external_initialize(
186236 // the old behavior is to not configure the sink at all, so do nothing
187237 }
188238
239+
240+
241+
189242 spdlog::register_logger (g_root_logger);
190243
191244 g_root_logger->set_pattern (" %v" );
0 commit comments