Pass error to log into shutdown function

Changing the power off utility function to take a
template parameter that specifies the error to log
before the shutdown instead of hardcoding it in
the function.

Also change the 2 callers of this function to pass
in the error type.

Change-Id: Ic83d87d5000f881ed9832092be207e91adf81c0c
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/power-sequencer/runtime_monitor.cpp b/power-sequencer/runtime_monitor.cpp
index 966becc..9bfeaf0 100644
--- a/power-sequencer/runtime_monitor.cpp
+++ b/power-sequencer/runtime_monitor.cpp
@@ -13,8 +13,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <org/open_power/Witherspoon/Fault/error.hpp>
 #include <phosphor-logging/log.hpp>
 #include "config.h"
+#include "elog-errors.hpp"
 #include "runtime_monitor.hpp"
 #include "utility.hpp"
 
@@ -24,6 +26,7 @@
 {
 
 using namespace phosphor::logging;
+using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error;
 
 int RuntimeMonitor::run()
 {
@@ -49,7 +52,7 @@
         //power, so it will be killed by systemd sometime shortly
         //after this power off is issued.
 
-        util::powerOff(bus);
+        util::powerOff<Shutdown>(bus);
     }
     catch (std::exception& e)
     {
diff --git a/power-sequencer/ucd90160.cpp b/power-sequencer/ucd90160.cpp
index 1f7f167..ac2caa5 100644
--- a/power-sequencer/ucd90160.cpp
+++ b/power-sequencer/ucd90160.cpp
@@ -398,7 +398,8 @@
 
     if (shutdown)
     {
-        util::powerOff(bus);
+        //Will be replaced with a GPU specific error in a future commit
+        util::powerOff<power_error::Shutdown>(bus);
     }
 
     return errorFound;
diff --git a/utility.cpp b/utility.cpp
index e54f839..ac614d4 100644
--- a/utility.cpp
+++ b/utility.cpp
@@ -13,8 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <org/open_power/Witherspoon/Fault/error.hpp>
-#include "elog-errors.hpp"
 #include "utility.hpp"
 
 namespace witherspoon
@@ -27,13 +25,8 @@
 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
-constexpr auto SYSTEMD_SERVICE   = "org.freedesktop.systemd1";
-constexpr auto SYSTEMD_ROOT      = "/org/freedesktop/systemd1";
-constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
-constexpr auto POWEROFF_TARGET   = "obmc-chassis-hard-poweroff@0.target";
 
 using namespace phosphor::logging;
-using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error;
 
 
 std::string getService(const std::string& path,
@@ -74,24 +67,6 @@
     return response.begin()->first;
 }
 
-
-void powerOff(sdbusplus::bus::bus& bus)
-{
-    log<level::INFO>("Powering off due to a power fault");
-    report<Shutdown>();
-
-    auto method = bus.new_method_call(SYSTEMD_SERVICE,
-            SYSTEMD_ROOT,
-            SYSTEMD_INTERFACE,
-            "StartUnit");
-
-    method.append(POWEROFF_TARGET);
-    method.append("replace");
-
-    bus.call_noreply(method);
-}
-
-
 }
 }
 }
diff --git a/utility.hpp b/utility.hpp
index fdae252..0a9bdb4 100644
--- a/utility.hpp
+++ b/utility.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
 #include <sdbusplus/bus.hpp>
 #include <string>
 
@@ -11,6 +12,10 @@
 namespace util
 {
 
+constexpr auto SYSTEMD_SERVICE   = "org.freedesktop.systemd1";
+constexpr auto SYSTEMD_ROOT      = "/org/freedesktop/systemd1";
+constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+constexpr auto POWEROFF_TARGET   = "obmc-chassis-hard-poweroff@0.target";
 constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
 
 /**
@@ -48,9 +53,9 @@
     sdbusplus::message::variant<T> property;
 
     auto method = bus.new_method_call(service.c_str(),
-            path.c_str(),
-            PROPERTY_INTF,
-            "Get");
+                                      path.c_str(),
+                                      PROPERTY_INTF,
+                                      "Get");
 
     method.append(interface, propertyName);
 
@@ -59,9 +64,9 @@
     {
         using namespace phosphor::logging;
         log<level::ERR>("Error in property get call",
-                entry("PATH=%s", path.c_str()),
-                entry("PROPERTY=%s", propertyName.c_str()));
-        //
+                        entry("PATH=%s", path.c_str()),
+                        entry("PROPERTY=%s", propertyName.c_str()));
+
         // TODO openbmc/openbmc#851 - Once available, throw returned error
         throw std::runtime_error("Error in property get call");
     }
@@ -71,12 +76,26 @@
 }
 
 /**
- * Powers off the system and logs an error
- * saying it was due to a power fault.
+ * Logs an error and powers off the system.
  *
+ * @tparam T - error that will be logged before the power off
  * @param[in] bus - D-Bus object
  */
-void powerOff(sdbusplus::bus::bus& bus);
+template<typename T>
+void powerOff(sdbusplus::bus::bus& bus)
+{
+    phosphor::logging::report<T>();
+
+    auto method = bus.new_method_call(SYSTEMD_SERVICE,
+                                      SYSTEMD_ROOT,
+                                      SYSTEMD_INTERFACE,
+                                      "StartUnit");
+
+    method.append(POWEROFF_TARGET);
+    method.append("replace");
+
+    bus.call_noreply(method);
+}
 
 }
 }