bt_file_logger.cpp
Go to the documentation of this file.
3 
4 namespace BT
5 {
6 FileLogger::FileLogger(BT::TreeNode* root_node, const char* filename, uint16_t buffer_size)
7  : StatusChangeLogger(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, root_node);
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 }
std::array< uint8_t, 12 > SerializedTransition
std::ofstream file_os_
static raw_event_t * buffer
Definition: minitrace.cpp:54
virtual void flush() override
FileLogger(TreeNode *root_node, const char *filename, uint16_t buffer_size=10)
uint16_t UID() const
Definition: tree_node.cpp:108
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
Definition: flatbuffers.h:727
virtual void callback(Duration timestamp, const TreeNode &node, NodeStatus prev_status, NodeStatus status) override
builder
Definition: build.py:70
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
Definition: flatbuffers.h:779
std::vector< SerializedTransition > buffer_
void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder &builder, BT::TreeNode *root_node)
NodeStatus
Definition: basic_types.h:28
virtual ~FileLogger() override
void enableTransitionToIdle(bool enable)
std::chrono::high_resolution_clock::duration Duration
Definition: tree_node.h:36
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
Definition: flatbuffers.h:784
SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp, NodeStatus prev_status, NodeStatus status)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32