PLDM: Implementing Phosphor-Logging/LG2 logging

This commit adds changes in PLDM for implementing
structured LG2 logging, thereby moving away from
std::cout/cerr practice of logging which are
output streams and not logging mechanism.

PLDM now can make use of lg2 features like accurate
CODE LINE Number and CODE_FUNCTION Name and better
detailing in json object values which can be used in
log tracking.

More detailed logging change:
https://gist.github.com/riyadixitagra/c251685c1ba84248181891f7bc282395

Tested:
Ran a power off, on, cycle, and reset-reload.

Change-Id: I0485035f15f278c3fd172f0581b053c1c37f3a5b
Signed-off-by: Riya Dixit <riyadixitagra@gmail.com>
diff --git a/requester/handler.hpp b/requester/handler.hpp
index 12c36fe..bdcd7ee 100644
--- a/requester/handler.hpp
+++ b/requester/handler.hpp
@@ -11,6 +11,7 @@
 #include <sys/socket.h>
 
 #include <function2/function2.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/timer.hpp>
 #include <sdeventplus/event.hpp>
 #include <sdeventplus/source/event.hpp>
@@ -21,12 +22,12 @@
 #include <tuple>
 #include <unordered_map>
 
+PHOSPHOR_LOG2_USING;
+
 namespace pldm
 {
-
 namespace requester
 {
-
 /** @struct RequestKey
  *
  *  RequestKey uniquely identifies the PLDM request message to match it with the
@@ -132,21 +133,20 @@
         auto instanceIdExpiryCallBack = [key, this](void) {
             if (this->handlers.contains(key))
             {
-                std::cerr << "Response not received for the request, instance "
-                             "ID expired."
-                          << " EID = " << (unsigned)key.eid
-                          << " INSTANCE_ID = " << (unsigned)key.instanceId
-                          << " TYPE = " << (unsigned)key.type
-                          << " COMMAND = " << (unsigned)key.command << "\n";
+                error(
+                    "Response not received for the request, instance ID expired. EID = {EID} INSTANCE_ID = {INST_ID} TYPE = {REQ_KEY_TYPE} COMMAND = {REQ_KEY_CMD}",
+                    "EID", (unsigned)key.eid, "INST_ID",
+                    (unsigned)key.instanceId, "REQ_KEY_TYPE",
+                    (unsigned)key.type, "REQ_KEY_CMD", (unsigned)key.command);
                 auto& [request, responseHandler, timerInstance] =
                     this->handlers[key];
                 request->stop();
                 auto rc = timerInstance->stop();
                 if (rc)
                 {
-                    std::cerr
-                        << "Failed to stop the instance ID expiry timer. RC = "
-                        << rc << "\n";
+                    error(
+                        "Failed to stop the instance ID expiry timer. RC = {RC}",
+                        "RC", static_cast<int>(rc));
                 }
                 // Call response handler with an empty response to indicate no
                 // response
@@ -175,8 +175,7 @@
         if (rc)
         {
             requester.markFree(eid, instanceId);
-            std::cerr << "Failure to send the PLDM request message"
-                      << "\n";
+            error("Failure to send the PLDM request message");
             return rc;
         }
 
@@ -188,8 +187,9 @@
         catch (const std::runtime_error& e)
         {
             requester.markFree(eid, instanceId);
-            std::cerr << "Failed to start the instance ID expiry timer. RC = "
-                      << e.what() << "\n";
+            error(
+                "Failed to start the instance ID expiry timer. RC = {ERR_EXCEP}",
+                "ERR_EXCEP", e.what());
             return PLDM_ERROR;
         }
 
@@ -220,9 +220,8 @@
             auto rc = timerInstance->stop();
             if (rc)
             {
-                std::cerr
-                    << "Failed to stop the instance ID expiry timer. RC = "
-                    << rc << "\n";
+                error("Failed to stop the instance ID expiry timer. RC = {RC}",
+                      "RC", static_cast<int>(rc));
             }
             responseHandler(eid, response, respMsgLen);
             requester.markFree(key.eid, key.instanceId);
diff --git a/requester/request.hpp b/requester/request.hpp
index ffbb169..25e0030 100644
--- a/requester/request.hpp
+++ b/requester/request.hpp
@@ -8,6 +8,7 @@
 #include <libpldm/pldm.h>
 #include <sys/socket.h>
 
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/timer.hpp>
 #include <sdeventplus/event.hpp>
 
@@ -15,12 +16,12 @@
 #include <functional>
 #include <iostream>
 
+PHOSPHOR_LOG2_USING;
+
 namespace pldm
 {
-
 namespace requester
 {
-
 /** @class RequestRetryTimer
  *
  *  The abstract base class for implementing the PLDM request retry logic. This
@@ -74,8 +75,8 @@
         }
         catch (const std::runtime_error& e)
         {
-            std::cerr << "Failed to start the request timer. RC = " << e.what()
-                      << "\n";
+            error("Failed to start the request timer. RC = {ERR_EXCEP}",
+                  "ERR_EXCEP", e.what());
             return PLDM_ERROR;
         }
 
@@ -88,8 +89,8 @@
         auto rc = timer.stop();
         if (rc)
         {
-            std::cerr << "Failed to stop the request timer. RC = " << rc
-                      << "\n";
+            error("Failed to stop the request timer. RC = {RC}", "RC",
+                  static_cast<int>(rc));
         }
     }
 
@@ -186,11 +187,10 @@
                            sizeof(currentSendbuffSize));
             if (res == -1)
             {
-                std::cerr
-                    << "Requester : Failed to set the new send buffer size [bytes] : "
-                    << currentSendbuffSize
-                    << " from current size [bytes]: " << oldSendbuffSize
-                    << " , Error : " << strerror(errno) << std::endl;
+                error(
+                    "Requester : Failed to set the new send buffer size [bytes] : {CURR_SND_BUF_SIZE} from current size [bytes]: {OLD_BUF_SIZE} , Error : {ERR}",
+                    "CURR_SND_BUF_SIZE", currentSendbuffSize, "OLD_BUF_SIZE",
+                    oldSendbuffSize, "ERR", strerror(errno));
                 return PLDM_ERROR;
             }
         }
@@ -199,8 +199,8 @@
         auto rc = pldm_send(eid, fd, requestMsg.data(), requestMsg.size());
         if (rc < 0)
         {
-            std::cerr << "Failed to send PLDM message. RC = " << rc
-                      << ", errno = " << errno << "\n";
+            error("Failed to send PLDM message. RC = {RC}, errno = {ERR}", "RC",
+                  static_cast<int>(rc), "ERR", errno);
             return PLDM_ERROR;
         }
         return PLDM_SUCCESS;
diff --git a/requester/test/meson.build b/requester/test/meson.build
index c5c0168..1e63997 100644
--- a/requester/test/meson.build
+++ b/requester/test/meson.build
@@ -21,6 +21,7 @@
                          libpldm_dep,
                          nlohmann_json,
                          phosphor_dbus_interfaces,
+                         phosphor_logging_dep,
                          sdbusplus,
                          sdeventplus,
                          test_src]),