00001 00002 #include "behaviortree_cpp/loggers/bt_minitrace_logger.h" 00003 #include "minitrace/minitrace.h" 00004 00005 namespace BT 00006 { 00007 std::atomic<bool> MinitraceLogger::ref_count(false); 00008 00009 MinitraceLogger::MinitraceLogger(TreeNode* root_node, const char* filename_json) 00010 : StatusChangeLogger(root_node) 00011 { 00012 bool expected = false; 00013 if (!ref_count.compare_exchange_strong(expected, true)) 00014 { 00015 throw std::logic_error("Only one instance of StdCoutLogger shall be created"); 00016 } 00017 00018 minitrace::mtr_register_sigint_handler(); 00019 minitrace::mtr_init(filename_json); 00020 this->enableTransitionToIdle(true); 00021 } 00022 00023 MinitraceLogger::~MinitraceLogger() 00024 { 00025 minitrace::mtr_flush(); 00026 minitrace::mtr_shutdown(); 00027 ref_count = false; 00028 } 00029 00030 void MinitraceLogger::callback(Duration /*timestamp*/, 00031 const TreeNode& node, NodeStatus prev_status, 00032 NodeStatus status) 00033 { 00034 using namespace minitrace; 00035 00036 const bool statusCompleted = (status == NodeStatus::SUCCESS || status == NodeStatus::FAILURE); 00037 00038 const char* category = toStr(node.type()); 00039 const char* name = node.name().c_str(); 00040 00041 if (prev_status == NodeStatus::IDLE && statusCompleted) 00042 { 00043 MTR_INSTANT(category, name); 00044 } 00045 else if (status == NodeStatus::RUNNING) 00046 { 00047 MTR_BEGIN(category, name); 00048 } 00049 else if (prev_status == NodeStatus::RUNNING && statusCompleted) 00050 { 00051 MTR_END(category, name); 00052 } 00053 } 00054 00055 void MinitraceLogger::flush() 00056 { 00057 minitrace::mtr_flush(); 00058 } 00059 } // end namespace