bt_file_logger.cpp
Go to the documentation of this file.
00001 #include "behaviortree_cpp/loggers/bt_file_logger.h"
00002 #include "behaviortree_cpp/loggers/bt_flatbuffer_helper.h"
00003 
00004 namespace BT
00005 {
00006 FileLogger::FileLogger(BT::TreeNode* root_node, const char* filename, uint16_t buffer_size)
00007   : StatusChangeLogger(root_node), buffer_max_size_(buffer_size)
00008 {
00009     if (buffer_max_size_ != 0)
00010     {
00011         buffer_.reserve(buffer_max_size_);
00012     }
00013 
00014     enableTransitionToIdle(true);
00015 
00016     flatbuffers::FlatBufferBuilder builder(1024);
00017     CreateFlatbuffersBehaviorTree(builder, root_node);
00018 
00019     //-------------------------------------
00020 
00021     file_os_.open(filename, std::ofstream::binary | std::ofstream::out);
00022 
00023     // serialize the length of the buffer in the first 4 bytes
00024     char size_buff[4];
00025     flatbuffers::WriteScalar(size_buff, static_cast<int32_t>(builder.GetSize()));
00026 
00027     file_os_.write(size_buff, 4);
00028     file_os_.write(reinterpret_cast<const char*>(builder.GetBufferPointer()), builder.GetSize());
00029 }
00030 
00031 FileLogger::~FileLogger()
00032 {
00033     this->flush();
00034     file_os_.close();
00035 }
00036 
00037 void FileLogger::callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
00038                           NodeStatus status)
00039 {
00040     SerializedTransition buffer =
00041         SerializeTransition(node.UID(), timestamp, prev_status, status);
00042 
00043     if (buffer_max_size_ == 0)
00044     {
00045         file_os_.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
00046     }
00047     else
00048     {
00049         buffer_.push_back(buffer);
00050         if (buffer_.size() >= buffer_max_size_)
00051         {
00052             this->flush();
00053         }
00054     }
00055 }
00056 
00057 void FileLogger::flush()
00058 {
00059     for (const auto& array : buffer_)
00060     {
00061         file_os_.write(reinterpret_cast<const char*>(array.data()), array.size());
00062     }
00063     file_os_.flush();
00064     buffer_.clear();
00065 }
00066 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:09