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: catch bad fmt format strings at compile-time #267

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
58 changes: 24 additions & 34 deletions src/iguana/algorithms/Algorithm.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Algorithm.h"
#include "iguana/services/LoggerMacros.h"

#include <numeric>

Expand All @@ -22,12 +23,12 @@ namespace iguana {
auto val = opt ? opt.value() : m_yaml_config->GetScalar<OPTION_TYPE>(node_path);
if(key != "") {
m_option_cache[key] = val;
m_log->Debug("CACHED OPTION: {:>20} = {}", key, PrintOptionValue(key));
DEBUG("CACHED OPTION: {:>20} = {}", key, PrintOptionValue(key));
}
return val;
}
catch(std::runtime_error const& ex) {
m_log->Error("Failed to `GetOptionScalar` for key '{}'", key);
ERROR("Failed to `GetOptionScalar` for key '{}'", key);
throw std::runtime_error("config file parsing issue");
}
}
Expand All @@ -46,12 +47,12 @@ namespace iguana {
auto val = opt ? opt.value() : m_yaml_config->GetVector<OPTION_TYPE>(node_path);
if(key != "") {
m_option_cache[key] = val;
m_log->Debug("CACHED OPTION: {:>20} = {}", key, PrintOptionValue(key));
DEBUG("CACHED OPTION: {:>20} = {}", key, PrintOptionValue(key));
}
return val;
}
catch(std::runtime_error const& ex) {
m_log->Error("Failed to `GetOptionVector` for key '{}'", key);
ERROR("Failed to `GetOptionVector` for key '{}'", key);
throw std::runtime_error("config file parsing issue");
}
}
Expand Down Expand Up @@ -117,15 +118,15 @@ namespace iguana {
if(!m_yaml_config) {
o_user_config_file = GetCachedOption<std::string>("config_file").value_or("");
o_user_config_dir = GetCachedOption<std::string>("config_dir").value_or("");
m_log->Debug("Instantiating `YAMLReader`");
DEBUG("Instantiating `YAMLReader`");
m_yaml_config = std::make_unique<YAMLReader>("config|" + m_name);
m_yaml_config->SetLogLevel(m_log->GetLevel());
m_yaml_config->SetLogLevel(GetLogLevel());
m_yaml_config->AddDirectory(o_user_config_dir);
m_yaml_config->AddFile(m_default_config_file);
m_yaml_config->AddFile(o_user_config_file);
}
else
m_log->Debug("`YAMLReader` already instantiated for this algorithm; using that");
DEBUG("`YAMLReader` already instantiated for this algorithm; using that");
m_yaml_config->LoadFiles();
}

Expand All @@ -141,14 +142,14 @@ namespace iguana {
[&bank_name](auto& bank)
{ return bank.getSchema().getName() == bank_name; });
if(it == banks.end()) {
m_log->Error("required input bank '{}' not found; cannot `Start` algorithm '{}'", bank_name, m_class_name);
ERROR("required input bank '{}' not found; cannot `Start` algorithm '{}'", bank_name, m_class_name);
auto creators = AlgorithmFactory::QueryNewBank(bank_name);
if(creators)
m_log->Error(" -> this bank is created by algorithm(s) [{}]; please `Start` ONE of them BEFORE this algorithm", fmt::join(creators.value(), ", "));
ERROR(" -> this bank is created by algorithm(s) [{}]; please `Start` ONE of them BEFORE this algorithm", fmt::join(creators.value(), ", "));
throw std::runtime_error("cannot cache bank index");
}
auto idx = std::distance(banks.begin(), it);
m_log->Debug("cached index of bank '{}' is {}", bank_name, idx);
DEBUG("cached index of bank '{}' is {}", bank_name, idx);
return idx;
}

Expand All @@ -175,12 +176,12 @@ namespace iguana {
return fmt::format("({}) [{}]", fmt::join(valQuoted, ", "), "vector<string>");
}
else {
m_log->Error("option '{}' type has no printer defined in Algorithm::PrintOptionValue", key);
ERROR("option '{}' type has no printer defined in Algorithm::PrintOptionValue", key);
return "UNKNOWN";
}
}
else
m_log->Error("option '{}' not found by Algorithm::PrintOptionValue", key);
ERROR("option '{}' not found by Algorithm::PrintOptionValue", key);
return "UNKNOWN";
}

Expand All @@ -189,21 +190,22 @@ namespace iguana {
hipo::bank& Algorithm::GetBank(hipo::banklist& banks, hipo::banklist::size_type const idx, std::string const& expected_bank_name) const
{
if(m_rows_only) {
m_log->Error("algorithm is in 'rows only' mode; cannot call `Run` since banks are not cached; use action function(s) instead");
ERROR("algorithm is in 'rows only' mode; cannot call `Run` since banks are not cached; use action function(s) instead");
}
else {
try {
auto& result = banks.at(idx);
if(expected_bank_name != "" && result.getSchema().getName() != expected_bank_name)
m_log->Error("expected input bank '{}' at index={}; got bank named '{}'", expected_bank_name, idx, result.getSchema().getName());
if(expected_bank_name != "" && result.getSchema().getName() != expected_bank_name) {
ERROR("expected input bank '{}' at index={}; got bank named '{}'", expected_bank_name, idx, result.getSchema().getName());
}
else
return result;
}
catch(std::out_of_range const& o) {
m_log->Error("required input bank '{}' not found; cannot `Run` algorithm '{}'", expected_bank_name, m_class_name);
ERROR("required input bank '{}' not found; cannot `Run` algorithm '{}'", expected_bank_name, m_class_name);
auto creators = AlgorithmFactory::QueryNewBank(expected_bank_name);
if(creators)
m_log->Error(" -> this bank is created by algorithm(s) [{}]; please `Run` ONE of them BEFORE this algorithm", fmt::join(creators.value(), ", "));
ERROR(" -> this bank is created by algorithm(s) [{}]; please `Run` ONE of them BEFORE this algorithm", fmt::join(creators.value(), ", "));
}
}
throw std::runtime_error("GetBank failed");
Expand All @@ -220,11 +222,11 @@ namespace iguana {
int item_id) const
{
if(!AlgorithmFactory::QueryNewBank(bank_name)) {
m_log->Error("{:?} creates bank {:?}, which is not registered; new banks must be included in `REGISTER_IGUANA_ALGORITHM` arguments", m_class_name, bank_name);
ERROR("{:?} creates bank {:?}, which is not registered; new banks must be included in `REGISTER_IGUANA_ALGORITHM` arguments", m_class_name, bank_name);
throw std::runtime_error("CreateBank failed");
}
if(schema_def.empty()) {
m_log->Error("empty schema_def in CreateBank");
ERROR("empty schema_def in CreateBank");
throw std::runtime_error("CreateBank failed");
}
hipo::schema bank_schema(bank_name.c_str(), group_id, item_id);
Expand All @@ -241,23 +243,11 @@ namespace iguana {

///////////////////////////////////////////////////////////////////////////////

void Algorithm::ShowBanks(hipo::banklist& banks, std::string_view message, Logger::Level const level) const
{
if(m_log->GetLevel() <= level) {
if(message != "")
m_log->Print(level, message);
for(auto& bank : banks)
bank.show();
}
}

///////////////////////////////////////////////////////////////////////////////

void Algorithm::ShowBank(hipo::bank& bank, std::string_view message, Logger::Level const level) const
{
if(m_log->GetLevel() <= level) {
if(GetLogLevel() <= level) {
if(message != "")
m_log->Print(level, message);
PRINT_LOG(level, message);
bank.show();
}
}
Expand All @@ -274,7 +264,7 @@ namespace iguana {
return std::get<OPTION_TYPE>(it->second);
}
catch(std::bad_variant_access const& ex) {
m_log->Warn("user called SetOption for option '{}' and set it to '{}', which is the wrong type; IGNORING", key, PrintOptionValue(key));
WARN("user called SetOption for option '{}' and set it to '{}', which is the wrong type; IGNORING", key, PrintOptionValue(key));
}
}
return {};
Expand Down
12 changes: 3 additions & 9 deletions src/iguana/algorithms/Algorithm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <optional>
#include <set>

Expand Down Expand Up @@ -82,13 +83,12 @@ namespace iguana {
std::is_same<OPTION_TYPE, std::string>,
std::is_same<OPTION_TYPE, char const*>,
std::is_same<OPTION_TYPE, Logger::Level>>::value)
m_log->SetLevel(val);
SetLogLevel(val);
else
m_log->Error("Option '{}' must be a string or a Logger::Level", key);
throw std::runtime_error(fmt::format("Option '{}' must be a string or a Logger::Level", key));
}
else {
m_option_cache[key] = val;
m_log->Debug(" USER OPTION: {:>20} = {}", key, PrintOptionValue(key));
}
return val;
}
Expand Down Expand Up @@ -175,12 +175,6 @@ namespace iguana {
int group_id, // FIXME: generalize group_id and item_id setting
int item_id) const noexcept(false);

/// Dump all banks in a `hipo::banklist`
/// @param banks the banks to show
/// @param message if specified, print a header message
/// @param level the log level
void ShowBanks(hipo::banklist& banks, std::string_view message = "", Logger::Level const level = Logger::trace) const;

/// Dump a single bank
/// @param bank the bank to show
/// @param message if specified, print a header message
Expand Down
9 changes: 5 additions & 4 deletions src/iguana/algorithms/AlgorithmSequence.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "AlgorithmSequence.h"
#include "iguana/services/LoggerMacros.h"

namespace iguana {

Expand All @@ -24,7 +25,7 @@ namespace iguana {
{
auto algo = AlgorithmFactory::Create(class_name);
if(algo == nullptr) {
m_log->Error("algorithm '{}' does not exist", class_name);
ERROR("algorithm '{}' does not exist", class_name);
throw std::runtime_error("AlgorithmFactory cannot create non-existent algorithm");
}
algo->SetName(instance_name == "" ? class_name : instance_name);
Expand All @@ -41,7 +42,7 @@ namespace iguana {
m_sequence.push_back(std::move(algo));
// check for duplicate algorithm name
if(m_algo_names.size() < m_sequence.size()) {
m_log->Error("Duplicate algorithm name '{}' detected; please make sure all of your algorithms have unique names", algoName);
ERROR("Duplicate algorithm name '{}' detected; please make sure all of your algorithms have unique names", algoName);
throw std::runtime_error("cannot Add algorithm");
}
}
Expand All @@ -62,9 +63,9 @@ namespace iguana {

void AlgorithmSequence::PrintSequence(Logger::Level level) const
{
m_log->Print(level, "algorithms in this sequence:");
PRINT_LOG(level, "algorithms in this sequence:");
for(auto const& algo : m_sequence)
m_log->Print(level, " - {}", algo->GetName());
PRINT_LOG(level, " - {}", algo->GetName());
}

}
3 changes: 1 addition & 2 deletions src/iguana/algorithms/AlgorithmSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ namespace iguana {
{
if(auto it{m_algo_names.find(instance_name)}; it != m_algo_names.end())
return dynamic_cast<ALGORITHM*>(m_sequence[it->second].get());
m_log->Error("cannot find algorithm '{}' in sequence", instance_name);
throw std::runtime_error("cannot Get algorithm");
throw std::runtime_error(fmt::format("cannot find algorithm '{}' in sequence", instance_name));
}

/// Set an algorithm option
Expand Down
10 changes: 9 additions & 1 deletion src/iguana/algorithms/clas12/EventBuilderFilter/Algorithm.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "Algorithm.h"
#include "iguana/services/LoggerMacros.h"
#include "iguana/services/LoggerMacros.h"

namespace iguana::clas12 {

REGISTER_IGUANA_ALGORITHM(EventBuilderFilter);

void EventBuilderFilter::Start(hipo::banklist& banks)
{
SetLogLevel(Logger::Level::trace); // FIXME: remove this after testing

// define options, their default values, and cache them
ParseYAMLConfig();
Expand All @@ -29,7 +32,7 @@ namespace iguana::clas12 {
particleBank.getMutableRowList().filter([this](auto bank, auto row) {
auto pid = bank.getInt("pid", row);
auto accept = Filter(pid);
m_log->Debug("input PID {} -- accept = {}", pid, accept);
DEBUG("input PID {} -- accept = {}", pid, accept);
return accept ? 1 : 0;
});

Expand All @@ -53,6 +56,11 @@ namespace iguana::clas12 {

void EventBuilderFilter::Stop()
{
TRACE("test TRACE {}", 7);
DEBUG("test DEBUG {}", 7);
INFO("test INFO {}", 7);
WARN("test WARN {}", 7);
ERROR("test ERROR {}", 7);
}

}
5 changes: 3 additions & 2 deletions src/iguana/algorithms/clas12/FiducialFilter/Algorithm.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Algorithm.h"
#include "iguana/services/LoggerMacros.h"
#include "Pass1CutData.h"

namespace iguana::clas12 {
Expand All @@ -16,7 +17,7 @@ namespace iguana::clas12 {

o_pass = GetCachedOption<int>("pass").value_or(1);
if(o_pass!=1){
m_log->Warn("FiducialFilter only contains fiducial cuts for pass1...we will default to using those...");
WARN("FiducialFilter only contains fiducial cuts for pass1...we will default to using those...")
}

}
Expand Down Expand Up @@ -55,7 +56,7 @@ void FiducialFilter::Run(hipo::banklist& banks) const {
{

if(torus!=1&&torus!=-1){
m_log->Warn("torus={}...value must be either -1 or 1, otherwise fiducial cuts are not defined...filtering out all particles...",torus);
WARN("torus={}...value must be either -1 or 1, otherwise fiducial cuts are not defined...filtering out all particles...",torus);
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/iguana/algorithms/clas12/FiducialFilter/Validator.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Validator.h"
#include "Algorithm.h"
#include "iguana/services/LoggerMacros.h"

#include <TProfile.h>
#include <TStyle.h>
Expand Down Expand Up @@ -175,7 +176,7 @@ namespace iguana::clas12 {
canv->SaveAs(m_output_file_basename + "_after_DC" + std::to_string(r+1) + ".png");
}
m_output_file->Write();
m_log->Info("Wrote output file {}", m_output_file->GetName());
INFO("Wrote output file {}", m_output_file->GetName());
m_output_file->Close();
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/iguana/algorithms/clas12/LorentzTransformer/Algorithm.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Algorithm.h"
#include "iguana/services/LoggerMacros.h"

// ROOT
#include <Math/Boost.h>
Expand All @@ -24,7 +25,7 @@ namespace iguana::clas12 {
o_beam_energy = GetCachedOption<double>("beam_energy").value_or(10.6); // FIXME
}
else {
m_log->Error("unknown frame '{}'", o_frame);
ERROR("unknown frame '{}'", o_frame);
throw std::runtime_error("cannot Start LorentzTransformer algorithm");
}

Expand Down Expand Up @@ -73,15 +74,15 @@ namespace iguana::clas12 {
vector_element_t const beta_y,
vector_element_t const beta_z) const
{
m_log->Debug(fmt::format("{::<30}", "Boost "));
m_log->Debug(fmt::format("{:>8} = ({:10f}, {:10f}, {:10f}, {:10f})", "p_in", px, py, pz, E));
DEBUG(fmt::format("{::<30}", "Boost "));
DEBUG(fmt::format("{:>8} = ({:10f}, {:10f}, {:10f}, {:10f})", "p_in", px, py, pz, E));

// check if |beta| <= 1
auto beta_mag = std::hypot(beta_x, beta_y, beta_z);
if(beta_mag > 1) {
m_log->Error("attempt to boost with beta > 1 (faster than the speed of light); will NOT boost this momentum");
m_log->Debug("{:>8} = {}", "|beta|", beta_mag);
m_log->Debug("{:>8} = ({:10f}, {:10f}, {:10f})", "beta", beta_x, beta_y, beta_z);
ERROR("attempt to boost with beta > 1 (faster than the speed of light); will NOT boost this momentum");
DEBUG("{:>8} = {}", "|beta|", beta_mag);
DEBUG("{:>8} = ({:10f}, {:10f}, {:10f})", "beta", beta_x, beta_y, beta_z);
return {px, py, pz, E};
}

Expand All @@ -90,9 +91,9 @@ namespace iguana::clas12 {
ROOT::Math::Boost beta(beta_x, beta_y, beta_z);
auto p_out = beta(p_in);

if(m_log->GetLevel() <= Logger::Level::debug) {
m_log->Debug(fmt::format("{:>8} = ({:10f}, {:10f}, {:10f})", "beta", beta.BetaVector().X(), beta.BetaVector().Y(), beta.BetaVector().Z()));
m_log->Debug(fmt::format("{:>8} = ({:10f}, {:10f}, {:10f}, {:10f})", "p_out", p_out.Px(), p_out.Py(), p_out.Pz(), p_out.E()));
if(GetLogLevel() <= Logger::Level::debug) {
DEBUG(fmt::format("{:>8} = ({:10f}, {:10f}, {:10f})", "beta", beta.BetaVector().X(), beta.BetaVector().Y(), beta.BetaVector().Z()));
DEBUG(fmt::format("{:>8} = ({:10f}, {:10f}, {:10f}, {:10f})", "p_out", p_out.Px(), p_out.Py(), p_out.Pz(), p_out.E()));
}
return {p_out.Px(), p_out.Py(), p_out.Pz(), p_out.E()};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Validator.h"
#include "iguana/services/LoggerMacros.h"

#include <TProfile.h>
#include <TStyle.h>
Expand Down Expand Up @@ -114,7 +115,7 @@ namespace iguana::clas12 {
canv->SaveAs(m_output_file_basename + "_" + std::to_string(pdg) + ".png");
}
m_output_file->Write();
m_log->Info("Wrote output file {}", m_output_file->GetName());
INFO("Wrote output file {}", m_output_file->GetName());
m_output_file->Close();
}
}
Expand Down
Loading
Loading