bt_file_logger.cpp
Go to the documentation of this file.
3 
4 namespace BT
5 {
6 FileLogger::FileLogger(const BT::Tree& tree, const char* filename, uint16_t buffer_size)
7  : StatusChangeLogger(tree.root_node), buffer_max_size_(buffer_size)
8 {
9  if (buffer_max_size_ != 0)
10  {
11  buffer_.reserve(buffer_max_size_);
12  }
13 
15 
17  CreateFlatbuffersBehaviorTree(builder, tree);
18 
19  //-------------------------------------
20 
21  file_os_.open(filename, std::ofstream::binary | std::ofstream::out);
22 
23  // serialize the length of the buffer in the first 4 bytes
24  char size_buff[4];
25  flatbuffers::WriteScalar(size_buff, static_cast<int32_t>(builder.GetSize()));
26 
27  file_os_.write(size_buff, 4);
28  file_os_.write(reinterpret_cast<const char*>(builder.GetBufferPointer()), builder.GetSize());
29 }
30 
32 {
33  this->flush();
34  file_os_.close();
35 }
36 
37 void FileLogger::callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
38  NodeStatus status)
39 {
41  SerializeTransition(node.UID(), timestamp, prev_status, status);
42 
43  if (buffer_max_size_ == 0)
44  {
45  file_os_.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
46  }
47  else
48  {
49  buffer_.push_back(buffer);
50  if (buffer_.size() >= buffer_max_size_)
51  {
52  this->flush();
53  }
54  }
55 }
56 
58 {
59  for (const auto& array : buffer_)
60  {
61  file_os_.write(reinterpret_cast<const char*>(array.data()), array.size());
62  }
63  file_os_.flush();
64  buffer_.clear();
65 }
66 }
FileLogger(const Tree &tree, const char *filename, uint16_t buffer_size=10)
std::ofstream file_os_
static raw_event_t * buffer
Definition: minitrace.cpp:54
virtual void flush() override
void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder &builder, const BT::Tree &tree)
std::array< uint8_t, 12 > SerializedTransition
uint16_t UID() const
Definition: tree_node.cpp:89
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
virtual void callback(Duration timestamp, const TreeNode &node, NodeStatus prev_status, NodeStatus status) override
builder
Definition: build.py:70
size_t buffer_max_size_
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:53
std::vector< SerializedTransition > buffer_
NodeStatus
Definition: basic_types.h:35
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:78
virtual ~FileLogger() override
void enableTransitionToIdle(bool enable)
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:339
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp, NodeStatus prev_status, NodeStatus status)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04