Enable verbose flag '-v' in pldmtool

Resolves ibm-openbmc/dev/issues/#1753

With verbose support as shown below

pldmtool base GetPLDMTypes
Supported types: 0(base) 2(platform) 3(bios) 4(fru)

pldmtool base GetPLDMTypes -v
Request Message:
08 01 80 00 04
Success in creating the socket : RC = 3
Success in connecting to socket : RC = 0
Success in sending message type as pldm to mctp : RC = 0
Write to socket successful : RC = 5
Total length:5
Loopback response message:
08 01 80 00 04
On first recv(),response == request : RC = 0
Total length:14
Shutdown Socket successful : RC = 0
Response Message:
08 01 00 00 04 00 1d 00 00 00 00 00 00 00
Supported types: 0(base) 2(platform) 3(bios) 4(fru)

./pldmtool bios GetBIOSTable -t 0
PLDM StringTable:
BIOSStringHandle : BIOSString
0 : Allowed
1 : Disabled
2 : Enabled
3 : Not Allowed
4 : Perm
5 : Temp
6 : pvm-fw-boot-side
7 : pvm-inband-code-update
8 : pvm-os-boot-side
9 : pvm-pcie-error-inject
10 : pvm-surveillance
11 : pvm-system-name
12 : vmi-if-count

./pldmtool fru GetFruRecordTableMetadata
FRUDATAMajorVersion : 1
FRUDATAMinorVersion : 0
FRUTableMaximumSize : 4294967295
FRUTableLength : 60
Total number of Record Set Identifiers in table : 1
Total number of records in table :  1
FRU DATAStructureTableIntegrityChecksum :  2516700077

./pldmtool raw -d 0x80 0x00 0x04 0x00 0x00
Request Message:
08 01 80 00 04 00 00
Response Message:
08 01 00 00 04 00 1d 00 00 00 00 00 00 00

./pldmtool raw -d 0x80 0x00 0x04 0x00 0x00 -v
Request Message:
08 01 80 00 04 00 00
Success in creating the socket : RC = 4
Success in connecting to socket : RC = 0
Success in sending message type as pldm to mctp : RC = 0
Write to socket successful : RC = 7
Total length:14
Shutdown Socket successful :  RC = 0
Response Message:
08 01 00 00 04 00 0d 00 00 00 00 00 00 00

Change-Id: I476c4492d2ab7e35565e028e201739fd8dafbc82
Signed-off-by: Sridevi Ramesh <sridevra@in.ibm.com>
diff --git a/tool/pldm_base_cmd.cpp b/tool/pldm_base_cmd.cpp
index b1028d0..7edc33c 100644
--- a/tool/pldm_base_cmd.cpp
+++ b/tool/pldm_base_cmd.cpp
@@ -199,7 +199,6 @@
                       << "rc=" << rc << ",cc=" << (int)cc << "\n";
             return;
         }
-        std::cout << "Parsed Response Msg: " << std::endl;
         std::cout << "TID : " << static_cast<uint32_t>(tid) << std::endl;
     }
 };
@@ -267,7 +266,6 @@
     void printPldmCommands(std::vector<bitfield8_t>& cmdTypes,
                            pldm_supported_types pldmType)
     {
-        std::cout << "Parsed Response Msg:" << std::endl;
         std::cout << "Supported Commands :";
         for (int i = 0; i < PLDM_MAX_CMDS_PER_TYPE; i++)
         {
diff --git a/tool/pldm_bios_cmd.cpp b/tool/pldm_bios_cmd.cpp
index f2830a2..60a7239 100644
--- a/tool/pldm_bios_cmd.cpp
+++ b/tool/pldm_bios_cmd.cpp
@@ -338,7 +338,6 @@
 
     void decodeStringTable(const std::optional<Table>& stringTable)
     {
-        std::cout << "Parsed Response Msg: " << std::endl;
         if (!stringTable)
         {
             std::cerr << "GetBIOSStringTable Error" << std::endl;
@@ -359,7 +358,6 @@
     void decodeAttributeTable(const std::optional<Table>& attrTable,
                               const std::optional<Table>& stringTable)
     {
-        std::cout << "Parsed Response Msg: " << std::endl;
         if (!stringTable)
         {
             std::cerr << "GetBIOSAttributeTable Error" << std::endl;
@@ -472,7 +470,6 @@
                                    const std::optional<Table>& attrTable,
                                    const std::optional<Table>& stringTable)
     {
-        std::cout << "Parsed Response Msg: " << std::endl;
         if (!attrValTable)
         {
             std::cerr << "GetBIOSAttributeValueTable Error" << std::endl;
diff --git a/tool/pldm_cmd_helper.cpp b/tool/pldm_cmd_helper.cpp
index 17485cd..4da1d37 100644
--- a/tool/pldm_cmd_helper.cpp
+++ b/tool/pldm_cmd_helper.cpp
@@ -16,29 +16,30 @@
 namespace helper
 {
 /*
- * print the input buffer
+ * print the input buffer if pldm verbosity is enabled.
  *
  */
-void printBuffer(const std::vector<uint8_t>& buffer)
+void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose)
 {
-    std::ostringstream tempStream;
-    if (!buffer.empty())
+    if (pldmVerbose && !buffer.empty())
     {
+        std::ostringstream tempStream;
         for (int byte : buffer)
         {
             tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
                        << " ";
         }
+        std::cout << tempStream.str() << std::endl;
     }
-    std::cout << tempStream.str() << std::endl;
 }
 /*
  * Initialize the socket, send pldm command & recieve response from socket
  *
  */
 int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
-                     std::vector<uint8_t>& responseMsg)
+                     std::vector<uint8_t>& responseMsg, bool pldmVerbose)
 {
+
     const char devPath[] = "\0mctp-mux";
     int returnCode = 0;
 
@@ -49,8 +50,7 @@
         std::cerr << "Failed to create the socket : RC = " << sockFd << "\n";
         return returnCode;
     }
-    std::cout << "Success in creating the socket : RC = " << sockFd
-              << std::endl;
+    Logger(pldmVerbose, "Success in creating the socket : RC = ", sockFd);
 
     struct sockaddr_un addr
     {
@@ -69,8 +69,7 @@
                   << "\n";
         return returnCode;
     }
-    std::cout << "Success in connecting to socket : RC = " << returnCode
-              << std::endl;
+    Logger(pldmVerbose, "Success in connecting to socket : RC = ", returnCode);
 
     auto pldmType = MCTP_MSG_TYPE_PLDM;
     result = write(socketFd(), &pldmType, sizeof(pldmType));
@@ -81,8 +80,9 @@
                   << returnCode << "\n";
         return returnCode;
     }
-    std::cout << "Success in sending message type as pldm to mctp : RC = "
-              << returnCode << std::endl;
+    Logger(
+        pldmVerbose,
+        "Success in sending message type as pldm to mctp : RC = ", returnCode);
 
     result = send(socketFd(), requestMsg.data(), requestMsg.size(), 0);
     if (-1 == result)
@@ -91,7 +91,7 @@
         std::cerr << "Write to socket failure : RC = " << returnCode << "\n";
         return returnCode;
     }
-    std::cout << "Write to socket successful : RC = " << result << std::endl;
+    Logger(pldmVerbose, "Write to socket successful : RC = ", result);
 
     // Read the response from socket
     ssize_t peekedLength = recv(socketFd(), nullptr, 0, MSG_TRUNC | MSG_PEEK);
@@ -124,7 +124,7 @@
                 resphdr->instance_id == reqhdr->instance_id &&
                 resphdr->request != PLDM_REQUEST)
             {
-                std::cout << "Total length: " << recvDataLength << std::endl;
+                Logger(pldmVerbose, "Total length:", recvDataLength);
                 break;
             }
             else if (recvDataLength != peekedLength)
@@ -145,8 +145,7 @@
         return returnCode;
     }
 
-    std::cout << "Shutdown Socket successful :  RC = " << returnCode
-              << std::endl;
+    Logger(pldmVerbose, "Shutdown Socket successful :  RC = ", returnCode);
     return PLDM_SUCCESS;
 }
 
@@ -179,8 +178,6 @@
         return;
     }
 
-    std::cout << "Encode request successfully" << std::endl;
-
     std::vector<uint8_t> responseMsg;
     rc = pldmSendRecv(requestMsg, responseMsg);
 
@@ -197,13 +194,22 @@
 int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg,
                                    std::vector<uint8_t>& responseMsg)
 {
+
     // Insert the PLDM message type and EID at the beginning of the
     // msg.
     requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM);
     requestMsg.insert(requestMsg.begin(), mctp_eid);
 
-    std::cout << "Request Message:" << std::endl;
-    printBuffer(requestMsg);
+    bool mctpVerbose = pldmVerbose;
+
+    // By default enable request/response msgs for pldmtool raw commands.
+    if (CommandInterface::pldmType == "raw")
+    {
+        pldmVerbose = true;
+    }
+
+    Logger(pldmVerbose, "Request Message:", "");
+    printBuffer(requestMsg, pldmVerbose);
 
     if (mctp_eid != PLDM_ENTITY_ID)
     {
@@ -219,19 +225,19 @@
         pldm_send_recv(mctp_eid, fd, requestMsg.data() + 2,
                        requestMsg.size() - 2, &responseMessage,
                        &responseMessageSize);
-        std::cout << "Response Message:" << std::endl;
 
+        Logger(pldmVerbose, "Response Message:", "");
         responseMsg.resize(responseMessageSize);
         memcpy(responseMsg.data(), responseMessage, responseMsg.size());
 
         free(responseMessage);
-        printBuffer(responseMsg);
+        printBuffer(responseMsg, pldmVerbose);
     }
     else
     {
-        mctpSockSendRecv(requestMsg, responseMsg);
-        std::cout << "Response Message:" << std::endl;
-        printBuffer(responseMsg);
+        mctpSockSendRecv(requestMsg, responseMsg, mctpVerbose);
+        Logger(pldmVerbose, "Response Message:", "");
+        printBuffer(responseMsg, pldmVerbose);
         responseMsg.erase(responseMsg.begin(),
                           responseMsg.begin() + 2 /* skip the mctp header */);
     }
diff --git a/tool/pldm_cmd_helper.hpp b/tool/pldm_cmd_helper.hpp
index bea01bd..978da83 100644
--- a/tool/pldm_cmd_helper.hpp
+++ b/tool/pldm_cmd_helper.hpp
@@ -31,22 +31,43 @@
 /** @brief Print the buffer
  *
  *  @param[in]  buffer  - Buffer to print
+ *  @param[in]  pldmVerbose -verbosity flag - true/false
  *
  *  @return - None
  */
-void printBuffer(const std::vector<uint8_t>& buffer);
+void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose);
 
+/** @brief print the input message if pldmverbose is enabled
+ *
+ *  @param[in]  pldmVerbose - verbosity flag - true/false
+ *  @param[in]  msg         - message to print
+ *  @param[in]  data        - data to print
+ *
+ *  @return - None
+ */
+
+template <class T>
+void Logger(bool pldmverbose, const char* msg, const T& data)
+{
+    if (pldmverbose)
+    {
+        std::stringstream s;
+        s << data;
+        std::cout << msg << s.str() << std::endl;
+    }
+}
 /** @brief MCTP socket read/recieve
  *
  *  @param[in]  requestMsg - Request message to compare against loopback
  *              message recieved from mctp socket
  *  @param[out] responseMsg - Response buffer recieved from mctp socket
+ *  @param[in]  pldmVerbose - verbosity flag - true/false
  *
  *  @return -   0 on success.
  *             -1 or -errno on failure.
  */
 int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
-                     std::vector<uint8_t>& responseMsg);
+                     std::vector<uint8_t>& responseMsg, bool pldmVerbose);
 
 class CommandInterface
 {
@@ -58,8 +79,10 @@
         commandName(name), mctp_eid(PLDM_ENTITY_ID), instanceId(0)
     {
         app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
+        app->add_flag("-v, --verbose", pldmVerbose);
         app->callback([&]() { exec(); });
     }
+
     virtual ~CommandInterface() = default;
 
     virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
@@ -76,6 +99,7 @@
     const std::string pldmType;
     const std::string commandName;
     uint8_t mctp_eid;
+    bool pldmVerbose;
 
   protected:
     uint8_t instanceId;
diff --git a/tool/pldm_fru_cmd.cpp b/tool/pldm_fru_cmd.cpp
index 2638d60..a5fe5a3 100644
--- a/tool/pldm_fru_cmd.cpp
+++ b/tool/pldm_fru_cmd.cpp
@@ -57,7 +57,6 @@
                       << "rc=" << rc << ",cc=" << (int)cc << std::endl;
             return;
         }
-        std::cout << "Parsed Response Msg : " << std::endl;
         std::cout << "FRUDATAMajorVersion : "
                   << static_cast<uint32_t>(fru_data_major_version) << std::endl;
         std::cout << "FRUDATAMinorVersion : "
diff --git a/tool/pldm_platform_cmd.cpp b/tool/pldm_platform_cmd.cpp
index d2754ae..d3ca8f7 100644
--- a/tool/pldm_platform_cmd.cpp
+++ b/tool/pldm_platform_cmd.cpp
@@ -216,7 +216,6 @@
             return;
         }
 
-        std::cout << "Parsed Response Msg: " << std::endl;
         std::cout << "nextRecordHandle: " << nextRecordHndl << std::endl;
         std::cout << "responseCount: " << respCnt << std::endl;