Skip to content

Commit

Permalink
add metadata to SqliteLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Apr 2, 2024
1 parent 7771171 commit 1dec097
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
15 changes: 15 additions & 0 deletions examples/t12_groot_howto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ int main()
bool append_to_database = true;
BT::SqliteLogger sqlite_logger(tree, "t12_sqlitelog.db3", append_to_database);

// We can add some extra information to the SqliteLogger, for instance the value of the
// "door_open" blackboard entry, at the end of node "tryOpen" (Fallback)

auto sqlite_callback = [](BT::Duration timestamp, const BT::TreeNode& node,
BT::NodeStatus prev_status,
BT::NodeStatus status) -> std::string {
if(node.name() == "tryOpen" && BT::isStatusCompleted(status))
{
auto is_open = BT::toStr(node.config().blackboard->get<bool>("door_open"));
return "[tryOpen] door_open=" + is_open;
}
return {};
};
sqlite_logger.setMetadataCallback(sqlite_callback);

while(1)
{
std::cout << "Start" << std::endl;
Expand Down
9 changes: 9 additions & 0 deletions include/behaviortree_cpp/loggers/bt_sqlite_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class SqliteLogger : public StatusChangeLogger

virtual ~SqliteLogger() override;

// You can inject a function that add a metadata filed (a string) to the raw in the table.
// The arguments of the function are the same as SqliteLogger::callback()
using MetadataFunc =
std::function<std::string(Duration, const TreeNode&, NodeStatus, NodeStatus)>;
void setMetadataCallback(MetadataFunc func);

virtual void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
NodeStatus status) override;

Expand All @@ -56,6 +62,7 @@ class SqliteLogger : public StatusChangeLogger
int64_t timestamp;
int64_t duration;
NodeStatus status;
std::string metadata;
};

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

MetadataFunc meta_func_;

void writerLoop();
};

Expand Down
17 changes: 14 additions & 3 deletions src/loggers/bt_sqlite_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ SqliteLogger::SqliteLogger(const Tree& tree, std::filesystem::path const& filepa
"session_id INTEGER NOT NULL, "
"uid INTEGER NOT NULL, "
"duration INTEGER, "
"state INTEGER NOT NULL);");
"state INTEGER NOT NULL,"
"metadata VARCHAR );");

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

void SqliteLogger::setMetadataCallback(MetadataFunc func)
{
meta_func_ = func;
}

void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
NodeStatus prev_status, NodeStatus status)
{
Expand Down Expand Up @@ -91,6 +97,11 @@ void SqliteLogger::callback(Duration timestamp, const TreeNode& node,
trans.node_uid = node.UID();
trans.status = status;

if(meta_func_)
{
trans.metadata = meta_func_(timestamp, node, prev_status, status);
}

{
std::scoped_lock lk(queue_mutex_);
transitions_queue_.push_back(trans);
Expand All @@ -116,9 +127,9 @@ void SqliteLogger::writerLoop()
auto const trans = transitions.front();
transitions.pop_front();

sqlite::Statement(*db_, "INSERT INTO Transitions VALUES (?, ?, ?, ?, ?)",
sqlite::Statement(*db_, "INSERT INTO Transitions VALUES (?, ?, ?, ?, ?, ?)",
trans.timestamp, session_id_, trans.node_uid, trans.duration,
static_cast<int>(trans.status));
static_cast<int>(trans.status), trans.metadata);
}
}
}
Expand Down

0 comments on commit 1dec097

Please sign in to comment.