|
| 1 | +#include "behaviortree_cpp/loggers/bt_file_logger_v2.h" |
| 2 | +#include "behaviortree_cpp/flatbuffers/base.h" |
| 3 | +#include "behaviortree_cpp/xml_parsing.h" |
| 4 | + |
| 5 | +namespace BT |
| 6 | +{ |
| 7 | + |
| 8 | +int64_t ToUsec(Duration ts) |
| 9 | +{ |
| 10 | + return std::chrono::duration_cast<std::chrono::microseconds>(ts).count(); |
| 11 | +} |
| 12 | + |
| 13 | +FileLogger2::FileLogger2(const BT::Tree& tree, std::filesystem::path const& filepath) : |
| 14 | + StatusChangeLogger(tree.rootNode()) |
| 15 | +{ |
| 16 | + enableTransitionToIdle(true); |
| 17 | + |
| 18 | + //------------------------------------- |
| 19 | + file_stream_.open(filepath, std::ofstream::binary | std::ofstream::out); |
| 20 | + |
| 21 | + std::string const xml = WriteTreeToXML(tree, true); |
| 22 | + |
| 23 | + // serialize the length of the buffer in the first 4 bytes |
| 24 | + char write_buffer[8]; |
| 25 | + flatbuffers::WriteScalar(write_buffer, static_cast<int32_t>(xml.size())); |
| 26 | + file_stream_.write(write_buffer, 4); |
| 27 | + |
| 28 | + // write the XML definition |
| 29 | + file_stream_.write(xml.data(), int(xml.size())); |
| 30 | + |
| 31 | + first_timestamp_ = std::chrono::system_clock::now().time_since_epoch(); |
| 32 | + |
| 33 | + // save the first timestamp in the next 8 bytes (microseconds) |
| 34 | + int64_t timestamp_usec = ToUsec(first_timestamp_); |
| 35 | + flatbuffers::WriteScalar(write_buffer, timestamp_usec); |
| 36 | + file_stream_.write(write_buffer, 8); |
| 37 | + |
| 38 | + writer_thread_ = std::thread(&FileLogger2::writerLoop, this); |
| 39 | +} |
| 40 | + |
| 41 | +FileLogger2::~FileLogger2() |
| 42 | +{ |
| 43 | + loop_ = false; |
| 44 | + queue_cv_.notify_one(); |
| 45 | + writer_thread_.join(); |
| 46 | + file_stream_.close(); |
| 47 | +} |
| 48 | + |
| 49 | +void FileLogger2::callback(Duration timestamp, const TreeNode& node, |
| 50 | + NodeStatus /*prev_status*/, NodeStatus status) |
| 51 | +{ |
| 52 | + Transition trans; |
| 53 | + trans.timestamp_usec = uint64_t(ToUsec(timestamp - first_timestamp_)); |
| 54 | + trans.node_uid = node.UID(); |
| 55 | + trans.status = static_cast<uint64_t>(status); |
| 56 | + { |
| 57 | + std::scoped_lock lock(queue_mutex_); |
| 58 | + transitions_queue_.push_back(trans); |
| 59 | + } |
| 60 | + queue_cv_.notify_one(); |
| 61 | +} |
| 62 | + |
| 63 | +void FileLogger2::flush() |
| 64 | +{ |
| 65 | + file_stream_.flush(); |
| 66 | +} |
| 67 | + |
| 68 | +void FileLogger2::writerLoop() |
| 69 | +{ |
| 70 | + // local buffer in this thread |
| 71 | + std::deque<Transition> transitions; |
| 72 | + |
| 73 | + while(loop_) |
| 74 | + { |
| 75 | + transitions.clear(); |
| 76 | + { |
| 77 | + std::unique_lock lock(queue_mutex_); |
| 78 | + queue_cv_.wait_for(lock, std::chrono::milliseconds(10), |
| 79 | + [this]() {return !transitions_queue_.empty() && loop_; } ); |
| 80 | + // simple way to pop all the transitions from transitions_queue_ into transitions |
| 81 | + std::swap(transitions, transitions_queue_); |
| 82 | + } |
| 83 | + while(!transitions.empty()) |
| 84 | + { |
| 85 | + char write_buffer[8]; |
| 86 | + flatbuffers::WriteScalar(write_buffer, transitions.front()); |
| 87 | + file_stream_.write(write_buffer, 8); |
| 88 | + transitions.pop_front(); |
| 89 | + } |
| 90 | + file_stream_.flush(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace BT |
0 commit comments