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/oem/ibm/libpldmresponder/file_io.cpp b/oem/ibm/libpldmresponder/file_io.cpp
index acf9d4a..96a1c71 100644
--- a/oem/ibm/libpldmresponder/file_io.cpp
+++ b/oem/ibm/libpldmresponder/file_io.cpp
@@ -14,11 +14,15 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <phosphor-logging/lg2.hpp>
+
 #include <cstring>
 #include <fstream>
 #include <iostream>
 #include <memory>
 
+PHOSPHOR_LOG2_USING;
+
 namespace pldm
 {
 using namespace pldm::responder::utils;
@@ -67,9 +71,9 @@
     if (dmaFd < 0)
     {
         rc = -errno;
-        std::cerr << "transferHostDataToSocket: Failed to open the XDMA device,"
-                     " RC="
-                  << rc << "\n";
+        error(
+            "transferHostDataToSocket: Failed to open the XDMA device, RC={RC}",
+            "RC", rc);
         return rc;
     }
 
@@ -81,9 +85,9 @@
     if (MAP_FAILED == vgaMem)
     {
         rc = -errno;
-        std::cerr << "transferHostDataToSocket: Failed to mmap the XDMA device,"
-                     " RC="
-                  << rc << "\n";
+        error(
+            "transferHostDataToSocket : Failed to mmap the XDMA device, RC={RC}",
+            "RC", rc);
         return rc;
     }
 
@@ -98,10 +102,9 @@
     if (rc < 0)
     {
         rc = -errno;
-        std::cerr << "transferHostDataToSocket: Failed to execute the DMA "
-                     "operation, RC="
-                  << rc << " ADDRESS=" << address << " LENGTH=" << length
-                  << "\n";
+        error(
+            "transferHostDataToSocket: Failed to execute the DMA operation, RC={RC} ADDRESS={ADDR} LENGTH={LEN}",
+            "RC", rc, "ADDR", address, "LEN", length);
         return rc;
     }
 
@@ -111,9 +114,9 @@
     {
         rc = -errno;
         close(fd);
-        std::cerr << "transferHostDataToSocket: Closing socket as "
-                     "writeToUnixSocket faile with RC="
-                  << rc << std::endl;
+        error(
+            "transferHostDataToSocket: Closing socket as writeToUnixSocket faile with RC={RC}",
+            "RC", rc);
         return rc;
     }
     return 0;
@@ -139,10 +142,8 @@
         }
         else
         {
-            std::cerr
-                << "transferDataHost: Received interrupt during DMA transfer."
-                   " Skipping Unmap."
-                << std::endl;
+            error(
+                "transferDataHost: Received interrupt during DMA transfer. Skipping Unmap.");
         }
     };
 
@@ -151,8 +152,8 @@
     if (dmaFd < 0)
     {
         rc = -errno;
-        std::cerr << "transferDataHost: Failed to open the XDMA device, RC="
-                  << rc << "\n";
+        error("transferDataHost : Failed to open the XDMA device, RC={RC}",
+              "RC", rc);
         return rc;
     }
 
@@ -164,8 +165,8 @@
     if (MAP_FAILED == vgaMem)
     {
         rc = -errno;
-        std::cerr << "transferDataHost: Failed to mmap the XDMA device, RC="
-                  << rc << "\n";
+        error("transferDataHost : Failed to mmap the XDMA device, RC={RC}",
+              "RC", rc);
         return rc;
     }
 
@@ -176,9 +177,9 @@
         rc = lseek(fd, offset, SEEK_SET);
         if (rc == -1)
         {
-            std::cerr << "transferDataHost upstream: lseek failed, ERROR="
-                      << errno << ", UPSTREAM=" << upstream
-                      << ", OFFSET=" << offset << "\n";
+            error(
+                "transferDataHost upstream : lseek failed, ERROR={ERR}, UPSTREAM={UPSTREAM}, OFFSET={OFFSET}",
+                "ERR", errno, "UPSTREAM", upstream, "OFFSET", offset);
             return rc;
         }
 
@@ -190,16 +191,17 @@
         rc = read(fd, buffer.data(), length);
         if (rc == -1)
         {
-            std::cerr << "transferDataHost upstream: file read failed, ERROR="
-                      << errno << ", UPSTREAM=" << upstream
-                      << ", LENGTH=" << length << ", OFFSET=" << offset << "\n";
+            error(
+                "transferDataHost upstream : file read failed, ERROR={ERR}, UPSTREAM={UPSTREAM}, LENGTH={LEN}, OFFSET={OFFSET}",
+                "ERR", errno, "UPSTREAM", upstream, "LEN", length, "OFFSET",
+                offset);
             return rc;
         }
         if (rc != static_cast<int>(length))
         {
-            std::cerr << "transferDataHost upstream: mismatch between number of"
-                      << "characters to read and the length read, LENGTH="
-                      << length << " COUNT=" << rc << "\n";
+            error(
+                "transferDataHost upstream : mismatch between number of characters to read and the length read, LENGTH={LEN} COUNT={RC}",
+                "LEN", length, "RC", rc);
             return -1;
         }
         memcpy(static_cast<char*>(vgaMemPtr.get()), buffer.data(),
@@ -215,10 +217,9 @@
     if (rc < 0)
     {
         rc = -errno;
-        std::cerr << "transferDataHost: Failed to execute the DMA operation,"
-                     " RC="
-                  << rc << " UPSTREAM=" << upstream << " ADDRESS=" << address
-                  << " LENGTH=" << length << "\n";
+        error(
+            "transferDataHost : Failed to execute the DMA operation, RC={RC} UPSTREAM={UPSTREAM} ADDRESS={ADDR} LENGTH={LEN}",
+            "RC", rc, "UPSTREAM", upstream, "ADDR", address, "LEN", length);
         return rc;
     }
 
@@ -227,18 +228,18 @@
         rc = lseek(fd, offset, SEEK_SET);
         if (rc == -1)
         {
-            std::cerr << "transferDataHost downstream: lseek failed, ERROR="
-                      << errno << ", UPSTREAM=" << upstream
-                      << ", OFFSET=" << offset << "\n";
+            error(
+                "transferDataHost downstream : lseek failed, ERROR={ERR}, UPSTREAM={UPSTREAM}, OFFSET={OFFSET}",
+                "ERR", errno, "UPSTREAM", upstream, "OFFSET", offset);
             return rc;
         }
         rc = write(fd, static_cast<const char*>(vgaMemPtr.get()), length);
         if (rc == -1)
         {
-            std::cerr << "transferDataHost downstream: file write failed,"
-                         " ERROR="
-                      << errno << ", UPSTREAM=" << upstream
-                      << ", LENGTH=" << length << ", OFFSET=" << offset << "\n";
+            error(
+                "transferDataHost downstream : file write failed, ERROR={ERR}, UPSTREAM={UPSTREAM}, LENGTH={LEN}, OFFSET={OFFSET}",
+                "ERR", errno, "UPSTREAM", upstream, "LEN", length, "OFFSET",
+                offset);
             return rc;
         }
     }
@@ -282,8 +283,9 @@
     }
     catch (const std::exception& e)
     {
-        std::cerr << "File handle does not exist in the file table, HANDLE="
-                  << fileHandle << "\n";
+        error(
+            "File handle does not exist in the file table, HANDLE={FILE_HANDLE}",
+            "FILE_HANDLE", fileHandle);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_READ_FILE_INTO_MEMORY,
                                    PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
@@ -292,7 +294,8 @@
 
     if (!fs::exists(value.fsPath))
     {
-        std::cerr << "File does not exist, HANDLE=" << fileHandle << "\n";
+        error("File does not exist, HANDLE={FILE_HANDLE}", "FILE_HANDLE",
+              fileHandle);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_READ_FILE_INTO_MEMORY,
                                    PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
@@ -302,8 +305,8 @@
     auto fileSize = fs::file_size(value.fsPath);
     if (offset >= fileSize)
     {
-        std::cerr << "Offset exceeds file size, OFFSET=" << offset
-                  << " FILE_SIZE=" << fileSize << "\n";
+        error("Offset exceeds file size, OFFSET={OFFSTE} FILE_SIZE={FILE_SIZE}",
+              "OFFSET", offset, "FILE_SIZE", fileSize);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_READ_FILE_INTO_MEMORY,
                                    PLDM_DATA_OUT_OF_RANGE, 0, responsePtr);
@@ -317,8 +320,8 @@
 
     if (length % dma::minSize)
     {
-        std::cerr << "Read length is not a multiple of DMA minSize, LENGTH="
-                  << length << "\n";
+        error("Read length is not a multiple of DMA minSize, LENGTH={LEN}",
+              "LEN", length);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_READ_FILE_INTO_MEMORY,
                                    PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
@@ -356,8 +359,8 @@
 
     if (length % dma::minSize)
     {
-        std::cerr << "Write length is not a multiple of DMA minSize, LENGTH="
-                  << length << "\n";
+        error("Write length is not a multiple of DMA minSize, LENGTH={LEN}",
+              "LEN", length);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_WRITE_FILE_FROM_MEMORY,
                                    PLDM_ERROR_INVALID_LENGTH, 0, responsePtr);
@@ -374,8 +377,9 @@
     }
     catch (const std::exception& e)
     {
-        std::cerr << "File handle does not exist in the file table, HANDLE="
-                  << fileHandle << "\n";
+        error(
+            "File handle does not exist in the file table, HANDLE={FILE_HANDLE}",
+            "FILE_HANDLE", fileHandle);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_WRITE_FILE_FROM_MEMORY,
                                    PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
@@ -384,7 +388,8 @@
 
     if (!fs::exists(value.fsPath))
     {
-        std::cerr << "File does not exist, HANDLE=" << fileHandle << "\n";
+        error("File does not exist, HANDLE={FILE_HANDLE}", "FILE_HANDLE",
+              fileHandle);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_WRITE_FILE_FROM_MEMORY,
                                    PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
@@ -394,8 +399,8 @@
     auto fileSize = fs::file_size(value.fsPath);
     if (offset >= fileSize)
     {
-        std::cerr << "Offset exceeds file size, OFFSET=" << offset
-                  << " FILE_SIZE=" << fileSize << "\n";
+        error("Offset exceeds file size, OFFSET={OFFSET} FILE_SIZE={FILE_SIZE}",
+              "OFFSET", offset, "FILE_SIZE", fileSize);
         encode_rw_file_memory_resp(request->hdr.instance_id,
                                    PLDM_WRITE_FILE_FROM_MEMORY,
                                    PLDM_DATA_OUT_OF_RANGE, 0, responsePtr);
@@ -499,8 +504,9 @@
     }
     catch (const std::exception& e)
     {
-        std::cerr << "File handle does not exist in the file table, HANDLE="
-                  << fileHandle << "\n";
+        error(
+            "File handle does not exist in the file table, HANDLE={FILE_HANDLE}",
+            "FILE_HANDLE", fileHandle);
         encode_read_file_resp(request->hdr.instance_id,
                               PLDM_INVALID_FILE_HANDLE, length, responsePtr);
         return response;
@@ -508,7 +514,8 @@
 
     if (!fs::exists(value.fsPath))
     {
-        std::cerr << "File does not exist, HANDLE=" << fileHandle << "\n";
+        error("File does not exist, HANDLE={FILE_HANDLE}", "FILE_HANDLE",
+              fileHandle);
         encode_read_file_resp(request->hdr.instance_id,
                               PLDM_INVALID_FILE_HANDLE, length, responsePtr);
         return response;
@@ -517,8 +524,8 @@
     auto fileSize = fs::file_size(value.fsPath);
     if (offset >= fileSize)
     {
-        std::cerr << "Offset exceeds file size, OFFSET=" << offset
-                  << " FILE_SIZE=" << fileSize << "\n";
+        error("Offset exceeds file size, OFFSET={OFFSET} FILE_SIZE={FILE_SIZE}",
+              "OFFSET", offset, "FILE_SIZE", fileSize);
         encode_read_file_resp(request->hdr.instance_id, PLDM_DATA_OUT_OF_RANGE,
                               length, responsePtr);
         return response;
@@ -580,8 +587,9 @@
     }
     catch (const std::exception& e)
     {
-        std::cerr << "File handle does not exist in the file table, HANDLE="
-                  << fileHandle << "\n";
+        error(
+            "File handle does not exist in the file table, HANDLE={FILE_HANDLE}",
+            "FILE_HANDLE", fileHandle);
         encode_write_file_resp(request->hdr.instance_id,
                                PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
         return response;
@@ -589,7 +597,8 @@
 
     if (!fs::exists(value.fsPath))
     {
-        std::cerr << "File does not exist, HANDLE=" << fileHandle << "\n";
+        error("File does not exist, HANDLE={FILE_HANDLE}", "FILE_HANDLE",
+              fileHandle);
         encode_write_file_resp(request->hdr.instance_id,
                                PLDM_INVALID_FILE_HANDLE, 0, responsePtr);
         return response;
@@ -598,8 +607,8 @@
     auto fileSize = fs::file_size(value.fsPath);
     if (offset >= fileSize)
     {
-        std::cerr << "Offset exceeds file size, OFFSET=" << offset
-                  << " FILE_SIZE=" << fileSize << "\n";
+        error("Offset exceeds file size, OFFSET={OFFSET} FILE_SIZE={FILE_SIZE}",
+              "OFFSET", offset, "FILE_SIZE", fileSize);
         encode_write_file_resp(request->hdr.instance_id, PLDM_DATA_OUT_OF_RANGE,
                                0, responsePtr);
         return response;
@@ -651,8 +660,8 @@
     }
     if (length % dma::minSize)
     {
-        std::cerr << "Length is not a multiple of DMA minSize, LENGTH="
-                  << length << "\n";
+        error("Length is not a multiple of DMA minSize, LENGTH={LEN}", "LEN",
+              length);
         encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd,
                                            PLDM_ERROR_INVALID_LENGTH, 0,
                                            responsePtr);
@@ -666,7 +675,7 @@
     }
     catch (const InternalFailure& e)
     {
-        std::cerr << "unknown file type, TYPE=" << fileType << "\n";
+        error("unknown file type, TYPE={FILE_TYPE}", "FILE_TYPE", fileType);
         encode_rw_file_by_type_memory_resp(request->hdr.instance_id, cmd,
                                            PLDM_INVALID_FILE_TYPE, 0,
                                            responsePtr);
@@ -731,7 +740,7 @@
     }
     catch (const InternalFailure& e)
     {
-        std::cerr << "unknown file type, TYPE=" << fileType << "\n";
+        error("unknown file type, TYPE={FILE_TYPE}", "FILE_TYPE", fileType);
         encode_rw_file_by_type_resp(request->hdr.instance_id,
                                     PLDM_WRITE_FILE_BY_TYPE,
                                     PLDM_INVALID_FILE_TYPE, 0, responsePtr);
@@ -780,7 +789,7 @@
     }
     catch (const InternalFailure& e)
     {
-        std::cerr << "unknown file type, TYPE=" << fileType << "\n";
+        error("unknown file type, TYPE={FILE_TYPE}", "FILE_TYPE", fileType);
         encode_rw_file_by_type_resp(request->hdr.instance_id,
                                     PLDM_READ_FILE_BY_TYPE,
                                     PLDM_INVALID_FILE_TYPE, 0, responsePtr);
@@ -900,7 +909,7 @@
     }
     catch (const InternalFailure& e)
     {
-        std::cerr << "unknown file type, TYPE=" << fileType << "\n";
+        error("unknown file type, TYPE={FILE_TYPE}", "FILE_TYPE", fileType);
         return CmdHandler::ccOnlyResponse(request, PLDM_INVALID_FILE_TYPE);
     }