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

Revision history [back]

click to hide/show revision 1
initial version

I'd actually recommend option 2 (passing the logger instance given by the node to your custom class), unless your class also needs the node, in which case option 1 is fine (no need to pass both the node and the logger).

If only a few methods of the custom class need to do logging you might consider option 3 or 4, but again I'd prefer option 4 to option 3, preferring to limit what information you pass into the class to only what you need.

In all cases, I'll point out that you can "extend" the logger given to the class to add extra namespace if you desire it, see: http://docs.ros2.org/bouncy/api/rclcpp/classrclcpp_1_1_logger.html#a68ab28459973c1072d3ad7a593ac994e

Also, you might consider taking a function for logging to completely decouple your custom class from ROS primitives altogether. For example:

MyNodeClass : public rclcpp::Node
{
public:
  MyNodeClass()
  : Node("my_node_class") {
    custom_class(
      [logger = this->get_logger()] (const std::string & msg) -> void {RCLCPP_INFO(logger, msg);}
    );
  }

  MyNodeClass::do_stuff(){
    custom_class.do_stuff();
  }

  CustomClass custom_class;
}