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/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);
+}
 
 }
 }