logging: switch to lg2
After switching to C++20, it is recommended to use `phosphor::lg2`
to format log, and the correct `CODE_LINE` and `CODE_FUNC` values
can be used in log tracking.
Tested: built phosphor-srvcfg-manager successfully and Unit Test
passes.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ib676a0d8082b0e3e72b51bd5115bf5b05599e6ec
diff --git a/inc/utils.hpp b/inc/utils.hpp
index 234124d..dd2124a 100644
--- a/inc/utils.hpp
+++ b/inc/utils.hpp
@@ -16,6 +16,7 @@
#pragma once
#include <boost/asio.hpp>
#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
diff --git a/src/main.cpp b/src/main.cpp
index 1d9ebed..5723de2 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -220,8 +220,8 @@
const std::vector<ListUnitsType>& listUnits) {
if (ec)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "async_method_call error: ListUnits failed");
+ lg2::error("async_method_call error: ListUnits failed: {EC}",
+ "EC", ec.value());
return;
}
handleListUnitsResponse(server, conn, ec, listUnits);
@@ -238,8 +238,8 @@
const std::variant<uint64_t>& value) {
if (ec)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "async_method_call error: ListUnits failed");
+ lg2::error("async_method_call error: ListUnits failed: {EC}",
+ "EC", ec.value());
return;
}
if (std::get<uint64_t>(value))
@@ -267,8 +267,9 @@
}
if (ec)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "service config mgr - init - async wait error.");
+ lg2::error(
+ "service config mgr - init - async wait error: {EC}",
+ "EC", ec.value());
return;
}
checkAndInit(server, conn);
diff --git a/src/srvcfg_manager.cpp b/src/srvcfg_manager.cpp
index bd23b44..b7b891b 100644
--- a/src/srvcfg_manager.cpp
+++ b/src/srvcfg_manager.cpp
@@ -125,9 +125,9 @@
propertyMap) {
if (ec)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "async_method_call error: Failed to service unit "
- "properties");
+ lg2::error(
+ "async_method_call error: Failed to service unit properties: {EC}",
+ "EC", ec.value());
return;
}
try
@@ -141,10 +141,9 @@
std::string, VariantType>& propertyMap) {
if (ec)
{
- phosphor::logging::log<
- phosphor::logging::level::ERR>(
- "async_method_call error: Failed to get "
- "all property");
+ lg2::error(
+ "async_method_call error: Failed to get all property: {EC}",
+ "EC", ec.value());
return;
}
try
@@ -157,11 +156,9 @@
}
catch (const std::exception& e)
{
- phosphor::logging::log<
- phosphor::logging::level::ERR>(
- "Exception in getting socket properties",
- phosphor::logging::entry("WHAT=%s",
- e.what()));
+ lg2::error(
+ "Exception in getting socket properties: {ERROR}",
+ "ERROR", e);
return;
}
},
@@ -175,9 +172,8 @@
}
catch (const std::exception& e)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Exception in getting socket properties",
- phosphor::logging::entry("WHAT=%s", e.what()));
+ lg2::error("Exception in getting socket properties: {ERROR}",
+ "ERROR", e);
return;
}
},
@@ -198,9 +194,8 @@
{
if (!std::filesystem::create_directories(ovrUnitFileDir))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Unable to create the directory.",
- phosphor::logging::entry("DIR=%s", ovrUnitFileDir.c_str()));
+ lg2::error("Unable to create the {DIR} directory.", "DIR",
+ ovrUnitFileDir);
phosphor::logging::elog<sdbusplus::xyz::openbmc_project::
Common::Error::InternalFailure>();
}
@@ -255,9 +250,7 @@
// No updates / masked - Just return.
return;
}
- phosphor::logging::log<phosphor::logging::level::INFO>(
- "Applying new settings.",
- phosphor::logging::entry("OBJPATH=%s", objPath.c_str()));
+ lg2::info("Applying new settings: {OBJPATH}", "OBJPATH", objPath);
if (subStateValue == subStateRunning || subStateValue == subStateListening)
{
if (!socketObjectPath.empty())
@@ -308,8 +301,8 @@
std::ofstream cfgFile(tmpFile, std::ios::out);
if (!cfgFile.good())
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Failed to open override.conf_tmp file");
+ lg2::error("Failed to open the {TMPFILE} file.", "TMPFILE",
+ tmpFile);
phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
Error::InternalFailure>();
}
@@ -324,8 +317,8 @@
if (std::rename(tmpFile.c_str(), ovrCfgFile.c_str()) != 0)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Failed to rename tmp file as override.conf");
+ lg2::error("Failed to rename {TMPFILE} file as {OVERCFGFILE} file.",
+ "TMPFILE", tmpFile, "OVERCFGFILE", ovrCfgFile);
std::remove(tmpFile.c_str());
phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
Error::InternalFailure>();
@@ -378,9 +371,7 @@
// Reset the flag
updatedFlag = 0;
- phosphor::logging::log<phosphor::logging::level::INFO>(
- "Applied new settings",
- phosphor::logging::entry("OBJPATH=%s", objPath.c_str()));
+ lg2::info("Applied new settings: {OBJPATH}", "OBJPATH", objPath);
queryAndUpdateProperties();
return;
@@ -397,8 +388,7 @@
}
else if (ec)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "async wait error.");
+ lg2::error("async wait error: {EC}", "EC", ec.value());
return;
}
updateInProgress = true;
@@ -503,8 +493,7 @@
}
if (unitMaskedState)
{ // block updating if masked
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Invalid value specified");
+ lg2::error("Invalid value specified");
return -EINVAL;
}
unitEnabledState = req;
@@ -531,8 +520,7 @@
}
if (unitMaskedState)
{ // block updating if masked
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Invalid value specified");
+ lg2::error("Invalid value specified");
return -EINVAL;
}
unitRunningState = req;
diff --git a/src/utils.cpp b/src/utils.cpp
index 908f34f..1ed4c1c 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -21,7 +21,7 @@
if (ec)
{
std::string msgToLog = ec.message() + (msg.empty() ? "" : " - " + msg);
- phosphor::logging::log<phosphor::logging::level::ERR>(msgToLog.c_str());
+ lg2::error("Check and throw internal failure: {MSG}", "MSG", msgToLog);
phosphor::logging::elog<
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
}
@@ -44,8 +44,7 @@
auto pos = path.rfind("/");
if (pos == std::string::npos)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Unable to get job id from job path");
+ lg2::error("Unable to get job id from {PATH}.", "PATH", path);
phosphor::logging::elog<
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
}
@@ -80,8 +79,8 @@
// Queued job is done, return now
return;
}
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Systemd operation failed for job query");
+ lg2::error("Systemd operation failed for job query: {EC}", "EC",
+ ec.value());
phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
Error::InternalFailure>();
}