regulators: Modify close() to use services

Modify the Device::close() method in the Device class to have a new
first parameter: Services& services.

Modify Device::close() to log messages using the new Journal
interface.

Modify the Chassis::closeDevices() method in the Chassis class to
have a new first parameter: Services& services.

Modify Chassis::closeDevices() to log messages using the new
Journal interface.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I2a07417d6f7470685f2c27c878ef7936e9f1aa8a
diff --git a/phosphor-regulators/src/chassis.cpp b/phosphor-regulators/src/chassis.cpp
index eb7f496..85e8275 100644
--- a/phosphor-regulators/src/chassis.cpp
+++ b/phosphor-regulators/src/chassis.cpp
@@ -16,7 +16,6 @@
 
 #include "chassis.hpp"
 
-#include "journal.hpp"
 #include "system.hpp"
 
 namespace phosphor::power::regulators
@@ -31,15 +30,16 @@
     }
 }
 
-void Chassis::closeDevices()
+void Chassis::closeDevices(Services& services)
 {
     // Log debug message in journal
-    journal::logDebug("Closing devices in chassis " + std::to_string(number));
+    services.getJournal().logDebug("Closing devices in chassis " +
+                                   std::to_string(number));
 
     // Close devices
     for (std::unique_ptr<Device>& device : devices)
     {
-        device->close();
+        device->close(services);
     }
 }
 
diff --git a/phosphor-regulators/src/chassis.hpp b/phosphor-regulators/src/chassis.hpp
index b61b79c..86d99f8 100644
--- a/phosphor-regulators/src/chassis.hpp
+++ b/phosphor-regulators/src/chassis.hpp
@@ -87,8 +87,10 @@
 
     /**
      * Close the devices within this chassis, if any.
+     *
+     * @param services system services like error logging and the journal
      */
-    void closeDevices();
+    void closeDevices(Services& services);
 
     /**
      * Configure the devices within this chassis, if any.
diff --git a/phosphor-regulators/src/device.cpp b/phosphor-regulators/src/device.cpp
index df73d1a..062ff27 100644
--- a/phosphor-regulators/src/device.cpp
+++ b/phosphor-regulators/src/device.cpp
@@ -18,7 +18,6 @@
 
 #include "chassis.hpp"
 #include "exception_utils.hpp"
-#include "journal.hpp"
 #include "system.hpp"
 
 #include <exception>
@@ -38,7 +37,7 @@
     }
 }
 
-void Device::close()
+void Device::close(Services& services)
 {
     try
     {
@@ -51,8 +50,8 @@
     catch (const std::exception& e)
     {
         // Log error messages in journal
-        exception_utils::log(e);
-        journal::logErr("Unable to close device " + id);
+        services.getJournal().logError(exception_utils::getMessages(e));
+        services.getJournal().logError("Unable to close device " + id);
 
         // TODO: Create error log entry
     }
diff --git a/phosphor-regulators/src/device.hpp b/phosphor-regulators/src/device.hpp
index d0466eb..ccdb30a 100644
--- a/phosphor-regulators/src/device.hpp
+++ b/phosphor-regulators/src/device.hpp
@@ -91,8 +91,10 @@
      *
      * Closes any interfaces that are open to this device.  Releases any other
      * operating system resources associated with this device.
+     *
+     * @param services system services like error logging and the journal
      */
-    void close();
+    void close(Services& services);
 
     /**
      * Configure this device.
diff --git a/phosphor-regulators/src/manager.cpp b/phosphor-regulators/src/manager.cpp
index bb0e3a8..1bffe24 100644
--- a/phosphor-regulators/src/manager.cpp
+++ b/phosphor-regulators/src/manager.cpp
@@ -123,7 +123,7 @@
             // normally disabled because the system is being powered off.  The
             // devices should be closed in case hardware is removed or replaced
             // while the system is at standby.
-            system->closeDevices();
+            system->closeDevices(services);
         }
     }
 }
diff --git a/phosphor-regulators/src/system.cpp b/phosphor-regulators/src/system.cpp
index 8f3412c..a032935 100644
--- a/phosphor-regulators/src/system.cpp
+++ b/phosphor-regulators/src/system.cpp
@@ -34,12 +34,12 @@
     }
 }
 
-void System::closeDevices()
+void System::closeDevices(Services& services)
 {
     // Close devices in each chassis
     for (std::unique_ptr<Chassis>& oneChassis : chassis)
     {
-        oneChassis->closeDevices();
+        oneChassis->closeDevices(services);
     }
 }
 
diff --git a/phosphor-regulators/src/system.hpp b/phosphor-regulators/src/system.hpp
index 16c4ee0..2ef14b8 100644
--- a/phosphor-regulators/src/system.hpp
+++ b/phosphor-regulators/src/system.hpp
@@ -62,8 +62,10 @@
 
     /**
      * Close the regulator devices in the system.
+     *
+     * @param services system services like error logging and the journal
      */
-    void closeDevices();
+    void closeDevices(Services& services);
 
     /**
      * Configure the regulator devices in the system.
diff --git a/phosphor-regulators/test/chassis_tests.cpp b/phosphor-regulators/test/chassis_tests.cpp
index 08304d3..3844ca4 100644
--- a/phosphor-regulators/test/chassis_tests.cpp
+++ b/phosphor-regulators/test/chassis_tests.cpp
@@ -19,7 +19,6 @@
 #include "device.hpp"
 #include "i2c_interface.hpp"
 #include "id_map.hpp"
-#include "journal.hpp"
 #include "mock_journal.hpp"
 #include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
@@ -116,23 +115,27 @@
 {
     // Test where no devices were specified in constructor
     {
+        // Create mock services.  Expect logDebug() to be called.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug("Closing devices in chassis 2")).Times(1);
+
         // Create Chassis
         Chassis chassis{2};
 
         // Call closeDevices()
-        journal::clear();
-        chassis.closeDevices();
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
-        EXPECT_EQ(journal::getInfoMessages().size(), 0);
-        std::vector<std::string> expectedDebugMessages{
-            "Closing devices in chassis 2"};
-        EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
+        chassis.closeDevices(services);
     }
 
     // Test where devices were specified in constructor
     {
         std::vector<std::unique_ptr<Device>> devices{};
 
+        // Create mock services.  Expect logDebug() to be called.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug("Closing devices in chassis 1")).Times(1);
+
         // Create Device vdd0_reg
         {
             // Create mock I2CInterface: isOpen() and close() should be called
@@ -167,13 +170,7 @@
         Chassis chassis{1, std::move(devices)};
 
         // Call closeDevices()
-        journal::clear();
-        chassis.closeDevices();
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
-        EXPECT_EQ(journal::getInfoMessages().size(), 0);
-        std::vector<std::string> expectedDebugMessages{
-            "Closing devices in chassis 1"};
-        EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
+        chassis.closeDevices(services);
     }
 }
 
diff --git a/phosphor-regulators/test/device_tests.cpp b/phosphor-regulators/test/device_tests.cpp
index 62cf97e..19271d4 100644
--- a/phosphor-regulators/test/device_tests.cpp
+++ b/phosphor-regulators/test/device_tests.cpp
@@ -19,7 +19,6 @@
 #include "device.hpp"
 #include "i2c_interface.hpp"
 #include "id_map.hpp"
-#include "journal.hpp"
 #include "mock_action.hpp"
 #include "mock_journal.hpp"
 #include "mock_services.hpp"
@@ -154,14 +153,19 @@
         EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false));
         EXPECT_CALL(*i2cInterface, close).Times(0);
 
+        // Create mock services.  No logError should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
+            .Times(0);
+
         // Create Device
         Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
                       std::move(i2cInterface)};
 
         // Close Device
-        journal::clear();
-        device.close();
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        device.close(services);
     }
 
     // Test where works: I2C interface is open
@@ -172,14 +176,19 @@
         EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
         EXPECT_CALL(*i2cInterface, close).Times(1);
 
+        // Create mock services.  No logError should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
+            .Times(0);
+
         // Create Device
         Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
                       std::move(i2cInterface)};
 
         // Close Device
-        journal::clear();
-        device.close();
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        device.close(services);
     }
 
     // Test where fails: closing I2C interface fails
@@ -193,17 +202,21 @@
             .WillOnce(Throw(
                 i2c::I2CException{"Failed to close", "/dev/i2c-1", 0x70}));
 
+        // Create mock services.  Expect logError() to be called.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        std::vector<std::string> expectedErrMessagesException{
+            "I2CException: Failed to close: bus /dev/i2c-1, addr 0x70"};
+        EXPECT_CALL(journal, logError("Unable to close device vdd_reg"))
+            .Times(1);
+        EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
+
         // Create Device
         Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
                       std::move(i2cInterface)};
 
         // Close Device
-        journal::clear();
-        device.close();
-        std::vector<std::string> expectedErrMessages{
-            "I2CException: Failed to close: bus /dev/i2c-1, addr 0x70",
-            "Unable to close device vdd_reg"};
-        EXPECT_EQ(journal::getErrMessages(), expectedErrMessages);
+        device.close(services);
     }
 }
 
diff --git a/phosphor-regulators/test/system_tests.cpp b/phosphor-regulators/test/system_tests.cpp
index 5d1d720..809d2d4 100644
--- a/phosphor-regulators/test/system_tests.cpp
+++ b/phosphor-regulators/test/system_tests.cpp
@@ -71,6 +71,14 @@
     // Specify an empty rules vector
     std::vector<std::unique_ptr<Rule>> rules{};
 
+    // Create mock services.  Expect logDebug() to be called.
+    MockServices services{};
+    MockJournal& journal = services.getMockJournal();
+    EXPECT_CALL(journal, logDebug("Closing devices in chassis 1")).Times(1);
+    EXPECT_CALL(journal, logDebug("Closing devices in chassis 3")).Times(1);
+    EXPECT_CALL(journal, logInfo(A<const std::string&>())).Times(0);
+    EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
     // Create Chassis
     std::vector<std::unique_ptr<Chassis>> chassis{};
     chassis.emplace_back(std::make_unique<Chassis>(1));
@@ -80,13 +88,7 @@
     System system{std::move(rules), std::move(chassis)};
 
     // Call closeDevices()
-    journal::clear();
-    system.closeDevices();
-    EXPECT_EQ(journal::getErrMessages().size(), 0);
-    EXPECT_EQ(journal::getInfoMessages().size(), 0);
-    std::vector<std::string> expectedDebugMessages{
-        "Closing devices in chassis 1", "Closing devices in chassis 3"};
-    EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
+    system.closeDevices(services);
 }
 
 TEST(SystemTests, Configure)