Add logging for Fan Presence / Redundancy

Add logging and fix bug with shared_ptr usage. We
now use a std::optional * to the shared redundancy
object, so that it can 1. point to object / null and 2.
exist in config or not.

Tested: cat /var/log/redfish to see logs made by removing
and adding fans, and using sensor override to change
redundancy.

Change-Id: Ic4b319cf7484cbbd7ce7dbdf7556f48bebe11cb0
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/TachSensor.hpp b/include/TachSensor.hpp
index c6f255f..b160a60 100644
--- a/include/TachSensor.hpp
+++ b/include/TachSensor.hpp
@@ -1,5 +1,7 @@
 #pragma once
 
+#include <systemd/sd-journal.h>
+
 #include <Thresholds.hpp>
 #include <boost/container/flat_map.hpp>
 #include <boost/container/flat_set.hpp>
@@ -12,7 +14,7 @@
 
   public:
     PresenceSensor(const size_t index, bool inverted,
-                   boost::asio::io_service& io);
+                   boost::asio::io_service& io, const std::string& name);
     ~PresenceSensor();
 
     void monitorPresence(void);
@@ -24,8 +26,16 @@
     bool inverted;
     boost::asio::ip::tcp::socket inputDev;
     int fd;
+    std::string name;
 };
 
+namespace redundancy
+{
+constexpr const char* full = "Full";
+constexpr const char* degraded = "Degraded";
+constexpr const char* failed = "Failed";
+} // namespace redundancy
+
 class RedundancySensor
 {
   public:
@@ -38,6 +48,7 @@
 
   private:
     size_t count;
+    std::string state = redundancy::full;
     std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
     sdbusplus::asio::object_server& objectServer;
@@ -51,7 +62,7 @@
                sdbusplus::asio::object_server& objectServer,
                std::shared_ptr<sdbusplus::asio::connection>& conn,
                std::unique_ptr<PresenceSensor>&& presence,
-               const std::shared_ptr<RedundancySensor>& redundancy,
+               std::optional<RedundancySensor>* redundancy,
                boost::asio::io_service& io, const std::string& fanName,
                std::vector<thresholds::Threshold>&& thresholds,
                const std::string& sensorConfiguration,
@@ -60,7 +71,7 @@
 
   private:
     sdbusplus::asio::object_server& objServer;
-    std::shared_ptr<RedundancySensor> redundancy;
+    std::optional<RedundancySensor>* redundancy;
     std::unique_ptr<PresenceSensor> presence;
     std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
     boost::asio::posix::stream_descriptor inputDev;
@@ -71,3 +82,35 @@
     void handleResponse(const boost::system::error_code& err);
     void checkThresholds(void) override;
 };
+
+inline void logFanInserted(const std::string& device)
+{
+
+    sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
+                    "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanInserted",
+                    "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
+}
+
+inline void logFanRemoved(const std::string& device)
+{
+
+    sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
+                    "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRemoved",
+                    "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
+}
+
+inline void logFanRedundancyLost(void)
+{
+
+    sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
+                    "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRedundancyLost",
+                    NULL);
+}
+
+inline void logFanRedundancyRestored(void)
+{
+
+    sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
+                    "REDFISH_MESSAGE_ID=%s",
+                    "OpenBMC.0.1.FanRedundancyRegained", NULL);
+}