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: I4fe8f4dec90e5062096168e05947b6d9bc355bb2
diff --git a/presence/gpio_presence.cpp b/presence/gpio_presence.cpp
index 35326a9..fd1fc8c 100644
--- a/presence/gpio_presence.cpp
+++ b/presence/gpio_presence.cpp
@@ -7,7 +7,7 @@
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <fstream>
@@ -45,10 +45,9 @@
}
catch (const sdbusplus::exception_t& e)
{
- log<level::ERR>("Error in mapper call to get service name",
- entry("PATH=%s", path.c_str()),
- entry("INTERFACE=%s", interface.c_str()),
- entry("ERROR=%s", e.what()));
+ lg2::error(
+ "Error in mapper call to get service name, path: {PATH}, interface: {INTERFACE}, error: {ERROR}",
+ "PATH", path, "INTERFACE", interface, "ERROR", e);
elog<InternalFailure>();
}
@@ -63,8 +62,8 @@
&value);
if (0 == fetch_rc)
{
- log<level::ERR>("Device does not support event type",
- entry("KEYCODE=%d", key));
+ lg2::error("Device does not support event type, key: {KEYCODE}",
+ "KEYCODE", key);
elog<InternalFailure>();
return;
}
@@ -159,9 +158,9 @@
{
ObjectMap invObj = getObjectMap(present);
- log<level::INFO>("Updating inventory present property",
- entry("PRESENT=%d", present),
- entry("PATH=%s", inventory.c_str()));
+ lg2::info(
+ "Updating inventory present property value to {PRESENT}, path: {PATH}",
+ "PRESENT", present, "PATH", inventory);
auto invService = getService(INVENTORY_PATH, INVENTORY_INTF, bus);
@@ -175,8 +174,9 @@
}
catch (const sdbusplus::exception_t& e)
{
- log<level::ERR>("Error in inventory manager call to update inventory",
- entry("ERROR=%s", e.what()));
+ lg2::error(
+ "Error in inventory manager call to update inventory: {ERROR}",
+ "ERROR", e);
elog<InternalFailure>();
}
}
@@ -192,15 +192,13 @@
if (present)
{
- log<level::INFO>("Binding a device driver",
- entry("PATH=%s", path.c_str()),
- entry("DEVICE=%s", device.c_str()));
+ lg2::info("Binding a {DEVICE} driver: {PATH}", "DEVICE", device,
+ "PATH", path);
}
else
{
- log<level::INFO>("Unbinding a device driver",
- entry("PATH=%s", path.c_str()),
- entry("DEVICE=%s", device.c_str()));
+ lg2::info("Unbinding a {DEVICE} driver: {PATH}", "DEVICE", device,
+ "PATH", path);
}
std::ofstream file;
@@ -216,13 +214,9 @@
}
catch (const std::exception& e)
{
- auto err = errno;
-
- log<level::ERR>("Failed binding or unbinding a device "
- "after a card was removed or added",
- entry("PATH=%s", path.c_str()),
- entry("DEVICE=%s", device.c_str()),
- entry("ERRNO=%d", err));
+ lg2::error(
+ "Failed binding or unbinding a {DEVICE} after a card was removed or added, path: {PATH}, error: {ERROR}",
+ "DEVICE", device, "PATH", path, "ERROR", e);
}
}
}
diff --git a/presence/main.cpp b/presence/main.cpp
index 96f6f8f..cb573fc 100644
--- a/presence/main.cpp
+++ b/presence/main.cpp
@@ -3,11 +3,10 @@
#include <systemd/sd-event.h>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <iostream>
-using namespace phosphor::logging;
using namespace phosphor::gpio;
using namespace phosphor::gpio::presence;
@@ -49,7 +48,8 @@
}
else
{
- std::cerr << "Invalid path,device combination: " << entry << "\n";
+ lg2::error("Invalid path,device combination: {ENTRY}", "ENTRY",
+ entry);
return -1;
}
}
@@ -120,7 +120,7 @@
rc = sd_event_default(&event);
if (rc < 0)
{
- log<level::ERR>("Error creating a default sd_event handler");
+ lg2::error("Error creating a default sd_event handler");
return rc;
}
EventPtr eventP{event};
@@ -136,8 +136,7 @@
rc = sd_event_run(eventP.get(), (uint64_t)-1);
if (rc < 0)
{
- log<level::ERR>("Failure in processing request",
- entry("ERROR=%s", strerror(-rc)));
+ lg2::error("Failure in processing request: {RC}", "RC", rc);
break;
}
}