ROS 2 Bouncy : Node, Log and Classes best practice
Hello I am working with ROS 2 Bouncy and was wondering what is the best practice to log stuff in classes that are instantiated in a Node ?
I know it's now recommended to inherit the rclcpp::Node
class and you can simply log by doing RCLCPP_INFO(this->get_logger(), "My message")
.
My question is about the best practice to log in customs classes inside the Node. For example, let's say we have this class :
class MyNodeClass : public rclcpp::Node
{
public:
MyNodeClass()
: Node("my_node_class") {
CustomClass custom_class();
}
MyNodeClass::do_stuff(){
custom_class.do_stuff();
}
}
Which of this 4 options is the recommended way to give CustomClass
access to the logger ?
Option 1 : We give the Node to the CustomClass
constructor which save it in some var to be able to log in any function.
MyNodeClass : public rclcpp::Node
{
public:
MyNodeClass()
: Node("my_node_class") {
CustomClass custom_class(this);
}
MyNodeClass::do_stuff(){
custom_class.do_stuff();
}
}
Option 2 : We give the logger to the CustomClass
constructor which save it in some var to be able to use it in any function.
MyNodeClass : public rclcpp::Node
{
public:
MyNodeClass()
: Node("my_node_class") {
CustomClass custom_class(this->get_logger());
}
MyNodeClass::do_stuff(){
custom_class.do_stuff();
}
}
Option 3 : We give the node to each CustomClass
function if they need to log stuff.
class MyNodeClass : public rclcpp::Node
{
public:
MyNodeClass()
: Node("my_node_class") {
CustomClass custom_class();
}
MyNodeClass::do_stuff(){
custom_class.do_stuff(this);
}
}
Option 4 : We only give the Node's logger to each CustomClass
function.
class MyNodeClass : public rclcpp::Node
{
public:
MyNodeClass()
: Node("my_node_class") {
CustomClass custom_class();
}
MyNodeClass::do_stuff(){
custom_class.do_stuff(this->get_logger());
}
}
Any other option ? Does any of this really matter or can we choose any option ?
Thank you.