PEL: Add ability to create event logs

There are cases where the PEL code wants to be able to create OpenBMC
event logs (and thus PELs) for problems it encounters when trying to
create or import other PELs.  For example, if the host were to send down
a malformed PEL, this code would like to create a new event log and
capture part of that bad PEL in the new PEL for debug purposes, as the
malformed PEL cannot be reported anywhere since it is malformed.

To handle this, create the EventLogger class that provides a log()
function that allows the PEL extension code to create OpenBMC event logs
(and thus PELs) from within.

The underlying function to do the event log creating is passed in via
the constructor so that it can be changed for testing.

The sd_event_add_defer function (wrapped by sdeventplus) is used to
dispatch the creation of a single event, so that the entry point is from
the event loop.  If there are still events left on the queue after that,
then they will be also be scheduled with sd_event_add_defer so that the
events are always created from event loop calls.

EventLogger does not allow events to be added to the queue if it is
being done from within the creation function so that the code can't
get stuck in a loop of creating a new event every time an event is
created.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I6a9062074dc62cfb6043139ff0a9f3dfcd06c708
diff --git a/extensions/openpower-pels/manager.hpp b/extensions/openpower-pels/manager.hpp
index 14904a9..bec67ca 100644
--- a/extensions/openpower-pels/manager.hpp
+++ b/extensions/openpower-pels/manager.hpp
@@ -3,6 +3,7 @@
 #include "config.h"
 
 #include "data_interface.hpp"
+#include "event_logger.hpp"
 #include "host_notifier.hpp"
 #include "log_manager.hpp"
 #include "paths.hpp"
@@ -40,11 +41,16 @@
      *
      * @param[in] logManager - internal::Manager object
      * @param[in] dataIface - The data interface object
+     * @param[in] creatorFunc - The function that EventLogger will
+     *                          use for creating event logs
      */
     Manager(phosphor::logging::internal::Manager& logManager,
-            std::unique_ptr<DataInterfaceBase> dataIface) :
+            std::unique_ptr<DataInterfaceBase> dataIface,
+            EventLogger::LogFunction creatorFunc) :
         PELInterface(logManager.getBus(), OBJ_LOGGING),
-        _logManager(logManager), _repo(getPELRepoPath()),
+        _logManager(logManager),
+        _eventLogger(logManager.getBus().get_event(), std::move(creatorFunc)),
+        _repo(getPELRepoPath()),
         _registry(getMessageRegistryPath() / message::registryFileName),
         _dataIface(std::move(dataIface))
     {
@@ -55,12 +61,15 @@
      *
      * @param[in] logManager - internal::Manager object
      * @param[in] dataIface - The data interface object
+     * @param[in] creatorFunc - The function that EventLogger will
+     *                          use for creating event logs
      * @param[in] hostIface - The hostInterface object
      */
     Manager(phosphor::logging::internal::Manager& logManager,
             std::unique_ptr<DataInterfaceBase> dataIface,
+            EventLogger::LogFunction creatorFunc,
             std::unique_ptr<HostInterface> hostIface) :
-        Manager(logManager, std::move(dataIface))
+        Manager(logManager, std::move(dataIface), std::move(creatorFunc))
     {
         _hostNotifier = std::make_unique<HostNotifier>(
             _repo, *(_dataIface.get()), std::move(hostIface));
@@ -204,6 +213,12 @@
     phosphor::logging::internal::Manager& _logManager;
 
     /**
+     * @brief Handles creating event logs/PELs from within
+     *        the PEL extension code
+     */
+    EventLogger _eventLogger;
+
+    /**
      * @brief The PEL repository object
      */
     Repository _repo;