Optionally send reports to D-Bus Logging

Follow the trend set by common OpenBMC components (phosphor-sel-logger,
dbus-sensors etc.) and allow to send all essential reports to the
dedicated Logging interface instead of the system journal directly.

Tested: built with "send-to-logger" option enabled and run on a system
where mem_thermtrip_monitor detected a problem. Observed a message
becoming visible via Redfish (bmcweb built with
"redfish-dbus-log=enabled").

Change-Id: I29287972d85433a7e85660c82fe95fff5b023e5a
Signed-off-by: Paul Fertser <fercerpav@gmail.com>
diff --git a/include/error_monitors/base_monitor.hpp b/include/error_monitors/base_monitor.hpp
index a752cdf..25c21ae 100644
--- a/include/error_monitors/base_monitor.hpp
+++ b/include/error_monitors/base_monitor.hpp
@@ -15,6 +15,7 @@
 */
 #pragma once
 #include <sdbusplus/asio/object_server.hpp>
+#include <xyz/openbmc_project/Logging/Entry/common.hpp>
 
 #include <iostream>
 
@@ -47,5 +48,29 @@
     {
         return valid;
     }
+
+  protected:
+    void log_message(int priority, const std::string& msg,
+                     const std::string& redfish_id,
+                     const std::string& redfish_msg)
+    {
+#ifdef SEND_TO_LOGGING_SERVICE
+        (void)redfish_id;
+        (void)redfish_msg;
+        using namespace sdbusplus::common::xyz::openbmc_project::logging;
+        sdbusplus::message_t newLogEntry = conn->new_method_call(
+            "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
+            "xyz.openbmc_project.Logging.Create", "Create");
+        const std::string logLevel =
+            Entry::convertLevelToString(static_cast<Entry::Level>(priority));
+        newLogEntry.append(msg, std::move(logLevel),
+                           std::map<std::string, std::string>{});
+        conn->call(newLogEntry);
+#else
+        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
+                        priority, "REDFISH_MESSAGE_ID=%s", redfish_id.c_str(),
+                        "REDFISH_MESSAGE_ARGS=%s", redfish_msg.c_str(), NULL);
+#endif
+    }
 };
 } // namespace host_error_monitor::base_monitor
diff --git a/include/error_monitors/cpld_crc_monitor.hpp b/include/error_monitors/cpld_crc_monitor.hpp
index cd5eb24..f10cf40 100644
--- a/include/error_monitors/cpld_crc_monitor.hpp
+++ b/include/error_monitors/cpld_crc_monitor.hpp
@@ -36,10 +36,7 @@
         std::string cpuNumber = "CPU " + std::to_string(cpuNum);
         std::string msg = cpuNumber + " CPLD CRC error.";
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     bool getCPUPresence(const std::string& cpuPresenceName)
diff --git a/include/error_monitors/cpu_early_error_monitor.hpp b/include/error_monitors/cpu_early_error_monitor.hpp
index 5e048b2..44ac41e 100644
--- a/include/error_monitors/cpu_early_error_monitor.hpp
+++ b/include/error_monitors/cpu_early_error_monitor.hpp
@@ -33,10 +33,7 @@
         std::string cpuNumber = "CPU " + std::to_string(cpuNum);
         std::string msg = cpuNumber + " early error.";
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_ERR, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_ERR, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
   public:
diff --git a/include/error_monitors/cpu_mismatch_monitor.hpp b/include/error_monitors/cpu_mismatch_monitor.hpp
index 2d65767..ffdc104 100644
--- a/include/error_monitors/cpu_mismatch_monitor.hpp
+++ b/include/error_monitors/cpu_mismatch_monitor.hpp
@@ -34,10 +34,10 @@
 
     void cpuMismatchLog()
     {
-        sd_journal_send("MESSAGE=HostError: CPU %d mismatch", cpuNum,
-                        "PRIORITY=%i", LOG_ERR, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUMismatch", "REDFISH_MESSAGE_ARGS=%d",
-                        cpuNum, NULL);
+        const std::string cpuS = std::to_string(cpuNum);
+
+        log_message(LOG_ERR, "CPU " + cpuS + " mismatch",
+                    "OpenBMC.0.1.CPUMismatch", cpuS);
     }
 
     bool requestCPUMismatchInput()
diff --git a/include/error_monitors/cpu_presence_monitor.hpp b/include/error_monitors/cpu_presence_monitor.hpp
index 2866d48..49ab043 100644
--- a/include/error_monitors/cpu_presence_monitor.hpp
+++ b/include/error_monitors/cpu_presence_monitor.hpp
@@ -34,10 +34,7 @@
     {
         std::string msg = "CPU " + std::to_string(cpuNum) + " missing";
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     void CPUPresenceAssertHandler(
diff --git a/include/error_monitors/cpu_thermtrip_monitor.hpp b/include/error_monitors/cpu_thermtrip_monitor.hpp
index f1b7758..41ca1b6 100644
--- a/include/error_monitors/cpu_thermtrip_monitor.hpp
+++ b/include/error_monitors/cpu_thermtrip_monitor.hpp
@@ -37,12 +37,10 @@
 
     void cpuThermTripLog()
     {
-        std::string msg = "CPU " + std::to_string(cpuNum) + " thermal trip";
+        const std::string cpuS = std::to_string(cpuNum);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUThermalTrip", "REDFISH_MESSAGE_ARGS=%d",
-                        cpuNum, NULL);
+        log_message(LOG_INFO, "CPU " + cpuS + " thermal trip",
+                    "OpenBMC.0.1.CPUThermalTrip", cpuS);
     }
 
     void deassertHandler() override
diff --git a/include/error_monitors/err_pin_monitor.hpp b/include/error_monitors/err_pin_monitor.hpp
index 04e3654..95cfb2e 100644
--- a/include/error_monitors/err_pin_monitor.hpp
+++ b/include/error_monitors/err_pin_monitor.hpp
@@ -56,10 +56,7 @@
     {
         std::string msg = "ERR" + std::to_string(errPin);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     void errPinLog(const int cpuNum)
@@ -67,10 +64,7 @@
         std::string msg = "ERR" + std::to_string(errPin) + " on CPU " +
                           std::to_string(cpuNum + 1);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
   public:
diff --git a/include/error_monitors/err_pin_timeout_monitor.hpp b/include/error_monitors/err_pin_timeout_monitor.hpp
index 5afdb10..852c491 100644
--- a/include/error_monitors/err_pin_timeout_monitor.hpp
+++ b/include/error_monitors/err_pin_timeout_monitor.hpp
@@ -57,10 +57,7 @@
     {
         std::string msg = "ERR" + std::to_string(errPin) + " Timeout";
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     void errPinTimeoutLog(const int cpuNum)
@@ -68,10 +65,7 @@
         std::string msg = "ERR" + std::to_string(errPin) + " Timeout on CPU " +
                           std::to_string(cpuNum + 1);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     void startPolling() override
diff --git a/include/error_monitors/ierr_monitor.hpp b/include/error_monitors/ierr_monitor.hpp
index ebd5f20..41453c0 100644
--- a/include/error_monitors/ierr_monitor.hpp
+++ b/include/error_monitors/ierr_monitor.hpp
@@ -54,29 +54,21 @@
 
     void cpuIERRLog()
     {
-        sd_journal_send("MESSAGE=HostError: IERR", "PRIORITY=%i", LOG_INFO,
-                        "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.CPUError",
-                        "REDFISH_MESSAGE_ARGS=%s", "IERR", NULL);
+        log_message(LOG_INFO, "IERR", "OpenBMC.0.1.CPUError", "IERR");
     }
 
     void cpuIERRLog(const int cpuNum)
     {
         std::string msg = "IERR on CPU " + std::to_string(cpuNum + 1);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     void cpuIERRLog(const int cpuNum, const std::string& type)
     {
         std::string msg = type + " IERR on CPU " + std::to_string(cpuNum + 1);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
     bool checkIERRCPUs()
diff --git a/include/error_monitors/mcerr_monitor.hpp b/include/error_monitors/mcerr_monitor.hpp
index 8a1f9e6..98eca7e 100644
--- a/include/error_monitors/mcerr_monitor.hpp
+++ b/include/error_monitors/mcerr_monitor.hpp
@@ -29,10 +29,7 @@
     {
         std::string msg = "MCERR on CPU " + std::to_string(cpuNum);
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        msg.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.CPUError", msg);
     }
 
   public:
diff --git a/include/error_monitors/mem_thermtrip_monitor.hpp b/include/error_monitors/mem_thermtrip_monitor.hpp
index ee46463..52c091b 100644
--- a/include/error_monitors/mem_thermtrip_monitor.hpp
+++ b/include/error_monitors/mem_thermtrip_monitor.hpp
@@ -34,10 +34,7 @@
         std::string cpuNumber = "CPU " + std::to_string(cpuNum);
         std::string msg = cpuNumber + " Memory Thermal trip.";
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_ERR, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.MemoryThermTrip",
-                        "REDFISH_MESSAGE_ARGS=%s", cpuNumber.c_str(), NULL);
+        log_message(LOG_ERR, msg, "OpenBMC.0.1.MemoryThermTrip", cpuNumber);
         assertInterface->set_property("Asserted", true);
     }
 
diff --git a/include/error_monitors/memhot_monitor.hpp b/include/error_monitors/memhot_monitor.hpp
index 90beaaa..824895f 100644
--- a/include/error_monitors/memhot_monitor.hpp
+++ b/include/error_monitors/memhot_monitor.hpp
@@ -34,10 +34,8 @@
         std::string msg = cpuNumber + " Memhot.";
         std::string redfishMsgArgs = cpuNumber + " memory";
 
-        sd_journal_send(
-            "MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i", LOG_ERR,
-            "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.ComponentOverTemperature",
-            "REDFISH_MESSAGE_ARGS=%s", redfishMsgArgs.c_str(), NULL);
+        log_message(LOG_ERR, msg, "OpenBMC.0.1.ComponentOverTemperature",
+                    redfishMsgArgs);
     }
 
   public:
diff --git a/include/error_monitors/pch_thermtrip_monitor.hpp b/include/error_monitors/pch_thermtrip_monitor.hpp
index a5b395d..9a8f02d 100644
--- a/include/error_monitors/pch_thermtrip_monitor.hpp
+++ b/include/error_monitors/pch_thermtrip_monitor.hpp
@@ -33,9 +33,8 @@
 
     void logEvent() override
     {
-        sd_journal_send("MESSAGE=HostError: SSB thermal trip", "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.SsbThermalTrip", NULL);
+        log_message(LOG_INFO, "SSB thermal trip", "OpenBMC.0.1.SsbThermalTrip",
+                    "");
     }
 
     void assertHandler() override
diff --git a/include/error_monitors/prochot_monitor.hpp b/include/error_monitors/prochot_monitor.hpp
index 9ea6273..ad18aaa 100644
--- a/include/error_monitors/prochot_monitor.hpp
+++ b/include/error_monitors/prochot_monitor.hpp
@@ -34,10 +34,8 @@
         std::string msg = cpuNumber + " Prochot.";
         std::string redfishMsgArgs = cpuNumber;
 
-        sd_journal_send(
-            "MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i", LOG_ERR,
-            "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.ComponentOverTemperature",
-            "REDFISH_MESSAGE_ARGS=%s", redfishMsgArgs.c_str(), NULL);
+        log_message(LOG_ERR, msg, "OpenBMC.0.1.ComponentOverTemperature",
+                    redfishMsgArgs);
     }
 
   public:
diff --git a/include/error_monitors/smi_monitor.hpp b/include/error_monitors/smi_monitor.hpp
index 921f95d..f8b1787 100644
--- a/include/error_monitors/smi_monitor.hpp
+++ b/include/error_monitors/smi_monitor.hpp
@@ -37,10 +37,8 @@
 
     void logEvent() override
     {
-        sd_journal_send("MESSAGE=HostError: SMI Timeout", "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
-                        "SMI Timeout", NULL);
+        log_message(LOG_INFO, "SMI Timeout", "OpenBMC.0.1.CPUError",
+                    "SMI Timeout");
     }
 
     void assertHandler() override
diff --git a/include/error_monitors/vr_hot_monitor.hpp b/include/error_monitors/vr_hot_monitor.hpp
index fe8aa39..3fecffd 100644
--- a/include/error_monitors/vr_hot_monitor.hpp
+++ b/include/error_monitors/vr_hot_monitor.hpp
@@ -32,10 +32,8 @@
     {
         std::string msg = vrName + " Voltage Regulator Overheated.";
 
-        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                        "OpenBMC.0.1.VoltageRegulatorOverheated",
-                        "REDFISH_MESSAGE_ARGS=%s", vrName.c_str(), NULL);
+        log_message(LOG_INFO, msg, "OpenBMC.0.1.VoltageRegulatorOverheated",
+                    vrName);
     }
 
   public:
diff --git a/meson.build b/meson.build
index 06035ff..5021056 100644
--- a/meson.build
+++ b/meson.build
@@ -20,6 +20,10 @@
   add_project_arguments('-DCRASHDUMP', language : 'cpp')
 endif
 
+if(get_option('send-to-logger').enabled())
+  add_project_arguments('-DSEND_TO_LOGGING_SERVICE', language : 'cpp')
+endif
+
 sdbusplus = dependency('sdbusplus')
 gpiodcxx = dependency('libgpiodcxx',
     default_options: ['bindings=cxx'],
@@ -46,12 +50,15 @@
   language : 'cpp'
 )
 
+phosphor_dbus_interfaces = dependency('phosphor-dbus-interfaces')
+
 bindir = get_option('prefix') + '/' + get_option('bindir')
 
 deps = [
   boost,
   gpiodcxx,
   sdbusplus,
+  phosphor_dbus_interfaces,
 ]
 
 if(get_option('libpeci').enabled())
diff --git a/meson.options b/meson.options
index 2e05965..a8637ae 100644
--- a/meson.options
+++ b/meson.options
@@ -18,3 +18,10 @@
     value: 'disabled',
     description: 'Enable unit tests',
 )
+
+option(
+    'send-to-logger',
+    type: 'feature',
+    value: 'disabled',
+    description: 'Use D-Bus Logging interface for reporting'
+)
diff --git a/subprojects/phosphor-dbus-interfaces.wrap b/subprojects/phosphor-dbus-interfaces.wrap
new file mode 100644
index 0000000..935a8b2
--- /dev/null
+++ b/subprojects/phosphor-dbus-interfaces.wrap
@@ -0,0 +1,3 @@
+[wrap-git]
+url = https://github.com/openbmc/phosphor-dbus-interfaces.git
+revision = HEAD