25 #include "./arguments_impl.h"
27 #include "rcl/error_handling.h"
30 #include "rcl/macros.h"
31 #include "rcl_logging_interface/rcl_logging_interface.h"
32 #include "rcutils/logging.h"
33 #include "rcutils/time.h"
35 #define RCL_LOGGING_MAX_OUTPUT_FUNCS (4)
37 static rcutils_logging_output_handler_t
38 g_rcl_logging_out_handlers[RCL_LOGGING_MAX_OUTPUT_FUNCS] = {0};
40 static uint8_t g_rcl_logging_num_out_handlers = 0;
42 static bool g_rcl_logging_stdout_enabled =
false;
43 static bool g_rcl_logging_rosout_enabled =
false;
44 static bool g_rcl_logging_ext_lib_enabled =
false;
51 rcl_logging_ext_lib_output_handler(
52 const rcutils_log_location_t * location,
53 int severity,
const char * name, rcutils_time_point_value_t timestamp,
54 const char * format, va_list * args);
65 RCUTILS_LOGGING_AUTOINIT_WITH_ALLOCATOR(*allocator);
66 g_logging_allocator = *allocator;
67 int default_level = -1;
75 g_rcl_logging_num_out_handlers = 0;
80 rcutils_logging_set_default_logger_level(default_level);
84 rcutils_ret_t rcutils_status = rcutils_logging_set_logger_level(
87 if (RCUTILS_RET_OK != rcutils_status) {
92 if (g_rcl_logging_stdout_enabled) {
93 g_rcl_logging_out_handlers[g_rcl_logging_num_out_handlers++] =
94 rcutils_logging_console_output_handler;
96 if (g_rcl_logging_rosout_enabled) {
99 g_rcl_logging_out_handlers[g_rcl_logging_num_out_handlers++] =
103 if (g_rcl_logging_ext_lib_enabled) {
104 status = rcl_logging_external_initialize(
105 file_name_prefix, config_file, g_logging_allocator);
107 rcl_logging_ret_t logging_status = rcl_logging_external_set_logger_level(
108 NULL, default_level);
109 if (RCL_LOGGING_RET_OK != logging_status) {
112 g_rcl_logging_out_handlers[g_rcl_logging_num_out_handlers++] =
113 rcl_logging_ext_lib_output_handler;
116 rcutils_logging_set_output_handler(output_handler);
130 rcutils_logging_set_output_handler(rcutils_logging_console_output_handler);
135 g_rcl_logging_num_out_handlers = 1;
136 g_rcl_logging_out_handlers[0] = rcutils_logging_console_output_handler;
138 if (g_rcl_logging_rosout_enabled) {
141 if (
RCL_RET_OK == status && g_rcl_logging_ext_lib_enabled) {
142 status = rcl_logging_external_shutdown();
150 return g_rcl_logging_rosout_enabled;
155 const rcutils_log_location_t * location,
156 int severity,
const char * name, rcutils_time_point_value_t timestamp,
157 const char * format, va_list * args)
160 i < g_rcl_logging_num_out_handlers && NULL != g_rcl_logging_out_handlers[i]; ++i)
162 g_rcl_logging_out_handlers[i](location, severity, name, timestamp, format, args);
168 rcl_logging_ext_lib_output_handler(
169 const rcutils_log_location_t * location,
172 rcutils_time_point_value_t timestamp,
177 char msg_buf[1024] =
"";
178 rcutils_char_array_t msg_array = {
180 .owns_buffer =
false,
182 .buffer_capacity =
sizeof(msg_buf),
183 .allocator = g_logging_allocator
186 char output_buf[1024] =
"";
187 rcutils_char_array_t output_array = {
188 .buffer = output_buf,
189 .owns_buffer =
false,
191 .buffer_capacity =
sizeof(output_buf),
192 .allocator = g_logging_allocator
195 status = rcutils_char_array_vsprintf(&msg_array, format, *args);
198 status = rcutils_logging_format_message(
199 location, severity, name, timestamp, msg_array.buffer, &output_array);
201 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to format log message: ");
202 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
204 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
207 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to format user log message: ");
208 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
210 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
212 rcl_logging_external_log(severity, name, output_array.buffer);
213 status = rcutils_char_array_fini(&msg_array);
215 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to finalize char array: ");
216 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
218 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
220 status = rcutils_char_array_fini(&output_array);
222 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to finalize char array: ");
223 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
225 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_logging_rosout_enabled(void)
See if logging rosout is enabled.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_configure(const rcl_arguments_t *global_args, const rcl_allocator_t *allocator)
Configure the logging system.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_fini(void)
rcutils_logging_output_handler_t rcl_logging_output_handler_t
The function signature to log messages.
RCL_PUBLIC void rcl_logging_multiple_output_handler(const rcutils_log_location_t *location, int severity, const char *name, rcutils_time_point_value_t timestamp, const char *format, va_list *args)
Default output handler used by rcl.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_configure_with_output_handler(const rcl_arguments_t *global_args, const rcl_allocator_t *allocator, rcl_logging_output_handler_t output_handler)
Configure the logging system with the provided output handler.
RCL_PUBLIC void rcl_logging_rosout_output_handler(const rcutils_log_location_t *location, int severity, const char *name, rcutils_time_point_value_t timestamp, const char *format, va_list *args)
The output handler outputs log messages to rosout topics.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_rosout_fini(void)
Uninitializes the rcl_logging_rosout features.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_rosout_init(const rcl_allocator_t *allocator)
Initializes the rcl_logging_rosout features.
char * external_log_config_file
A file used to configure the external logging library.
rcl_log_levels_t log_levels
Log levels parsed from arguments.
bool log_rosout_disabled
A boolean value indicating if the rosout topic handler should be used for log output.
bool log_stdout_disabled
A boolean value indicating if the standard out handler should be used for log output.
bool log_ext_lib_disabled
A boolean value indicating if the external lib handler should be used for log output.
char * external_log_file_name_prefix
A prefix used to external log file name.
Hold output of parsing command line arguments.
rcl_arguments_impl_t * impl
Private implementation pointer.
Hold default logger level and other logger setting.
rcl_logger_setting_t * logger_settings
Array of logger setting.
rcl_log_severity_t default_logger_level
Minimum default logger level severity.
size_t num_logger_settings
Number of logger settings.
rcl_log_severity_t level
Minimum log level severity of the logger.
const char * name
Name for the logger.
#define RCL_RET_OK
Success return code.
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
#define RCL_RET_ERROR
Unspecified error return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.