bt_log_cat.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <iostream>
3 #include <fstream>
4 #include <unordered_map>
6 
7 int main(int argc, char* argv[])
8 {
9  if (argc != 2)
10  {
11  printf("Wrong number of arguments\nUsage: %s [filename]\n", argv[0]);
12  return 1;
13  }
14 
15  FILE* file = fopen(argv[1], "rb");
16 
17  if (!file)
18  {
19  printf("Failed to open file: [%s]\n", argv[1]);
20  return 1;
21  }
22 
23  fseek(file, 0L, SEEK_END);
24  const size_t length = ftell(file);
25  fseek(file, 0L, SEEK_SET);
26  std::vector<char> buffer(length);
27  fread(buffer.data(), sizeof(char), length, file);
28  fclose(file);
29 
30  const int bt_header_size = flatbuffers::ReadScalar<uint32_t>(&buffer[0]);
31 
32  auto behavior_tree = BT_Serialization::GetBehaviorTree(&buffer[4]);
33 
34  std::unordered_map<uint16_t, std::string> names_by_uid;
35  std::unordered_map<uint16_t, const BT_Serialization::TreeNode*> node_by_uid;
36 
37  for (const BT_Serialization::TreeNode* node : *(behavior_tree->nodes()))
38  {
39  names_by_uid.insert({node->uid(), std::string(node->instance_name()->c_str())});
40  node_by_uid.insert({node->uid(), node});
41  }
42 
43  printf("----------------------------\n");
44 
45  std::function<void(uint16_t, int)> recursiveStep;
46 
47  recursiveStep = [&](uint16_t uid, int indent) {
48  for (int i = 0; i < indent; i++)
49  {
50  printf(" ");
51  names_by_uid[uid] = std::string(" ") + names_by_uid[uid];
52  }
53  printf("%s\n", names_by_uid[uid].c_str());
54  std::cout << std::flush;
55 
56  const auto& node = node_by_uid[uid];
57 
58  for (size_t i = 0; i < node->children_uid()->size(); i++)
59  {
60  recursiveStep(node->children_uid()->Get(i), indent + 1);
61  }
62  };
63 
64  recursiveStep(behavior_tree->root_uid(), 0);
65 
66  printf("----------------------------\n");
67 
68  constexpr const char* whitespaces = " ";
69  constexpr const size_t ws_count = 25;
70 
71  auto printStatus = [](BT_Serialization::Status status) {
72  switch (status)
73  {
75  return ("\x1b[32m"
76  "SUCCESS"
77  "\x1b[0m"); // RED
79  return ("\x1b[31m"
80  "FAILURE"
81  "\x1b[0m"); // GREEN
83  return ("\x1b[33m"
84  "RUNNING"
85  "\x1b[0m"); // YELLOW
87  return ("\x1b[36m"
88  "IDLE "
89  "\x1b[0m"); // CYAN
90  }
91  return "Undefined";
92  };
93 
94  for (size_t index = bt_header_size + 4; index < length; index += 12)
95  {
96  const uint16_t uid = flatbuffers::ReadScalar<uint16_t>(&buffer[index + 8]);
97  const std::string& name = names_by_uid[uid];
98  const uint32_t t_sec = flatbuffers::ReadScalar<uint32_t>(&buffer[index]);
99  const uint32_t t_usec = flatbuffers::ReadScalar<uint32_t>(&buffer[index + 4]);
100 
101  printf("[%d.%06d]: %s%s %s -> %s\n", t_sec, t_usec, name.c_str(),
102  &whitespaces[std::min(ws_count, name.size())],
103  printStatus(flatbuffers::ReadScalar<BT_Serialization::Status>(&buffer[index + 10])),
104  printStatus(flatbuffers::ReadScalar<BT_Serialization::Status>(&buffer[index + 11])));
105  }
106 
107  return 0;
108 }
static raw_event_t * buffer
Definition: minitrace.cpp:54
int main(int argc, char *argv[])
Definition: bt_log_cat.cpp:7
static FILE * file
Definition: minitrace.cpp:59
const BT_Serialization::BehaviorTree * GetBehaviorTree(const void *buf)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32