From eb255dec9e0a7ccc9173a2fca787303614378a90 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 11 Dec 2022 10:21:32 +0100 Subject: [PATCH] Avoid throwing in Tlm_CRC_Stats destructor (fix bugprone-exception-escape check). More consistent private member naming. --- .../telemetry_decoder/libs/tlm_crc_stats.cc | 104 ++++++++++-------- .../telemetry_decoder/libs/tlm_crc_stats.h | 12 +- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.cc b/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.cc index 840a3aba9..2a57d55ac 100644 --- a/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.cc +++ b/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.cc @@ -23,21 +23,21 @@ #include // for std::move -void Tlm_CRC_Stats::initialize(std::string dump_crc_stats_filename_) +void Tlm_CRC_Stats::initialize(std::string dump_crc_stats_filename) { - d_dump_crc_stats_filename = std::move(dump_crc_stats_filename_); + d_dump_crc_stats_filename = std::move(dump_crc_stats_filename); - enable_crc_stats = true; - num_crc_ok = 0; - num_crc_not_ok = 0; + d_enable_crc_stats = true; + d_num_crc_ok = 0; + d_num_crc_not_ok = 0; } -bool Tlm_CRC_Stats::set_channel(int32_t channel_) +bool Tlm_CRC_Stats::set_channel(int32_t channel) { std::string dump_path; - channel = channel_; + d_channel = channel; // Get path if (d_dump_crc_stats_filename.find_last_of('/') != std::string::npos) @@ -57,7 +57,7 @@ bool Tlm_CRC_Stats::set_channel(int32_t channel_) } d_dump_crc_stats_filename.append("_ch"); - d_dump_crc_stats_filename.append(std::to_string(channel)); + d_dump_crc_stats_filename.append(std::to_string(d_channel)); d_dump_crc_stats_filename.append(".txt"); d_dump_crc_stats_filename = dump_path + fs::path::preferred_separator + d_dump_crc_stats_filename; @@ -65,17 +65,17 @@ bool Tlm_CRC_Stats::set_channel(int32_t channel_) if (!gnss_sdr_create_directory(dump_path)) { std::cerr << "GNSS-SDR cannot create telemetry CRC stats dump file for the Telemetry block. The telemetry CRC statistics has been disabled. Wrong permissions?\n"; - enable_crc_stats = false; + d_enable_crc_stats = false; } - d_dump_file.exceptions(std::ifstream::failbit | std::ifstream::badbit); + d_dump_file.exceptions(std::ofstream::failbit | std::ofstream::badbit); try { d_dump_file.open(d_dump_crc_stats_filename.c_str(), std::ios::out); } - catch (const std::ifstream::failure &e) + catch (const std::ofstream::failure &e) { LOG(WARNING) << "Exception opening telemetry CRC stats dump file " << e.what(); - enable_crc_stats = false; + d_enable_crc_stats = false; } return true; @@ -86,61 +86,69 @@ void Tlm_CRC_Stats::update_CRC_stats(bool CRC) { if (CRC) { - num_crc_ok++; + d_num_crc_ok++; } else { - num_crc_not_ok++; + d_num_crc_not_ok++; } } Tlm_CRC_Stats::~Tlm_CRC_Stats() { - uint32_t num_crc_tests = num_crc_ok + num_crc_not_ok; + const uint32_t num_crc_tests = d_num_crc_ok + d_num_crc_not_ok; float success_rate = 0.0; if (num_crc_tests > 0) { - success_rate = static_cast(num_crc_ok) / static_cast(num_crc_tests); + success_rate = static_cast(d_num_crc_ok) / static_cast(num_crc_tests); } - std::string txt_num_crc_tests("Num CRC Tests"); - uint32_t align_num_crc_tests = txt_num_crc_tests.length(); - std::string txt_success_tests(" | Successful Tests"); - uint32_t align_success_tests = txt_success_tests.length(); - std::string txt_success_rate(" | Success rate"); - uint32_t align_success_rate = txt_success_rate.length(); - std::string txt_delimiter(" |"); - uint32_t align_delimiter = txt_delimiter.length(); - if (enable_crc_stats) + try { - // write results to the telemetry CRC statistics output file - try - { - d_dump_file << txt_num_crc_tests << txt_success_tests << txt_success_rate << std::endl; - d_dump_file << std::setw(align_num_crc_tests) << num_crc_tests << txt_delimiter << std::setw(align_success_tests - align_delimiter) << num_crc_ok << txt_delimiter << std::setw(align_success_rate - align_delimiter) << std::setprecision(4) << success_rate << std::endl; - } - catch (const std::exception &ex) - { - DLOG(INFO) << "Telemetry CRC stats cannot write on the output file " << d_dump_crc_stats_filename.c_str(); - } + const std::string txt_num_crc_tests("Num CRC Tests"); + const auto align_num_crc_tests = txt_num_crc_tests.length(); + const std::string txt_success_tests(" | Successful Tests"); + const auto align_success_tests = txt_success_tests.length(); + const std::string txt_success_rate(" | Success rate"); + const auto align_success_rate = txt_success_rate.length(); + const std::string txt_delimiter(" |"); + const auto align_delimiter = txt_delimiter.length(); - const auto pos = d_dump_file.tellp(); - try + if (d_enable_crc_stats && d_dump_file.is_open()) { - d_dump_file.close(); - } - catch (const std::exception &ex) - { - LOG(WARNING) << "Exception in destructor closing the telemetry CRC stats dump file " << ex.what(); - } - if (pos == 0) - { - errorlib::error_code ec; - if (!fs::remove(fs::path(d_dump_crc_stats_filename), ec)) + // write results to the telemetry CRC statistics output file + try { - std::cerr << "Problem removing telemetry CRC stats temporary file " << d_dump_crc_stats_filename << '\n'; + d_dump_file << txt_num_crc_tests << txt_success_tests << txt_success_rate << std::endl; + d_dump_file << std::setw(align_num_crc_tests) << num_crc_tests << txt_delimiter << std::setw(align_success_tests - align_delimiter) << d_num_crc_ok << txt_delimiter << std::setw(align_success_rate - align_delimiter) << std::setprecision(4) << success_rate << std::endl; + } + catch (const std::exception &ex) + { + DLOG(INFO) << "Telemetry CRC stats cannot write on the output file " << d_dump_crc_stats_filename.c_str(); + } + + const auto pos = d_dump_file.tellp(); + try + { + d_dump_file.close(); + } + catch (const std::exception &ex) + { + LOG(WARNING) << "Exception in destructor closing the telemetry CRC stats dump file " << ex.what(); + } + if (pos == 0) + { + errorlib::error_code ec; + if (!fs::remove(fs::path(d_dump_crc_stats_filename), ec)) + { + std::cerr << "Problem removing telemetry CRC stats temporary file " << d_dump_crc_stats_filename << '\n'; + } } } } + catch (const std::exception &e) + { + std::cerr << "Problem in Tlm_CRC_Stats destructor: " << e.what() << '\n'; + } } diff --git a/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.h b/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.h index 110464ba1..9da8e4c9e 100644 --- a/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.h +++ b/src/algorithms/telemetry_decoder/libs/tlm_crc_stats.h @@ -39,12 +39,12 @@ public: /*! * \brief Initialize the telemetry CRC statistics */ - void initialize(std::string dump_crc_stats_filename_); + void initialize(std::string dump_crc_stats_filename); /*! * \brief Initialize the channel number and output file */ - bool set_channel(int32_t channel_); + bool set_channel(int32_t channel); /*! * \brief Update the CRC statistics @@ -54,10 +54,10 @@ public: private: std::ofstream d_dump_file; std::string d_dump_crc_stats_filename; - uint32_t num_crc_ok{0}; - uint32_t num_crc_not_ok{0}; - int32_t channel{0}; - bool enable_crc_stats{false}; + uint32_t d_num_crc_ok{0}; + uint32_t d_num_crc_not_ok{0}; + int32_t d_channel{0}; + bool d_enable_crc_stats{false}; };