Implementing Phosphor-Logging/LG2 logging

This commit introduces changes in the openpower-debug-collector
repository to implement structured logging using the LG2
framework. The existing log calls in the repository,
have been replaced with LG2 logging, facilitates better
log tracking and troubleshooting by offering improved
detail in JSON object values.

Change-Id: Iabea1d0044d27a286f73e34588205b45952ae146
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/watchdog/ffdc_file.cpp b/watchdog/ffdc_file.cpp
index bcc016f..5aaa92a 100644
--- a/watchdog/ffdc_file.cpp
+++ b/watchdog/ffdc_file.cpp
@@ -6,9 +6,8 @@
 #include <sys/stat.h>  // for open()
 #include <sys/types.h> // for open()
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 
-#include <format>
 #include <stdexcept>
 #include <string>
 
@@ -16,7 +15,6 @@
 {
 namespace dump
 {
-using namespace phosphor::logging;
 
 FFDCFile::FFDCFile(const json& calloutDataObject) :
     calloutData(calloutDataObject.dump())
@@ -29,9 +27,8 @@
     // Close file descriptor.  Does nothing if descriptor was already closed.
     if (descriptor.close() == -1)
     {
-        log<level::ERR>(std::format("Unable to close FFDC file: errormsg({})",
-                                    strerror(errno))
-                            .c_str());
+        lg2::error("Unable to close FFDC file: errormsg({ERRORMSG})",
+                   "ERRORMSG", strerror(errno));
     }
 
     // Delete temporary file.  Does nothing if file was already deleted.
@@ -52,33 +49,30 @@
 
     if (rc == -1)
     {
-        log<level::ERR>(std::format("Failed to write callout info "
-                                    "in file({}), errorno({}), errormsg({})",
-                                    tempFile.getPath().c_str(), errno,
-                                    strerror(errno))
-                            .c_str());
+        lg2::error("Failed to write callout info in file({FILE}), "
+                   "errorno({ERRNO}), errormsg({ERRORMSG})",
+                   "FILE", tempFile.getPath(), "ERRNO", errno, "ERRORMSG",
+                   strerror(errno));
+
         throw std::runtime_error("Failed to write phalPELCallouts info");
     }
     else if (rc != static_cast<ssize_t>(calloutData.size()))
     {
-        log<level::WARNING>(std::format("Could not write all callout "
-                                        "info in file({}), written byte({}) "
-                                        "and total byte({})",
-                                        tempFile.getPath().c_str(), rc,
-                                        calloutData.size())
-                                .c_str());
+        lg2::warning("Could not write all callout info in file({FILE}), "
+                     "written byte({WRITTEN}), total byte({TOTAL})",
+                     "FILE", tempFile.getPath(), "WRITTEN", rc, "TOTAL",
+                     calloutData.size());
     }
 
     int retCode = lseek(fd, 0, SEEK_SET);
 
     if (retCode == -1)
     {
-        log<level::ERR>(
-            std::format("Failed to seek file postion to the beginning"
-                        "in file({}), errorno({}) "
-                        "and errormsg({})",
-                        tempFile.getPath().c_str(), errno, strerror(errno))
-                .c_str());
+        lg2::error("Failed to seek file position to the beginning in "
+                   "file({FILE}), errorno({ERRNO}), errormsg({ERRORMSG})",
+                   "FILE", tempFile.getPath(), "ERRNO", errno, "ERRORMSG",
+                   strerror(errno));
+
         throw std::runtime_error(
             "Failed to seek file postion to the beginning of the file");
     }
diff --git a/watchdog/watchdog_dbus.cpp b/watchdog/watchdog_dbus.cpp
index 1606be1..ed7e750 100644
--- a/watchdog/watchdog_dbus.cpp
+++ b/watchdog/watchdog_dbus.cpp
@@ -1,10 +1,9 @@
 #include <unistd.h>
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <watchdog_dbus.hpp>
 #include <watchdog_logging.hpp>
 
-#include <format>
 #include <string>
 #include <vector>
 
@@ -13,8 +12,6 @@
 namespace dump
 {
 
-using namespace phosphor::logging;
-
 int dbusMethod(const std::string& path, const std::string& interface,
                const std::string& function, sdbusplus::message_t& method,
                const std::string& extended)
@@ -71,16 +68,16 @@
         else
         {
             // This trace will be picked up in event log
-            log<level::INFO>("dbusMethod service not found");
+            lg2::info("dbusMethod service not found");
             std::string traceMsgPath = std::string(path, maxTraceLen);
-            log<level::INFO>(traceMsgPath.c_str());
+            lg2::info(traceMsgPath.c_str());
             std::string traceMsgIface = std::string(interface, maxTraceLen);
-            log<level::INFO>(traceMsgIface.c_str());
+            lg2::info(traceMsgIface.c_str());
         }
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>("Error in dbusMethod", entry("ERROR=%s", e.what()));
+        lg2::error("Error in dbusMethod ({ERROR})", "ERROR", e);
     }
 
     return rc;
@@ -122,8 +119,8 @@
         }
         catch (const sdbusplus::exception_t& e)
         {
-            log<level::ERR>("Error in createPel CreatePELWithFFDCFiles",
-                            entry("ERROR=%s", e.what()));
+            lg2::error("Error in createPel CreatePELWithFFDCFiles ({ERROR})",
+                       "ERROR", e);
         }
     }
 
@@ -159,10 +156,8 @@
         }
         catch (const sdbusplus::exception_t& e)
         {
-            log<level::ERR>(
-                std::format("Failed to read CurrentHostState property ({})",
-                            e.what())
-                    .c_str());
+            lg2::error("Failed to read CurrentHostState property ({ERROR})",
+                       "ERROR", e);
         }
     }
 
diff --git a/watchdog/watchdog_handler.cpp b/watchdog/watchdog_handler.cpp
index b3e1520..c94059f 100644
--- a/watchdog/watchdog_handler.cpp
+++ b/watchdog/watchdog_handler.cpp
@@ -1,19 +1,15 @@
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/bus/match.hpp>
 #include <watchdog_dbus.hpp>
 #include <watchdog_handler.hpp>
 #include <watchdog_logging.hpp>
 
-#include <format>
-
 namespace watchdog
 {
 namespace dump
 {
 
-using namespace phosphor::logging;
-
 /**
  * @brief Callback for dump request properties change signal monitor
  *
@@ -44,8 +40,8 @@
         {
             // dump is not in InProgress state, trace some info and change in
             // progress status
-            log<level::INFO>(path.c_str());
-            log<level::INFO>((*status).c_str());
+            lg2::info("{PATH}", "PATH", path);
+            lg2::info("{STATUS}", "STATUS", *status);
 
             if ("xyz.openbmc_project.Common.Progress.OperationStatus."
                 "Completed" == *status)
@@ -106,16 +102,16 @@
 
     if (timedOut)
     {
-        log<level::ERR>("Dump progress status did not change to "
-                        "complete within the timeout interval, exiting...");
+        lg2::error("Dump progress status did not change to "
+                   "complete within the timeout interval, exiting...");
     }
     else if (DumpProgressStatus::Completed == progressStatus)
     {
-        log<level::INFO>("dump collection completed");
+        lg2::info("dump collection completed");
     }
     else
     {
-        log<level::INFO>("dump collection failed");
+        lg2::info("dump collection failed");
     }
 }
 
@@ -138,13 +134,13 @@
                 uint64_t(dumpParameters.logId);
             if (DumpType::Hostboot == dumpParameters.dumpType)
             {
-                log<level::INFO>("hostboot dump requested");
+                lg2::info("hostboot dump requested");
                 createParams["com.ibm.Dump.Create.CreateParameters.DumpType"] =
                     "com.ibm.Dump.Create.DumpType.Hostboot";
             }
             else if (DumpType::SBE == dumpParameters.dumpType)
             {
-                log<level::INFO>("SBE dump requested");
+                lg2::info("SBE dump requested");
                 createParams["com.ibm.Dump.Create.CreateParameters.DumpType"] =
                     "com.ibm.Dump.Create.DumpType.SBE";
                 createParams
@@ -172,19 +168,15 @@
             if (e.name() == ERROR_DUMP_DISABLED)
             {
                 // Dump is disabled, Skip the dump collection.
-                log<level::INFO>(
-                    std::format(
-                        "Dump is disabled on({}), skipping dump collection",
-                        dumpParameters.unitId)
-                        .c_str());
+                lg2::info("Dump is disabled on({UNIT}), "
+                          "skipping dump collection",
+                          "UNIT", dumpParameters.unitId);
             }
             else
             {
-                log<level::ERR>(
-                    std::format("D-Bus call createDump exception ",
-                                "OBJPATH={}, INTERFACE={}, EXCEPTION={}", path,
-                                interface, e.what())
-                        .c_str());
+                lg2::error("D-Bus call createDump exception OBJPATH={OBJPATH}, "
+                           "INTERFACE={INTERFACE}, EXCEPTION={ERROR}",
+                           "OBJPATH", path, "INTERFACE", interface, "ERROR", e);
             }
         }
     }
diff --git a/watchdog/watchdog_main.cpp b/watchdog/watchdog_main.cpp
index 7ad1ced..b124be3 100644
--- a/watchdog/watchdog_main.cpp
+++ b/watchdog/watchdog_main.cpp
@@ -7,7 +7,7 @@
 
 #include <libphal.H>
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <watchdog_common.hpp>
 #include <watchdog_dbus.hpp>
 #include <watchdog_handler.hpp>
@@ -18,8 +18,6 @@
 namespace dump
 {
 
-using namespace phosphor::logging;
-
 void triggerHostbootDump(const uint32_t timeout)
 {
     constexpr auto HOST_STATE_DIAGNOSTIC_MODE =
@@ -71,9 +69,8 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(std::format("getLocationCode({}): Exception({})",
-                                    pdbg_target_path(procTarget), e.what())
-                            .c_str());
+        lg2::error("getLocationCode({LOCATION}): Exception({ERROR})",
+                   "LOCATION", pdbg_target_path(procTarget), "ERROR", e);
     }
 }
 
@@ -92,8 +89,7 @@
     catch (const std::exception& e)
     {
         // Failed to collect FFDC information
-        log<level::ERR>(
-            std::format("captureFFDC: Exception{}", e.what()).c_str());
+        lg2::error("captureFFDC: Exception{ERROR}", "ERROR", e);
         dumpIsRequired = true;
     }
 
@@ -102,13 +98,13 @@
     if ((sbeError.errType() == exception::SBE_FFDC_NO_DATA) ||
         (sbeError.errType() == exception::SBE_CMD_TIMEOUT) || (dumpIsRequired))
     {
-        log<level::INFO>("No FFDC data");
+        lg2::info("No FFDC data");
         event = "org.open_power.Processor.Error.SbeBootTimeout";
         dumpIsRequired = true;
     }
     else
     {
-        log<level::ERR>("SBE Boot failure");
+        lg2::error("SBE Boot failure");
         event = "org.open_power.Processor.Error.SbeBootFailure";
     }
 
@@ -156,10 +152,8 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(
-            std::format("Skipping SBE special callout due to Exception({})",
-                        e.what())
-                .c_str());
+        lg2::error("Skipping SBE special callout due to Exception({ERROR})",
+                   "ERROR", e);
     }
     auto pelId = createPel(event, additionalData, ffdc);
 
@@ -174,15 +168,14 @@
             if (!dumpAllowed)
             {
                 // Possibly another collection in progress, skip dump collection
-                log<level::INFO>("Another collection is in progress, skipping "
-                                 "dump collection");
+                lg2::error("Another collection is in progress, skipping "
+                           "dump collection");
                 return;
             }
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>(
-                std::format("Exception {} occurred", e.what()).c_str());
+            lg2::error("Exception {ERROR} occurred", "ERROR", e);
             return;
         }
 
diff --git a/watchdog_timeout.cpp b/watchdog_timeout.cpp
index 91fa616..f616312 100644
--- a/watchdog_timeout.cpp
+++ b/watchdog_timeout.cpp
@@ -11,12 +11,11 @@
 
 #include <libphal.H>
 
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <watchdog/watchdog_common.hpp>
 #include <watchdog/watchdog_dbus.hpp>
 #include <watchdog/watchdog_main.hpp>
 
-#include <format>
 #else
 #include <org/open_power/Host/Boot/error.hpp>
 #include <phosphor-logging/elog-errors.hpp>
@@ -37,10 +36,9 @@
     CLI11_PARSE(app, argc, argv);
 
 #ifdef WATCHDOG_DUMP_COLLECTION
-    using namespace phosphor::logging;
     using namespace watchdog::dump;
 
-    log<level::INFO>("Host did not respond within watchdog timeout interval");
+    lg2::info("Host did not respond within watchdog timeout interval");
     try
     {
         using namespace openpower::phal;
@@ -58,26 +56,26 @@
             // Collect hostboot dump only if the host is in 'Running' state
             if (!isHostStateRunning())
             {
-                log<level::INFO>(
+                lg2::info(
                     "CurrentHostState is not in 'Running' state. Dump maybe "
                     "already occurring, skipping this dump request...");
                 return EXIT_SUCCESS;
             }
 
             // SBE boot done, Need to collect hostboot dump
-            log<level::INFO>("Handle Hostboot boot failure");
+            lg2::info("Handle Hostboot boot failure");
             triggerHostbootDump(timeout);
         }
         else
         {
             // SBE boot window, handle SBE boot failure
-            log<level::INFO>("Handle SBE boot failure");
+            lg2::info("Handle SBE boot failure");
             handleSbeBootError(procTarget, timeout);
         }
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(std::format("Exception {} occurred", e.what()).c_str());
+        lg2::error("Exception {ERROR} occurred", "ERROR", e);
         std::string eventType =
             "org.open_power.Host.Boot.Error.WatchdogTimedOut";
         auto ffdc = std::vector<FFDCTuple>{};
@@ -85,7 +83,7 @@
 
         if (!createPel(eventType, additionalData, ffdc))
         {
-            log<level::ERR>("Failed to create PEL");
+            lg2::error("Failed to create PEL");
         }
 
         return EXIT_SUCCESS;