Skip to content

Add ROS2 logger #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: humble
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion behaviortree_ros2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ generate_parameter_library(
add_library(${PROJECT_NAME}
src/bt_ros2.cpp
src/bt_utils.cpp
src/tree_execution_server.cpp )
src/tree_execution_server.cpp
src/bt_ros_logger.cpp )

ament_target_dependencies(${PROJECT_NAME} ${THIS_PACKAGE_DEPS})

Expand Down
32 changes: 32 additions & 0 deletions behaviortree_ros2/include/behaviortree_ros2/bt_ros_logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef BT_ROS_LOGGER_H
#define BT_ROS_LOGGER_H

#include <cstring>
#include <rclcpp/executors.hpp>
#include <rclcpp/allocator/allocator_common.hpp>
#include <rclcpp/logging.hpp>
#include "behaviortree_cpp/loggers/abstract_logger.h"

namespace BT
{
/**
* @brief RosLogger is a very simple logger that
* displays all the transitions on the console.
Copy link

@b-adkins b-adkins Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* displays all the transitions on the console.
* displays all behavior node status transitions
* on the console.

*/

class RosLogger : public StatusChangeLogger
{
public:
RosLogger(const BT::Tree& tree, std::shared_ptr<rclcpp::Node> node);
~RosLogger() override;

virtual void flush() override;

private:
virtual void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
NodeStatus status) override;
std::weak_ptr<rclcpp::Node> node_;
};
} // namespace BT

#endif // BT_ROS_LOGGER_H
38 changes: 38 additions & 0 deletions behaviortree_ros2/src/bt_ros_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "behaviortree_ros2/bt_ros_logger.hpp"

namespace BT
{

RosLogger::RosLogger(const BT::Tree& tree, std::shared_ptr<rclcpp::Node> node) : StatusChangeLogger(tree.rootNode()), node_(node)
{}
RosLogger::~RosLogger()
{}

void RosLogger::callback(Duration timestamp, const TreeNode& node,
NodeStatus prev_status, NodeStatus status)
{
using namespace std::chrono;

// get ros node pointer
auto ros_node = node_.lock();

if (ros_node) {

constexpr const char* whitespaces = " ";
constexpr const size_t ws_count = 25;

double since_epoch = duration<double>(timestamp).count();

RCLCPP_DEBUG(
ros_node->get_logger(), "[%.3f]: %s%s %s -> %s",
since_epoch, node.name().c_str(),
&whitespaces[std::min(ws_count, node.name().size())],
toStr(prev_status, true).c_str(), toStr(status, true).c_str());
}
}

void RosLogger::flush()
{
}

} // namespace BT