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/package_parser.cpp b/fw-update/package_parser.cpp
index 3d5d066..7a387ad 100644
--- a/fw-update/package_parser.cpp
+++ b/fw-update/package_parser.cpp
@@ -5,11 +5,14 @@
 #include <libpldm/firmware_update.h>
 #include <libpldm/utils.h>
 
+#include <phosphor-logging/lg2.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 #include <iostream>
 #include <memory>
 
+PHOSPHOR_LOG2_USING;
+
 namespace pldm
 {
 
@@ -39,8 +42,8 @@
             &compImageSetVersionStr, &recordDescriptors, &fwDevicePkgData);
         if (rc)
         {
-            std::cerr << "Decoding firmware device ID record failed, RC=" << rc
-                      << "\n";
+            error("Decoding firmware device ID record failed, RC={RC}", "RC",
+                  rc);
             throw InternalFailure();
         }
 
@@ -56,9 +59,9 @@
                 &descriptorType, &descriptorData);
             if (rc)
             {
-                std::cerr
-                    << "Decoding descriptor type, length and value failed, RC="
-                    << rc << "\n";
+                error(
+                    "Decoding descriptor type, length and value failed, RC={RC}",
+                    "RC", rc);
                 throw InternalFailure();
             }
 
@@ -80,9 +83,9 @@
                     &descTitleStrType, &descTitleStr, &vendorDefinedDescData);
                 if (rc)
                 {
-                    std::cerr << "Decoding Vendor-defined descriptor value "
-                                 "failed, RC="
-                              << rc << "\n";
+                    error(
+                        "Decoding Vendor-defined descriptor value failed, RC={RC}",
+                        "RC", rc);
                     throw InternalFailure();
                 }
 
@@ -151,8 +154,8 @@
                                               &compImageInfo, &compVersion);
         if (rc)
         {
-            std::cerr << "Decoding component image information failed, RC="
-                      << rc << "\n";
+            error("Decoding component image information failed, RC={RC}", "RC",
+                  rc);
             throw InternalFailure();
         }
 
@@ -194,12 +197,11 @@
 
         if (compLocOffset != calcPkgSize)
         {
-            std::cerr << "Validating the component location offset failed, "
-                         "COMP_VERSION="
-                      << std::get<static_cast<size_t>(
-                             ComponentImageInfoPos::CompVersionPos)>(
-                             componentImageInfo)
-                      << "\n";
+            auto cmpVersion = std::get<static_cast<size_t>(
+                ComponentImageInfoPos::CompVersionPos)>(componentImageInfo);
+            error(
+                "Validating the component location offset failed, COMP_VERSION={COMP_VERS}",
+                "COMP_VERS", cmpVersion);
             throw InternalFailure();
         }
 
@@ -208,9 +210,9 @@
 
     if (calcPkgSize != pkgSize)
     {
-        std::cerr
-            << "Package size does not match calculated package size, PKG_SIZE="
-            << pkgSize << " ,CALC_PKG_SIZE=" << calcPkgSize << "\n";
+        error(
+            "Package size does not match calculated package size, PKG_SIZE={PKG_SIZE}, CALC_PKG_SIZE={CAL_PKG_SIZE}",
+            "PKG_SIZE", pkgSize, "CAL_PKG_SIZE", calcPkgSize);
         throw InternalFailure();
     }
 }
@@ -220,16 +222,16 @@
 {
     if (pkgHeaderSize != pkgHdr.size())
     {
-        std::cerr << "Package header size is invalid, PKG_HDR_SIZE="
-                  << pkgHeaderSize << "\n";
+        error("Package header size is invalid, PKG_HDR_SIZE={PKG_HDR_SIZE}",
+              "PKG_HDR_SIZE", pkgHeaderSize);
         throw InternalFailure();
     }
 
     size_t offset = sizeof(pldm_package_header_information) + pkgVersion.size();
     if (offset + sizeof(DeviceIDRecordCount) >= pkgHeaderSize)
     {
-        std::cerr << "Parsing package header failed, PKG_HDR_SIZE="
-                  << pkgHeaderSize << "\n";
+        error("Parsing package header failed, PKG_HDR_SIZE={PKG_HDR_SIZE}",
+              "PKG_HDR_SIZE", pkgHeaderSize);
         throw InternalFailure();
     }
 
@@ -239,15 +241,15 @@
     offset = parseFDIdentificationArea(deviceIdRecCount, pkgHdr, offset);
     if (deviceIdRecCount != fwDeviceIDRecords.size())
     {
-        std::cerr
-            << "DeviceIDRecordCount entries not found, DEVICE_ID_REC_COUNT="
-            << deviceIdRecCount << "\n";
+        error(
+            "DeviceIDRecordCount entries not found, DEVICE_ID_REC_COUNT={DREC_CNT}",
+            "DREC_CNT", deviceIdRecCount);
         throw InternalFailure();
     }
     if (offset + sizeof(ComponentImageCount) >= pkgHeaderSize)
     {
-        std::cerr << "Parsing package header failed, PKG_HDR_SIZE="
-                  << pkgHeaderSize << "\n";
+        error("Parsing package header failed, PKG_HDR_SIZE={PKG_HDR_SIZE}",
+              "PKG_HDR_SIZE", pkgHeaderSize);
         throw InternalFailure();
     }
 
@@ -258,15 +260,16 @@
     offset = parseCompImageInfoArea(compImageCount, pkgHdr, offset);
     if (compImageCount != componentImageInfos.size())
     {
-        std::cerr << "ComponentImageCount entries not found, COMP_IMAGE_COUNT="
-                  << compImageCount << "\n";
+        error(
+            "ComponentImageCount entries not found, COMP_IMAGE_COUNT={COMP_IMG_CNT}",
+            "COMP_IMG_CNT", compImageCount);
         throw InternalFailure();
     }
 
     if (offset + sizeof(PackageHeaderChecksum) != pkgHeaderSize)
     {
-        std::cerr << "Parsing package header failed, PKG_HDR_SIZE="
-                  << pkgHeaderSize << "\n";
+        error("Parsing package header failed, PKG_HDR_SIZE={PKG_HDR_SIZE}",
+              "PKG_HDR_SIZE", pkgHeaderSize);
         throw InternalFailure();
     }
 
@@ -276,8 +279,9 @@
                 (pkgHdr[offset + 2] << 16) | (pkgHdr[offset + 3] << 24)));
     if (calcChecksum != checksum)
     {
-        std::cerr << "Parsing package header failed, CALC_CHECKSUM="
-                  << calcChecksum << ", PKG_HDR_CHECKSUM=" << checksum << "\n";
+        error(
+            "Parsing package header failed, CALC_CHECKSUM={CHK_SUM}, PKG_HDR_CHECKSUM={PKG_HDR_CHK_SUM}",
+            "CHK_SUM", calcChecksum, "PKG_HDR_CHK_SUM", checksum);
         throw InternalFailure();
     }
 
@@ -297,8 +301,8 @@
                                               &pkgHeader, &pkgVersion);
     if (rc)
     {
-        std::cerr << "Decoding PLDM package header information failed, RC="
-                  << rc << "\n";
+        error("Decoding PLDM package header information failed, RC={RC}", "RC",
+              rc);
         return nullptr;
     }