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 ledManager successfully and Unit Test passes.

Before: The file does not exist or is empty
After: The file does not exist or is empty, FILE_PATH = /usr/share/phosphor-led-manager/lamp-test-led-overrides.json

MESSAGE=File does not exist, FILE_PATH = /var/lib/phosphor-led-manager/savedGroups
LOG2_FMTMSG=File does not exist, FILE_PATH = {PATH}
CODE_FILE=../../../../../../fp5280g2-workspace/sources/phosphor-led-manager/serialize.cpp
CODE_LINE=63
CODE_FUNC=void phosphor::led::Serialize::restoreGroups()
PATH=/var/lib/phosphor-led-manager/savedGroups
SYSLOG_IDENTIFIER=phosphor-ledmanager
_PID=298
_COMM=phosphor-ledman
_EXE=/usr/bin/phosphor-ledmanager
_CMDLINE=phosphor-ledmanager
_SYSTEMD_CGROUP=/system.slice/xyz.openbmc_project.LED.GroupManager.service
_SYSTEMD_UNIT=xyz.openbmc_project.LED.GroupManager.service
_SYSTEMD_INVOCATION_ID=5ddd4960a3b04fe29c34e4ce03b6be06

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I5ec8530e0b539bcf8d7d211c802430698aebd343
diff --git a/fault-monitor/fru-fault-monitor.cpp b/fault-monitor/fru-fault-monitor.cpp
index 047994f..57c7b78 100644
--- a/fault-monitor/fru-fault-monitor.cpp
+++ b/fault-monitor/fru-fault-monitor.cpp
@@ -3,6 +3,7 @@
 #include "elog-errors.hpp"
 
 #include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/exception.hpp>
 #include <xyz/openbmc_project/Led/Fru/Monitor/error.hpp>
 #include <xyz/openbmc_project/Led/Mapper/error.hpp>
@@ -63,9 +64,9 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>(
-            "Failed to GetObject or parse getService mapper response",
-            entry("ERROR=%s", e.what()));
+        lg2::error(
+            "Failed to parse getService mapper response, ERROR = {ERROR}",
+            "ERROR", e);
         using namespace xyz::openbmc_project::Led::Mapper;
         elog<ObjectNotFoundErr>(ObjectNotFoundError::METHOD_NAME("GetObject"),
                                 ObjectNotFoundError::PATH(path.c_str()),
@@ -127,8 +128,7 @@
     catch (const sdbusplus::exception::exception& e)
     {
         // Log an info message, system may not have all the LED Groups defined
-        log<level::INFO>("Failed to Assert LED Group",
-                         entry("ERROR=%s", e.what()));
+        lg2::info("Failed to Assert LED Group, ERROR = {ERROR}", "ERROR", e);
     }
 
     return;
@@ -146,9 +146,8 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed to parse created message",
-                        entry("ERROR=%s", e.what()),
-                        entry("REPLY_SIG=%s", msg.get_signature()));
+        lg2::error("Failed to parse created message, ERROR = {ERROR}", "ERROR",
+                   e);
         return;
     }
 
@@ -166,8 +165,9 @@
 
     // Nothing else shows when a specific error log
     // has been created. Do it here.
-    std::string message{objectPath.str + " created"};
-    log<level::INFO>(message.c_str());
+    // TODO:(phosphor-logging#25): support sdbusplus::message::object_path
+    // directly.
+    lg2::info("{PATH} created", "PATH", objectPath.str);
 
     auto attr = iter->second.find("Associations");
     if (attr == iter->second.end())
@@ -211,8 +211,9 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed to parse existing callouts subtree message",
-                        entry("ERROR=%s", e.what()));
+        lg2::error(
+            "Failed to parse existing callouts subtree message, ERROR = {ERROR}",
+            "ERROR", e);
     }
 }
 
@@ -238,7 +239,7 @@
         if (reply.is_method_error())
         {
             // do not stop, continue with next elog
-            log<level::ERR>("Error in getting associations");
+            lg2::error("Error in getting associations");
             continue;
         }
 
@@ -249,9 +250,9 @@
         }
         catch (const sdbusplus::exception::exception& e)
         {
-            log<level::ERR>("Failed to get Associations or  parse existing "
-                            "callouts associations message",
-                            entry("ERROR=%s", e.what()));
+            lg2::error(
+                "Failed to parse existing callouts associations message, ERROR = {ERROR}",
+                "ERROR", e);
             continue;
         }
         auto& assocs = std::get<AssociationList>(assoc);
diff --git a/fault-monitor/operational-status-monitor.cpp b/fault-monitor/operational-status-monitor.cpp
index 66c6d3f..ffed55f 100644
--- a/fault-monitor/operational-status-monitor.cpp
+++ b/fault-monitor/operational-status-monitor.cpp
@@ -1,6 +1,7 @@
 #include "operational-status-monitor.hpp"
 
 #include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 namespace phosphor
 {
@@ -12,7 +13,6 @@
 {
 namespace monitor
 {
-using namespace phosphor::logging;
 
 void Monitor::matchHandler(sdbusplus::message::message& msg)
 {
@@ -32,8 +32,9 @@
         const bool* value = std::get_if<bool>(&it->second);
         if (!value)
         {
-            log<level::ERR>("Faild to get the Functional property",
-                            entry("INVENTORY_PATH=%s", invObjectPath.c_str()));
+            lg2::error(
+                "Faild to get the Functional property, INVENTORY_PATH = {PATH}",
+                "PATH", invObjectPath);
             return;
         }
 
@@ -42,9 +43,10 @@
         auto ledGroupPath = getLedGroupPaths(invObjectPath);
         if (ledGroupPath.empty())
         {
-            log<level::INFO>("The inventory D-Bus object is not associated "
-                             "with the LED group D-Bus object.",
-                             entry("INVENTORY_PATH=%s", invObjectPath.c_str()));
+            lg2::info(
+                "The inventory D-Bus object is not associated with the LED "
+                "group D-Bus object. INVENTORY_PATH = {PATH}",
+                "PATH", invObjectPath);
             return;
         }
 
@@ -72,9 +74,9 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed to get endpoints property",
-                        entry("ERROR=%s", e.what()),
-                        entry("PATH=%s", faultLedAssociation.c_str()));
+        lg2::error(
+            "Failed to get endpoints property, ERROR = {ERROR}, PATH = {PATH}",
+            "ERROR", e, "PATH", faultLedAssociation);
 
         return {};
     }
@@ -100,9 +102,9 @@
         }
         catch (const sdbusplus::exception::exception& e)
         {
-            log<level::ERR>("Failed to set Asserted property",
-                            entry("ERROR=%s", e.what()),
-                            entry("PATH=%s", path.c_str()));
+            lg2::error(
+                "Failed to set Asserted property, ERROR = {ERROR}, PATH = {PATH}",
+                "ERROR", e, "PATH", path);
         }
     }
 }
diff --git a/json-config.hpp b/json-config.hpp
index d625d49..9fb8ee1 100644
--- a/json-config.hpp
+++ b/json-config.hpp
@@ -1,6 +1,6 @@
 #include "utils.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/exception.hpp>
 #include <sdeventplus/event.hpp>
 
@@ -9,8 +9,6 @@
 
 namespace fs = std::filesystem;
 
-using namespace phosphor::logging;
-
 namespace phosphor
 {
 namespace led
@@ -186,11 +184,10 @@
                 catch (const sdbusplus::exception::exception& e)
                 {
                     // Property unavailable on object.
-                    log<level::ERR>(
-                        "Failed to get Names property",
-                        entry("ERROR=%s", e.what()),
-                        entry("INTERFACE=%s", confCompatibleInterface),
-                        entry("PATH=%s", path.c_str()));
+                    lg2::error(
+                        "Failed to get Names property, ERROR = {ERROR}, INTERFACES = {INTERFACES}, PATH = {PATH}",
+                        "ERROR", e, "INTERFACE", confCompatibleInterface,
+                        "PATH", path);
 
                     confFile.clear();
                 }
@@ -198,9 +195,9 @@
         }
         catch (const sdbusplus::exception::exception& e)
         {
-            log<level::ERR>("Failed to call the SubTreePaths method",
-                            entry("ERROR=%s", e.what()),
-                            entry("INTERFACE=%s", confCompatibleInterface));
+            lg2::error(
+                "Failed to call the SubTreePaths method, ERROR = {ERROR}, INTERFACE = {INTERFACE}",
+                "ERROR", e, "INTERFACE", confCompatibleInterface);
         }
         return;
     }
diff --git a/json-parser.hpp b/json-parser.hpp
index 245587d..a18db5d 100644
--- a/json-parser.hpp
+++ b/json-parser.hpp
@@ -4,7 +4,7 @@
 #include "ledlayout.hpp"
 
 #include <nlohmann/json.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdeventplus/event.hpp>
 
@@ -30,12 +30,11 @@
  */
 const Json readJson(const fs::path& path)
 {
-    using namespace phosphor::logging;
 
     if (!fs::exists(path) || fs::is_empty(path))
     {
-        log<level::ERR>("Incorrect File Path or empty file",
-                        entry("FILE_PATH=%s", path.c_str()));
+        lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}",
+                   "PATH", path);
         throw std::runtime_error("Incorrect File Path or empty file");
     }
 
@@ -46,9 +45,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed to parse config file",
-                        entry("ERROR=%s", e.what()),
-                        entry("FILE_PATH=%s", path.c_str()));
+        lg2::error(
+            "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}",
+            "ERROR", e, "PATH", path);
         throw std::runtime_error("Failed to parse config file");
     }
 }
@@ -79,7 +78,6 @@
                       const phosphor::led::Layout::Action& priority,
                       PriorityMap& priorityMap)
 {
-    using namespace phosphor::logging;
 
     auto iter = priorityMap.find(name);
     if (iter == priorityMap.end())
@@ -90,10 +88,10 @@
 
     if (iter->second != priority)
     {
-        log<level::ERR>("Priority of LED is not same across all",
-                        entry("Name=%s", name.c_str()),
-                        entry(" Old Priority=%d", iter->second),
-                        entry(" New priority=%d", priority));
+        lg2::error(
+            "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
+            "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO",
+            int(priority));
 
         throw std::runtime_error(
             "Priority of at least one LED is not same across groups");
diff --git a/lamptest.cpp b/lamptest.cpp
index 1182768..4c58e80 100644
--- a/lamptest.cpp
+++ b/lamptest.cpp
@@ -1,13 +1,12 @@
 #include "lamptest.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 namespace phosphor
 {
 namespace led
 {
 
-using namespace phosphor::logging;
 using Json = nlohmann::json;
 
 bool LampTest::processLEDUpdates(const Manager::group& ledsAssert,
@@ -124,9 +123,9 @@
         auto name = object_path.filename();
         if (name.empty())
         {
-            log<level::ERR>(
-                "Failed to get the name of member of physical LED path",
-                entry("PATH=%s", path.c_str()), entry("NAME=%s", name.c_str()));
+            lg2::error(
+                "Failed to get the name of member of physical LED path, PATH = {PATH}, NAME = {NAME}",
+                "PATH", path, "NAME", name);
             continue;
         }
 
@@ -143,9 +142,9 @@
         }
         catch (const sdbusplus::exception::exception& e)
         {
-            log<level::ERR>("Failed to get All properties",
-                            entry("ERROR=%s", e.what()),
-                            entry("PATH=%s", path.c_str()));
+            lg2::error(
+                "Failed to get All properties, ERROR = {ERROR}, PATH = {PATH}",
+                "ERROR", e, "PATH", path);
             continue;
         }
 
@@ -203,7 +202,7 @@
     // set the Asserted property of lamp test to false
     if (!groupObj)
     {
-        log<level::ERR>("the Group object is nullptr");
+        lg2::error("the Group object is nullptr");
         throw std::runtime_error("the Group object is nullptr");
     }
 
@@ -254,9 +253,9 @@
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        log<level::ERR>("Failed to set Asserted property",
-                        entry("ERROR=%s", e.what()),
-                        entry("PATH=%s", HOST_LAMP_TEST_OBJECT));
+        lg2::error(
+            "Failed to set Asserted property, ERROR = {ERROR}, PATH = {PATH}",
+            "ERROR", e, "PATH", std::string(HOST_LAMP_TEST_OBJECT));
     }
 }
 
@@ -264,8 +263,8 @@
 {
     if (!fs::exists(path) || fs::is_empty(path))
     {
-        log<level::INFO>("The file does not exist or is empty",
-                         entry("FILE_PATH=%s", path.c_str()));
+        lg2::info("The file does not exist or is empty, FILE_PATH = {PATH}",
+                  "PATH", path);
         return;
     }
 
@@ -290,9 +289,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed to parse config file",
-                        entry("ERROR=%s", e.what()),
-                        entry("FILE_PATH=%s", path.c_str()));
+        lg2::error(
+            "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}",
+            "ERROR", e, "PATH", path);
     }
     return;
 }
diff --git a/manager.cpp b/manager.cpp
index fe0f605..998f22d 100644
--- a/manager.cpp
+++ b/manager.cpp
@@ -2,7 +2,7 @@
 
 #include "manager.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/exception.hpp>
 #include <xyz/openbmc_project/Led/Physical/server.hpp>
 
@@ -14,8 +14,6 @@
 namespace led
 {
 
-using namespace phosphor::logging;
-
 // Assert -or- De-assert
 bool Manager::setGroupState(const std::string& path, bool assert,
                             group& ledsAssert, group& ledsDeAssert)
@@ -125,8 +123,7 @@
         for (const auto& it : ledsDeAssert)
         {
             std::string objPath = std::string(PHY_LED_PATH) + it.name;
-            log<level::DEBUG>("De-Asserting LED",
-                              entry("NAME=%s", it.name.c_str()));
+            lg2::debug("De-Asserting LED, NAME = {NAME}", "NAME", it.name);
             drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn,
                              it.period);
         }
@@ -137,8 +134,7 @@
         for (const auto& it : ledsAssert)
         {
             std::string objPath = std::string(PHY_LED_PATH) + it.name;
-            log<level::DEBUG>("Asserting LED",
-                              entry("NAME=%s", it.name.c_str()));
+            lg2::debug("Asserting LED, NAME = {NAME}", "NAME", it.name);
             drivePhysicalLED(objPath, it.action, it.dutyOn, it.period);
         }
     }
@@ -169,9 +165,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Error setting property for physical LED",
-                        entry("ERROR=%s", e.what()),
-                        entry("OBJECT_PATH=%s", objPath.c_str()));
+        lg2::error(
+            "Error setting property for physical LED, ERROR = {ERROR}, OBJECT_PATH = {PATH}",
+            "ERROR", e, "PATH", objPath);
     }
 
     return;
diff --git a/serialize.cpp b/serialize.cpp
index 897b336..7bde9b7 100644
--- a/serialize.cpp
+++ b/serialize.cpp
@@ -5,7 +5,7 @@
 #include <cereal/archives/json.hpp>
 #include <cereal/types/set.hpp>
 #include <cereal/types/string.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <filesystem>
 #include <fstream>
@@ -55,12 +55,10 @@
 
 void Serialize::restoreGroups()
 {
-    using namespace phosphor::logging;
 
     if (!fs::exists(path))
     {
-        log<level::INFO>("File does not exist",
-                         entry("FILE_PATH=%s", path.c_str()));
+        lg2::info("File does not exist, FILE_PATH = {PATH}", "PATH", path);
         return;
     }
 
@@ -72,7 +70,7 @@
     }
     catch (cereal::Exception& e)
     {
-        log<level::ERR>(e.what());
+        lg2::error("Failed to restore groups, ERROR = {ERROR}", "ERROR", e);
         fs::remove(path);
     }
 }
diff --git a/utils.cpp b/utils.cpp
index 7a565a4..7a60bbc 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -1,6 +1,6 @@
 #include "utils.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 namespace phosphor
 {
@@ -9,8 +9,6 @@
 namespace utils
 {
 
-using namespace phosphor::logging;
-
 // Get service name
 const std::string DBusHandler::getService(const std::string& path,
                                           const std::string& interface) const
@@ -29,9 +27,9 @@
     mapperResponseMsg.read(mapperResponse);
     if (mapperResponse.empty())
     {
-        log<level::ERR>("Failed to read getService mapper response",
-                        entry("OBJECT_PATH=%s", path.c_str()),
-                        entry("INTERFACE=%s", interface.c_str()));
+        lg2::error(
+            "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}",
+            "PATH", path, "INTERFACE", interface);
         return "";
     }