ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2 how to programmatically set rosout logging handler

asked 2019-07-29 08:47:50 -0500

alsora gravatar image

updated 2019-07-29 13:46:40 -0500

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 with RCL_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...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-08-01 09:17:13 -0500

alsora gravatar image

The code I posted is supposed to work.

rcl_logging_rosout_output_handler is marked as RCL_PUBLIC, but it was not accessible due to a bug addressed by this PR https://github.com/ros2/rcl/pull/478

rcl_logging_rosout_init should be marked with RCL_PUBLICtoo since otherwise the handler is useless. https://github.com/ros2/rcl/pull/479

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-07-29 08:47:50 -0500

Seen: 755 times

Last updated: Aug 01 '19