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.cpp b/health_metric.cpp
index 4c391cd..8ee427b 100644
--- a/health_metric.cpp
+++ b/health_metric.cpp
@@ -60,8 +60,7 @@
}
default:
{
- error("Invalid Memory metric {TYPE}", "TYPE",
- std::to_underlying(subType));
+ error("Invalid Memory metric {TYPE}", "TYPE", subType);
return "";
}
}
@@ -130,8 +129,7 @@
}
default:
{
- error("Invalid threshold bound {BOUND}", "BOUND",
- std::to_underlying(bound));
+ error("Invalid threshold bound {BOUND}", "BOUND", bound);
return false;
}
}
@@ -162,8 +160,7 @@
{
error(
"ASSERT: Health Metric {METRIC} crossed {TYPE} upper threshold",
- "METRIC", config.name, "TYPE",
- sdbusplus::message::convert_to_string(type));
+ "METRIC", config.name, "TYPE", type);
startUnit(bus, tConfig.target);
}
}
@@ -178,8 +175,7 @@
{
info(
"DEASSERT: Health Metric {METRIC} is below {TYPE} upper threshold",
- "METRIC", config.name, "TYPE",
- sdbusplus::message::convert_to_string(type));
+ "METRIC", config.name, "TYPE", type);
}
}
}
diff --git a/health_metric_collection.cpp b/health_metric_collection.cpp
index ae7d593..ea587ac 100644
--- a/health_metric_collection.cpp
+++ b/health_metric_collection.cpp
@@ -99,9 +99,8 @@
preTotalTime[config.subType] = totalTime;
activePercValue = (100.0 * activeTimeDiff) / totalTimeDiff;
- debug("CPU Metric {SUBTYPE}: {VALUE}", "SUBTYPE",
- std::to_underlying(config.subType), "VALUE",
- (double)activePercValue);
+ debug("CPU Metric {SUBTYPE}: {VALUE}", "SUBTYPE", config.subType,
+ "VALUE", (double)activePercValue);
/* For CPU, both user and monitor uses percentage values */
metrics[config.subType]->update(MValue(activePercValue, 100));
}
@@ -159,8 +158,7 @@
auto value = memoryValues.at(config.subType) * 1024;
auto total = memoryValues.at(MetricIntf::SubType::memoryTotal) * 1024;
debug("Memory Metric {SUBTYPE}: {VALUE}, {TOTAL}", "SUBTYPE",
- std::to_underlying(config.subType), "VALUE", value, "TOTAL",
- total);
+ config.subType, "VALUE", value, "TOTAL", total);
metrics[config.subType]->update(MValue(value, total));
}
return true;
@@ -181,8 +179,7 @@
double value = buffer.f_bfree * buffer.f_frsize;
double total = buffer.f_blocks * buffer.f_frsize;
debug("Storage Metric {SUBTYPE}: {VALUE}, {TOTAL}", "SUBTYPE",
- std::to_underlying(config.subType), "VALUE", value, "TOTAL",
- total);
+ config.subType, "VALUE", value, "TOTAL", total);
metrics[config.subType]->update(MValue(value, total));
}
return true;
@@ -218,8 +215,7 @@
}
default:
{
- error("Unknown health metric type {TYPE}", "TYPE",
- std::to_underlying(type));
+ error("Unknown health metric type {TYPE}", "TYPE", type);
break;
}
}
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
diff --git a/health_metric_config.hpp b/health_metric_config.hpp
index 49f39ad..6391462 100644
--- a/health_metric_config.hpp
+++ b/health_metric_config.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include <sdbusplus/message.hpp>
#include <xyz/openbmc_project/Common/Threshold/server.hpp>
#include <chrono>
@@ -41,6 +42,9 @@
NA
};
+auto to_string(Type) -> std::string;
+auto to_string(SubType) -> std::string;
+
namespace config
{
diff --git a/health_monitor.cpp b/health_monitor.cpp
index d0e3d4f..772bdf9 100644
--- a/health_monitor.cpp
+++ b/health_monitor.cpp
@@ -25,8 +25,7 @@
for (auto& [type, collectionConfig] : configs)
{
- info("Creating Health Metric Collection for {TYPE}", "TYPE",
- std::to_underlying(type));
+ info("Creating Health Metric Collection for {TYPE}", "TYPE", type);
collections[type] =
std::make_unique<CollectionIntf::HealthMetricCollection>(
ctx.get_bus(), type, collectionConfig, bmcPaths);
@@ -42,8 +41,7 @@
{
for (auto& [type, collection] : collections)
{
- debug("Reading Health Metric Collection for {TYPE}", "TYPE",
- std::to_underlying(type));
+ debug("Reading Health Metric Collection for {TYPE}", "TYPE", type);
collection->read();
}
co_await sdbusplus::async::sleep_for(ctx, std::chrono::seconds(1));
diff --git a/test/meson.build b/test/meson.build
index c4f8993..a93e1f8 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -44,6 +44,7 @@
'test_health_metric.cpp',
'../health_metric.cpp',
'../health_utils.cpp',
+ '../health_metric_config.cpp',
dependencies: [
gtest_dep,
gmock_dep,