-
Notifications
You must be signed in to change notification settings - Fork 638
/
abstract_logger.h
96 lines (77 loc) · 2.29 KB
/
abstract_logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef ABSTRACT_LOGGER_H
#define ABSTRACT_LOGGER_H
#include "behaviortree_cpp/behavior_tree.h"
#include "behaviortree_cpp/bt_factory.h"
namespace BT
{
enum class TimestampType
{
absolute,
relative
};
class StatusChangeLogger
{
public:
StatusChangeLogger(TreeNode* root_node);
virtual ~StatusChangeLogger() = default;
StatusChangeLogger(const StatusChangeLogger& other) = delete;
StatusChangeLogger& operator=(const StatusChangeLogger& other) = delete;
StatusChangeLogger(StatusChangeLogger&& other) = default;
StatusChangeLogger& operator=(StatusChangeLogger&& other) = default;
virtual void callback(BT::Duration timestamp, const TreeNode& node,
NodeStatus prev_status, NodeStatus status) = 0;
virtual void flush() = 0;
void setEnabled(bool enabled)
{
enabled_ = enabled;
}
void setTimestampType(TimestampType type)
{
type_ = type;
}
bool enabled() const
{
return enabled_;
}
// false by default.
bool showsTransitionToIdle() const
{
return show_transition_to_idle_;
}
void enableTransitionToIdle(bool enable)
{
show_transition_to_idle_ = enable;
}
private:
bool enabled_;
bool show_transition_to_idle_;
std::vector<TreeNode::StatusChangeSubscriber> subscribers_;
TimestampType type_;
BT::TimePoint first_timestamp_ = {};
};
//--------------------------------------------
inline StatusChangeLogger::StatusChangeLogger(TreeNode* root_node)
: enabled_(true), show_transition_to_idle_(true), type_(TimestampType::absolute)
{
first_timestamp_ = std::chrono::high_resolution_clock::now();
auto subscribeCallback = [this](TimePoint timestamp, const TreeNode& node,
NodeStatus prev, NodeStatus status) {
if(enabled_ && (status != NodeStatus::IDLE || show_transition_to_idle_))
{
if(type_ == TimestampType::absolute)
{
this->callback(timestamp.time_since_epoch(), node, prev, status);
}
else
{
this->callback(timestamp - first_timestamp_, node, prev, status);
}
}
};
auto visitor = [this, subscribeCallback](TreeNode* node) {
subscribers_.push_back(node->subscribeToStatusChange(std::move(subscribeCallback)));
};
applyRecursiveVisitor(root_node, visitor);
}
} // namespace BT
#endif // ABSTRACT_LOGGER_H