Skip to content

Commit 28123db

Browse files
committed
updated FileLogger2
1 parent 78908bd commit 28123db

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

include/behaviortree_cpp/loggers/bt_file_logger_v2.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ namespace BT
2323
class FileLogger2 : public StatusChangeLogger
2424
{
2525
public:
26+
/**
27+
* @brief To correctly read this log with Groot2, you must use the suffix ".btlog".
28+
* Constructor will throw otherwise.
29+
*
30+
* @param tree the tree to log
31+
* @param filepath path of the file where info will be stored
32+
*/
2633
FileLogger2(const Tree& tree, std::filesystem::path const& filepath);
2734

2835
virtual ~FileLogger2() override;
@@ -32,12 +39,11 @@ class FileLogger2 : public StatusChangeLogger
3239

3340
struct Transition
3441
{
35-
// 45 bits are enough for 35 million seconds (more than 1 year) relative to the first timestamp
36-
uint64_t timestamp_usec : 45;
42+
uint64_t timestamp_usec;
3743
// if you have more than 64.000 nodes, you are doing something wrong :)
38-
uint64_t node_uid : 16;
44+
uint16_t node_uid;
3945
// enough bits to contain NodeStatus
40-
uint64_t status : 3;
46+
uint8_t status;
4147
};
4248

4349
void flush() override;

include/behaviortree_cpp/loggers/bt_sqlite_logger.h

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ namespace BT
2424
class SqliteLogger : public StatusChangeLogger
2525
{
2626
public:
27+
/**
28+
* @brief To correctly read this log with Groot2, you must use the suffix ".db3".
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+
* @param append if true, add this recording to the database
34+
*/
2735
SqliteLogger(const Tree &tree,
2836
std::filesystem::path const& file,
2937
bool append = false);

src/loggers/bt_file_logger_v2.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ int64_t ToUsec(Duration ts)
1313
FileLogger2::FileLogger2(const BT::Tree& tree, std::filesystem::path const& filepath) :
1414
StatusChangeLogger(tree.rootNode())
1515
{
16+
if(filepath.filename().extension() != ".btlog")
17+
{
18+
throw RuntimeError("FileLogger2: the file extension must be [.btlog]");
19+
}
20+
1621
enableTransitionToIdle(true);
1722

1823
//-------------------------------------
@@ -24,6 +29,10 @@ FileLogger2::FileLogger2(const BT::Tree& tree, std::filesystem::path const& file
2429

2530
file_stream_ << "BTCPP4-FileLogger2";
2631

32+
const uint8_t protocol = 1;
33+
34+
file_stream_ << protocol;
35+
2736
std::string const xml = WriteTreeToXML(tree, true, true);
2837

2938
// serialize the length of the buffer in the first 4 bytes
@@ -88,9 +97,13 @@ void FileLogger2::writerLoop()
8897
}
8998
while(!transitions.empty())
9099
{
91-
char write_buffer[8];
92-
flatbuffers::WriteScalar(write_buffer, transitions.front());
93-
file_stream_.write(write_buffer, 8);
100+
const auto trans = transitions.front();
101+
std::array<char, 9> write_buffer;
102+
std::memcpy(write_buffer.data(), &trans.timestamp_usec, 6 );
103+
std::memcpy(write_buffer.data() + 6, &trans.node_uid, 2 );
104+
std::memcpy(write_buffer.data() + 8, &trans.status, 1 );
105+
106+
file_stream_.write(write_buffer.data(), 9);
94107
transitions.pop_front();
95108
}
96109
file_stream_.flush();

src/loggers/bt_sqlite_logger.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
namespace BT {
66

77
SqliteLogger::SqliteLogger(const Tree &tree,
8-
std::filesystem::path const& file,
8+
std::filesystem::path const& filepath,
99
bool append):
1010
StatusChangeLogger(tree.rootNode())
1111
{
12+
if(filepath.filename().extension() != ".db3")
13+
{
14+
throw RuntimeError("SqliteLogger: the file extension must be [.db3]");
15+
}
16+
1217
enableTransitionToIdle(true);
1318

14-
db_ = std::make_unique<sqlite::Connection>(file.string());
19+
db_ = std::make_unique<sqlite::Connection>(filepath.string());
1520

1621
sqlite::Statement(*db_,
1722
"CREATE TABLE IF NOT EXISTS Transitions ("

0 commit comments

Comments
 (0)