25 #include "./arguments_impl.h"
27 #include "rcl/error_handling.h"
31 #include "rcl_logging_interface/rcl_logging_interface.h"
32 #include "rcutils/error_handling.h"
33 #include "rcutils/logging.h"
34 #include "rcutils/time.h"
35 #include "rcutils/types/char_array.h"
36 #include "rcutils/types/rcutils_ret.h"
38 #define RCL_LOGGING_MAX_OUTPUT_FUNCS (4)
40 static rcutils_logging_output_handler_t
41 g_rcl_logging_out_handlers[RCL_LOGGING_MAX_OUTPUT_FUNCS] = {0};
43 static uint8_t g_rcl_logging_num_out_handlers = 0;
45 static bool g_rcl_logging_stdout_enabled =
false;
46 static bool g_rcl_logging_rosout_enabled =
false;
47 static bool g_rcl_logging_ext_lib_enabled =
false;
54 rcl_logging_ext_lib_output_handler(
55 const rcutils_log_location_t * location,
56 int severity,
const char * name, rcutils_time_point_value_t timestamp,
57 const char * format, va_list * args);
68 RCUTILS_LOGGING_AUTOINIT_WITH_ALLOCATOR(*allocator);
69 g_logging_allocator = *allocator;
70 int default_level = -1;
78 g_rcl_logging_num_out_handlers = 0;
83 rcutils_logging_set_default_logger_level(default_level);
87 rcutils_ret_t rcutils_status = rcutils_logging_set_logger_level(
90 if (RCUTILS_RET_OK != rcutils_status) {
95 if (g_rcl_logging_stdout_enabled) {
96 g_rcl_logging_out_handlers[g_rcl_logging_num_out_handlers++] =
97 rcutils_logging_console_output_handler;
99 if (g_rcl_logging_rosout_enabled) {
102 g_rcl_logging_out_handlers[g_rcl_logging_num_out_handlers++] =
106 if (g_rcl_logging_ext_lib_enabled) {
107 status = rcl_logging_external_initialize(
108 file_name_prefix, config_file, g_logging_allocator);
110 rcl_logging_ret_t logging_status = rcl_logging_external_set_logger_level(
111 NULL, default_level);
112 if (RCL_LOGGING_RET_OK != logging_status) {
115 g_rcl_logging_out_handlers[g_rcl_logging_num_out_handlers++] =
116 rcl_logging_ext_lib_output_handler;
119 rcutils_logging_set_output_handler(output_handler);
133 rcutils_logging_set_output_handler(rcutils_logging_console_output_handler);
138 g_rcl_logging_num_out_handlers = 1;
139 g_rcl_logging_out_handlers[0] = rcutils_logging_console_output_handler;
141 if (g_rcl_logging_rosout_enabled) {
144 if (
RCL_RET_OK == status && g_rcl_logging_ext_lib_enabled) {
145 status = rcl_logging_external_shutdown();
153 return g_rcl_logging_rosout_enabled;
158 const rcutils_log_location_t * location,
159 int severity,
const char * name, rcutils_time_point_value_t timestamp,
160 const char * format, va_list * args)
163 i < g_rcl_logging_num_out_handlers && NULL != g_rcl_logging_out_handlers[i]; ++i)
165 g_rcl_logging_out_handlers[i](location, severity, name, timestamp, format, args);
171 rcl_logging_ext_lib_output_handler(
172 const rcutils_log_location_t * location,
175 rcutils_time_point_value_t timestamp,
180 char msg_buf[1024] =
"";
181 rcutils_char_array_t msg_array = {
183 .owns_buffer =
false,
185 .buffer_capacity =
sizeof(msg_buf),
186 .allocator = g_logging_allocator
189 char output_buf[1024] =
"";
190 rcutils_char_array_t output_array = {
191 .buffer = output_buf,
192 .owns_buffer =
false,
194 .buffer_capacity =
sizeof(output_buf),
195 .allocator = g_logging_allocator
198 status = rcutils_char_array_vsprintf(&msg_array, format, *args);
201 status = rcutils_logging_format_message(
202 location, severity, name, timestamp, msg_array.buffer, &output_array);
204 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to format log message: ");
205 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
207 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
210 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to format user log message: ");
211 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
213 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
215 rcl_logging_external_log(severity, name, output_array.buffer);
216 status = rcutils_char_array_fini(&msg_array);
218 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to finalize char array: ");
219 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
221 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
223 status = rcutils_char_array_fini(&output_array);
225 RCUTILS_SAFE_FWRITE_TO_STDERR(
"failed to finalize char array: ");
226 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
228 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.