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.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I7983e39c9afdd455b21923f860f827cf9a9c257c
diff --git a/src/gpio.cpp b/src/gpio.cpp
index faf5639..9f8b98d 100644
--- a/src/gpio.cpp
+++ b/src/gpio.cpp
@@ -18,19 +18,19 @@
#include "gpio.hpp"
+#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <gpioplus/utility/aspeed.hpp>
#include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <filesystem>
#include <fstream>
const std::string gpioDev = "/sys/class/gpio";
-using namespace phosphor::logging;
namespace fs = std::filesystem;
void closeGpio(int fd)
@@ -68,7 +68,7 @@
}
}
- log<level::ERR>("Could not find GPIO base");
+ lg2::error("Could not find GPIO base");
throw std::runtime_error("Could not find GPIO base!");
#else
return 0;
@@ -94,11 +94,9 @@
result = configGpio(gpioCfg);
if (result < 0)
{
- std::string errorMsg =
- "Error configuring gpio: GPIO_NUM=" +
- std::to_string(gpioCfg.number) +
- ",BUTTON_NAME=" + buttonIFConfig.formFactorName;
- log<level::ERR>(errorMsg.c_str());
+ lg2::error("{NAME}: Error configuring gpio-{NUM}: {RESULT}", "NAME",
+ buttonIFConfig.formFactorName, "NUM", gpioCfg.number,
+ "RESULT", result);
break;
}
@@ -124,7 +122,7 @@
if (fs::exists(fullPath))
{
- log<level::INFO>("GPIO exported", entry("PATH=%s", devPath.c_str()));
+ lg2::info("GPIO exported: {PATH}", "PATH", devPath);
}
else
{
@@ -139,9 +137,8 @@
catch (const std::exception& e)
{
- log<level::ERR>("Error in writing!",
- entry("PATH=%s", devPath.c_str()),
- entry("NUM=%d", gpioNum));
+ lg2::error("{NUM} error in writing {PATH}: {ERROR}", "NUM", gpioNum,
+ "PATH", devPath, "ERROR", e);
return -1;
}
}
@@ -161,8 +158,8 @@
catch (const std::exception& e)
{
- log<level::ERR>("Error in reading!",
- entry("PATH=%s", devPath.c_str()));
+ lg2::error("Error in reading {PATH}: {ERROR}", "PATH", devPath,
+ "ERROR", e);
return -1;
}
@@ -180,7 +177,7 @@
catch (const std::exception& e)
{
- log<level::ERR>("Error in writing!");
+ lg2::error("Error in writing: {ERROR}", "ERROR", e);
return -1;
}
}
@@ -197,7 +194,7 @@
catch (const std::exception& e)
{
- log<level::ERR>("Error in writing!");
+ lg2::error("Error in writing: {ERROR}", "ERROR", e);
return -1;
}
}
@@ -218,7 +215,7 @@
catch (const std::exception& e)
{
- log<level::ERR>("Error in writing!");
+ lg2::error("Error in writing: {ERROR}", "ERROR", e);
return -1;
}
devPath.clear();
@@ -236,7 +233,7 @@
catch (const std::exception& e)
{
- log<level::ERR>("Error in writing!");
+ lg2::error("Error in writing: {ERROR}", "ERROR", e);
return -1;
}
}
@@ -247,7 +244,8 @@
if (fd < 0)
{
- log<level::ERR>("open error!");
+ lg2::error("Open {PATH} error: {ERROR}", "PATH", devPath, "ERROR",
+ errno);
return -1;
}