BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
bt_file_logger_v2.h
1#pragma once
2#include "behaviortree_cpp/loggers/abstract_logger.h"
3
4#include <deque>
5#include <filesystem>
6#include <memory>
7
8namespace BT
9{
10
11/**
12 * @brief The FileLogger2 is a logger that saves the tree as
13 * XML and all the transitions.
14 * Data is written to file in a separate thread, to minimize latency.
15 *
16 * Format:
17 *
18 * - first 4 bytes: size of the XML string (N)
19 * - next N bytes: string containing the XML representing the tree.
20 * - next 8 bytes: first timestamp (microseconds since epoch)
21 * - next: each 9 bytes is a FileLogger2::Transition. See definition.
22 *
23 */
25{
26public:
27 /**
28 * @brief To correctly read this log with Groot2, you must use the suffix ".btlog".
29 * Constructor will throw otherwise.
30 *
31 * @param tree the tree to log
32 * @param filepath path of the file where info will be stored
33 */
34 FileLogger2(const Tree& tree, std::filesystem::path const& filepath);
35
36 FileLogger2(const FileLogger2& other) = delete;
37 FileLogger2& operator=(const FileLogger2& other) = delete;
38
39 FileLogger2(FileLogger2&& other) = delete;
40 FileLogger2& operator=(FileLogger2&& other) = delete;
41
42 virtual ~FileLogger2() override;
43
44 void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
45 NodeStatus status) override;
46
47 struct Transition
48 {
49 // when serializing, we will remove the initial time and serialize only
50 // 6 bytes, instead of 8
51 uint64_t timestamp_usec;
52 // if you have more than 64.000 nodes, you are doing something wrong :)
53 uint16_t node_uid;
54 // enough bits to contain NodeStatus
55 uint8_t status;
56 };
57
58 void flush() override;
59
60private:
61 struct Pimpl;
62 std::unique_ptr<Pimpl> _p;
63
64 void writerLoop();
65 // Write one batch of transitions to the file (the loop's own 9-byte layout, one place).
66 void writeBatch(std::deque<Transition>& transitions);
67 // Move whatever is queued onto this thread and write it: the destructor's drain after the
68 // writer thread has joined.
69 void drainQueue();
70};
71
72} // namespace BT
The FileLogger2 is a logger that saves the tree as XML and all the transitions. Data is written to fi...
Definition: bt_file_logger_v2.h:25
FileLogger2(const Tree &tree, std::filesystem::path const &filepath)
To correctly read this log with Groot2, you must use the suffix ".btlog". Constructor will throw othe...
Definition: abstract_logger.h:18
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:100
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: bt_file_logger_v2.h:48