Skip to content

Commit c65f358

Browse files
committed
add metadata to SqliteLogger
1 parent 3cc7b4e commit c65f358

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

examples/t12_groot_howto.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ int main()
125125
bool append_to_database = true;
126126
BT::SqliteLogger sqlite_logger(tree, "t12_sqlitelog.db3", append_to_database);
127127

128+
// We can add some extra information to the SqliteLogger, for instance the value of the
129+
// "door_open" blackboard entry, at the end of node "tryOpen" (Fallback)
130+
131+
auto sqlite_callback = [](BT::Duration timestamp, const BT::TreeNode& node,
132+
BT::NodeStatus prev_status,
133+
BT::NodeStatus status) -> std::string {
134+
if(node.name() == "tryOpen" && BT::isStatusCompleted(status))
135+
{
136+
auto is_open = BT::toStr(node.config().blackboard->get<bool>("door_open"));
137+
return "[tryOpen] door_open=" + is_open;
138+
}
139+
return {};
140+
};
141+
sqlite_logger.setMetadataCallback(sqlite_callback);
142+
128143
while(1)
129144
{
130145
std::cout << "Start" << std::endl;

include/behaviortree_cpp/loggers/bt_sqlite_logger.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class SqliteLogger : public StatusChangeLogger
3737

3838
virtual ~SqliteLogger() override;
3939

40+
// You can inject a function that add a metadata filed (a string) to the raw in the table.
41+
// The arguments of the function are the same as SqliteLogger::callback()
42+
using MetadataFunc =
43+
std::function<std::string(Duration, const TreeNode&, NodeStatus, NodeStatus)>;
44+
void setMetadataCallback(MetadataFunc func);
45+
4046
virtual void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
4147
NodeStatus status) override;
4248

@@ -56,6 +62,7 @@ class SqliteLogger : public StatusChangeLogger
5662
int64_t timestamp;
5763
int64_t duration;
5864
NodeStatus status;
65+
std::string metadata;
5966
};
6067

6168
std::deque<Transition> transitions_queue_;
@@ -65,6 +72,8 @@ class SqliteLogger : public StatusChangeLogger
6572
std::thread writer_thread_;
6673
std::atomic_bool loop_ = true;
6774

75+
MetadataFunc meta_func_;
76+
6877
void writerLoop();
6978
};
7079

src/loggers/bt_sqlite_logger.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
2424
"session_id INTEGER NOT NULL, "
2525
"uid INTEGER NOT NULL, "
2626
"duration INTEGER, "
27-
"state INTEGER NOT NULL);");
27+
"state INTEGER NOT NULL,"
28+
"metadata VARCHAR );");
2829

2930
sqlite::Statement(*db_, "CREATE TABLE IF NOT EXISTS Definitions ("
3031
"session_id INTEGER PRIMARY KEY AUTOINCREMENT, "
@@ -61,6 +62,11 @@ SqliteLogger::~SqliteLogger()
6162
sqlite::Statement(*db_, "PRAGMA optimize;");
6263
}
6364

65+
void SqliteLogger::setMetadataCallback(MetadataFunc func)
66+
{
67+
meta_func_ = func;
68+
}
69+
6470
void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
6571
NodeStatus prev_status, NodeStatus status)
6672
{
@@ -91,6 +97,11 @@ void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
9197
trans.node_uid = node.UID();
9298
trans.status = status;
9399

100+
if(meta_func_)
101+
{
102+
trans.metadata = meta_func_(timestamp, node, prev_status, status);
103+
}
104+
94105
{
95106
std::scoped_lock lk(queue_mutex_);
96107
transitions_queue_.push_back(trans);
@@ -116,9 +127,9 @@ void SqliteLogger::writerLoop()
116127
auto const trans = transitions.front();
117128
transitions.pop_front();
118129

119-
sqlite::Statement(*db_, "INSERT INTO Transitions VALUES (?, ?, ?, ?, ?)",
130+
sqlite::Statement(*db_, "INSERT INTO Transitions VALUES (?, ?, ?, ?, ?, ?)",
120131
trans.timestamp, session_id_, trans.node_uid, trans.duration,
121-
static_cast<int>(trans.status));
132+
static_cast<int>(trans.status), trans.metadata);
122133
}
123134
}
124135
}

0 commit comments

Comments
 (0)