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/evdev.cpp b/evdev.cpp
index 056093a..3ec1cac 100644
--- a/evdev.cpp
+++ b/evdev.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>
namespace phosphor
{
@@ -20,14 +20,11 @@
// Populate the file descriptor for passed in device
int Evdev::openDevice()
{
- using namespace phosphor::logging;
-
auto fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
if (fd < 0)
{
- log<level::ERR>("Failed to open device path",
- entry("DEVICEPATH=%s", path.c_str()),
- entry("ERRNO=%d", errno));
+ lg2::error("Failed to open {DEVICEPATH}: {ERRNO}", "DEVICEPATH", path,
+ "ERRNO", errno);
elog<InternalFailure>();
}
return fd;
@@ -46,7 +43,7 @@
auto rc = libevdev_new_from_fd((fd)(), &evdev);
if (rc < 0)
{
- log<level::ERR>("Failed to initialize evdev");
+ lg2::error("Failed to initialize evdev");
elog<InternalFailure>();
return;
}
@@ -65,8 +62,7 @@
if (rc < 0)
{
- log<level::ERR>("Failed to register callback handler",
- entry("ERROR=%s", strerror(-rc)));
+ lg2::error("Failed to register callback handler: {RC}", "RC", rc);
elog<InternalFailure>();
}
}
diff --git a/gpio-util/gpio.cpp b/gpio-util/gpio.cpp
index 0b671a8..0e825b9 100644
--- a/gpio-util/gpio.cpp
+++ b/gpio-util/gpio.cpp
@@ -18,17 +18,16 @@
#include <fcntl.h>
#include <sys/ioctl.h>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <cassert>
+#include <cstring>
namespace phosphor
{
namespace gpio
{
-using namespace phosphor::logging;
-
void GPIO::set(Value value)
{
assert(direction == Direction::output);
@@ -41,8 +40,7 @@
auto rc = ioctl(lineFD(), GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
if (rc == -1)
{
- auto e = errno;
- log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
+ lg2::error("Failed SET_LINE_VALUES ioctl: {ERRNO}", "ERRNO", errno);
throw std::runtime_error("Failed SET_LINE_VALUES ioctl");
}
}
@@ -58,10 +56,8 @@
FileDescriptor fd{open(device.c_str(), 0)};
if (fd() == -1)
{
- auto e = errno;
- log<level::ERR>("Failed opening GPIO device",
- entry("DEVICE=%s", device.c_str()),
- entry("ERRNO=%d", e));
+ lg2::error("Failed opening {DEVICE}: {ERRNO}", "DEVICE", device,
+ "ERRNO", errno);
throw std::runtime_error("Failed opening GPIO device");
}
@@ -85,9 +81,8 @@
auto rc = ioctl(fd(), GPIO_GET_LINEHANDLE_IOCTL, &request);
if (rc == -1)
{
- auto e = errno;
- log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
- entry("ERRNO=%d", e));
+ lg2::error("Failed GET_LINEHANDLE ioctl {GPIO}: {ERRNO}", "GPIO", gpio,
+ "ERRNO", errno);
throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
}
diff --git a/gpio-util/main.cpp b/gpio-util/main.cpp
index 27da697..d7cdc3d 100644
--- a/gpio-util/main.cpp
+++ b/gpio-util/main.cpp
@@ -26,8 +26,6 @@
#include "argument.hpp"
#include "gpio.hpp"
-#include <phosphor-logging/log.hpp>
-
#include <algorithm>
#include <chrono>
#include <iostream>
@@ -35,7 +33,6 @@
#include <thread>
using namespace phosphor::gpio;
-using namespace phosphor::logging;
typedef void (*gpioFunction)(GPIO&, unsigned int);
using gpioFunctionMap = std::map<std::string, gpioFunction>;
diff --git a/gpioMon.cpp b/gpioMon.cpp
index 0b5bbd9..d29bd3e 100644
--- a/gpioMon.cpp
+++ b/gpioMon.cpp
@@ -16,7 +16,7 @@
#include "gpioMon.hpp"
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
namespace phosphor
@@ -32,8 +32,6 @@
constexpr auto falling = "FALLING";
constexpr auto rising = "RISING";
-using namespace phosphor::logging;
-
void GpioMonitor::scheduleEventHandler()
{
gpioEventDescriptor.async_wait(
@@ -41,9 +39,8 @@
[this](const boost::system::error_code& ec) {
if (ec)
{
- std::string msg = gpioLineMsg + "event handler error" +
- std::string(ec.message());
- log<level::ERR>(msg.c_str());
+ lg2::error("{GPIO} event handler error: {ERROR}", "GPIO",
+ gpioLineMsg, "ERROR", ec.message());
return;
}
gpioEventHandler();
@@ -57,17 +54,18 @@
if (gpiod_line_event_read_fd(gpioEventDescriptor.native_handle(),
&gpioLineEvent) < 0)
{
- log<level::ERR>("Failed to read gpioLineEvent from fd",
- entry("GPIO_LINE=%s", gpioLineMsg.c_str()));
+ lg2::error("Failed to read {GPIO} from fd", "GPIO", gpioLineMsg);
return;
}
- std::string logMessage =
- gpioLineMsg + (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE
- ? " Asserted"
- : " Deasserted");
-
- log<level::INFO>(logMessage.c_str());
+ if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
+ {
+ lg2::info("{GPIO} Asserted", "GPIO", gpioLineMsg);
+ }
+ else
+ {
+ lg2::info("{GPIO} Deasserted", "GPIO", gpioLineMsg);
+ }
/* Execute the target if it is defined. */
if (!target.empty())
@@ -127,21 +125,18 @@
/* Request an event to monitor for respected gpio line */
if (gpiod_line_request(gpioLine, &gpioConfig, 0) < 0)
{
- log<level::ERR>("Failed to request gpioLineEvent",
- entry("GPIO_LINE=%s", gpioLineMsg.c_str()));
+ lg2::error("Failed to request {GPIO}", "GPIO", gpioLineMsg);
return -1;
}
int gpioLineFd = gpiod_line_event_get_fd(gpioLine);
if (gpioLineFd < 0)
{
- log<level::ERR>("Failed to get fd for gpioLineEvent",
- entry("GPIO_LINE=%s", gpioLineMsg.c_str()));
+ lg2::error("Failed to get fd for {GPIO}", "GPIO", gpioLineMsg);
return -1;
}
- std::string logMsg = gpioLineMsg + " monitoring started";
- log<level::INFO>(logMsg.c_str());
+ lg2::info("{GPIO} monitoring started", "GPIO", gpioLineMsg);
/* Assign line fd to descriptor for monitoring */
gpioEventDescriptor.assign(gpioLineFd);
diff --git a/gpioMonMain.cpp b/gpioMonMain.cpp
index 83e14b6..09f92cb 100644
--- a/gpioMonMain.cpp
+++ b/gpioMonMain.cpp
@@ -19,12 +19,10 @@
#include <CLI/CLI.hpp>
#include <boost/asio/io_context.hpp>
#include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <fstream>
-using namespace phosphor::logging;
-
namespace phosphor
{
namespace gpio
@@ -68,8 +66,8 @@
std::ifstream file(gpioFileName);
if (!file)
{
- log<level::ERR>("GPIO monitor config file not found",
- entry("GPIO_MON_FILE=%s", gpioFileName.c_str()));
+ lg2::error("GPIO monitor config file not found: {FILE}", "FILE",
+ gpioFileName);
return -1;
}
@@ -114,9 +112,8 @@
if (obj.find("GpioNum") == obj.end() ||
obj.find("ChipId") == obj.end())
{
- log<level::ERR>(
- "Failed to find line name or gpio number",
- entry("GPIO_JSON_FILE_NAME=%s", gpioFileName.c_str()));
+ lg2::error("Failed to find line name or gpio number: {FILE}",
+ "FILE", gpioFileName);
return -1;
}
@@ -138,8 +135,7 @@
if (line == NULL)
{
- errMsg = "Failed to find the " + lineMsg;
- log<level::ERR>(errMsg.c_str());
+ lg2::error("Failed to find the {GPIO}", "GPIO", errMsg);
return -1;
}
@@ -152,8 +148,8 @@
auto findEvent = phosphor::gpio::polarityMap.find(eventStr);
if (findEvent == phosphor::gpio::polarityMap.end())
{
- errMsg = "Incorrect GPIO monitor event defined " + lineMsg;
- log<level::ERR>(errMsg.c_str());
+ lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg,
+ "EVENT", eventStr);
return -1;
}
diff --git a/mainapp.cpp b/mainapp.cpp
index 4816b84..7a3300f 100644
--- a/mainapp.cpp
+++ b/mainapp.cpp
@@ -19,12 +19,11 @@
#include <systemd/sd-event.h>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <iostream>
#include <string>
-using namespace phosphor::logging;
static void exitWithError(const char* err, char** argv)
{
phosphor::gpio::ArgumentParser::usage(argv);
@@ -72,7 +71,7 @@
auto r = sd_event_default(&event);
if (r < 0)
{
- log<level::ERR>("Error creating a default sd_event handler");
+ lg2::error("Error creating a default sd_event handler");
return r;
}
phosphor::gpio::EventPtr eventP{event};
@@ -90,8 +89,7 @@
r = sd_event_run(eventP.get(), (uint64_t)-1);
if (r < 0)
{
- log<level::ERR>("Failure in processing request",
- entry("ERROR=%s", strerror(-r)));
+ lg2::error("Failure in processing request: {RC}", "RC", r);
break;
}
}
diff --git a/monitor.cpp b/monitor.cpp
index 2c8569e..e8b844b 100644
--- a/monitor.cpp
+++ b/monitor.cpp
@@ -18,7 +18,7 @@
#include <fcntl.h>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
namespace phosphor
{
@@ -30,12 +30,10 @@
constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
-using namespace phosphor::logging;
-
// Callback handler when there is an activity on the FD
int Monitor::processEvents(sd_event_source*, int, uint32_t, void* userData)
{
- log<level::INFO>("GPIO line altered");
+ lg2::info("GPIO line altered");
auto monitor = static_cast<Monitor*>(userData);
monitor->analyzeEvent();
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;
}
}