bt_log_cat.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <fstream>
00004 #include <unordered_map>
00005 #include "behaviortree_cpp/flatbuffers/BT_logger_generated.h"
00006 
00007 int main(int argc, char* argv[])
00008 {
00009     if (argc != 2)
00010     {
00011         printf("Wrong number of arguments\nUsage: %s [filename]\n", argv[0]);
00012         return 1;
00013     }
00014 
00015     FILE* file = fopen(argv[1], "rb");
00016 
00017     if (!file)
00018     {
00019         printf("Failed to open file: [%s]\n", argv[1]);
00020         return 1;
00021     }
00022 
00023     fseek(file, 0L, SEEK_END);
00024     const size_t length = ftell(file);
00025     fseek(file, 0L, SEEK_SET);
00026     std::vector<char> buffer(length);
00027     fread(buffer.data(), sizeof(char), length, file);
00028     fclose(file);
00029 
00030     const int bt_header_size = flatbuffers::ReadScalar<uint32_t>(&buffer[0]);
00031 
00032     auto behavior_tree = Serialization::GetBehaviorTree(&buffer[4]);
00033 
00034     std::unordered_map<uint16_t, std::string> names_by_uid;
00035     std::unordered_map<uint16_t, const Serialization::TreeNode*> node_by_uid;
00036 
00037     for (const Serialization::TreeNode* node : *(behavior_tree->nodes()))
00038     {
00039         names_by_uid.insert({node->uid(), std::string(node->instance_name()->c_str())});
00040         node_by_uid.insert({node->uid(), node});
00041     }
00042 
00043     printf("----------------------------\n");
00044 
00045     std::function<void(uint16_t, int)> recursiveStep;
00046 
00047     recursiveStep = [&](uint16_t uid, int indent) {
00048         for (int i = 0; i < indent; i++)
00049         {
00050             printf("    ");
00051             names_by_uid[uid] = std::string("   ") + names_by_uid[uid];
00052         }
00053         printf("%s\n", names_by_uid[uid].c_str());
00054         std::cout << std::flush;
00055 
00056         const auto& node = node_by_uid[uid];
00057 
00058         for (size_t i = 0; i < node->children_uid()->size(); i++)
00059         {
00060             recursiveStep(node->children_uid()->Get(i), indent + 1);
00061         }
00062     };
00063 
00064     recursiveStep(behavior_tree->root_uid(), 0);
00065 
00066     printf("----------------------------\n");
00067 
00068     constexpr const char* whitespaces = "                         ";
00069     constexpr const size_t ws_count = 25;
00070 
00071     auto printStatus = [](Serialization::NodeStatus status) {
00072         switch (status)
00073         {
00074             case Serialization::NodeStatus::SUCCESS:
00075                 return ("\x1b[32m"
00076                         "SUCCESS"
00077                         "\x1b[0m");   // RED
00078             case Serialization::NodeStatus::FAILURE:
00079                 return ("\x1b[31m"
00080                         "FAILURE"
00081                         "\x1b[0m");   // GREEN
00082             case Serialization::NodeStatus::RUNNING:
00083                 return ("\x1b[33m"
00084                         "RUNNING"
00085                         "\x1b[0m");   // YELLOW
00086             case Serialization::NodeStatus::IDLE:
00087                 return ("\x1b[36m"
00088                         "IDLE   "
00089                         "\x1b[0m");   // CYAN
00090         }
00091         return "Undefined";
00092     };
00093 
00094     for (size_t index = bt_header_size + 4; index < length; index += 12)
00095     {
00096         const uint16_t uid = flatbuffers::ReadScalar<uint16_t>(&buffer[index + 8]);
00097         const std::string& name = names_by_uid[uid];
00098         const uint32_t t_sec = flatbuffers::ReadScalar<uint32_t>(&buffer[index]);
00099         const uint32_t t_usec = flatbuffers::ReadScalar<uint32_t>(&buffer[index + 4]);
00100 
00101         printf("[%d.%06d]: %s%s %s -> %s\n", t_sec, t_usec, name.c_str(),
00102                &whitespaces[std::min(ws_count, name.size())],
00103                printStatus(flatbuffers::ReadScalar<Serialization::NodeStatus>(&buffer[index + 10])),
00104                printStatus(flatbuffers::ReadScalar<Serialization::NodeStatus>(&buffer[index + 11])));
00105     }
00106 
00107     return 0;
00108 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 20:17:15