Skip to content

Commit

Permalink
fix(autoware_processing_time_checker): fix bugprone-exception-escape (#…
Browse files Browse the repository at this point in the history
…9780)

* fix: bugprone-error

Signed-off-by: kobayu858 <[email protected]>

* fix: cpplint

Signed-off-by: kobayu858 <[email protected]>

---------

Signed-off-by: kobayu858 <[email protected]>
  • Loading branch information
kobayu858 authored Dec 25, 2024
1 parent fe597e2 commit d0c39d3
Showing 1 changed file with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -103,45 +104,51 @@ ProcessingTimeChecker::~ProcessingTimeChecker()
return;
}

// generate json data
nlohmann::json j;
for (const auto & accumulator_iterator : processing_time_accumulator_map_) {
const auto module_name = accumulator_iterator.first;
const auto processing_time_accumulator = accumulator_iterator.second;
j[module_name + "/min"] = processing_time_accumulator.min();
j[module_name + "/max"] = processing_time_accumulator.max();
j[module_name + "/mean"] = processing_time_accumulator.mean();
j[module_name + "/count"] = processing_time_accumulator.count();
j[module_name + "/description"] = "processing time of " + module_name + "[ms]";
}
try {
// generate json data
nlohmann::json j;
for (const auto & accumulator_iterator : processing_time_accumulator_map_) {
const auto module_name = accumulator_iterator.first;
const auto processing_time_accumulator = accumulator_iterator.second;
j[module_name + "/min"] = processing_time_accumulator.min();
j[module_name + "/max"] = processing_time_accumulator.max();
j[module_name + "/mean"] = processing_time_accumulator.mean();
j[module_name + "/count"] = processing_time_accumulator.count();
j[module_name + "/description"] = "processing time of " + module_name + "[ms]";
}

// 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_processing_time_checker-" + 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);
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_processing_time_checker-" + 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 ProcessingTimeChecker: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Unknown exception in ProcessingTimeChecker" << std::endl;
}
}

Expand Down

0 comments on commit d0c39d3

Please sign in to comment.