PEL: Delete event logs with bad PELs

There are two cases where there would be an OpenBMC D-Bus event log
without an associated PEL:
1) The event log/PEL came from the host, and the PEL was malformed.
2) The event log/PEL came from hostboot, and the ID of the PEL is
   already in use by an existing PEL so the PEL is archived.

This commit will delete the OpenBMC D-Bus event logs in these cases
pretty much right after they are created, because:

a) Host originated D-Bus event logs are aren't useful for either service
nor debug since the PEL contains all of the useful information.

b) These event logs aren't seen by the PEL code's size/space management
algorithms that delete logs, so they continue to accumulate, taking up
more and more space in the serialization directory and possibly slowing
down D-Bus calls that act on all entries.

While this shouldn't really occur that often, there isn't anything the
BMC can do to keep it from happening since it's due to host issues.

An alternative could be to delay emitting the InterfacesAdded signal
until after the event log extension code runs and have that code say if
it is OK to create the event log or not, but as most users of
phosphor-logging don't use extensions it seems more intrusive. Also, if
there are multiple extensions and only one is upset, it isn't that clear
what should happen.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I6c7faf47c1c340ecbce0771bfe667560720ee269
diff --git a/extensions/openpower-pels/manager.cpp b/extensions/openpower-pels/manager.cpp
index 4c90d1e..6bbdf11 100644
--- a/extensions/openpower-pels/manager.cpp
+++ b/extensions/openpower-pels/manager.cpp
@@ -156,6 +156,9 @@
                         .c_str());
 
                 _repo.archivePEL(*pel);
+
+                // No need to keep around the openBMC event log entry
+                scheduleObmcLogDelete(obmcLogID);
                 return;
             }
         }
@@ -225,6 +228,9 @@
         std::ofstream pelFile{getPELRepoPath() / "badPEL"};
         pelFile.write(reinterpret_cast<const char*>(pelData.data()),
                       pelData.size());
+
+        // No need to keep around the openBMC event log entry
+        scheduleObmcLogDelete(obmcLogID);
     }
 }
 
@@ -902,5 +908,20 @@
     }
 }
 
+void Manager::scheduleObmcLogDelete(uint32_t obmcLogID)
+{
+    _obmcLogDeleteEventSource = std::make_unique<sdeventplus::source::Defer>(
+        _event, std::bind(std::mem_fn(&Manager::deleteObmcLog), this,
+                          std::placeholders::_1, obmcLogID));
+}
+
+void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
+{
+    log<level::INFO>(
+        fmt::format("Removing event log with no PEL: {}", obmcLogID).c_str());
+    _logManager.erase(obmcLogID);
+    _obmcLogDeleteEventSource.reset();
+}
+
 } // namespace pels
 } // namespace openpower