pldmd: add runtime switch to toggle debug logs

Currently the pldm daemon has a way to enable verbose logging via a
configure flag, however, a runtime verbose command line argument will
prove more helpful since that way the daemon can quickly be restarted to
enable/disable verbose logging without having to rebuild it.

Usage:
pldmd -v/--verbose $VERBOSE, where $VERBOSE has value 0 being for no
verbosity and 1 being full verbosity.
No option specified will have $VERBOSE value defaulted to 0.

Tested:
Without verbosity option (verbosity disabled by default)
root@witherspoon:/tmp# ./pldmd &
[1] 2274

With verbosity disabled
root@witherspoon:/tmp# ./pldmd --verbose 0 &
[1] 2275

With verbosity enabled
root@witherspoon:/tmp# ./pldmd --verbose 1 &
[1] 2276

with verbosity value error
root@witherspoon:/tmp# ./pldmd --verbose 3 &
[1] 2277
root@witherspoon:/tmp# Usage: ./pldmd [options]
Options:
  --verbose=<0/1>  0 - Disable verbosity, 1 - Enable verbosity

Signed-off-by: Jinu Joy Thomas <jinu.joy.thomas@in.ibm.com>
Change-Id: I48b1a0e919bc87f732e3c87f40225e0a63b727aa
diff --git a/pldmd.cpp b/pldmd.cpp
index e02bb57..7072312 100644
--- a/pldmd.cpp
+++ b/pldmd.cpp
@@ -4,6 +4,7 @@
 #include "registration.hpp"
 
 #include <err.h>
+#include <getopt.h>
 #include <poll.h>
 #include <stdlib.h>
 #include <sys/socket.h>
@@ -14,9 +15,11 @@
 #include <cstdio>
 #include <cstring>
 #include <iomanip>
+#include <iostream>
 #include <iterator>
 #include <phosphor-logging/log.hpp>
 #include <sstream>
+#include <string>
 #include <vector>
 
 #include "libpldm/base.h"
@@ -89,9 +92,44 @@
     log<level::INFO>(tempStream.str().c_str());
 }
 
+void optionUsage(void)
+{
+    std::cerr << "Usage: pldmd [options]\n";
+    std::cerr << "Options:\n";
+    std::cerr
+        << "  --verbose=<0/1>  0 - Disable verbosity, 1 - Enable verbosity\n";
+    std::cerr << "Defaulted settings:  --verbose=0 \n";
+}
+
 int main(int argc, char** argv)
 {
 
+    bool verbose = false;
+    static struct option long_options[] = {
+        {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
+
+    auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
+    switch (argflag)
+    {
+        case 'v':
+            switch (std::stoi(optarg))
+            {
+                case 0:
+                    verbose = false;
+                    break;
+                case 1:
+                    verbose = true;
+                    break;
+                default:
+                    optionUsage();
+                    break;
+            }
+            break;
+        default:
+            optionUsage();
+            break;
+    }
+
     pldm::responder::base::registerHandlers();
     pldm::responder::bios::registerHandlers();
 
@@ -160,13 +198,14 @@
                 sockfd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
             if (recvDataLength == peekedLength)
             {
-#ifdef VERBOSE
-                log<level::INFO>("Received Msg ",
-                                 entry("LENGTH=%zu", recvDataLength),
-                                 entry("EID=0x%02x", requestMsg[0]),
-                                 entry("TYPE=0x%02x", requestMsg[1]));
-                printBuffer(requestMsg);
-#endif
+                if (verbose)
+                {
+                    log<level::INFO>("Received Msg ",
+                                     entry("LENGTH=%zu", recvDataLength),
+                                     entry("EID=0x%02x", requestMsg[0]),
+                                     entry("TYPE=0x%02x", requestMsg[1]));
+                    printBuffer(requestMsg);
+                }
                 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
                 {
                     // Skip this message and continue.
@@ -179,10 +218,11 @@
                     auto response = processRxMsg(requestMsg);
                     if (!response.empty())
                     {
-#ifdef VERBOSE
-                        log<level::INFO>("Sending Msg ");
-                        printBuffer(response);
-#endif
+                        if (verbose)
+                        {
+                            log<level::INFO>("Sending Msg ");
+                            printBuffer(response);
+                        }
                         result = sendto(socketFd(), response.data(),
                                         response.size(), 0, nullptr, 0);
                         if (-1 == result)