From bbd14eb137b2626c8921b5525255186258969626 Mon Sep 17 00:00:00 2001 From: Tom Eichlersmith <31970302+tomeichlersmith@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:19:38 -0500 Subject: [PATCH] dereference boost.log's attribute pointer only if defined (#1463) this avoids clang-tidy complaining about de-referencing nullptrs since there is a possibility the logs will contain messages that don't have a Channel and/or Severity attribute --- Framework/src/Framework/Logger.cxx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Framework/src/Framework/Logger.cxx b/Framework/src/Framework/Logger.cxx index 52eddbe2e..cbebf6d42 100644 --- a/Framework/src/Framework/Logger.cxx +++ b/Framework/src/Framework/Logger.cxx @@ -35,6 +35,16 @@ logger makeLogger(const std::string& name) { return boost::move(lg); } +template +const T& safe_extract(log::attribute_value attr) { + static const T empty_value = {}; + auto attr_val = log::extract(attr); + if (attr_val) { + return *attr_val; + } + return empty_value; +} + /** * Our filter implementation aligning with Boost.Log * @@ -52,8 +62,8 @@ class Filter { : fallback_level_{fallback}, custom_levels_{custom} {} Filter(level fallback) : Filter(fallback, {}) {} bool operator()(log::attribute_value_set const& attrs) { - const std::string& channel{*log::extract(attrs["Channel"])}; - const level& msg_level{*log::extract(attrs["Severity"])}; + const std::string& channel{safe_extract(attrs["Channel"])}; + const level& msg_level{safe_extract(attrs["Severity"])}; auto it = custom_levels_.find(channel); if (it != custom_levels_.end()) { return msg_level >= it->second; @@ -157,7 +167,7 @@ void Formatter::operator()(const log::record_view& view, * We de-reference the value out of the log into our own type * so that we can compare and convert it into a string. */ - const level& msg_level = *log::extract("Severity", view); + const level& msg_level{safe_extract(view["Severity"])}; switch (msg_level) { case level::debug: os << "debug";