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 biosConfigManager successfully.
journalctl -o json-pretty SYSLOG_IDENTIFIER=biosconfig-manager
{
"_TRANSPORT" : "journal",
"_PID" : "244",
"__MONOTONIC_TIMESTAMP" : "513878067",
"_SYSTEMD_SLICE" : "system.slice",
"CODE_LINE" : "123",
"_SOURCE_REALTIME_TIMESTAMP" : "513877941",
"_HOSTNAME" : "fp5280g2",
"_CAP_EFFECTIVE" : "1ffffffffff",
"_BOOT_ID" : "40382c3612f54e1a96a6ecf41fe6b38e",
"SYSLOG_IDENTIFIER" : "biosconfig-manager",
"_CMDLINE" : "/usr/bin/biosconfig-manager",
"_EXE" : "/usr/bin/biosconfig-manager",
"LOG2_FMTMSG" : "BIOS attribute not found in the BaseBIOSTable",
"_SYSTEMD_UNIT" : "xyz.openbmc_project.biosconfig_manager.service",
"MESSAGE" : "BIOS attribute not found in the BaseBIOSTable",
"PRIORITY" : "3",
"CODE_FILE" : "../biosconfig-manager/src/manager.cpp",
"CODE_FUNC" : "virtual bios_config::Manager::PendingAttributes
bios_config::Manager::pendingAttributes
(bios_config::Manager::PendingAttributes)"
...
}
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I274d61827279b10ba3fe9d61404ce8b654ccdd9e
diff --git a/src/manager.cpp b/src/manager.cpp
index 14654be..0cb4259 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -21,6 +21,7 @@
#include <boost/asio.hpp>
#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
@@ -119,8 +120,7 @@
// BIOS attribute not found in the BaseBIOSTable
if (iter == biosTable.end())
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "BIOS attribute not found in the BaseBIOSTable");
+ lg2::error("BIOS attribute not found in the BaseBIOSTable");
throw AttributeNotFound();
}
@@ -128,8 +128,7 @@
std::get<static_cast<uint8_t>(Index::attributeType)>(iter->second);
if (attributeType != std::get<0>(pair.second))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "attributeType is not same with bios base table");
+ lg2::error("attributeType is not same with bios base table");
throw InvalidArgument();
}
@@ -139,8 +138,7 @@
// For enumeration the expected variant types is Enumeration
if (std::get<1>(pair.second).index() == 0)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Enumeration property value is not enum");
+ lg2::error("Enumeration property value is not enum");
throw InvalidArgument();
}
@@ -163,8 +161,7 @@
if (!found)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "No valid attribute");
+ lg2::error("No valid attribute");
throw InvalidArgument();
}
}
@@ -174,8 +171,7 @@
// For enumeration the expected variant types is std::string
if (std::get<1>(pair.second).index() == 0)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "String property value is not string");
+ lg2::error("String property value is not string");
throw InvalidArgument();
}
@@ -196,10 +192,8 @@
if (optionsIterator == options.end())
{
- std::string error =
- attrValue + " is not a valid value for " + pair.first;
- phosphor::logging::log<phosphor::logging::level::ERR>(
- error.c_str());
+ lg2::error("{ATTRVALUE} is not a valid value for {NAME}",
+ "ATTRVALUE", attrValue, "NAME", pair.first);
throw InvalidArgument();
}
}
@@ -209,8 +203,7 @@
// For enumeration the expected variant types is Integer
if (std::get<1>(pair.second).index() == 1)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Enumeration property value is not int");
+ lg2::error("Enumeration property value is not int");
throw InvalidArgument();
}
@@ -241,16 +234,16 @@
if ((attrValue < lowerBound) || (attrValue > upperBound))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Integer, bound is invalid");
+ lg2::error("Integer, bound is invalid");
throw InvalidArgument();
}
if (((std::abs(attrValue - lowerBound)) % scalarIncrement) != 0)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "((std::abs(attrValue - lowerBound)) % scalarIncrement) != "
- "0");
+ lg2::error(
+ "((std::abs({ATTR_VALUE} - {LOWER_BOUND})) % {SCALAR_INCREMENT}) != 0",
+ "ATTR_VALUE", attrValue, "LOWER_BOUND", lowerBound,
+ "SCALAR_INCREMENT", scalarIncrement);
throw InvalidArgument();
}
}
diff --git a/src/manager_serialize.cpp b/src/manager_serialize.cpp
index 0756006..18163b7 100644
--- a/src/manager_serialize.cpp
+++ b/src/manager_serialize.cpp
@@ -7,15 +7,13 @@
#include <cereal/types/tuple.hpp>
#include <cereal/types/variant.hpp>
#include <cereal/types/vector.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <fstream>
namespace bios_config
{
-using namespace phosphor::logging;
-
/** @brief Function required by Cereal to perform serialization.
*
* @tparam Archive - Cereal archive type (binary in this case).
@@ -77,13 +75,13 @@
}
catch (cereal::Exception& e)
{
- log<level::ERR>(e.what());
+ lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
fs::remove(path);
return false;
}
catch (const std::length_error& e)
{
- log<level::ERR>(e.what());
+ lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
fs::remove(path);
return false;
}
diff --git a/src/password.cpp b/src/password.cpp
index 3aacc45..5231120 100644
--- a/src/password.cpp
+++ b/src/password.cpp
@@ -21,6 +21,7 @@
#include <boost/algorithm/hex.hpp>
#include <boost/asio.hpp>
#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
@@ -36,7 +37,7 @@
const std::array<uint8_t, maxSeedSize>& seed,
const std::string rawData, const std::string algo)
{
- phosphor::logging::log<phosphor::logging::level::ERR>("isMatch");
+ lg2::error("isMatch");
if (algo == "SHA256")
{
@@ -49,9 +50,8 @@
reinterpret_cast<const unsigned char*>(seed.data()),
seed.size(), iterValue, EVP_sha256(), hashLen, output.data()))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Generate PKCS5_PBKDF2_HMAC_SHA256 Integrity Check Value "
- "failed");
+ lg2::error(
+ "Generate PKCS5_PBKDF2_HMAC_SHA256 Integrity Check Value failed");
throw InternalFailure();
}
@@ -78,9 +78,8 @@
reinterpret_cast<const unsigned char*>(seed.data()),
seed.size(), iterValue, EVP_sha384(), hashLen, output.data()))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- "Generate PKCS5_PBKDF2_HMAC_SHA384 Integrity Check Value "
- "failed");
+ lg2::error(
+ "Generate PKCS5_PBKDF2_HMAC_SHA384 Integrity Check Value failed");
throw InternalFailure();
}
@@ -121,8 +120,8 @@
}
catch (const nlohmann::json::parse_error& e)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
- e.what());
+ lg2::error("Failed to parse JSON file: {ERROR}", "ERROR",
+ e);
throw InternalFailure();
}
@@ -142,7 +141,7 @@
}
catch (nlohmann::detail::exception& e)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+ lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", e);
throw InternalFailure();
}
if (userName == "AdminPassword")
@@ -171,7 +170,7 @@
seed.size(), iterValue, EVP_sha256(), mdLen,
mNewPwdHash.data()))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
+ lg2::error(
"Verify PKCS5_PBKDF2_HMAC_SHA256 Integrity Check failed");
throw InternalFailure();
}
@@ -188,7 +187,7 @@
seed.size(), iterValue, EVP_sha384(), mdLen,
mNewPwdHash.data()))
{
- phosphor::logging::log<phosphor::logging::level::ERR>(
+ lg2::error(
"Verify PKCS5_PBKDF2_HMAC_SHA384 Integrity Check failed");
throw InternalFailure();
}
@@ -200,8 +199,7 @@
void Password::changePassword(std::string userName, std::string currentPassword,
std::string newPassword)
{
- phosphor::logging::log<phosphor::logging::level::DEBUG>(
- "BIOS config changePassword");
+ lg2::debug("BIOS config changePassword");
verifyPassword(userName, currentPassword, newPassword);
std::ifstream fs(seedFile.c_str());
@@ -215,7 +213,7 @@
}
catch (const nlohmann::json::parse_error& e)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+ lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", e);
throw InternalFailure();
}
@@ -233,8 +231,7 @@
}
else
{
- phosphor::logging::log<phosphor::logging::level::DEBUG>(
- "Cannot open file stream");
+ lg2::debug("Cannot open file stream");
throw InternalFailure();
}
}
@@ -244,8 +241,7 @@
*systemBus, objectPathPwd),
objServer(objectServer), systemBus(systemBus)
{
- phosphor::logging::log<phosphor::logging::level::DEBUG>(
- "BIOS config password is runing");
+ lg2::debug("BIOS config password is runing");
try
{
fs::path biosDir(BIOS_PERSIST_PATH);
@@ -254,7 +250,7 @@
}
catch (const fs::filesystem_error& e)
{
- phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+ lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", e);
throw InternalFailure();
}
}