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/fw-update/device_updater.cpp b/fw-update/device_updater.cpp
index 8c468a1..6123365 100644
--- a/fw-update/device_updater.cpp
+++ b/fw-update/device_updater.cpp
@@ -5,8 +5,12 @@
 
 #include <libpldm/firmware_update.h>
 
+#include <phosphor-logging/lg2.hpp>
+
 #include <functional>
 
+PHOSPHOR_LOG2_USING;
+
 namespace pldm
 {
 
@@ -45,8 +49,8 @@
     if (rc)
     {
         updateManager->requester.markFree(eid, instanceId);
-        std::cerr << "encode_request_update_req failed, EID=" << unsigned(eid)
-                  << ", RC=" << rc << "\n";
+        error("encode_request_update_req failed, EID = {EID}, RC = {RC}", "EID",
+              unsigned(eid), "RC", rc);
         // Handle error scenario
     }
 
@@ -55,8 +59,8 @@
         std::move(std::bind_front(&DeviceUpdater::requestUpdate, this)));
     if (rc)
     {
-        std::cerr << "Failed to send RequestUpdate request, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n ";
+        error("Failed to send RequestUpdate request, EID = {EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         // Handle error scenario
     }
 }
@@ -67,8 +71,8 @@
     if (response == nullptr || !respMsgLen)
     {
         // Handle error scenario
-        std::cerr << "No response received for RequestUpdate, EID="
-                  << unsigned(eid) << "\n";
+        error("No response received for RequestUpdate, EID = {EID}", "EID",
+              unsigned(eid));
         return;
     }
 
@@ -80,16 +84,15 @@
                                          &fdMetaDataLen, &fdWillSendPkgData);
     if (rc)
     {
-        std::cerr << "Decoding RequestUpdate response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Decoding RequestUpdate response failed, EID = {EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         return;
     }
     if (completionCode)
     {
-        std::cerr << "RequestUpdate response failed with error "
-                     "completion code, EID="
-                  << unsigned(eid) << ", CC=" << unsigned(completionCode)
-                  << "\n";
+        error(
+            "RequestUpdate response failed with error completion code, EID = {EID}, CC = {CC}",
+            "EID", unsigned(eid), "CC", unsigned(completionCode));
         return;
     }
 
@@ -168,8 +171,8 @@
     if (rc)
     {
         updateManager->requester.markFree(eid, instanceId);
-        std::cerr << "encode_pass_component_table_req failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("encode_pass_component_table_req failed, EID = {EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         // Handle error scenario
     }
 
@@ -179,8 +182,9 @@
         std::move(std::bind_front(&DeviceUpdater::passCompTable, this)));
     if (rc)
     {
-        std::cerr << "Failed to send PassComponentTable request, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n ";
+        error(
+            "Failed to send PassComponentTable request, EID = {EID}, RC = {RC}",
+            "EID", unsigned(eid), "RC", rc);
         // Handle error scenario
     }
 }
@@ -191,8 +195,8 @@
     if (response == nullptr || !respMsgLen)
     {
         // Handle error scenario
-        std::cerr << "No response received for PassComponentTable, EID="
-                  << unsigned(eid) << "\n";
+        error("No response received for PassComponentTable, EID = {EID}", "EID",
+              unsigned(eid));
         return;
     }
 
@@ -206,17 +210,17 @@
     if (rc)
     {
         // Handle error scenario
-        std::cerr << "Decoding PassComponentTable response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error(
+            "Decoding PassComponentTable response failed, EID={EID}, RC = {RC}",
+            "EID", unsigned(eid), "RC", rc);
         return;
     }
     if (completionCode)
     {
         // Handle error scenario
-        std::cerr << "PassComponentTable response failed with error "
-                     "completion code, EID="
-                  << unsigned(eid) << ", CC=" << unsigned(completionCode)
-                  << "\n";
+        error(
+            "PassComponentTable response failed with error completion code, EID = {EID}, CC = {CC}",
+            "EID", unsigned(eid), "CC", unsigned(completionCode));
         return;
     }
     // Handle ComponentResponseCode
@@ -294,8 +298,8 @@
     if (rc)
     {
         updateManager->requester.markFree(eid, instanceId);
-        std::cerr << "encode_update_component_req failed, EID=" << unsigned(eid)
-                  << ", RC=" << rc << "\n";
+        error("encode_update_component_req failed, EID={EID}, RC = {RC}", "EID",
+              unsigned(eid), "RC", rc);
         // Handle error scenario
     }
 
@@ -304,8 +308,8 @@
         std::move(std::bind_front(&DeviceUpdater::updateComponent, this)));
     if (rc)
     {
-        std::cerr << "Failed to send UpdateComponent request, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n ";
+        error("Failed to send UpdateComponent request, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         // Handle error scenario
     }
 }
@@ -316,8 +320,8 @@
     if (response == nullptr || !respMsgLen)
     {
         // Handle error scenario
-        std::cerr << "No response received for updateComponent, EID="
-                  << unsigned(eid) << "\n";
+        error("No response received for updateComponent, EID={EID}", "EID",
+              unsigned(eid));
         return;
     }
 
@@ -333,16 +337,15 @@
         &timeBeforeReqFWData);
     if (rc)
     {
-        std::cerr << "Decoding UpdateComponent response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Decoding UpdateComponent response failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         return;
     }
     if (completionCode)
     {
-        std::cerr << "UpdateComponent response failed with error "
-                     "completion code, EID="
-                  << unsigned(eid) << ", CC=" << unsigned(completionCode)
-                  << "\n";
+        error(
+            "UpdateComponent response failed with error completion code, EID = {EID}, CC = {CC}",
+            "EID", unsigned(eid), "CC", unsigned(completionCode));
         return;
     }
 }
@@ -359,15 +362,17 @@
                                                &length);
     if (rc)
     {
-        std::cerr << "Decoding RequestFirmwareData request failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error(
+            "Decoding RequestFirmwareData request failed, EID={EID}, RC = {RC}",
+            "EID", unsigned(eid), "RC", rc);
         rc = encode_request_firmware_data_resp(
             request->hdr.instance_id, PLDM_ERROR_INVALID_DATA, responseMsg,
             sizeof(completionCode));
         if (rc)
         {
-            std::cerr << "Encoding RequestFirmwareData response failed, EID="
-                      << unsigned(eid) << ", RC=" << rc << "\n";
+            error(
+                "Encoding RequestFirmwareData response failed, EID = {EID}, RC = {RC}",
+                "EID", unsigned(eid), "RC", rc);
         }
         return response;
     }
@@ -377,9 +382,8 @@
     const auto& comp = compImageInfos[applicableComponents[componentIndex]];
     auto compOffset = std::get<5>(comp);
     auto compSize = std::get<6>(comp);
-    std::cerr << "offset = " << unsigned(offset)
-              << ", length = " << unsigned(length) << "\n";
-
+    error("offset = {OFFSET}, length = {LEN}", "OFFSET", unsigned(offset),
+          "LEN", unsigned(length));
     if (length < PLDM_FWUP_BASELINE_TRANSFER_SIZE || length > maxTransferSize)
     {
         rc = encode_request_firmware_data_resp(
@@ -387,8 +391,9 @@
             responseMsg, sizeof(completionCode));
         if (rc)
         {
-            std::cerr << "Encoding RequestFirmwareData response failed, EID="
-                      << unsigned(eid) << ", RC=" << rc << "\n";
+            error(
+                "Encoding RequestFirmwareData response failed, EID={EID}, RC = {RC}",
+                "EID", unsigned(eid), "RC", rc);
         }
         return response;
     }
@@ -400,8 +405,9 @@
             sizeof(completionCode));
         if (rc)
         {
-            std::cerr << "Encoding RequestFirmwareData response failed, EID="
-                      << unsigned(eid) << ", RC=" << rc << "\n";
+            error(
+                "Encoding RequestFirmwareData response failed, EID={EID}, RC = {RC}",
+                "EID", unsigned(eid), "RC", rc);
         }
         return response;
     }
@@ -424,8 +430,9 @@
                                            sizeof(completionCode));
     if (rc)
     {
-        std::cerr << "Encoding RequestFirmwareData response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error(
+            "Encoding RequestFirmwareData response failed, EID={EID}, RC = {RC}",
+            "EID", unsigned(eid), "RC", rc);
         return response;
     }
 
@@ -444,15 +451,16 @@
         decode_transfer_complete_req(request, payloadLength, &transferResult);
     if (rc)
     {
-        std::cerr << "Decoding TransferComplete request failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Decoding TransferComplete request failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         rc = encode_transfer_complete_resp(request->hdr.instance_id,
                                            PLDM_ERROR_INVALID_DATA, responseMsg,
                                            sizeof(completionCode));
         if (rc)
         {
-            std::cerr << "Encoding TransferComplete response failed, EID="
-                      << unsigned(eid) << ", RC=" << rc << "\n";
+            error(
+                "Encoding TransferComplete response failed, EID={EID}, RC = {RC}",
+                "EID", unsigned(eid), "RC", rc);
         }
         return response;
     }
@@ -464,22 +472,24 @@
 
     if (transferResult == PLDM_FWUP_TRANSFER_SUCCESS)
     {
-        std::cout << "Component Transfer complete, EID=" << unsigned(eid)
-                  << ", COMPONENT_VERSION=" << compVersion << "\n";
+        info(
+            "Component Transfer complete, EID = {EID}, COMPONENT_VERSION = {COMP_VERS}",
+            "EID", unsigned(eid), "COMP_VERS", compVersion);
     }
     else
     {
-        std::cerr << "Transfer of the component failed, EID=" << unsigned(eid)
-                  << ", COMPONENT_VERSION=" << compVersion
-                  << ", TRANSFER_RESULT=" << unsigned(transferResult) << "\n";
+        error(
+            "Transfer of the component failed, EID={EID}, COMPONENT_VERSION = {COMP_VERS}, TRANSFER_RESULT = {TRANS_RES}",
+            "EID", unsigned(eid), "COMP_VERS", compVersion, "TRANS_RES",
+            unsigned(transferResult));
     }
 
     rc = encode_transfer_complete_resp(request->hdr.instance_id, completionCode,
                                        responseMsg, sizeof(completionCode));
     if (rc)
     {
-        std::cerr << "Encoding TransferComplete response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Encoding TransferComplete response failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         return response;
     }
 
@@ -497,15 +507,16 @@
     auto rc = decode_verify_complete_req(request, payloadLength, &verifyResult);
     if (rc)
     {
-        std::cerr << "Decoding VerifyComplete request failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Decoding VerifyComplete request failed, EID = {EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         rc = encode_verify_complete_resp(request->hdr.instance_id,
                                          PLDM_ERROR_INVALID_DATA, responseMsg,
                                          sizeof(completionCode));
         if (rc)
         {
-            std::cerr << "Encoding VerifyComplete response failed, EID="
-                      << unsigned(eid) << ", RC=" << rc << "\n";
+            error(
+                "Encoding VerifyComplete response failed, EID={EID}, RC = {RC}",
+                "EID", unsigned(eid), "RC", rc);
         }
         return response;
     }
@@ -517,22 +528,24 @@
 
     if (verifyResult == PLDM_FWUP_VERIFY_SUCCESS)
     {
-        std::cout << "Component verification complete, EID=" << unsigned(eid)
-                  << ", COMPONENT_VERSION=" << compVersion << "\n";
+        info(
+            "Component verification complete, EID={EID}, COMPONENT_VERSION={COMP_VERS}",
+            "EID", unsigned(eid), "COMP_VERS", compVersion);
     }
     else
     {
-        std::cerr << "Component verification failed, EID=" << unsigned(eid)
-                  << ", COMPONENT_VERSION=" << compVersion
-                  << ", VERIFY_RESULT=" << unsigned(verifyResult) << "\n";
+        error(
+            "Component verification failed, EID={EID}, COMPONENT_VERSION={COMP_VERS}, VERIFY_RESULT={VERIFY_RES}",
+            "EID", unsigned(eid), "COMP_VERS", compVersion, "VERIFY_RES",
+            unsigned(verifyResult));
     }
 
     rc = encode_verify_complete_resp(request->hdr.instance_id, completionCode,
                                      responseMsg, sizeof(completionCode));
     if (rc)
     {
-        std::cerr << "Encoding VerifyComplete response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Encoding VerifyComplete response failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         return response;
     }
 
@@ -552,15 +565,16 @@
                                         &compActivationModification);
     if (rc)
     {
-        std::cerr << "Decoding ApplyComplete request failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Decoding ApplyComplete request failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         rc = encode_apply_complete_resp(request->hdr.instance_id,
                                         PLDM_ERROR_INVALID_DATA, responseMsg,
                                         sizeof(completionCode));
         if (rc)
         {
-            std::cerr << "Encoding ApplyComplete response failed, EID="
-                      << unsigned(eid) << ", RC=" << rc << "\n";
+            error(
+                "Encoding ApplyComplete response failed, EID={EID}, RC = {RC}",
+                "EID", unsigned(eid), "RC", rc);
         }
         return response;
     }
@@ -573,23 +587,25 @@
     if (applyResult == PLDM_FWUP_APPLY_SUCCESS ||
         applyResult == PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD)
     {
-        std::cout << "Component apply complete, EID=" << unsigned(eid)
-                  << ", COMPONENT_VERSION=" << compVersion << "\n";
+        info(
+            "Component apply complete, EID = {EID}, COMPONENT_VERSION = {COMP_VERS}",
+            "EID", unsigned(eid), "COMP_VERS", compVersion);
         updateManager->updateActivationProgress();
     }
     else
     {
-        std::cerr << "Component apply failed, EID=" << unsigned(eid)
-                  << ", COMPONENT_VERSION=" << compVersion
-                  << ", APPLY_RESULT=" << unsigned(applyResult) << "\n";
+        error(
+            "Component apply failed, EID = {EID}, COMPONENT_VERSION = {COMP_VERS}, APPLY_RESULT = {APPLY_RES}",
+            "EID", unsigned(eid), "COMP_VERS", compVersion, "APPLY_RES",
+            unsigned(applyResult));
     }
 
     rc = encode_apply_complete_resp(request->hdr.instance_id, completionCode,
                                     responseMsg, sizeof(completionCode));
     if (rc)
     {
-        std::cerr << "Encoding ApplyComplete response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Encoding ApplyComplete response failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         return response;
     }
 
@@ -626,8 +642,8 @@
     if (rc)
     {
         updateManager->requester.markFree(eid, instanceId);
-        std::cerr << "encode_activate_firmware_req failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("encode_activate_firmware_req failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
     }
 
     rc = updateManager->handler.registerRequest(
@@ -635,8 +651,8 @@
         std::move(std::bind_front(&DeviceUpdater::activateFirmware, this)));
     if (rc)
     {
-        std::cerr << "Failed to send ActivateFirmware request, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n ";
+        error("Failed to send ActivateFirmware request, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
     }
 }
 
@@ -646,8 +662,8 @@
     if (response == nullptr || !respMsgLen)
     {
         // Handle error scenario
-        std::cerr << "No response received for ActivateFirmware, EID="
-                  << unsigned(eid) << "\n";
+        error("No response received for ActivateFirmware, EID={EID}", "EID",
+              unsigned(eid));
         return;
     }
 
@@ -659,17 +675,16 @@
     if (rc)
     {
         // Handle error scenario
-        std::cerr << "Decoding ActivateFirmware response failed, EID="
-                  << unsigned(eid) << ", RC=" << rc << "\n";
+        error("Decoding ActivateFirmware response failed, EID={EID}, RC = {RC}",
+              "EID", unsigned(eid), "RC", rc);
         return;
     }
     if (completionCode)
     {
         // Handle error scenario
-        std::cerr << "ActivateFirmware response failed with error "
-                     "completion code, EID="
-                  << unsigned(eid) << ", CC=" << unsigned(completionCode)
-                  << "\n";
+        error(
+            "ActivateFirmware response failed with error completion code, EID = {EID}, CC = {CC}",
+            "EID", unsigned(eid), "CC", unsigned(completionCode));
         return;
     }