Skip to content

Commit 3bb2df2

Browse files
authored
fix(autoware_planning_evaluator): fix bugprone-exception-escape (#9730)
fix: bugprone-exception-escape Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp>
1 parent 07db183 commit 3bb2df2

File tree

2 files changed

+86
-73
lines changed

2 files changed

+86
-73
lines changed

evaluator/autoware_planning_evaluator/src/motion_evaluator_node.cpp

+43-37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <chrono>
2020
#include <filesystem>
2121
#include <fstream>
22+
#include <iostream>
2223
#include <memory>
2324
#include <string>
2425
#include <vector>
@@ -50,47 +51,52 @@ MotionEvaluatorNode::~MotionEvaluatorNode()
5051
if (!output_metrics_) {
5152
return;
5253
}
53-
54-
// generate json data
55-
using json = nlohmann::json;
56-
json j;
57-
for (Metric metric : metrics_) {
58-
const std::string base_name = metric_to_str.at(metric) + "/";
59-
const auto & stat = metrics_calculator_.calculate(metric, accumulated_trajectory_);
60-
if (stat) {
61-
j[base_name + "min"] = stat->min();
62-
j[base_name + "max"] = stat->max();
63-
j[base_name + "mean"] = stat->mean();
54+
try {
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+
const auto & stat = metrics_calculator_.calculate(metric, accumulated_trajectory_);
61+
if (stat) {
62+
j[base_name + "min"] = stat->min();
63+
j[base_name + "max"] = stat->max();
64+
j[base_name + "mean"] = stat->mean();
65+
}
6466
}
65-
}
6667

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;
68+
// get output folder
69+
const std::string output_folder_str =
70+
rclcpp::get_logging_directory().string() + "/autoware_metrics";
71+
if (!std::filesystem::exists(output_folder_str)) {
72+
if (!std::filesystem::create_directories(output_folder_str)) {
73+
RCLCPP_ERROR(
74+
this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str());
75+
return;
76+
}
7577
}
76-
}
7778

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_motion_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());
79+
// get time stamp
80+
std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
81+
std::tm * local_time = std::localtime(&now_time_t);
82+
std::ostringstream oss;
83+
oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");
84+
std::string cur_time_str = oss.str();
85+
86+
// Write metrics .json to file
87+
const std::string output_file_str =
88+
output_folder_str + "/autoware_motion_evaluator-" + cur_time_str + ".json";
89+
std::ofstream f(output_file_str);
90+
if (f.is_open()) {
91+
f << j.dump(4);
92+
f.close();
93+
} else {
94+
RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str());
95+
}
96+
} catch (const std::exception & e) {
97+
std::cerr << "Exception in MotionEvaluatorNode destructor: " << e.what() << std::endl;
98+
} catch (...) {
99+
std::cerr << "Unknown exception in MotionEvaluatorNode destructor" << std::endl;
94100
}
95101
}
96102

evaluator/autoware_planning_evaluator/src/planning_evaluator_node.cpp

+43-36
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <chrono>
2626
#include <filesystem>
2727
#include <fstream>
28+
#include <iostream>
2829
#include <limits>
2930
#include <memory>
3031
#include <string>
@@ -74,45 +75,51 @@ PlanningEvaluatorNode::~PlanningEvaluatorNode()
7475
return;
7576
}
7677

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

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+
}
97100
}
98-
}
99101

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;
116123
}
117124
}
118125

0 commit comments

Comments
 (0)