Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autoware_control_evaluator): fix bugprone-exception-escape #9630

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
Expand Down Expand Up @@ -52,45 +53,51 @@
return;
}

// generate json data
using json = nlohmann::json;
json j;
for (Metric metric : metrics_) {
const std::string base_name = metric_to_str.at(metric) + "/";
j[base_name + "min"] = metric_accumulators_[static_cast<size_t>(metric)].min();
j[base_name + "max"] = metric_accumulators_[static_cast<size_t>(metric)].max();
j[base_name + "mean"] = metric_accumulators_[static_cast<size_t>(metric)].mean();
j[base_name + "count"] = metric_accumulators_[static_cast<size_t>(metric)].count();
j[base_name + "description"] = metric_descriptions.at(metric);
}
try {
// generate json data
using json = nlohmann::json;
json j;
for (Metric metric : metrics_) {
const std::string base_name = metric_to_str.at(metric) + "/";
j[base_name + "min"] = metric_accumulators_[static_cast<size_t>(metric)].min();
j[base_name + "max"] = metric_accumulators_[static_cast<size_t>(metric)].max();
j[base_name + "mean"] = metric_accumulators_[static_cast<size_t>(metric)].mean();
j[base_name + "count"] = metric_accumulators_[static_cast<size_t>(metric)].count();
j[base_name + "description"] = metric_descriptions.at(metric);
}

// get output folder
const std::string output_folder_str =
rclcpp::get_logging_directory().string() + "/autoware_metrics";
if (!std::filesystem::exists(output_folder_str)) {
if (!std::filesystem::create_directories(output_folder_str)) {
RCLCPP_ERROR(
this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str());
return;
// get output folder
const std::string output_folder_str =
rclcpp::get_logging_directory().string() + "/autoware_metrics";
if (!std::filesystem::exists(output_folder_str)) {
if (!std::filesystem::create_directories(output_folder_str)) {
RCLCPP_ERROR(
this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str());
return;
}
}
}

// get time stamp
std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm * local_time = std::localtime(&now_time_t);
std::ostringstream oss;
oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");
std::string cur_time_str = oss.str();

// Write metrics .json to file
const std::string output_file_str =
output_folder_str + "/autoware_control_evaluator-" + cur_time_str + ".json";
std::ofstream f(output_file_str);
if (f.is_open()) {
f << j.dump(4);
f.close();
} else {
RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str());
// get time stamp
std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm * local_time = std::localtime(&now_time_t);

Check warning on line 82 in evaluator/autoware_control_evaluator/src/control_evaluator_node.cpp

View check run for this annotation

Codecov / codecov/patch

evaluator/autoware_control_evaluator/src/control_evaluator_node.cpp#L81-L82

Added lines #L81 - L82 were not covered by tests
std::ostringstream oss;
oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");
std::string cur_time_str = oss.str();

// Write metrics .json to file
const std::string output_file_str =
output_folder_str + "/autoware_control_evaluator-" + cur_time_str + ".json";
std::ofstream f(output_file_str);
if (f.is_open()) {
f << j.dump(4);
f.close();
} else {
RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str());
}
} catch (const std::exception & e) {
std::cerr << "Exception in ControlEvaluatorNode destructor: " << e.what() << std::endl;
} catch (...) {

Check warning on line 99 in evaluator/autoware_control_evaluator/src/control_evaluator_node.cpp

View check run for this annotation

Codecov / codecov/patch

evaluator/autoware_control_evaluator/src/control_evaluator_node.cpp#L98-L99

Added lines #L98 - L99 were not covered by tests
std::cerr << "Unknown exception in ControlEvaluatorNode destructor" << std::endl;
}
}

Expand Down
Loading