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: I4aabaafe997e13c10d655a83a9ef0071ad11126e
diff --git a/command/channel_auth.cpp b/command/channel_auth.cpp
index 8368412..44d5cad 100644
--- a/command/channel_auth.cpp
+++ b/command/channel_auth.cpp
@@ -1,12 +1,13 @@
 #include "channel_auth.hpp"
 
+#include <errno.h>
 #include <ipmid/api.h>
 
 #include <ipmid/types.hpp>
 #include <ipmid/utils.hpp>
 #include <nlohmann/json.hpp>
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <user_channel/channel_layer.hpp>
 #include <user_channel/user_layer.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -120,14 +121,16 @@
     std::ifstream jsonFile(configFile);
     if (!jsonFile.is_open())
     {
-        log<level::ERR>("Channel Cipher suites file not found");
+        lg2::error("Channel Cipher suites file not found: {ERROR}", "ERROR",
+                   strerror(errno));
         elog<InternalFailure>();
     }
 
     auto data = Json::parse(jsonFile, nullptr, false);
     if (data.is_discarded())
     {
-        log<level::ERR>("Parsing channel cipher suites JSON failed");
+        lg2::error("Parsing channel cipher suites JSON failed: {ERROR}",
+                   "ERROR", strerror(errno));
         elog<InternalFailure>();
     }
 
@@ -231,7 +234,8 @@
     }
     if (!ipmi::isValidPayloadType(static_cast<ipmi::PayloadType>(payloadType)))
     {
-        log<level::DEBUG>("Get channel cipher suites - Invalid payload type");
+        lg2::debug("Get channel cipher suites - Invalid payload type: {ERROR}",
+                   "ERROR", strerror(errno));
         constexpr uint8_t ccPayloadTypeNotSupported = 0x80;
         return errorResponse(ccPayloadTypeNotSupported);
     }
@@ -258,7 +262,8 @@
          ipmi::EChannelSessSupported::none) ||
         !(ipmi::doesDeviceExist(rspChannel)))
     {
-        log<level::DEBUG>("Get channel cipher suites - Device does not exist");
+        lg2::debug("Get channel cipher suites - Device does not exist:{ERROR}",
+                   "ERROR", strerror(errno));
         return errorResponse(IPMI_CC_INVALID_FIELD_REQUEST);
     }
 
diff --git a/command/guid.cpp b/command/guid.cpp
index 56b459f..253a2dc 100644
--- a/command/guid.cpp
+++ b/command/guid.cpp
@@ -3,7 +3,7 @@
 #include <ipmid/api.h>
 #include <mapper.h>
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <sstream>
 #include <string>
@@ -45,9 +45,8 @@
         int rc = mapper_get_service(bus, guidObjPath, &busname);
         if (rc < 0)
         {
-            log<level::ERR>("Failed to get bus name",
-                            entry("PATH=%s", guidObjPath),
-                            entry("ERROR=%s", strerror(-rc)));
+            lg2::error("Failed to get bus name, path: {PATH}, error: {ERROR}",
+                       "PATH", guidObjPath, "ERROR", strerror(-rc));
             break;
         }
 
@@ -55,16 +54,16 @@
                                 &error, &reply, "ss", chassisIntf, "uuid");
         if (rc < 0)
         {
-            log<level::ERR>("Failed to call Get Method",
-                            entry("ERROR=%s", strerror(-rc)));
+            lg2::error("Failed to call Get Method: {ERROR}", "ERROR",
+                       strerror(-rc));
             break;
         }
 
         rc = sd_bus_message_read(reply, "v", "s", &uuid);
         if (rc < 0 || uuid == NULL)
         {
-            log<level::ERR>("Failed to get a response",
-                            entry("ERROR=%s", strerror(-rc)));
+            lg2::error("Failed to get a response: {ERROR}", "ERROR",
+                       strerror(-rc));
             break;
         }
 
diff --git a/command/open_session.cpp b/command/open_session.cpp
index 0d82a06..21e0f3f 100644
--- a/command/open_session.cpp
+++ b/command/open_session.cpp
@@ -4,9 +4,7 @@
 #include "endian.hpp"
 #include "sessions_manager.hpp"
 
-#include <phosphor-logging/log.hpp>
-
-using namespace phosphor::logging;
+#include <phosphor-logging/lg2.hpp>
 
 namespace command
 {
@@ -88,8 +86,8 @@
     {
         response->status_code =
             static_cast<uint8_t>(RAKP_ReturnCode::INSUFFICIENT_RESOURCE);
-        log<level::ERR>("openSession : Problem opening a session",
-                        entry("EXCEPTION=%s", e.what()));
+        lg2::error("openSession : Problem opening a session: {ERROR}", "ERROR",
+                   e);
         return outPayload;
     }
 
diff --git a/command/payload_cmds.cpp b/command/payload_cmds.cpp
index 4083209..b3bcca2 100644
--- a/command/payload_cmds.cpp
+++ b/command/payload_cmds.cpp
@@ -7,7 +7,7 @@
 #include <ipmid/api.h>
 
 #include <ipmid/api-types.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 namespace sol
 {
@@ -15,8 +15,6 @@
 namespace command
 {
 
-using namespace phosphor::logging;
-
 std::vector<uint8_t> activatePayload(const std::vector<uint8_t>& inPayload,
                                      std::shared_ptr<message::Handler>& handler)
 {
@@ -94,7 +92,7 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(e.what());
+        lg2::error("Failed to start SOL payload: {ERROR}", "ERROR", e);
         response->completionCode = IPMI_CC_UNSPECIFIED_ERROR;
         return outPayload;
     }
@@ -161,7 +159,8 @@
         }
         catch (const std::exception& e)
         {
-            log<level::INFO>(e.what());
+            lg2::info("Failed to call the activating method: {ERROR}", "ERROR",
+                      e);
             /*
              * In case session has been closed (like in the case of inactivity
              * timeout), then activating function would throw an exception,
@@ -173,7 +172,7 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(e.what());
+        lg2::error("Failed to call the getContext method: {ERROR}", "ERROR", e);
         response->completionCode = IPMI_CC_UNSPECIFIED_ERROR;
         return outPayload;
     }
diff --git a/command/rakp12.cpp b/command/rakp12.cpp
index a68cdcb..c72611e 100644
--- a/command/rakp12.cpp
+++ b/command/rakp12.cpp
@@ -8,14 +8,12 @@
 #include <openssl/rand.h>
 
 #include <ipmid/types.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <algorithm>
 #include <cstring>
 #include <iomanip>
 
-using namespace phosphor::logging;
-
 namespace command
 {
 
@@ -32,11 +30,10 @@
     std::string messageID = "OpenBMC." +
                             std::string(openBMCMessageRegistryVersion) +
                             "InvalidLoginAttempted";
-    phosphor::logging::log<phosphor::logging::level::ERR>(
-        journalMsg.c_str(),
-        phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageID.c_str()),
-        phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s",
-                                 messageArgs.value().c_str()));
+    lg2::error(
+        "message: {MSG}, id: {REDFISH_MESSAGE_ID}, args: {REDFISH_MESSAGE_ARGS}",
+        "MSG", journalMsg, "REDFISH_MESSAGE_ID", messageID,
+        "REDFISH_MESSAGE_ARGS", messageArgs.value());
 }
 std::vector<uint8_t> RAKP12(const std::vector<uint8_t>& inPayload,
                             std::shared_ptr<message::Handler>& /* handler */)
@@ -56,7 +53,7 @@
     if (endian::from_ipmi(request->managedSystemSessionID) ==
         session::sessionZero)
     {
-        log<level::INFO>("RAKP12: BMC invalid Session ID");
+        lg2::info("RAKP12: BMC invalid Session ID");
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
         return outPayload;
@@ -70,8 +67,7 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("RAKP12 : session not found",
-                        entry("EXCEPTION=%s", e.what()));
+        lg2::error("RAKP12 : session not found: {ERROR}", "ERROR", e);
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
         return outPayload;
@@ -211,8 +207,9 @@
     // Check whether user is already locked for failed attempts
     if (!ipmi::ipmiUserPamAuthenticate(userName, passwd))
     {
-        log<level::ERR>("Authentication failed - user already locked out",
-                        entry("USER-ID=%d", static_cast<uint8_t>(userId)));
+        lg2::error(
+            "Authentication failed - user already locked out, user id: {ID}",
+            "ID", userId);
 
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
@@ -234,8 +231,7 @@
     }
     if (!isChannelAccessModeEnabled(session->sessionChannelAccess.accessMode))
     {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Channel access mode disabled.");
+        lg2::error("Channel access mode disabled.");
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE);
         logInvalidLoginRedfishEvent(message);
@@ -274,8 +270,7 @@
         ((request->req_max_privilege_level & session::reqMaxPrivMask) !=
          session->sessionUserPrivAccess.privilege))
     {
-        log<level::INFO>(
-            "Username/Privilege lookup failed for requested privilege");
+        lg2::info("Username/Privilege lookup failed for requested privilege");
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
 
diff --git a/command/rakp34.cpp b/command/rakp34.cpp
index 626bc15..f14430e 100644
--- a/command/rakp34.cpp
+++ b/command/rakp34.cpp
@@ -6,13 +6,11 @@
 #include "rmcp.hpp"
 #include "sessions_manager.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <algorithm>
 #include <cstring>
 
-using namespace phosphor::logging;
-
 namespace command
 {
 
@@ -75,7 +73,7 @@
     // Check if the RAKP3 Payload Length is as expected
     if (inPayload.size() < sizeof(RAKP3request))
     {
-        log<level::INFO>("RAKP34: Invalid RAKP3 request");
+        lg2::info("RAKP34: Invalid RAKP3 request");
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::INVALID_INTEGRITY_VALUE);
         return outPayload;
@@ -85,7 +83,7 @@
     if (endian::from_ipmi(request->managedSystemSessionID) ==
         session::sessionZero)
     {
-        log<level::INFO>("RAKP34: BMC invalid Session ID");
+        lg2::info("RAKP34: BMC invalid Session ID");
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
         return outPayload;
@@ -99,8 +97,7 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("RAKP12 : session not found",
-                        entry("EXCEPTION=%s", e.what()));
+        lg2::error("RAKP12 : session not found: {ERROR}", "ERROR", e);
         response->rmcpStatusCode =
             static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
         return outPayload;
@@ -162,7 +159,7 @@
     if (inPayload.size() != (sizeof(RAKP3request) + output.size()) ||
         std::memcmp(output.data(), request + 1, output.size()))
     {
-        log<level::INFO>("Mismatch in HMAC sent by remote console");
+        lg2::info("Mismatch in HMAC sent by remote console");
 
         response->messageTag = request->messageTag;
         response->rmcpStatusCode =
diff --git a/command/session_cmds.cpp b/command/session_cmds.cpp
index 8a4014a..1405b6b 100644
--- a/command/session_cmds.cpp
+++ b/command/session_cmds.cpp
@@ -7,7 +7,7 @@
 
 #include <ipmid/sessionhelper.hpp>
 #include <ipmid/utils.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <chrono>
 
@@ -15,7 +15,6 @@
 
 namespace command
 {
-using namespace phosphor::logging;
 
 std::vector<uint8_t>
     setSessionPrivilegeLevel(const std::vector<uint8_t>& inPayload,
@@ -119,10 +118,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed in getting session state property",
-                        entry("service=%s", service.c_str()),
-                        entry("object path=%s", obj.c_str()),
-                        entry("interface=%s", session::sessionIntf));
+        lg2::error(
+            "Failed in getting session state property: {SERVICE}, {PATH}, {INTERFACE}",
+            "SERVICE", service, "PATH", obj, "INTERFACE", session::sessionIntf);
         return ipmi::ccUnspecifiedError;
     }
 
@@ -170,9 +168,9 @@
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>("Failed to fetch object from dbus",
-                        entry("INTERFACE=%s", session::sessionIntf),
-                        entry("ERRMSG=%s", e.what()));
+        lg2::error(
+            "Failed to fetch object from dbus, interface: {INTERFACE}, error: {ERROR}",
+            "INTERFACE", session::sessionIntf, "ERROR", e);
         return ipmi::ccUnspecifiedError;
     }
 
@@ -199,9 +197,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed to get session manager instance or sessionID "
-                        "by sessionHandle",
-                        entry("ERRMSG=%s", e.what()));
+        lg2::error(
+            "Failed to get session manager instance or sessionID by sessionHandle: {ERROR}",
+            "ERROR", e);
         return session::ccInvalidSessionHandle;
     }
 
@@ -218,8 +216,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed to get session manager instance or sessionID",
-                        entry("ERRMSG=%s", e.what()));
+        lg2::error(
+            "Failed to get session manager instance or sessionID: {ERROR}",
+            "ERROR", e);
         return session::ccInvalidSessionId;
     }
 
@@ -234,9 +233,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(
-            "Failed to get session manager instance or stop session",
-            entry("ERRMSG=%s", e.what()));
+        lg2::error(
+            "Failed to get session manager instance or stop session: {ERROR}",
+            "ERROR", e);
         return ipmi::ccUnspecifiedError;
     }
 
@@ -300,9 +299,9 @@
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>("Failed to fetch object from dbus",
-                        entry("INTERFACE=%s", session::sessionIntf),
-                        entry("ERRMSG=%s", e.what()));
+        lg2::error(
+            "Failed to fetch object from dbus, interface: {INTERFACE}, error: {ERROR}",
+            "INTERFACE", session::sessionIntf, "ERROR", e);
         response->completionCode = ipmi::ccUnspecifiedError;
         return outPayload;
     }
diff --git a/command/sol_cmds.cpp b/command/sol_cmds.cpp
index 8203a6e..ceee3d8 100644
--- a/command/sol_cmds.cpp
+++ b/command/sol_cmds.cpp
@@ -4,7 +4,7 @@
 #include "sol/sol_context.hpp"
 #include "sol/sol_manager.hpp"
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 namespace sol
 {
@@ -43,7 +43,7 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(e.what());
+        lg2::error("Failed to call the getContext method: {ERROR}", "ERROR", e);
         return std::vector<uint8_t>();
     }