sel-cache: Add match for new sel entry

Add match to get callback on new logging entry.

Tested: Verify the sel cache is updated with a new logging entry is
        created.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I4674f4b5e0336719f101b02920ea2be2077aa5e9
diff --git a/storagehandler.cpp b/storagehandler.cpp
index fdd5ffb..8927490 100644
--- a/storagehandler.cpp
+++ b/storagehandler.cpp
@@ -44,6 +44,7 @@
 constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
 constexpr auto PROPERTY_ELAPSED = "Elapsed";
 
+constexpr auto logWatchPath = "/xyz/openbmc_project/logging";
 constexpr auto logBasePath = "/xyz/openbmc_project/logging/entry";
 constexpr auto logEntryIntf = "xyz.openbmc_project.Logging.Entry";
 constexpr auto logDeleteIntf = "xyz.openbmc_project.Object.Delete";
@@ -75,6 +76,8 @@
 using SELCacheMap = std::map<SELRecordID, SELEntry>;
 
 SELCacheMap selCacheMap __attribute__((init_priority(101)));
+std::unique_ptr<sdbusplus::bus::match::match> selAddedMatch
+    __attribute__((init_priority(101)));
 
 std::pair<uint16_t, SELEntry> parseLoggingEntry(const std::string& p)
 {
@@ -85,6 +88,35 @@
     return {id, {}};
 }
 
+static void selAddedCallback(sdbusplus::message::message& m)
+{
+    sdbusplus::message::object_path objPath;
+    try
+    {
+        m.read(objPath);
+    }
+    catch (const sdbusplus::exception::exception& e)
+    {
+        log<level::ERR>("Failed to read object path");
+        return;
+    }
+    std::string p = objPath;
+    selCacheMap.insert(parseLoggingEntry(p));
+}
+
+void registerSelCallbackHandler()
+{
+    using namespace sdbusplus::bus::match::rules;
+    sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
+    if (!selAddedMatch)
+    {
+        selAddedMatch = std::make_unique<sdbusplus::bus::match::match>(
+            bus, interfacesAdded(logWatchPath),
+            std::bind(selAddedCallback, std::placeholders::_1));
+    }
+    // TODO: Add other callbacks
+}
+
 void initSELCache()
 {
     try
@@ -100,6 +132,7 @@
     {
         selCacheMap.insert(parseLoggingEntry(p));
     }
+    registerSelCallbackHandler();
 }
 
 /**