|
25 | 25 | #include <chrono>
|
26 | 26 | #include <filesystem>
|
27 | 27 | #include <fstream>
|
| 28 | +#include <iostream> |
28 | 29 | #include <limits>
|
29 | 30 | #include <memory>
|
30 | 31 | #include <string>
|
@@ -74,45 +75,51 @@ PlanningEvaluatorNode::~PlanningEvaluatorNode()
|
74 | 75 | return;
|
75 | 76 | }
|
76 | 77 |
|
77 |
| - // generate json data |
78 |
| - using json = nlohmann::json; |
79 |
| - json j; |
80 |
| - for (Metric metric : metrics_) { |
81 |
| - const std::string base_name = metric_to_str.at(metric) + "/"; |
82 |
| - j[base_name + "min"] = metric_accumulators_[static_cast<size_t>(metric)][0].min(); |
83 |
| - j[base_name + "max"] = metric_accumulators_[static_cast<size_t>(metric)][1].max(); |
84 |
| - j[base_name + "mean"] = metric_accumulators_[static_cast<size_t>(metric)][2].mean(); |
85 |
| - j[base_name + "count"] = metric_accumulators_[static_cast<size_t>(metric)][2].count(); |
86 |
| - j[base_name + "description"] = metric_descriptions.at(metric); |
87 |
| - } |
| 78 | + try { |
| 79 | + // generate json data |
| 80 | + using json = nlohmann::json; |
| 81 | + json j; |
| 82 | + for (Metric metric : metrics_) { |
| 83 | + const std::string base_name = metric_to_str.at(metric) + "/"; |
| 84 | + j[base_name + "min"] = metric_accumulators_[static_cast<size_t>(metric)][0].min(); |
| 85 | + j[base_name + "max"] = metric_accumulators_[static_cast<size_t>(metric)][1].max(); |
| 86 | + j[base_name + "mean"] = metric_accumulators_[static_cast<size_t>(metric)][2].mean(); |
| 87 | + j[base_name + "count"] = metric_accumulators_[static_cast<size_t>(metric)][2].count(); |
| 88 | + j[base_name + "description"] = metric_descriptions.at(metric); |
| 89 | + } |
88 | 90 |
|
89 |
| - // get output folder |
90 |
| - const std::string output_folder_str = |
91 |
| - rclcpp::get_logging_directory().string() + "/autoware_metrics"; |
92 |
| - if (!std::filesystem::exists(output_folder_str)) { |
93 |
| - if (!std::filesystem::create_directories(output_folder_str)) { |
94 |
| - RCLCPP_ERROR( |
95 |
| - this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str()); |
96 |
| - return; |
| 91 | + // get output folder |
| 92 | + const std::string output_folder_str = |
| 93 | + rclcpp::get_logging_directory().string() + "/autoware_metrics"; |
| 94 | + if (!std::filesystem::exists(output_folder_str)) { |
| 95 | + if (!std::filesystem::create_directories(output_folder_str)) { |
| 96 | + RCLCPP_ERROR( |
| 97 | + this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str()); |
| 98 | + return; |
| 99 | + } |
97 | 100 | }
|
98 |
| - } |
99 | 101 |
|
100 |
| - // get time stamp |
101 |
| - std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
102 |
| - std::tm * local_time = std::localtime(&now_time_t); |
103 |
| - std::ostringstream oss; |
104 |
| - oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S"); |
105 |
| - std::string cur_time_str = oss.str(); |
106 |
| - |
107 |
| - // Write metrics .json to file |
108 |
| - const std::string output_file_str = |
109 |
| - output_folder_str + "/autoware_planning_evaluator-" + cur_time_str + ".json"; |
110 |
| - std::ofstream f(output_file_str); |
111 |
| - if (f.is_open()) { |
112 |
| - f << j.dump(4); |
113 |
| - f.close(); |
114 |
| - } else { |
115 |
| - RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str()); |
| 102 | + // get time stamp |
| 103 | + std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
| 104 | + std::tm * local_time = std::localtime(&now_time_t); |
| 105 | + std::ostringstream oss; |
| 106 | + oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S"); |
| 107 | + std::string cur_time_str = oss.str(); |
| 108 | + |
| 109 | + // Write metrics .json to file |
| 110 | + const std::string output_file_str = |
| 111 | + output_folder_str + "/autoware_planning_evaluator-" + cur_time_str + ".json"; |
| 112 | + std::ofstream f(output_file_str); |
| 113 | + if (f.is_open()) { |
| 114 | + f << j.dump(4); |
| 115 | + f.close(); |
| 116 | + } else { |
| 117 | + RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str()); |
| 118 | + } |
| 119 | + } catch (const std::exception & e) { |
| 120 | + std::cerr << "Exception in MotionEvaluatorNode destructor: " << e.what() << std::endl; |
| 121 | + } catch (...) { |
| 122 | + std::cerr << "Unknown exception in MotionEvaluatorNode destructor" << std::endl; |
116 | 123 | }
|
117 | 124 | }
|
118 | 125 |
|
|
0 commit comments