|
21 | 21 | #include <chrono>
|
22 | 22 | #include <filesystem>
|
23 | 23 | #include <fstream>
|
| 24 | +#include <iostream> |
24 | 25 | #include <limits>
|
25 | 26 | #include <string>
|
26 | 27 | #include <vector>
|
@@ -52,45 +53,51 @@ ControlEvaluatorNode::~ControlEvaluatorNode()
|
52 | 53 | return;
|
53 | 54 | }
|
54 | 55 |
|
55 |
| - // generate json data |
56 |
| - using json = nlohmann::json; |
57 |
| - json j; |
58 |
| - for (Metric metric : metrics_) { |
59 |
| - const std::string base_name = metric_to_str.at(metric) + "/"; |
60 |
| - j[base_name + "min"] = metric_accumulators_[static_cast<size_t>(metric)].min(); |
61 |
| - j[base_name + "max"] = metric_accumulators_[static_cast<size_t>(metric)].max(); |
62 |
| - j[base_name + "mean"] = metric_accumulators_[static_cast<size_t>(metric)].mean(); |
63 |
| - j[base_name + "count"] = metric_accumulators_[static_cast<size_t>(metric)].count(); |
64 |
| - j[base_name + "description"] = metric_descriptions.at(metric); |
65 |
| - } |
| 56 | + try { |
| 57 | + // generate json data |
| 58 | + using json = nlohmann::json; |
| 59 | + json j; |
| 60 | + for (Metric metric : metrics_) { |
| 61 | + const std::string base_name = metric_to_str.at(metric) + "/"; |
| 62 | + j[base_name + "min"] = metric_accumulators_[static_cast<size_t>(metric)].min(); |
| 63 | + j[base_name + "max"] = metric_accumulators_[static_cast<size_t>(metric)].max(); |
| 64 | + j[base_name + "mean"] = metric_accumulators_[static_cast<size_t>(metric)].mean(); |
| 65 | + j[base_name + "count"] = metric_accumulators_[static_cast<size_t>(metric)].count(); |
| 66 | + j[base_name + "description"] = metric_descriptions.at(metric); |
| 67 | + } |
66 | 68 |
|
67 |
| - // get output folder |
68 |
| - const std::string output_folder_str = |
69 |
| - rclcpp::get_logging_directory().string() + "/autoware_metrics"; |
70 |
| - if (!std::filesystem::exists(output_folder_str)) { |
71 |
| - if (!std::filesystem::create_directories(output_folder_str)) { |
72 |
| - RCLCPP_ERROR( |
73 |
| - this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str()); |
74 |
| - return; |
| 69 | + // get output folder |
| 70 | + const std::string output_folder_str = |
| 71 | + rclcpp::get_logging_directory().string() + "/autoware_metrics"; |
| 72 | + if (!std::filesystem::exists(output_folder_str)) { |
| 73 | + if (!std::filesystem::create_directories(output_folder_str)) { |
| 74 | + RCLCPP_ERROR( |
| 75 | + this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str()); |
| 76 | + return; |
| 77 | + } |
75 | 78 | }
|
76 |
| - } |
77 | 79 |
|
78 |
| - // get time stamp |
79 |
| - std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
80 |
| - std::tm * local_time = std::localtime(&now_time_t); |
81 |
| - std::ostringstream oss; |
82 |
| - oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S"); |
83 |
| - std::string cur_time_str = oss.str(); |
84 |
| - |
85 |
| - // Write metrics .json to file |
86 |
| - const std::string output_file_str = |
87 |
| - output_folder_str + "/autoware_control_evaluator-" + cur_time_str + ".json"; |
88 |
| - std::ofstream f(output_file_str); |
89 |
| - if (f.is_open()) { |
90 |
| - f << j.dump(4); |
91 |
| - f.close(); |
92 |
| - } else { |
93 |
| - RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str()); |
| 80 | + // get time stamp |
| 81 | + std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
| 82 | + std::tm * local_time = std::localtime(&now_time_t); |
| 83 | + std::ostringstream oss; |
| 84 | + oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S"); |
| 85 | + std::string cur_time_str = oss.str(); |
| 86 | + |
| 87 | + // Write metrics .json to file |
| 88 | + const std::string output_file_str = |
| 89 | + output_folder_str + "/autoware_control_evaluator-" + cur_time_str + ".json"; |
| 90 | + std::ofstream f(output_file_str); |
| 91 | + if (f.is_open()) { |
| 92 | + f << j.dump(4); |
| 93 | + f.close(); |
| 94 | + } else { |
| 95 | + RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str()); |
| 96 | + } |
| 97 | + } catch (const std::exception & e) { |
| 98 | + std::cerr << "Exception in ControlEvaluatorNode destructor: " << e.what() << std::endl; |
| 99 | + } catch (...) { |
| 100 | + std::cerr << "Unknown exception in ControlEvaluatorNode destructor" << std::endl; |
94 | 101 | }
|
95 | 102 | }
|
96 | 103 |
|
|
0 commit comments