Convert from log() to lg2()
Convert from the old log() API to the new lg2() API for logging journal
messages. The new API is simpler to use, and the old API may be
deprecated in the future.
Tested:
* Tested all lg2() calls to verify the resulting journal message was
correct.
* For the complete test plan, see
https://gist.github.com/smccarney/f4ed140bf7a46773b31c76b488e739dc
Change-Id: Ieb46c5d1481ceb51e5b0d51392b1c32596d8eac8
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/src/activation.cpp b/src/activation.cpp
index 5c90860..ed985a1 100644
--- a/src/activation.cpp
+++ b/src/activation.cpp
@@ -5,7 +5,7 @@
#include "utils.hpp"
#include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <cassert>
#include <filesystem>
@@ -98,7 +98,7 @@
}
catch (const sdbusplus::exception_t& e)
{
- log<level::ERR>("Error staring service", entry("ERROR=%s", e.what()));
+ lg2::error("Error starting service: {ERROR}", "ERROR", e);
onUpdateFailed();
return false;
}
@@ -139,8 +139,7 @@
void Activation::onUpdateFailed()
{
// TODO: report an event
- log<level::ERR>("Failed to update PSU",
- entry("PSU=%s", psuQueue.front().c_str()));
+ lg2::error("Failed to update PSU {PSU}", "PSU", psuQueue.front());
std::queue<std::string>().swap(psuQueue); // Clear the queue
activation(Status::Failed);
}
@@ -150,15 +149,16 @@
// Check if the activation has file path
if (path().empty())
{
- log<level::WARNING>("No image for the activation, skipped",
- entry("VERSION_ID=%s", versionId.c_str()));
+ lg2::warning(
+ "No image for the activation, skipped version {VERSION_ID}",
+ "VERSION_ID", versionId);
return activation(); // Return the previous activation status
}
auto psuPaths = utils::getPSUInventoryPath(bus);
if (psuPaths.empty())
{
- log<level::WARNING>("No PSU inventory found");
+ lg2::warning("No PSU inventory found");
return Status::Failed;
}
@@ -168,22 +168,21 @@
{
if (utils::isAssociated(p, associations()))
{
- log<level::NOTICE>("PSU already running the image, skipping",
- entry("PSU=%s", p.c_str()));
+ lg2::notice("PSU {PSU} is already running the image, skipping",
+ "PSU", p);
continue;
}
psuQueue.push(p);
}
else
{
- log<level::NOTICE>("PSU not compatible",
- entry("PSU=%s", p.c_str()));
+ lg2::notice("PSU {PSU} is not compatible", "PSU", p);
}
}
if (psuQueue.empty())
{
- log<level::WARNING>("No PSU compatible with the software");
+ lg2::warning("No PSU compatible with the software");
return activation(); // Return the previous activation status
}
@@ -267,9 +266,9 @@
}
catch (const sdbusplus::exception_t& e)
{
- log<level::ERR>("Error performing call to Delete object path",
- entry("ERROR=%s", e.what()),
- entry("PATH=%s", objPath.c_str()));
+ lg2::error(
+ "Error performing call to Delete object path {PATH}: {ERROR}",
+ "PATH", objPath, "ERROR", e);
}
}
@@ -316,9 +315,8 @@
}
catch (const fs::filesystem_error& e)
{
- log<level::ERR>("Error storing PSU image", entry("ERROR=%s", e.what()),
- entry("SRC=%s", src.c_str()),
- entry("DST=%s", dst.c_str()));
+ lg2::error("Error storing PSU image: src={SRC}, dst={DST}: {ERROR}",
+ "SRC", src, "DST", dst, "ERROR", e);
}
}
@@ -341,7 +339,7 @@
void ActivationBlocksTransition::enableRebootGuard()
{
- log<level::INFO>("PSU image activating - BMC reboots are disabled.");
+ lg2::info("PSU image activating - BMC reboots are disabled.");
auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
SYSTEMD_INTERFACE, "StartUnit");
@@ -351,7 +349,7 @@
void ActivationBlocksTransition::disableRebootGuard()
{
- log<level::INFO>("PSU activation has ended - BMC reboots are re-enabled.");
+ lg2::info("PSU activation has ended - BMC reboots are re-enabled.");
auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
SYSTEMD_INTERFACE, "StartUnit");
diff --git a/src/item_updater.cpp b/src/item_updater.cpp
index 44612b3..aea02ee 100644
--- a/src/item_updater.cpp
+++ b/src/item_updater.cpp
@@ -5,7 +5,7 @@
#include "utils.hpp"
#include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <cassert>
@@ -92,8 +92,8 @@
auto pos = path.rfind('/');
if (pos == std::string::npos)
{
- log<level::ERR>("No version id found in object path",
- entry("OBJPATH=%s", path.c_str()));
+ lg2::error("No version id found in object path {OBJPATH}", "OBJPATH",
+ path);
return;
}
@@ -131,10 +131,9 @@
auto it = versions.find(versionId);
if (it == versions.end())
{
- log<level::ERR>(("Error: Failed to find version " + versionId +
- " in item updater versions map."
- " Unable to remove.")
- .c_str());
+ lg2::error("Error: Failed to find version {VERSION_ID} in "
+ "item updater versions map. Unable to remove.",
+ "VERSION_ID", versionId);
}
else
{
@@ -146,10 +145,9 @@
auto ita = activations.find(versionId);
if (ita == activations.end())
{
- log<level::ERR>(("Error: Failed to find version " + versionId +
- " in item updater activations map."
- " Unable to remove.")
- .c_str());
+ lg2::error("Error: Failed to find version {VERSION_ID} in "
+ "item updater activations map. Unable to remove.",
+ "VERSION_ID", versionId);
}
else
{
@@ -270,8 +268,8 @@
auto it = psuPathActivationMap.find(psuInventoryPath);
if (it == psuPathActivationMap.end())
{
- log<level::ERR>("No Activation found for PSU",
- entry("PSUPATH=%s", psuInventoryPath.c_str()));
+ lg2::error("No Activation found for PSU {PSUPATH}", "PSUPATH",
+ psuInventoryPath);
return;
}
const auto& activationPtr = it->second;
@@ -386,11 +384,9 @@
}
catch (const std::exception& e)
{
- log<level::ERR>(
- std::format(
- "Unable to handle inventory PropertiesChanged event: {}",
- e.what())
- .c_str());
+ lg2::error(
+ "Unable to handle inventory PropertiesChanged event: {ERROR}",
+ "ERROR", e);
}
}
@@ -443,8 +439,7 @@
}
if (!fs::is_directory(dir))
{
- log<level::ERR>("The path is not a directory",
- entry("PATH=%s", dir.c_str()));
+ lg2::error("The path is not a directory: {PATH}", "PATH", dir);
return;
}
@@ -459,21 +454,19 @@
}
if (path == dir)
{
- log<level::ERR>("Model directory not found");
+ lg2::error("Model directory not found");
return;
}
if (!fs::is_directory(path))
{
- log<level::ERR>("The path is not a directory",
- entry("PATH=%s", path.c_str()));
+ lg2::error("The path is not a directory: {PATH}", "PATH", path);
return;
}
if (!fs::exists(manifest))
{
- log<level::ERR>("No MANIFEST found",
- entry("PATH=%s", manifest.c_str()));
+ lg2::error("No MANIFEST found at {PATH}", "PATH", manifest);
return;
}
// If the model in manifest does not match the dir name
@@ -488,8 +481,8 @@
auto model = info["model"];
if (path.stem() != model)
{
- log<level::ERR>("Unmatched model", entry("PATH=%s", path.c_str()),
- entry("MODEL=%s", model.c_str()));
+ lg2::error("Unmatched model: path={PATH}, model={MODEL}", "PATH",
+ path, "MODEL", model);
}
else
{
@@ -520,8 +513,7 @@
}
else
{
- log<level::ERR>("MANIFEST is not a file",
- entry("PATH=%s", manifest.c_str()));
+ lg2::error("MANIFEST is not a file: {PATH}", "PATH", manifest);
}
}
@@ -576,9 +568,8 @@
{
if (!utils::isAssociated(p, assocs))
{
- log<level::INFO>(
- "Automatically update PSU",
- entry("VERSION_ID=%s", latestVersionId->c_str()));
+ lg2::info("Automatically update PSUs to version {VERSION_ID}",
+ "VERSION_ID", *latestVersionId);
invokeActivation(activation);
break;
}
@@ -632,10 +623,8 @@
}
catch (const std::exception& e)
{
- log<level::ERR>(
- std::format("Unable to handle inventory InterfacesAdded event: {}",
- e.what())
- .c_str());
+ lg2::error("Unable to handle inventory InterfacesAdded event: {ERROR}",
+ "ERROR", e);
}
}
diff --git a/src/utils.cpp b/src/utils.cpp
index 316ef31..6b7f602 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -4,14 +4,12 @@
#include <openssl/evp.h>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <algorithm>
#include <fstream>
#include <sstream>
-using namespace phosphor::logging;
-
namespace utils
{
@@ -109,7 +107,7 @@
mapperResponseMsg.read(mapperResponse);
if (mapperResponse.empty())
{
- log<level::ERR>("Error reading mapper response");
+ lg2::error("Error reading mapper response");
throw std::runtime_error("Error reading mapper response");
}
std::vector<std::string> ret;
@@ -122,8 +120,8 @@
}
catch (const sdbusplus::exception_t& ex)
{
- log<level::ERR>("GetObject call failed", entry("PATH=%s", path),
- entry("INTERFACE=%s", interface));
+ lg2::error("GetObject call failed: path={PATH}, interface={INTERFACE}",
+ "PATH", path, "INTERFACE", interface);
throw std::runtime_error("GetObject call failed");
}
}
@@ -132,7 +130,7 @@
{
if (version.empty())
{
- log<level::ERR>("Error version is empty");
+ lg2::error("Error version is empty");
return {};
}
@@ -213,9 +211,10 @@
}
catch (const sdbusplus::exception_t& ex)
{
- log<level::ERR>("GetProperty call failed", entry("PATH=%s", path),
- entry("INTERFACE=%s", interface),
- entry("PROPERTY=%s", propertyName));
+ lg2::error(
+ "GetProperty call failed: path={PATH}, interface={INTERFACE}, "
+ "property={PROPERTY}",
+ "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName);
throw std::runtime_error("GetProperty call failed");
}
}
diff --git a/src/version.cpp b/src/version.cpp
index f64e4ad..74664d8 100644
--- a/src/version.cpp
+++ b/src/version.cpp
@@ -3,7 +3,8 @@
#include "item_updater.hpp"
#include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <fstream>
@@ -28,7 +29,7 @@
{
if (filePath.empty())
{
- log<level::ERR>("Error filePath is empty");
+ lg2::error("Error filePath is empty");
elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"),
Argument::ARGUMENT_VALUE(filePath.c_str()));
}