ramoops: generate error log when ramoops detected

A ramoops being detected is a critical event for a BMC based system. It
indicates the BMC had an unexpected reboot because of a kernel panic.
Ensure a log is reported so the user of the system knows to look for a
BMC dump with the debug information.

Tested:
- Manually created some files in /var/lib/systemd/pstore/ and ran the
  ramoops application and verified the expected log was created.

Change-Id: Id1162fa0cca72e5dcc8cf59e75bd298d2ddada2e
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/ramoops_manager.cpp b/ramoops_manager.cpp
index a3df837..acd3598 100644
--- a/ramoops_manager.cpp
+++ b/ramoops_manager.cpp
@@ -4,6 +4,7 @@
 
 #include "dump_manager.hpp"
 
+#include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/exception.hpp>
@@ -30,12 +31,50 @@
         return;
     }
 
+    // Create error to notify user that a ramoops has been detected
+    createError();
+
     std::vector<std::string> files;
     files.push_back(filePath);
 
     createHelper(files);
 }
 
+void Manager::createError()
+{
+    try
+    {
+        std::map<std::string, std::string> additionalData;
+
+        // Always add the _PID on for some extra logging debug
+        additionalData.emplace("_PID", std::to_string(getpid()));
+
+        auto bus = sdbusplus::bus::new_default();
+        auto method = bus.new_method_call(
+            "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
+            "xyz.openbmc_project.Logging.Create", "Create");
+
+        method.append("xyz.openbmc_project.Dump.Error.Ramoops",
+                      sdbusplus::server::xyz::openbmc_project::logging::Entry::
+                          Level::Error,
+                      additionalData);
+        auto resp = bus.call(method);
+    }
+    catch (const sdbusplus::exception_t& e)
+    {
+        lg2::error(
+            "sdbusplus D-Bus call exception, error {ERROR} trying to create "
+            "an error for ramoops detection",
+            "ERROR", e);
+        // This is a best-effort logging situation so don't throw anything
+    }
+    catch (const std::exception& e)
+    {
+        lg2::error("D-bus call exception: {ERROR}", "ERROR", e);
+        // This is a best-effort logging situation so don't throw anything
+    }
+}
+
 void Manager::createHelper(const std::vector<std::string>& files)
 {
     constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
diff --git a/ramoops_manager.hpp b/ramoops_manager.hpp
index 57ab23c..8158116 100644
--- a/ramoops_manager.hpp
+++ b/ramoops_manager.hpp
@@ -39,6 +39,11 @@
      *  @param [in] files - ramoops files list
      */
     void createHelper(const std::vector<std::string>& files);
+
+    /** @brief Create an error indicating ramoops was found
+     *
+     */
+    void createError();
 };
 
 } // namespace ramoops