Move the monitor application to lg2
Update the logging API used for files in phosphor-fan-presence/monitor
from the older phosphor::logging::log to the more recent lg2::log.
Tested:
* Verified journal traces worked correctly in simulation.
* Modified json and simulation parameters to force various
application errors and verified correct output and key/value
pairs using journalctl.
Change-Id: Id6f5161edfd09b672aba8751a6d2c6518665188a
Signed-off-by: Anwaar Hadi <anwaar.hadi@ibm.com>
diff --git a/monitor/conditions.cpp b/monitor/conditions.cpp
index 62e272c..91d6a54 100644
--- a/monitor/conditions.cpp
+++ b/monitor/conditions.cpp
@@ -4,7 +4,7 @@
#include "types.hpp"
#include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <algorithm>
@@ -38,9 +38,8 @@
if (!condParams.contains("properties"))
{
// Log error on missing required parameter
- log<level::ERR>(
- "Missing fan monitor condition properties",
- entry("NAME=%s", condParams["name"].get<std::string>().c_str()));
+ lg2::error("Missing fan monitor condition properties {NAME}", "NAME",
+ condParams["name"].get<std::string>());
throw std::runtime_error("Missing fan monitor condition properties");
}
std::vector<PropertyState> propStates;
@@ -50,9 +49,8 @@
!param.contains("property"))
{
// Log error on missing required parameters
- log<level::ERR>("Missing propertiesMatch condition parameters",
- entry("REQUIRED_PARAMETERS=%s",
- "{object, interface, property}"));
+ lg2::error(
+ "Missing properties. Match condition parameters 'object, interface, property'");
throw std::runtime_error(
"Missing propertiesMatch condition parameters");
}
@@ -61,9 +59,8 @@
if (!propAttrs.contains("name") || !propAttrs.contains("value"))
{
// Log error on missing required parameters
- log<level::ERR>(
- "Missing propertiesMatch condition property attributes",
- entry("REQUIRED_ATTRIBUTES=%s", "{name, value}"));
+ lg2::error(
+ "Missing properties. Match condition property attributes 'name, value'");
throw std::runtime_error(
"Missing propertiesMatch condition property attributes");
}
diff --git a/monitor/fan.cpp b/monitor/fan.cpp
index a9ca013..ab8d46d 100644
--- a/monitor/fan.cpp
+++ b/monitor/fan.cpp
@@ -21,7 +21,7 @@
#include "types.hpp"
#include "utility.hpp"
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <format>
@@ -439,7 +439,7 @@
if (response.is_method_error())
{
- log<level::ERR>("Error in Notify call to update inventory");
+ lg2::error("Error in Notify call to update inventory");
dbusError = true;
}
diff --git a/monitor/fan_error.cpp b/monitor/fan_error.cpp
index ed8340d..54c2d22 100644
--- a/monitor/fan_error.cpp
+++ b/monitor/fan_error.cpp
@@ -21,6 +21,7 @@
#include <systemd/sd-journal.h>
#include <nlohmann/json.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Logging/Create/server.hpp>
#include <filesystem>
@@ -163,10 +164,8 @@
}
catch (const std::exception& e)
{
- log<level::ERR>(
- std::format("Could not save log contents in FFDC. Error msg: {}",
- e.what())
- .c_str());
+ lg2::error("Could not save log contents in FFDC. Error msg: {ERROR}",
+ "ERROR", e);
}
return nullptr;
}
diff --git a/monitor/json_parser.cpp b/monitor/json_parser.cpp
index 588cdd8..f24fd61 100644
--- a/monitor/json_parser.cpp
+++ b/monitor/json_parser.cpp
@@ -24,10 +24,9 @@
#include "types.hpp"
#include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <algorithm>
-#include <format>
#include <map>
#include <memory>
#include <optional>
@@ -71,9 +70,8 @@
if (!stg.contains("class") || !stg.contains("group"))
{
// Log error on missing required parameters
- log<level::ERR>(
- "Missing required fan monitor trust group parameters",
- entry("REQUIRED_PARAMETERS=%s", "{class, group}"));
+ lg2::error(
+ "Missing required fan monitor trust group parameters 'class, group'");
throw std::runtime_error(
"Missing required fan trust group parameters");
}
@@ -85,9 +83,9 @@
if (!member.contains("name"))
{
// Log error on missing required parameter
- log<level::ERR>(
- "Missing required fan monitor trust group member name",
- entry("CLASS=%s", tgClass.c_str()));
+ lg2::error(
+ "Missing required fan monitor trust group member name for class {CLASS}",
+ "CLASS", tgClass);
throw std::runtime_error(
"Missing required fan monitor trust group member name");
}
@@ -112,8 +110,8 @@
else
{
// Log error on unsupported trust group class
- log<level::ERR>("Invalid fan monitor trust group class",
- entry("CLASS=%s", tgClass.c_str()));
+ lg2::error("Invalid fan monitor trust group class {CLASS}",
+ "CLASS", tgClass);
throw std::runtime_error(
"Invalid fan monitor trust group class");
}
@@ -132,9 +130,8 @@
if (!sensor.contains("name") || !sensor.contains("has_target"))
{
// Log error on missing required parameters
- log<level::ERR>(
- "Missing required fan sensor definition parameters",
- entry("REQUIRED_PARAMETERS=%s", "{name, has_target}"));
+ lg2::error(
+ "Missing required fan sensor definition parameters 'name, has_target'");
throw std::runtime_error(
"Missing required fan sensor definition parameters");
}
@@ -202,10 +199,8 @@
!fan.contains("sensors"))
{
// Log error on missing required parameters
- log<level::ERR>(
- "Missing required fan monitor definition parameters",
- entry("REQUIRED_PARAMETERS=%s",
- "{inventory, deviation, sensors}"));
+ lg2::error(
+ "Missing required fan monitor definition parameters 'inventory, deviation, sensors'");
throw std::runtime_error(
"Missing required fan monitor definition parameters");
}
@@ -213,12 +208,11 @@
auto deviation = fan["deviation"].get<size_t>();
if (100 < deviation)
{
- auto msg = std::format(
- "Invalid deviation of {} found, must be between 0 and 100",
- deviation);
-
- log<level::ERR>(msg.c_str());
- throw std::runtime_error(msg.c_str());
+ lg2::error(
+ "Invalid deviation of {DEVIATION} found, must be between 0 and 100",
+ "DEVIATION", deviation);
+ throw std::runtime_error(
+ "Invalid deviation found, must be between 0 and 100");
}
// Upper deviation defaults to the deviation value and
@@ -229,13 +223,11 @@
upperDeviation = fan["upper_deviation"].get<size_t>();
if (100 < upperDeviation)
{
- auto msg =
- std::format("Invalid upper_deviation of {} found, must "
- "be between 0 and 100",
- upperDeviation);
-
- log<level::ERR>(msg.c_str());
- throw std::runtime_error(msg.c_str());
+ lg2::error(
+ "Invalid upper_deviation of {UPPER_DEVIATION} found, must be between 0 and 100",
+ "UPPER_DEVIATION", upperDeviation);
+ throw std::runtime_error(
+ "Invalid upper_deviation found, must be between 0 and 100");
}
}
@@ -264,7 +256,7 @@
else
{
// Log error on unsupported method parameter
- log<level::ERR>("Invalid fan method");
+ lg2::error("Invalid fan method");
throw std::runtime_error("Invalid fan method");
}
@@ -285,10 +277,8 @@
if (!fan.contains("allowed_out_of_range_time"))
{
// Log error on missing required parameter
- log<level::ERR>(
- "Missing required fan monitor definition parameters",
- entry("REQUIRED_PARAMETER=%s",
- "{allowed_out_of_range_time}"));
+ lg2::error(
+ "Missing required fan monitor definition parameters 'allowed_out_of_range_time'");
throw std::runtime_error(
"Missing required fan monitor definition parameters");
}
@@ -343,9 +333,8 @@
if (!fan["condition"].contains("name"))
{
// Log error on missing required parameter
- log<level::ERR>(
- "Missing required fan monitor condition parameter",
- entry("REQUIRED_PARAMETER=%s", "{name}"));
+ lg2::error(
+ "Missing required fan monitor condition parameter 'name'");
throw std::runtime_error(
"Missing required fan monitor condition parameter");
}
@@ -360,10 +349,10 @@
}
else
{
- log<level::INFO>(
- "No handler found for configured condition",
- entry("CONDITION_NAME=%s", name.c_str()),
- entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
+ lg2::info(
+ "No handler found for configured condition {CONDITION_NAME}",
+ "CONDITION_NAME", name, "JSON_DUMP",
+ fan["condition"].dump());
}
}
@@ -410,9 +399,8 @@
}
else if (state != "runtime")
{
- auto msg = std::format("Invalid power off state entry {}", state);
- log<level::ERR>(msg.c_str());
- throw std::runtime_error(msg.c_str());
+ lg2::error("Invalid power off state entry {STATE}", "STATE", state);
+ throw std::runtime_error("Invalid power off state entry");
}
}
@@ -425,10 +413,9 @@
if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
{
- const auto msg =
- "Missing 'count' or 'cause' entries in power off config";
- log<level::ERR>(msg);
- throw std::runtime_error(msg);
+ lg2::error("Missing 'count' or 'cause' entries in power off config");
+ throw std::runtime_error(
+ "Missing 'count' or 'cause' entries in power off config");
}
auto count = powerOffConfig.at("count").get<size_t>();
@@ -453,11 +440,11 @@
}
else
{
- auto msg =
- std::format("Invalid power off cause {} in power off config JSON",
- powerOffCause);
- log<level::ERR>(msg.c_str());
- throw std::runtime_error(msg.c_str());
+ lg2::error(
+ "Invalid power off cause {POWER_OFF_CAUSE} in power off config JSON",
+ "POWER_OFF_CAUSE", powerOffCause);
+ throw std::runtime_error(
+ "Invalid power off cause in power off config JSON");
}
return cause;
@@ -471,9 +458,8 @@
std::unique_ptr<PowerOffAction> action;
if (!powerOffConfig.contains("type"))
{
- const auto msg = "Missing 'type' entry in power off config";
- log<level::ERR>(msg);
- throw std::runtime_error(msg);
+ lg2::error("Missing 'type' entry in power off config");
+ throw std::runtime_error("Missing 'type' entry in power off config");
}
auto type = powerOffConfig.at("type").get<std::string>();
@@ -481,18 +467,17 @@
if (((type == "hard") || (type == "soft")) &&
!powerOffConfig.contains("delay"))
{
- const auto msg = "Missing 'delay' entry in power off config";
- log<level::ERR>(msg);
- throw std::runtime_error(msg);
+ lg2::error("Missing 'delay' entry in power off config");
+ throw std::runtime_error("Missing 'delay' entry in power off config");
}
else if ((type == "epow") &&
(!powerOffConfig.contains("service_mode_delay") ||
!powerOffConfig.contains("meltdown_delay")))
{
- const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
- "entry in power off config";
- log<level::ERR>(msg);
- throw std::runtime_error(msg);
+ lg2::error(
+ "Missing 'service_mode_delay' or 'meltdown_delay' entry in power off config");
+ throw std::runtime_error(
+ "Missing 'service_mode_delay' or 'meltdown_delay' entry in power off config");
}
if (type == "hard")
@@ -514,10 +499,9 @@
}
else
{
- auto msg =
- std::format("Invalid 'type' entry {} in power off config", type);
- log<level::ERR>(msg.c_str());
- throw std::runtime_error(msg.c_str());
+ lg2::error("Invalid 'type' entry {TYPE} in power off config", "TYPE",
+ type);
+ throw std::runtime_error("Invalid 'type' entry in power off config");
}
return action;
diff --git a/monitor/system.cpp b/monitor/system.cpp
index a4434dc..ff074c3 100644
--- a/monitor/system.cpp
+++ b/monitor/system.cpp
@@ -32,7 +32,7 @@
#include "hwmon_ffdc.hpp"
#include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdeventplus/event.hpp>
@@ -103,7 +103,7 @@
// Retrieve fan definitions and create fan objects to be monitored
setFans(fanDefs);
setFaultConfig(jsonObj);
- log<level::INFO>("Configuration loaded");
+ lg2::info("Configuration loaded");
_loaded = true;
#ifdef MONITOR_USE_JSON
@@ -235,8 +235,9 @@
}
catch (std::runtime_error& re)
{
- log<level::ERR>("Error reloading config, no config changes made",
- entry("LOAD_ERROR=%s", re.what()));
+ lg2::error(
+ "Error reloading config, no config changes made: {LOAD_ERROR}",
+ "LOAD_ERROR", re);
}
}
@@ -370,7 +371,7 @@
{
if (!_loaded)
{
- log<level::ERR>("No conf file found at power on");
+ lg2::error("No conf file found at power on");
throw std::runtime_error("No conf file found at power on");
}
@@ -591,7 +592,7 @@
std::ofstream file{System::dumpFile};
if (!file)
{
- log<level::ERR>("Could not open file for fan monitor dump");
+ lg2::error("Could not open file for fan monitor dump");
}
else
{
diff --git a/monitor/tach_sensor.cpp b/monitor/tach_sensor.cpp
index d26b246..5790bab 100644
--- a/monitor/tach_sensor.cpp
+++ b/monitor/tach_sensor.cpp
@@ -20,10 +20,9 @@
#include "utility.hpp"
#include <phosphor-logging/elog.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <filesystem>
-#include <format>
#include <functional>
#include <optional>
#include <utility>
@@ -66,7 +65,10 @@
}
catch (const std::exception& e)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+ lg2::error(
+ "getProperty failed on path {PATH}, interface {INTERFACE}, property {PROPERTY_NAME}, Error: {ERROR}",
+ "PATH", path, "INTERFACE", interface, "PROPERTY_NAME", propertyName,
+ "ERROR", e);
}
}
@@ -318,11 +320,10 @@
if (!timerRunning() || mode != _timerMode)
{
- log<level::DEBUG>(
- std::format("Start timer({}) on tach sensor {}. [delay = {}s]",
- static_cast<int>(mode), _name,
- duration_cast<seconds>(getDelay(mode)).count())
- .c_str());
+ lg2::debug(
+ "Start timer({MODE}) on tach sensor {NAME}. [delay = {DELAY}s]",
+ "MODE", static_cast<int>(mode), "NAME", _name, "DELAY",
+ duration_cast<seconds>(getDelay(mode)).count());
_timer.restartOnce(getDelay(mode));
_timerMode = mode;
}
@@ -340,8 +341,8 @@
return duration_cast<microseconds>(seconds(_funcDelay));
default:
// Log an internal error for undefined timer mode
- log<level::ERR>("Undefined timer mode",
- entry("TIMER_MODE=%u", mode));
+ lg2::error("Undefined timer mode: {TIMER_MODE}", "TIMER_MODE",
+ mode);
elog<InternalFailure>();
return duration_cast<microseconds>(seconds(0));
}
@@ -354,11 +355,9 @@
if (_counter < _threshold)
{
++_counter;
- log<level::DEBUG>(
- std::format(
- "Incremented error counter on {} to {} (threshold {})",
- _name, _counter, _threshold)
- .c_str());
+ lg2::debug(
+ "Incremented error counter on {NAME} to {COUNTER} (threshold {THRESHOLD})",
+ "NAME", _name, "COUNTER", _counter, "THRESHOLD", _threshold);
}
}
else
@@ -366,11 +365,9 @@
if (_counter > 0)
{
--_counter;
- log<level::DEBUG>(
- std::format(
- "Decremented error counter on {} to {} (threshold {})",
- _name, _counter, _threshold)
- .c_str());
+ lg2::debug(
+ "Decremented error counter on {NAME} to {COUNTER} (threshold {THRESHOLD})",
+ "NAME", _name, "COUNTER", _counter, "THRESHOLD", _threshold);
}
}
}
@@ -379,8 +376,7 @@
{
if (_countTimer)
{
- log<level::DEBUG>(
- std::format("Starting count timer on sensor {}", _name).c_str());
+ lg2::debug("Starting count timer on sensor {NAME}", "NAME", _name);
_countTimer->restart(std::chrono::seconds(_countInterval));
}
}
@@ -389,9 +385,8 @@
{
if (_countTimer && _countTimer->isEnabled())
{
- log<level::DEBUG>(
- std::format("Stopping count timer on tach sensor {}.", _name)
- .c_str());
+ lg2::debug("Stopping count timer on tach sensor {NAME}.", "NAME",
+ _name);
_countTimer->setEnabled(false);
}
}
@@ -408,7 +403,7 @@
if (response.is_method_error())
{
- log<level::ERR>("Error in notify update of tach sensor inventory");
+ lg2::error("Error in notify update of tach sensor inventory");
}
}
diff --git a/monitor/tach_sensor.hpp b/monitor/tach_sensor.hpp
index 74eede6..1dea170 100644
--- a/monitor/tach_sensor.hpp
+++ b/monitor/tach_sensor.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdeventplus/clock.hpp>
@@ -9,7 +9,6 @@
#include <chrono>
#include <deque>
-#include <format>
#include <optional>
#include <utility>
@@ -284,9 +283,7 @@
*/
inline void stopTimer()
{
- phosphor::logging::log<phosphor::logging::level::DEBUG>(
- std::format("Stop running timer on tach sensor {}.", _name)
- .c_str());
+ lg2::debug("Stop running timer on tach sensor {NAME}.", "NAME", _name);
_timer.setEnabled(false);
}
diff --git a/monitor/types.hpp b/monitor/types.hpp
index fd53522..b5848f5 100644
--- a/monitor/types.hpp
+++ b/monitor/types.hpp
@@ -4,7 +4,7 @@
#include "trust_group.hpp"
#include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Object/Enable/server.hpp>
#include <functional>
@@ -83,12 +83,9 @@
}
}
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Unsupported data type for JSON entry's value",
- phosphor::logging::entry("GIVEN_ENTRY_TYPE=%s", type.c_str()),
- phosphor::logging::entry("JSON_ENTRY=%s", entry.dump().c_str()),
- phosphor::logging::entry("SUPPORTED_TYPES=%s",
- "{bool, int64_t, std::string}"));
+ lg2::error(
+ "Unsupported data type {GIVEN_ENTRY_TYPE} for JSON entry's value. Supported types are 'bool, int64_t, std::string'",
+ "GIVEN_ENTRY_TYPE", type, "JSON_ENTRY", entry.dump());
throw std::runtime_error(
"Unsupported data type for JSON entry's value");
}