PEL: Use lg2 in Manager class

Modernize it a bit and it makes it easier to see debug traces which can
be done by just running the daemon from the command line.  There are
quite a few debug traces in this file.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Id1291e3a3c3882d1ed917d4a414cb6253ebf2644
diff --git a/extensions/openpower-pels/manager.cpp b/extensions/openpower-pels/manager.cpp
index 0894a45..1be0848 100644
--- a/extensions/openpower-pels/manager.cpp
+++ b/extensions/openpower-pels/manager.cpp
@@ -27,6 +27,7 @@
 #include <sys/inotify.h>
 #include <unistd.h>
 
+#include <phosphor-logging/lg2.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 #include <xyz/openbmc_project/Logging/Create/server.hpp>
 
@@ -113,9 +114,9 @@
                                          std::istreambuf_iterator<char>());
         if (file.fail())
         {
-            log<level::ERR>("Filesystem error reading a raw PEL",
-                            entry("PELFILE=%s", rawPelPath.c_str()),
-                            entry("OBMCLOGID=%d", obmcLogID));
+            lg2::error(
+                "Filesystem error reading a raw PEL. File = {FILE}, obmcLogID = {LOGID}",
+                "FILE", rawPelPath, "LOGID", obmcLogID);
             // TODO, Decide what to do here. Maybe nothing.
             return;
         }
@@ -129,9 +130,9 @@
     }
     else
     {
-        log<level::ERR>("Raw PEL file from BMC event log does not exist",
-                        entry("PELFILE=%s", (rawPelPath).c_str()),
-                        entry("OBMCLOGID=%d", obmcLogID));
+        lg2::error(
+            "Raw PEL file from BMC event log does not exit. File = {FILE}, obmcLogID = {LOGID}",
+            "FILE", rawPelPath, "LOGID", obmcLogID);
     }
 }
 
@@ -155,11 +156,9 @@
             auto result = _repo.hasPEL(id);
             if (result)
             {
-                log<level::WARNING>(
-                    fmt::format("Duplicate HostBoot PEL Id {:#X} found; "
-                                "moving it to archive folder",
-                                pel->id())
-                        .c_str());
+                lg2::warning(
+                    "Duplicate HostBoot PEL ID {ID} found, moving it to archive folder",
+                    "ID", lg2::hex, pel->id());
 
                 _repo.archivePEL(*pel);
 
@@ -177,11 +176,8 @@
 
         try
         {
-            log<level::DEBUG>(
-                fmt::format("Adding external PEL {:#x} (BMC ID {}) to repo",
-                            pel->id(), obmcLogID)
-                    .c_str());
-
+            lg2::debug("Adding external PEL {ID} (BMC ID {BMCID}) to repo",
+                       "ID", lg2::hex, pel->id(), "BMCID", obmcLogID);
             _repo.add(pel);
 
             if (_repo.sizeWarning())
@@ -196,8 +192,8 @@
         catch (const std::exception& e)
         {
             // Probably a full or r/o filesystem, not much we can do.
-            log<level::ERR>("Unable to add PEL to Repository",
-                            entry("PEL_ID=0x%X", pel->id()));
+            lg2::error("Unable to add PEL {ID} to Repository", "ID", lg2::hex,
+                       pel->id());
         }
 
         updateEventId(pel);
@@ -210,8 +206,8 @@
     }
     else
     {
-        log<level::ERR>("Invalid PEL received from the host",
-                        entry("OBMCLOGID=%d", obmcLogID));
+        lg2::error("Invalid PEL received from the host. BMC ID = {ID}", "ID",
+                   obmcLogID);
 
         AdditionalData ad;
         ad.add("PLID", getNumberString("0x%08X", pel->plid()));
@@ -246,8 +242,7 @@
 {
     std::vector<uint8_t> data;
 
-    log<level::DEBUG>("Adding PEL from ESEL",
-                      entry("OBMC_LOG_ID=%d", obmcLogID));
+    lg2::debug("Adding PEL from ESEL. BMC ID = {ID}", "ID", obmcLogID);
 
     try
     {
@@ -256,7 +251,7 @@
     catch (const std::exception& e)
     {
         // Try to add it below anyway, so it follows the usual bad data path.
-        log<level::ERR>("Problems converting ESEL string to a byte vector");
+        lg2::error("Problems converting ESEL string to a byte vector");
     }
 
     addPEL(data, obmcLogID);
@@ -275,9 +270,7 @@
 
     if (esel.size() <= pelStart)
     {
-        log<level::ERR>("ESEL data too short",
-                        entry("ESEL_SIZE=%d", esel.size()));
-
+        lg2::error("ESEL data too short, length = {LEN}", "LEN", esel.size());
         throw std::length_error("ESEL data too short");
     }
 
@@ -290,8 +283,8 @@
         }
         else
         {
-            log<level::ERR>("ESEL data too short",
-                            entry("ESEL_SIZE=%d", esel.size()));
+            lg2::error("ESEL data too short, length = {LEN}", "LEN",
+                       esel.size());
             throw std::length_error("ESEL data too short");
         }
     }
@@ -366,13 +359,13 @@
         // to this AD.  This way, there will at least be a PEL,
         // possibly with callouts, to allow users to debug the
         // issue that caused the error even without its own PEL.
-        msg = "Event not found in PEL message registry: " + message;
-        log<level::INFO>(msg.c_str());
+        lg2::error("Event not found in PEL message registry: {MSG}", "MSG",
+                   message);
 
         entry = _registry.lookup(defaultLogMessage, rg::LookupType::name);
         if (!entry)
         {
-            log<level::ERR>("Default event not found in PEL message registry");
+            lg2::error("Default event not found in PEL message registry");
             return;
         }
 
@@ -393,14 +386,14 @@
     auto src = pel->primarySRC();
     if (src)
     {
-        auto m = fmt::format("Created PEL {:#x} (BMC ID {}) with SRC {}",
-                             pel->id(), pel->obmcLogID(),
-                             (*src)->asciiString());
-        while (m.back() == ' ')
+        auto asciiString = (*src)->asciiString();
+        while (asciiString.back() == ' ')
         {
-            m.pop_back();
+            asciiString.pop_back();
         }
-        log<level::INFO>(m.c_str());
+        lg2::info("Created PEL {ID} (BMC ID {BMCID}) with SRC {SRC}", "ID",
+                  lg2::hex, pel->id(), "BMCID", pel->obmcLogID(), "SRC",
+                  asciiString);
     }
 
     // Check for severity 0x51 and update boot progress SRC
@@ -425,7 +418,7 @@
     Repository::LogID id{Repository::LogID::Pel(pelID)};
     std::optional<int> fd;
 
-    log<level::DEBUG>("getPEL", entry("PEL_ID=0x%X", pelID));
+    lg2::debug("getPEL {ID}", "ID", lg2::hex, pelID);
 
     try
     {
@@ -464,7 +457,7 @@
     Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
     std::optional<std::vector<uint8_t>> data;
 
-    log<level::DEBUG>("getPELFromOBMCID", entry("OBMC_LOG_ID=%d", obmcLogID));
+    lg2::debug("getPELFromOBMCID  {BMCID}", "BMCID", obmcLogID);
 
     try
     {
@@ -487,7 +480,7 @@
 {
     Repository::LogID id{Repository::LogID::Pel(pelID)};
 
-    log<level::DEBUG>("HostAck", entry("PEL_ID=0x%X", pelID));
+    lg2::debug("HostHack {ID}", "ID", lg2::hex, pelID);
 
     if (!_repo.hasPEL(id))
     {
@@ -504,8 +497,8 @@
 {
     Repository::LogID id{Repository::LogID::Pel(pelID)};
 
-    log<level::DEBUG>("HostReject", entry("PEL_ID=0x%X", pelID),
-                      entry("REASON=%d", static_cast<int>(reason)));
+    lg2::debug("HostReject {ID}, reason = {REASON}", "ID", lg2::hex, pelID,
+               "REASON", reason);
 
     if (!_repo.hasPEL(id))
     {
@@ -555,9 +548,7 @@
     if (-1 == _pelFileDeleteFD)
     {
         auto e = errno;
-        std::string msg = "inotify_init1 failed with errno " +
-                          std::to_string(e);
-        log<level::ERR>(msg.c_str());
+        lg2::error("inotify_init1 failed with errno {ERRNO}", "ERRNO", e);
         abort();
     }
 
@@ -566,9 +557,7 @@
     if (-1 == _pelFileDeleteWatchFD)
     {
         auto e = errno;
-        std::string msg = "inotify_add_watch failed with error " +
-                          std::to_string(e);
-        log<level::ERR>(msg.c_str());
+        lg2::error("inotify_add_watch failed with errno {ERRNO}", "ERRNO", e);
         abort();
     }
 
@@ -596,9 +585,8 @@
     if (bytesRead < 0)
     {
         auto e = errno;
-        std::string msg = "Failed reading data from inotify event, errno = " +
-                          std::to_string(e);
-        log<level::ERR>(msg.c_str());
+        lg2::error("Failed reading data from inotify event, errno = {ERRNO}",
+                   "ERRNO", e);
         abort();
     }
 
@@ -630,8 +618,8 @@
                 }
                 catch (const std::exception& e)
                 {
-                    log<level::INFO>("Could not find PEL ID from its filename",
-                                     entry("FILENAME=%s", filename.c_str()));
+                    lg2::info("Could not find PEL ID from its filename {NAME}",
+                              "NAME", filename);
                 }
             }
         }
@@ -663,7 +651,7 @@
     FILE* pipe = popen(cmd.c_str(), "r");
     if (!pipe)
     {
-        log<level::ERR>(fmt::format("Error running {}", cmd).c_str());
+        lg2::error("Error running cmd: {CMD}", "CMD", cmd);
         throw common_error::InternalFailure();
     }
 
@@ -677,8 +665,7 @@
     int rc = pclose(pipe);
     if (WEXITSTATUS(rc) != 0)
     {
-        log<level::ERR>(
-            fmt::format("Error running {}, rc = {}", cmd, rc).c_str());
+        lg2::error("Error running cmd: {CMD}, rc = {RC}", "CMD", cmd, "RC", rc);
         throw common_error::InternalFailure();
     }
 
@@ -692,13 +679,13 @@
         (pel->userHeader().severity() ==
          static_cast<uint8_t>(SeverityType::recovered)))
     {
-        log<level::DEBUG>(
+        lg2::debug(
             "PEL severity informational or recovered. no quiesce needed");
         return;
     }
     if (!_logManager.isQuiesceOnErrorEnabled())
     {
-        log<level::DEBUG>("QuiesceOnHwError not enabled, no quiesce needed");
+        lg2::debug("QuiesceOnHwError not enabled, no quiesce needed");
         return;
     }
 
@@ -715,7 +702,7 @@
     // Now check if it has any type of callout
     if (pel->isHwCalloutPresent())
     {
-        log<level::INFO>(
+        lg2::info(
             "QuiesceOnHwError enabled, PEL severity not nonError or recovered, "
             "and callout is present");
 
@@ -877,14 +864,11 @@
                                             sevType);
         if (newSeverity)
         {
-            log<level::INFO>(
-                fmt::format(
-                    "Changing event log {} severity from {} "
-                    "to {} to match PEL",
-                    entryN->second->id(),
-                    Entry::convertLevelToString(entryN->second->severity()),
-                    Entry::convertLevelToString(*newSeverity))
-                    .c_str());
+            lg2::info("Changing event log {ID} severity from {OLD} "
+                      "to {NEW} to match PEL",
+                      "ID", lg2::hex, entryN->second->id(), "OLD",
+                      Entry::convertLevelToString(entryN->second->severity()),
+                      "NEW", Entry::convertLevelToString(*newSeverity));
 
             entryN->second->severity(*newSeverity, true);
         }
@@ -1042,8 +1026,7 @@
 
 void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
 {
-    log<level::INFO>(
-        fmt::format("Removing event log with no PEL: {}", obmcLogID).c_str());
+    lg2::info("Removing event log with no PEL: {BMCID}", "BMCID", obmcLogID);
     _logManager.erase(obmcLogID);
     _obmcLogDeleteEventSource.reset();
 }
@@ -1087,11 +1070,10 @@
             continue;
         }
 
-        log<level::INFO>(
-            fmt::format(
-                "Clearing deconfig flag in PEL {:#x} with SRC {} because {} was replaced",
-                pel.id(), (*src)->asciiString().substr(0, 8), locationCode)
-                .c_str());
+        lg2::info(
+            "Clearing deconfig flag in PEL {ID} with SRC {SRC} because {LOC} was replaced",
+            "ID", lg2::hex, pel.id(), "SRC", (*src)->asciiString().substr(0, 8),
+            "LOC", locationCode);
         (*src)->clearErrorStatusFlag(SRC::ErrorStatusFlags::deconfigured);
         return true;
     }