improve enum logging
Ensure we're using an enumeration string everywhere we log one to
improve readability of the log statements.
This depends on I6850b0bb142f0ef5219a5fc07c2cb4e2c90d5779 from
phosphor-logging for `to_string` conversion support.
Tested:
Running directly on development system shows:
```
<7> TYPE=CPU, NAME=CPU SUBTYPE=CPU PATH=, FREQ=1, WSIZE=120
<7> THRESHOLD TYPE=xyz.openbmc_project.Common.Threshold.Type.Warning THRESHOLD BOUND=xyz.openbmc_project.Common.Threshold.Bound.Upper VALUE=80.000000 LOG=False TARGET=
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I5ea7a050e94833af272e79e6add716d1d3e66571
diff --git a/health_metric_config.cpp b/health_metric_config.cpp
index 8a2d5e7..e5312a0 100644
--- a/health_metric_config.cpp
+++ b/health_metric_config.cpp
@@ -7,13 +7,16 @@
#include <cmath>
#include <fstream>
+#include <ranges>
#include <unordered_map>
#include <unordered_set>
#include <utility>
PHOSPHOR_LOG2_USING;
-namespace phosphor::health::metric::config
+namespace phosphor::health::metric
+{
+namespace config
{
using json = nlohmann::json;
@@ -137,20 +140,18 @@
for (auto& config : configList)
{
debug(
- "MTYPE={MTYPE}, MNAME={MNAME} MSTYPE={MSTYPE} PATH={PATH}, FREQ={FREQ}, WSIZE={WSIZE}",
- "MTYPE", std::to_underlying(type), "MNAME", config.name,
- "MSTYPE", std::to_underlying(config.subType), "PATH",
- config.path, "FREQ", config.collectionFreq.count(), "WSIZE",
- config.windowSize);
+ "TYPE={TYPE}, NAME={NAME} SUBTYPE={SUBTYPE} PATH={PATH}, FREQ={FREQ}, WSIZE={WSIZE}",
+ "TYPE", type, "NAME", config.name, "SUBTYPE", config.subType,
+ "PATH", config.path, "FREQ", config.collectionFreq.count(),
+ "WSIZE", config.windowSize);
for (auto& [key, threshold] : config.thresholds)
{
debug(
"THRESHOLD TYPE={TYPE} THRESHOLD BOUND={BOUND} VALUE={VALUE} LOG={LOG} TARGET={TARGET}",
- "TYPE", std::to_underlying(get<ThresholdIntf::Type>(key)),
- "BOUND", std::to_underlying(get<ThresholdIntf::Bound>(key)),
- "VALUE", threshold.value, "LOG", threshold.log, "TARGET",
- threshold.target);
+ "TYPE", get<ThresholdIntf::Type>(key), "BOUND",
+ get<ThresholdIntf::Bound>(key), "VALUE", threshold.value,
+ "LOG", threshold.log, "TARGET", threshold.target);
}
}
}
@@ -277,4 +278,32 @@
}
})"_json;
-} // namespace phosphor::health::metric::config
+} // namespace config
+
+namespace details
+{
+auto reverse_map_search(const auto& m, auto v)
+{
+ if (auto match = std::ranges::find_if(
+ m, [=](const auto& p) { return p.second == v; });
+ match != std::end(m))
+ {
+ return match->first;
+ }
+ return std::format("Enum({})", std::to_underlying(v));
+}
+} // namespace details
+
+// to_string specialization for Type.
+auto to_string(Type t) -> std::string
+{
+ return details::reverse_map_search(config::validTypes, t);
+}
+
+// to_string specializaiton for SubType.
+auto to_string(SubType t) -> std::string
+{
+ return details::reverse_map_search(config::validSubTypes, t);
+}
+
+} // namespace phosphor::health::metric