lg2: commit: extract source location information and add to event

sdbusplus will add PID and std::source_location information, from
the event origination point, to the event.  Extract this and add
it to the event.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I6364ba0f74231fca2815c770bc3e56c2780d199e
diff --git a/lib/lg2_commit.cpp b/lib/lg2_commit.cpp
index 60dd30e..4d75594 100644
--- a/lib/lg2_commit.cpp
+++ b/lib/lg2_commit.cpp
@@ -56,6 +56,36 @@
     auto j = t.to_json()[t.name()];
     for (const auto& item : j.items())
     {
+        // Special cases for the "_SOURCE" fields, which contain debug
+        // information about the origin of the event.
+        if (item.key() == "_SOURCE")
+        {
+            for (const auto& source_item : item.value().items())
+            {
+                if (source_item.key() == "PID")
+                {
+                    result.emplace("_PID", source_item.value().dump());
+                    continue;
+                }
+                if (source_item.key() == "FILE")
+                {
+                    result.emplace("_CODE_FILE", source_item.value());
+                    continue;
+                }
+                if (source_item.key() == "FUNCTION")
+                {
+                    result.emplace("_CODE_FUNC", source_item.value());
+                    continue;
+                }
+                if (source_item.key() == "LINE")
+                {
+                    result.emplace("_CODE_LINE", source_item.value().dump());
+                    continue;
+                }
+            }
+            continue;
+        }
+
         result.emplace(item.key(), item.value().dump());
     }
 
diff --git a/test/log_manager_dbus_tests.cpp b/test/log_manager_dbus_tests.cpp
index ac33ded..e2bc8be 100644
--- a/test/log_manager_dbus_tests.cpp
+++ b/test/log_manager_dbus_tests.cpp
@@ -113,8 +113,10 @@
 {
     sdbusplus::message::object_path path{};
     std::string log_count{};
+    pid_t pid = 0;
+    std::string source_file{};
 
-    auto create_log = [this, &path, &log_count]() -> sdbusplus::async::task<> {
+    auto create_log = [&, this]() -> sdbusplus::async::task<> {
         // Log an event.
         path = co_await lg2::commit(data->client_ctx,
                                     LoggingCleared("NUMBER_OF_LOGS", 6));
@@ -125,12 +127,24 @@
                                   .path(path.str)
                                   .additional_data();
 
-        // Extract the NUMBER_OF_LOGS.
+        // Extract the NUMBER_OF_LOGS, PID, and CODE_FILE.
         for (const auto& value : additionalData)
         {
+            auto getValue = [&value]() {
+                return value.substr(value.find_first_of('=') + 1);
+            };
+
             if (value.starts_with("NUMBER_OF_LOGS="))
             {
-                log_count = value.substr(value.find_first_of('=') + 1);
+                log_count = getValue();
+            }
+            if (value.starts_with("_PID="))
+            {
+                pid = std::stoull(getValue());
+            }
+            if (value.starts_with("_CODE_FILE="))
+            {
+                source_file = getValue();
             }
         }
 
@@ -148,6 +162,8 @@
                     LoggingEntry::namespace_path::entry));
 
     EXPECT_EQ(log_count, "6");
+    EXPECT_EQ(pid, getpid());
+    EXPECT_EQ(source_file, std::source_location::current().file_name());
 }
 
 } // namespace phosphor::logging::test