ROS2 how to programmatically set rosout logging handler
Hi,
In ROS2 there are different types of logging handlers.
Command line arguments can be used to setup the loggers: these are passed to rclcpp::init()
and then to rcl::init()
.
The command line arguments can do the following: enable stdout logging, enable rosout logging, enable an external logging library (log4cxx or spdlog).
They do this by creating a logging_multiple_output_handler
and setting it through rcutils_logging_set_output_handler
.
I found myself in a situation where I want to use my own version of logging_multiple_output_handler
.
These handler should always use 2 loggers: rosout and an additional one that I'm defining.
For this reason, I created a function to be called right after rclcpp::init
in order to re-initialize the logging handlers to what I need.
void set_ros2_logger_handler()
{
auto allocator = rcl_get_default_allocator();
rcl_logging_rosout_init(&allocator);
auto 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) -> void
{
rcl_logging_rosout_output_handler(location, severity, name, timestamp, format, args);
my_custom_handler(location, severity, name, timestamp, format, args);
};
rcutils_logging_set_output_handler(logging_multiple_output_handler);
}
However, this does not really work and I'm wondering what should be the best approach. The problems are the following:
rcl_logging_rosout_init
is a hidden function (marked withRCL_LOCAL
) so how can I know if rosout has been enabled without using it?rcl_logging_rosout_output_handler
is not declared as hidden, but still I can't link to it for some reason.
EDIT: apparently rcl_logging_rosout_output_handler
should be accessible and the error is linked to the following line in rcl/CMakeLists.txt
rcl_set_symbol_visibility_hidden(${PROJECT_NAME} LANGUAGE "C")
TICKET: https://github.com/ros2/rcl/issues/476
DISCOURSE: https://discourse.ros.org/t/set-symbo...