1#ifndef ABSTRACT_LOGGER_H
2#define ABSTRACT_LOGGER_H
4#include "behaviortree_cpp/behavior_tree.h"
5#include "behaviortree_cpp/bt_factory.h"
9enum class TimestampType
21 virtual ~StatusChangeLogger() =
default;
29 virtual void callback(
BT::Duration timestamp,
const TreeNode& node,
32 virtual void flush() = 0;
34 void setEnabled(
bool enabled)
39 void setTimestampType(TimestampType type)
50 bool showsTransitionToIdle()
const
52 return show_transition_to_idle_;
55 void enableTransitionToIdle(
bool enable)
57 show_transition_to_idle_ = enable;
69 bool show_transition_to_idle_ =
true;
70 std::vector<TreeNode::StatusChangeSubscriber> subscribers_;
71 TimestampType type_ = TimestampType::absolute;
72 BT::TimePoint first_timestamp_ = {};
73 std::mutex callback_mutex_;
85 first_timestamp_ = std::chrono::high_resolution_clock::now();
87 auto subscribeCallback = [
this](TimePoint timestamp,
const TreeNode& node,
91 bool should_callback =
false;
92 Duration adjusted_timestamp;
94 std::unique_lock lk(callback_mutex_);
95 if(enabled_ && (status !=
NodeStatus::IDLE || show_transition_to_idle_))
97 should_callback =
true;
98 adjusted_timestamp = (type_ == TimestampType::absolute) ?
99 timestamp.time_since_epoch() :
100 (timestamp - first_timestamp_);
105 this->callback(adjusted_timestamp, node, prev, status);
109 auto visitor = [
this, subscribeCallback](
TreeNode* node) {
110 subscribers_.push_back(node->subscribeToStatusChange(std::move(subscribeCallback)));
113 applyRecursiveVisitor(root_node, visitor);
Definition: abstract_logger.h:16
void subscribeToTreeChanges(TreeNode *root_node)
Subscribe to status changes. Call at end of constructor for deferred subscription.
Definition: abstract_logger.h:83
StatusChangeLogger(TreeNode *root_node)
Construct and immediately subscribe to status changes.
Definition: abstract_logger.h:78
StatusChangeLogger()=default
Default constructor for deferred subscription. Call subscribeToTreeChanges() when ready.
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34