Skip to content

Commit 9167bfb

Browse files
authored
fix(autoware_control_evaluator): fix bugprone-exception-escape (#9630)
* fix: bugprone-exception-escape Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp> * fix: cpplint Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp> --------- Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp>
1 parent a85c67b commit 9167bfb

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

evaluator/autoware_control_evaluator/src/control_evaluator_node.cpp

+43-36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <chrono>
2222
#include <filesystem>
2323
#include <fstream>
24+
#include <iostream>
2425
#include <limits>
2526
#include <string>
2627
#include <vector>
@@ -52,45 +53,51 @@ ControlEvaluatorNode::~ControlEvaluatorNode()
5253
return;
5354
}
5455

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+
}
6668

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+
}
7578
}
76-
}
7779

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;
94101
}
95102
}
96103

0 commit comments

Comments
 (0)