Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
log_action.cpp
1 // Copyright (c) 2026 Open Navigation LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 
17 #include "nav2_behavior_tree/plugins/action/log_action.hpp"
18 #include "nav2_ros_common/lifecycle_node.hpp"
19 
20 namespace nav2_behavior_tree
21 {
22 
23 BT::NodeStatus LogAction::tick()
24 {
25  const auto level = getInput<std::string>("level");
26  const auto message = getInput<std::string>("message");
27  const auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
28  const auto logger = node->get_logger();
29 
30  if (!level || !message) {
31  RCLCPP_ERROR(logger, "Log action requires level and message inputs");
32  return BT::NodeStatus::FAILURE;
33  }
34 
35  if (level.value() == "DEBUG") {
36  RCLCPP_DEBUG(logger, "%s", message.value().c_str());
37  } else if (level.value() == "INFO") {
38  RCLCPP_INFO(logger, "%s", message.value().c_str());
39  } else if (level.value() == "WARN") {
40  RCLCPP_WARN(logger, "%s", message.value().c_str());
41  } else if (level.value() == "ERROR") {
42  RCLCPP_ERROR(logger, "%s", message.value().c_str());
43  } else if (level.value() == "FATAL") {
44  RCLCPP_FATAL(logger, "%s", message.value().c_str());
45  } else {
46  RCLCPP_ERROR(logger, "Invalid Log action level: %s", level.value().c_str());
47  return BT::NodeStatus::FAILURE;
48  }
49 
50  return BT::NodeStatus::SUCCESS;
51 }
52 
53 } // namespace nav2_behavior_tree
54 
55 #include "behaviortree_cpp/bt_factory.h"
56 BT_REGISTER_NODES(factory)
57 {
58  factory.registerNodeType<nav2_behavior_tree::LogAction>("Log");
59 }
A BT::SyncActionNode that logs a message through the BT navigator's ROS logger.
Definition: log_action.hpp:34
BT::NodeStatus tick() override
Logs the input message at the specified level using the blackboard's node.
Definition: log_action.cpp:23