regulators: Modify monitorSensors() to use Services

Modify the monitorSensors() method in the System, Chassis, Device, and
Rail classes to have a new first parameter: Services& services.

Modify SensorMonitoring::execute() to log messages using the new
Journal interface.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I05820cfa94bc3d15dee7a1714a3d253720aa7f92
diff --git a/phosphor-regulators/src/chassis.cpp b/phosphor-regulators/src/chassis.cpp
index 4a016e6..eb7f496 100644
--- a/phosphor-regulators/src/chassis.cpp
+++ b/phosphor-regulators/src/chassis.cpp
@@ -56,12 +56,12 @@
     }
 }
 
-void Chassis::monitorSensors(System& system)
+void Chassis::monitorSensors(Services& services, System& system)
 {
     // Monitor sensors in each device
     for (std::unique_ptr<Device>& device : devices)
     {
-        device->monitorSensors(system, *this);
+        device->monitorSensors(services, system, *this);
     }
 }
 
diff --git a/phosphor-regulators/src/chassis.hpp b/phosphor-regulators/src/chassis.hpp
index 3addd5e..b61b79c 100644
--- a/phosphor-regulators/src/chassis.hpp
+++ b/phosphor-regulators/src/chassis.hpp
@@ -130,9 +130,10 @@
      *
      * This method should be called once per second.
      *
+     * @param services system services like error logging and the journal
      * @param system system that contains the chassis
      */
-    void monitorSensors(System& system);
+    void monitorSensors(Services& services, System& system);
 
   private:
     /**
diff --git a/phosphor-regulators/src/device.cpp b/phosphor-regulators/src/device.cpp
index f085afe..df73d1a 100644
--- a/phosphor-regulators/src/device.cpp
+++ b/phosphor-regulators/src/device.cpp
@@ -73,13 +73,14 @@
     }
 }
 
-void Device::monitorSensors(System& system, Chassis& chassis)
+void Device::monitorSensors(Services& services, System& system,
+                            Chassis& chassis)
 {
 
     // Monitor sensors in each rail
     for (std::unique_ptr<Rail>& rail : rails)
     {
-        rail->monitorSensors(system, chassis, *this);
+        rail->monitorSensors(services, system, chassis, *this);
     }
 }
 
diff --git a/phosphor-regulators/src/device.hpp b/phosphor-regulators/src/device.hpp
index cb3a3d9..d0466eb 100644
--- a/phosphor-regulators/src/device.hpp
+++ b/phosphor-regulators/src/device.hpp
@@ -192,10 +192,11 @@
      *
      * This method should be called once per second.
      *
+     * @param services system services like error logging and the journal
      * @param system system that contains the chassis
      * @param chassis chassis that contains the device
      */
-    void monitorSensors(System& system, Chassis& chassis);
+    void monitorSensors(Services& services, System& system, Chassis& chassis);
 
   private:
     /**
diff --git a/phosphor-regulators/src/rail.cpp b/phosphor-regulators/src/rail.cpp
index 3739ef0..ee24427 100644
--- a/phosphor-regulators/src/rail.cpp
+++ b/phosphor-regulators/src/rail.cpp
@@ -33,12 +33,13 @@
     }
 }
 
-void Rail::monitorSensors(System& system, Chassis& chassis, Device& device)
+void Rail::monitorSensors(Services& services, System& system, Chassis& chassis,
+                          Device& device)
 {
     // If sensor monitoring is defined for this rail, read the sensors.
     if (sensorMonitoring)
     {
-        sensorMonitoring->execute(system, chassis, device, *this);
+        sensorMonitoring->execute(services, system, chassis, device, *this);
     }
 }
 
diff --git a/phosphor-regulators/src/rail.hpp b/phosphor-regulators/src/rail.hpp
index 9caafbe..ac55060 100644
--- a/phosphor-regulators/src/rail.hpp
+++ b/phosphor-regulators/src/rail.hpp
@@ -112,11 +112,13 @@
      *
      * This method should be called once per second.
      *
+     * @param services system services like error logging and the journal
      * @param system system that contains the chassis
      * @param chassis chassis that contains the device
      * @param device device that contains this rail
      */
-    void monitorSensors(System& system, Chassis& chassis, Device& device);
+    void monitorSensors(Services& services, System& system, Chassis& chassis,
+                        Device& device);
 
     /**
      * Returns the sensor monitoring for this rail, if any.
diff --git a/phosphor-regulators/src/sensor_monitoring.cpp b/phosphor-regulators/src/sensor_monitoring.cpp
index 6135b50..07ae635 100644
--- a/phosphor-regulators/src/sensor_monitoring.cpp
+++ b/phosphor-regulators/src/sensor_monitoring.cpp
@@ -21,7 +21,6 @@
 #include "chassis.hpp"
 #include "device.hpp"
 #include "exception_utils.hpp"
-#include "journal.hpp"
 #include "rail.hpp"
 #include "system.hpp"
 
@@ -30,8 +29,8 @@
 namespace phosphor::power::regulators
 {
 
-void SensorMonitoring::execute(System& system, Chassis& /*chassis*/,
-                               Device& device, Rail& rail)
+void SensorMonitoring::execute(Services& services, System& system,
+                               Chassis& /*chassis*/, Device& device, Rail& rail)
 {
     try
     {
@@ -44,8 +43,9 @@
     catch (const std::exception& e)
     {
         // Log error messages in journal
-        exception_utils::log(e);
-        journal::logErr("Unable to monitor sensors for rail " + rail.getID());
+        services.getJournal().logError(exception_utils::getMessages(e));
+        services.getJournal().logError("Unable to monitor sensors for rail " +
+                                       rail.getID());
 
         // TODO: Create error log entry
     }
diff --git a/phosphor-regulators/src/sensor_monitoring.hpp b/phosphor-regulators/src/sensor_monitoring.hpp
index f3491cd..211c534 100644
--- a/phosphor-regulators/src/sensor_monitoring.hpp
+++ b/phosphor-regulators/src/sensor_monitoring.hpp
@@ -16,6 +16,7 @@
 #pragma once
 
 #include "action.hpp"
+#include "services.hpp"
 
 #include <memory>
 #include <utility>
@@ -68,12 +69,14 @@
     /**
      * Executes the actions to read the sensors for a rail.
      *
+     * @param services system services like error logging and the journal
      * @param system system that contains the chassis
      * @param chassis chassis that contains the device
      * @param device device that contains the rail
      * @param rail rail associated with the sensors
      */
-    void execute(System& system, Chassis& chassis, Device& device, Rail& rail);
+    void execute(Services& services, System& system, Chassis& chassis,
+                 Device& device, Rail& rail);
 
     /**
      * Returns the actions that read the sensors for a rail.
diff --git a/phosphor-regulators/src/system.cpp b/phosphor-regulators/src/system.cpp
index d6be768..8f3412c 100644
--- a/phosphor-regulators/src/system.cpp
+++ b/phosphor-regulators/src/system.cpp
@@ -52,12 +52,12 @@
     }
 }
 
-void System::monitorSensors()
+void System::monitorSensors(Services& services)
 {
     // Monitor sensors in each chassis
     for (std::unique_ptr<Chassis>& oneChassis : chassis)
     {
-        oneChassis->monitorSensors(*this);
+        oneChassis->monitorSensors(services, *this);
     }
 }
 
diff --git a/phosphor-regulators/src/system.hpp b/phosphor-regulators/src/system.hpp
index 3ffeb5b..16c4ee0 100644
--- a/phosphor-regulators/src/system.hpp
+++ b/phosphor-regulators/src/system.hpp
@@ -113,8 +113,10 @@
      * any.
      *
      * This method should be called once per second.
+     *
+     * @param services system services like error logging and the journal
      */
-    void monitorSensors();
+    void monitorSensors(Services& services);
 
   private:
     /**
diff --git a/phosphor-regulators/test/chassis_tests.cpp b/phosphor-regulators/test/chassis_tests.cpp
index 639dc33..08304d3 100644
--- a/phosphor-regulators/test/chassis_tests.cpp
+++ b/phosphor-regulators/test/chassis_tests.cpp
@@ -301,6 +301,12 @@
 {
     // Test where no devices were specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create Chassis
         std::vector<std::unique_ptr<Device>> devices{};
         std::unique_ptr<Chassis> chassis =
@@ -314,14 +320,17 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors().  Should do nothing.
-        journal::clear();
-        chassisPtr->monitorSensors(system);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        chassisPtr->monitorSensors(services, system);
     }
 
     // Test where devices were specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         std::vector<std::unique_ptr<Device>> devices{};
 
         // Create PMBusReadSensorAction
@@ -375,9 +384,6 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors()
-        journal::clear();
-        chassisPtr->monitorSensors(system);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        chassisPtr->monitorSensors(services, system);
     }
 }
diff --git a/phosphor-regulators/test/device_tests.cpp b/phosphor-regulators/test/device_tests.cpp
index 690a510..62cf97e 100644
--- a/phosphor-regulators/test/device_tests.cpp
+++ b/phosphor-regulators/test/device_tests.cpp
@@ -455,6 +455,12 @@
 {
     // Test where Rails were not specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create mock I2CInterface.  A two-byte read should NOT occur.
         std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
             std::make_unique<i2c::MockedI2CInterface>();
@@ -479,15 +485,18 @@
         chassisVec.emplace_back(std::move(chassis));
         System system{std::move(rules), std::move(chassisVec)};
 
-        // Call monitorSensors().  Should do nothing.
-        journal::clear();
-        devicePtr->monitorSensors(system, *chassisPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        // Call monitorSensors().
+        devicePtr->monitorSensors(services, system, *chassisPtr);
     }
 
     // Test where Rails were specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         std::vector<std::unique_ptr<Rail>> rails{};
 
         // Create PMBusReadSensorAction
@@ -542,9 +551,6 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors().
-        journal::clear();
-        devicePtr->monitorSensors(system, *chassisPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        devicePtr->monitorSensors(services, system, *chassisPtr);
     }
 }
diff --git a/phosphor-regulators/test/rail_tests.cpp b/phosphor-regulators/test/rail_tests.cpp
index 190e48e..d5f8db2 100644
--- a/phosphor-regulators/test/rail_tests.cpp
+++ b/phosphor-regulators/test/rail_tests.cpp
@@ -18,7 +18,6 @@
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_interface.hpp"
-#include "journal.hpp"
 #include "mock_action.hpp"
 #include "mock_journal.hpp"
 #include "mock_services.hpp"
@@ -217,6 +216,12 @@
 {
     // Test where SensorMonitoring was not specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create mock I2CInterface.  A two-byte read should NOT occur.
         std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
             std::make_unique<i2c::MockedI2CInterface>();
@@ -250,15 +255,18 @@
         chassisVec.emplace_back(std::move(chassis));
         System system{std::move(rules), std::move(chassisVec)};
 
-        // Call monitorSensors().  Should do nothing.
-        journal::clear();
-        railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        // Call monitorSensors().
+        railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr);
     }
 
     // Test where SensorMonitoring was specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create PMBusReadSensorAction
         pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
         uint8_t command = 0x8C;
@@ -313,10 +321,7 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors().
-        journal::clear();
-        railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr);
     }
 }
 
diff --git a/phosphor-regulators/test/sensor_monitoring_tests.cpp b/phosphor-regulators/test/sensor_monitoring_tests.cpp
index 79c3b92..8f111eb 100644
--- a/phosphor-regulators/test/sensor_monitoring_tests.cpp
+++ b/phosphor-regulators/test/sensor_monitoring_tests.cpp
@@ -18,9 +18,9 @@
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_interface.hpp"
-#include "journal.hpp"
 #include "mock_action.hpp"
 #include "mock_journal.hpp"
+#include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
 #include "pmbus_read_sensor_action.hpp"
 #include "pmbus_utils.hpp"
@@ -60,6 +60,12 @@
 {
     // Test where works
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create PMBusReadSensorAction
         pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
         uint8_t command = 0x8C;
@@ -115,14 +121,26 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Execute sensorMonitoring
-        journal::clear();
-        sensorMonitoringPtr->execute(system, *chassisPtr, *devicePtr, *railPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
+                                     *railPtr);
     }
 
     // Test where fails
     {
+        // Create mock services.  Expect logError() to be called.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        std::vector<std::string> expectedErrMessagesException{
+            "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
+            "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
+            "format: linear_11 }"};
+
+        EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
+        EXPECT_CALL(journal,
+                    logError("Unable to monitor sensors for rail vio2"))
+            .Times(1);
+
         // Create PMBusReadSensorAction
         pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
         uint8_t command = 0x8C;
@@ -180,15 +198,8 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Execute sensorMonitoring
-        journal::clear();
-        sensorMonitoringPtr->execute(system, *chassisPtr, *devicePtr, *railPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        std::vector<std::string> expectedErrMessages{
-            "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
-            "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
-            "format: linear_11 }",
-            "Unable to monitor sensors for rail vio2"};
-        EXPECT_EQ(journal::getErrMessages(), expectedErrMessages);
+        sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
+                                     *railPtr);
     }
 }
 
diff --git a/phosphor-regulators/test/system_tests.cpp b/phosphor-regulators/test/system_tests.cpp
index 7adc5d8..5d1d720 100644
--- a/phosphor-regulators/test/system_tests.cpp
+++ b/phosphor-regulators/test/system_tests.cpp
@@ -16,7 +16,6 @@
 #include "chassis.hpp"
 #include "device.hpp"
 #include "id_map.hpp"
-#include "journal.hpp"
 #include "mock_journal.hpp"
 #include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
@@ -201,6 +200,12 @@
 
 TEST(SystemTests, MonitorSensors)
 {
+    // Create mock services.  No logging should occur.
+    MockServices services{};
+    MockJournal& journal = services.getMockJournal();
+    EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+    EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
     // Create PMBusReadSensorAction
     pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
     uint8_t command = 0x8C;
@@ -252,8 +257,5 @@
     System system{std::move(rules), std::move(chassisVec)};
 
     // Call monitorSensors()
-    journal::clear();
-    system.monitorSensors();
-    EXPECT_EQ(journal::getDebugMessages().size(), 0);
-    EXPECT_EQ(journal::getErrMessages().size(), 0);
+    system.monitorSensors(services);
 }