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/button_handler.cpp b/src/button_handler.cpp
index 4072e1d..b1c7ef9 100644
--- a/src/button_handler.cpp
+++ b/src/button_handler.cpp
@@ -2,7 +2,7 @@
 
 #include "button_handler.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <xyz/openbmc_project/State/Chassis/server.hpp>
 #include <xyz/openbmc_project/State/Host/server.hpp>
 namespace phosphor
@@ -12,7 +12,6 @@
 
 namespace sdbusRule = sdbusplus::bus::match::rules;
 using namespace sdbusplus::xyz::openbmc_project::State::server;
-using namespace phosphor::logging;
 
 constexpr auto chassisIface = "xyz.openbmc_project.State.Chassis";
 constexpr auto hostIface = "xyz.openbmc_project.State.Host";
@@ -39,7 +38,7 @@
     {
         if (!getService(POWER_DBUS_OBJECT_NAME, powerButtonIface).empty())
         {
-            log<level::INFO>("Starting power button handler");
+            lg2::info("Starting power button handler");
             powerButtonReleased = std::make_unique<sdbusplus::bus::match_t>(
                 bus,
                 sdbusRule::type::signal() + sdbusRule::member("Released") +
@@ -68,7 +67,7 @@
     {
         if (!getService(ID_DBUS_OBJECT_NAME, idButtonIface).empty())
         {
-            log<level::INFO>("Registering ID button handler");
+            lg2::info("Registering ID button handler");
             idButtonReleased = std::make_unique<sdbusplus::bus::match_t>(
                 bus,
                 sdbusRule::type::signal() + sdbusRule::member("Released") +
@@ -87,7 +86,7 @@
     {
         if (!getService(RESET_DBUS_OBJECT_NAME, resetButtonIface).empty())
         {
-            log<level::INFO>("Registering reset button handler");
+            lg2::info("Registering reset button handler");
             resetButtonReleased = std::make_unique<sdbusplus::bus::match_t>(
                 bus,
                 sdbusRule::type::signal() + sdbusRule::member("Released") +
@@ -126,7 +125,7 @@
 
     if (HSService.empty())
     {
-        log<level::INFO>("Host Selector dbus object not available");
+        lg2::info("Host Selector dbus object not available");
         throw std::invalid_argument("Host selector dbus object not available");
     }
 
@@ -145,8 +144,7 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Error reading Host selector Position",
-                        entry("ERROR=%s", e.what()));
+        lg2::error("Error reading Host selector Position: {ERROR}", "ERROR", e);
         throw;
     }
 }
@@ -179,8 +177,8 @@
     if (isMultiHostSystem)
     {
         hostNumber = getHostSelectorValue();
-        log<level::INFO>("Multi host system detected : ",
-                         entry("POSITION=%d", hostNumber));
+        lg2::info("Multi host system detected : {POSITION}", "POSITION",
+                  hostNumber);
     }
 
     std::string hostNumStr = std::to_string(hostNumber);
@@ -189,8 +187,8 @@
     if (isMultiHostSystem && (hostNumber == BMC_POSITION) &&
         (powerEventType != PowerEvent::longPowerPressed))
     {
-        log<level::INFO>("handlePowerEvent : BMC selected on multihost system."
-                         "ignoring power and reset button events...");
+        lg2::info(
+            "handlePowerEvent : BMC selected on multihost system. ignoring power and reset button events...");
         return;
     }
 
@@ -208,7 +206,7 @@
             {
                 transition = Host::Transition::Off;
             }
-            log<level::INFO>("handlePowerEvent : handle power button press ");
+            lg2::info("handlePowerEvent : handle power button press ");
 
             break;
         }
@@ -236,12 +234,10 @@
             }
             else if (!poweredOn(hostNumber))
             {
-                log<level::INFO>(
-                    "Power is off so ignoring long power button press");
+                lg2::info("Power is off so ignoring long power button press");
                 return;
             }
-            log<level::INFO>(
-                "handlePowerEvent : handle long power button press");
+            lg2::info("handlePowerEvent : handle long power button press");
 
             break;
         }
@@ -254,19 +250,19 @@
 
             if (!poweredOn(hostNumber))
             {
-                log<level::INFO>("Power is off so ignoring reset button press");
+                lg2::info("Power is off so ignoring reset button press");
                 return;
             }
 
-            log<level::INFO>("Handling reset button press");
+            lg2::info("Handling reset button press");
             transition = Host::Transition::Reboot;
             break;
         }
         default:
         {
-            log<level::ERR>(
-                "Invalid power event. skipping...",
-                entry("EVENT=%d", static_cast<int>(powerEventType)));
+            lg2::error("{EVENT} is invalid power event. skipping...", "EVENT",
+                       static_cast<std::underlying_type_t<PowerEvent>>(
+                           powerEventType));
 
             return;
         }
@@ -285,8 +281,8 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed power state change on a power button press",
-                        entry("ERROR=%s", e.what()));
+        lg2::error("Failed power state change on a power button press: {ERROR}",
+                   "ERROR", e);
     }
 }
 void Handler::longPowerPressed(sdbusplus::message::message& /* msg */)
@@ -297,8 +293,8 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed powering off on long power button press",
-                        entry("ERROR=%s", e.what()));
+        lg2::error("Failed powering off on long power button press: {ERROR}",
+                   "ERROR", e);
     }
 }
 
@@ -310,8 +306,8 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed power state change on a reset button press",
-                        entry("ERROR=%s", e.what()));
+        lg2::error("Failed power state change on a reset button press: {ERROR}",
+                   "ERROR", e);
     }
 }
 
@@ -324,8 +320,8 @@
 
     if (service.empty())
     {
-        log<level::INFO>("No identify LED group found during ID button press",
-                         entry("GROUP=%s", groupPath.c_str()));
+        lg2::info("No found {GROUP} during ID button press:", "GROUP",
+                  groupPath);
         return;
     }
 
@@ -341,9 +337,9 @@
 
         state = !std::get<bool>(state);
 
-        log<level::INFO>("Changing ID LED group state on ID LED press",
-                         entry("GROUP=%s", groupPath.c_str()),
-                         entry("STATE=%d", std::get<bool>(state)));
+        lg2::info(
+            "Changing ID LED group state on ID LED press, GROUP = {GROUP}, STATE = {STATE}",
+            "GROUP", groupPath, "STATE", std::get<bool>(state));
 
         method = bus.new_method_call(service.c_str(), groupPath.c_str(),
                                      propertyIface, "Set");
@@ -353,8 +349,8 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Error toggling ID LED group on ID button press",
-                        entry("ERROR=%s", e.what()));
+        lg2::error("Error toggling ID LED group on ID button press: {ERROR}",
+                   "ERROR", e);
     }
 }
 } // namespace button
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;
     }
 
diff --git a/src/hostSelector_switch.cpp b/src/hostSelector_switch.cpp
index dbdd836..66b79c8 100644
--- a/src/hostSelector_switch.cpp
+++ b/src/hostSelector_switch.cpp
@@ -1,9 +1,13 @@
 
 #include "hostSelector_switch.hpp"
 
+#include <error.h>
+
+#include <phosphor-logging/lg2.hpp>
+
 // add the button iface class to registry
 static ButtonIFRegister<HostSelector> buttonRegister;
-using namespace phosphor::logging;
+
 size_t HostSelector::getMappedHSConfig(size_t hsPosition)
 {
     size_t adjustedPosition = INVALID_INDEX; // set bmc as default value
@@ -16,9 +20,8 @@
     }
     else
     {
-        log<level::DEBUG>(
-            "getMappedHSConfig : no valid value in map.",
-            entry("FORM_FACTOR_TYPE=%s", (getFormFactorType()).c_str()));
+        lg2::debug("getMappedHSConfig : {TYPE}: no valid value in map.", "TYPE",
+                   getFormFactorType());
     }
     return adjustedPosition;
 }
@@ -43,9 +46,8 @@
 
         if (result < 0)
         {
-            log<level::ERR>(
-                "gpio fd lseek error!",
-                entry("FORM_FACTOR_TYPE=%s", (getFormFactorType()).c_str()));
+            lg2::error("{TYPE}: Gpio fd lseek error: {ERROR}", "TYPE",
+                       getFormFactorType(), "ERROR", errno);
             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
                 IOError();
         }
@@ -53,9 +55,8 @@
         result = ::read(config.gpios[index].fd, &buf, sizeof(buf));
         if (result < 0)
         {
-            log<level::ERR>(
-                "gpio fd read error!",
-                entry("FORM_FACTOR_TYPE=%s", (getFormFactorType()).c_str()));
+            lg2::error("{TYPE}: Gpio fd read error: {ERROR}", "TYPE",
+                       getFormFactorType(), "ERROR", errno);
             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
                 IOError();
         }
@@ -104,18 +105,16 @@
 
     if (n < 0)
     {
-        log<level::ERR>(
-            "gpio fd lseek error!",
-            entry("FORM_FACTOR_TYPE=%s", (getFormFactorType()).c_str()));
+        lg2::error("{TYPE}: Gpio fd lseek error: {ERROR}", "TYPE",
+                   getFormFactorType(), "ERROR", errno);
         return;
     }
 
     n = ::read(fd, &buf, sizeof(buf));
     if (n < 0)
     {
-        log<level::ERR>(
-            "gpio fd read error!",
-            entry("FORM_FACTOR_TYPE=%s", (getFormFactorType()).c_str()));
+        lg2::error("{TYPE}: Gpio fd read error: {ERROR}", "TYPE",
+                   getFormFactorType(), "ERROR", errno);
         throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
             IOError();
     }
diff --git a/src/main.cpp b/src/main.cpp
index 5288635..5b5341d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -23,7 +23,6 @@
 #include <fstream>
 static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
 
-using namespace phosphor::logging;
 nlohmann::json gpioDefs;
 
 int main(void)