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: I151fc489a93ec7f513411aea2f0b61829bc808f6
diff --git a/snmp_conf_manager.cpp b/snmp_conf_manager.cpp
index d77f8b7..4ddace1 100644
--- a/snmp_conf_manager.cpp
+++ b/snmp_conf_manager.cpp
@@ -9,7 +9,7 @@
 #include <arpa/inet.h>
 
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <filesystem>
 
@@ -44,8 +44,7 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Not a valid address"),
-            entry("ADDRESS=%s", address.c_str());
+        lg2::error("{ADDRESS} is not a valid address", "ADDRESS", address);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
                               Argument::ARGUMENT_VALUE(address.c_str()));
     }
@@ -70,7 +69,7 @@
 {
     if (address.empty())
     {
-        log<level::ERR>("Invalid address");
+        lg2::error("{ADDRESS} is not a valid address", "ADDRESS", address);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"),
                               Argument::ARGUMENT_VALUE(address.c_str()));
     }
@@ -80,7 +79,7 @@
         if (val.second.get()->address() == address &&
             val.second.get()->port() == port)
         {
-            log<level::ERR>("Client already exist");
+            lg2::error("Client already exist");
             // TODO Add the error(Object already exist) in the D-Bus interface
             // then make the change here,meanwhile send the Internal Failure.
             elog<InvalidArgument>(
@@ -95,8 +94,7 @@
     auto it = clients.find(id);
     if (it == clients.end())
     {
-        log<level::ERR>("Unable to delete the snmp client.",
-                        entry("ID=%d", id));
+        lg2::error("Unable to delete the snmp client: {ID}", "ID", id);
         return;
     }
 
@@ -109,15 +107,13 @@
     {
         if (!fs::remove(fileName, ec))
         {
-            log<level::ERR>("Unable to delete the file",
-                            entry("FILE=%s", fileName.c_str()),
-                            entry("ERROR=%d", ec.value()));
+            lg2::error("Unable to delete {FILE}: {EC}", "FILE", fileName, "EC",
+                       ec.value());
         }
     }
     else
     {
-        log<level::ERR>("File doesn't exist",
-                        entry("FILE=%s", fileName.c_str()));
+        lg2::error("{FILE} doesn't exist", "FILE", fileName);
     }
     // remove the D-Bus Object.
     this->clients.erase(it);
diff --git a/snmp_main.cpp b/snmp_main.cpp
index 0ae1eef..be4bea1 100644
--- a/snmp_main.cpp
+++ b/snmp_main.cpp
@@ -2,7 +2,7 @@
 
 #include "snmp_conf_manager.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/manager.hpp>
 
@@ -21,15 +21,13 @@
 
 int main(int /*argc*/, char** /*argv[]*/)
 {
-    using namespace phosphor::logging;
-
     auto bus = sdbusplus::bus::new_default();
 
     sd_event* event = nullptr;
     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: {RC}", "RC", r);
         return r;
     }
 
diff --git a/snmp_notification.cpp b/snmp_notification.cpp
index 2037765..cf1aa69 100644
--- a/snmp_notification.cpp
+++ b/snmp_notification.cpp
@@ -4,7 +4,7 @@
 #include "xyz/openbmc_project/Common/error.hpp"
 
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 namespace phosphor
 {
@@ -84,8 +84,8 @@
             nullptr, nullptr);
         if (!ss)
         {
-            log<level::ERR>("Unable to get the snmp session.",
-                            entry("SNMPMANAGER=%s", mgr.c_str()));
+            lg2::error("Unable to get the snmp session: {SNMPMANAGER}",
+                       "SNMPMANAGER", mgr);
             elog<InternalFailure>();
         }
 
@@ -97,7 +97,7 @@
         auto pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
         if (!pdu)
         {
-            log<level::ERR>("Failed to create notification PDU");
+            lg2::error("Failed to create notification PDU");
             elog<InternalFailure>();
         }
 
@@ -110,7 +110,7 @@
                          't', sysuptimeStr.c_str()))
 
         {
-            log<level::ERR>("Failed to add the SNMP var(systime)");
+            lg2::error("Failed to add the SNMP var(systime)");
             snmp_free_pdu(pdu);
             elog<InternalFailure>();
         }
@@ -125,7 +125,7 @@
                                    ASN_OBJECT_ID, trapInfo.first.data(),
                                    trapInfo.second * sizeof(oid)))
         {
-            log<level::ERR>("Failed to add the SNMP var(trapID)");
+            lg2::error("Failed to add the SNMP var(trapID)");
             snmp_free_pdu(pdu);
             elog<InternalFailure>();
         }
@@ -137,7 +137,7 @@
             if (!addPDUVar(*pdu, std::get<0>(object), std::get<1>(object),
                            std::get<2>(object), std::get<3>(object)))
             {
-                log<level::ERR>("Failed to add the SNMP var");
+                lg2::error("Failed to add the SNMP var");
                 snmp_free_pdu(pdu);
                 elog<InternalFailure>();
             }
@@ -145,11 +145,11 @@
         // pdu is freed by snmp_send
         if (!snmp_send(sessionPtr.get(), pdu))
         {
-            log<level::ERR>("Failed to send the snmp trap.");
+            lg2::error("Failed to send the snmp trap.");
             elog<InternalFailure>();
         }
 
-        log<level::DEBUG>("Sent SNMP Trap", entry("MGR=%s", mgr.c_str()));
+        lg2::debug("Sent SNMP Trap: {MGR}", "MGR", mgr);
     }
 }
 
diff --git a/snmp_serialize.cpp b/snmp_serialize.cpp
index 6f4e4bf..684c55f 100644
--- a/snmp_serialize.cpp
+++ b/snmp_serialize.cpp
@@ -7,7 +7,7 @@
 #include <cereal/archives/binary.hpp>
 #include <cereal/types/string.hpp>
 #include <cereal/types/vector.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <fstream>
 
@@ -23,8 +23,6 @@
 namespace snmp
 {
 
-using namespace phosphor::logging;
-
 /** @brief Function required by Cereal to perform serialization.
  *  @tparam Archive - Cereal archive type (binary in our case).
  *  @param[in] archive - reference to Cereal archive.
@@ -85,7 +83,7 @@
     }
     catch (const cereal::Exception& e)
     {
-        log<level::ERR>(e.what());
+        lg2::error("Deserialization failed: {ERROR}", "ERROR", e);
         std::error_code ec;
         fs::remove(path, ec);
         return false;
diff --git a/snmp_util.cpp b/snmp_util.cpp
index d0c8133..9f235d8 100644
--- a/snmp_util.cpp
+++ b/snmp_util.cpp
@@ -6,7 +6,7 @@
 #include <netdb.h>
 
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <string>
 
@@ -30,8 +30,7 @@
 
     if (reply.is_method_error())
     {
-        log<level::ERR>("Failed to get managed objects",
-                        entry("PATH=%s", objPath.c_str()));
+        lg2::error("Failed to get managed objects: {PATH}", "PATH", objPath);
         elog<InternalFailure>();
     }
 
@@ -54,8 +53,8 @@
     auto result = getaddrinfo(address.c_str(), NULL, &hints, &addr);
     if (result)
     {
-        log<level::ERR>("getaddrinfo failed",
-                        entry("ADDRESS=%s", address.c_str()));
+        lg2::error("getaddrinfo failed {ADDRESS}: {RC}", "ADDRESS", address,
+                   "RC", result);
         elog<InternalFailure>();
     }
 
@@ -67,8 +66,8 @@
                          sizeof(ipaddress), NULL, 0, NI_NUMERICHOST);
     if (result)
     {
-        log<level::ERR>("getnameinfo failed",
-                        entry("ADDRESS=%s", address.c_str()));
+        lg2::error("getnameinfo failed {ADDRESS}: {RC}", "ADDRESS", address,
+                   "RC", result);
         elog<InternalFailure>();
     }
 
@@ -76,8 +75,8 @@
     int isValid = inet_pton(AF_INET, ipaddress, buf);
     if (isValid < 0)
     {
-        log<level::ERR>("Invalid address",
-                        entry("ADDRESS=%s", address.c_str()));
+        lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address, "RC",
+                   isValid);
         elog<InternalFailure>();
     }
     if (isValid == 0)
@@ -85,8 +84,8 @@
         int isValid6 = inet_pton(AF_INET6, ipaddress, buf);
         if (isValid6 < 1)
         {
-            log<level::ERR>("Invalid address",
-                            entry("ADDRESS=%s", address.c_str()));
+            lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address,
+                       "RC", isValid);
             elog<InternalFailure>();
         }
     }
@@ -135,7 +134,7 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>(e.what());
+            lg2::error("Invalid address: {ERROR}", "ERROR", e);
         }
     }
     return managers;