src: Convert logging to lg2

Change-Id: I28bf669e5139062f0f6145277db65d6821d48e78
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/src/sensor.hpp b/src/sensor.hpp
index 2eab853..07d70ec 100644
--- a/src/sensor.hpp
+++ b/src/sensor.hpp
@@ -6,6 +6,7 @@
 #include "Thresholds.hpp"
 #include "Utils.hpp"
 
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 #include <sdbusplus/exception.hpp>
@@ -16,7 +17,6 @@
 #include <cstddef>
 #include <cstdlib>
 #include <functional>
-#include <iostream>
 #include <limits>
 #include <memory>
 #include <string>
@@ -136,7 +136,7 @@
         size_t index = static_cast<size_t>(lev);
         if (index >= thresholdInterfaces.size())
         {
-            std::cout << "Unknown threshold level \n";
+            lg2::info("Unknown threshold level");
             return nullptr;
         }
         std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
@@ -162,9 +162,10 @@
         // Show constants if first reading (even if unsuccessful)
         if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0))
         {
-            std::cerr << "Sensor " << name << ": Configuration min=" << minValue
-                      << ", max=" << maxValue << ", type=" << configInterface
-                      << ", path=" << configurationPath << "\n";
+            lg2::info(
+                "Sensor name: {NAME}, min: {MIN}, max: {MAX}, type: {TYPE}, path: {PATH}",
+                "NAME", name, "MIN", minValue, "MAX", maxValue, "TYPE",
+                configInterface, "PATH", configurationPath);
         }
 
         // Sensors can use "nan" to indicate unavailable reading
@@ -173,12 +174,12 @@
             // Only show this if beginning a new streak
             if (inst.numStreakMisses == 0)
             {
-                std::cerr << "Sensor " << name
-                          << ": Missing reading, Reading counts good="
-                          << inst.numCollectsGood
-                          << ", miss=" << inst.numCollectsMiss
-                          << ", Prior good streak=" << inst.numStreakGreats
-                          << "\n";
+                lg2::warning(
+                    "Sensor name: {NAME}, Missing reading, Reading counts good= {NUM_COLLECTS_GOOD},"
+                    " miss= {NUM_COLLECTS_MISS}, Prior good streak= {NUM_STREAK_GREATS}",
+                    "NAME", name, "NUM_COLLECTS_GOOD", inst.numCollectsGood,
+                    "NUM_COLLECTS_MISS", inst.numCollectsMiss,
+                    "NUM_STREAK_GREATS", inst.numStreakGreats);
             }
 
             inst.numStreakGreats = 0;
@@ -191,18 +192,19 @@
         // Only show this if beginning a new streak and not the first time
         if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0))
         {
-            std::cerr << "Sensor " << name
-                      << ": Recovered reading, Reading counts good="
-                      << inst.numCollectsGood
-                      << ", miss=" << inst.numCollectsMiss
-                      << ", Prior miss streak=" << inst.numStreakMisses << "\n";
+            lg2::info(
+                "Sensor name: {NAME}, Recovered reading, Reading counts good= {NUM_COLLECTS_GOOD},"
+                " miss= {NUM_COLLECTS_MISS}, Prior good streak= {NUM_STREAK_GREATS}",
+                "NAME", name, "NUM_COLLECTS_GOOD", inst.numCollectsGood,
+                "NUM_COLLECTS_MISS", inst.numCollectsMiss, "NUM_STREAK_GREATS",
+                inst.numStreakGreats);
         }
 
         // Initialize min/max if the first successful reading
         if (inst.numCollectsGood == 0)
         {
-            std::cerr << "Sensor " << name << ": First reading=" << readValue
-                      << "\n";
+            lg2::info("Sensor name: {NAME}, First reading: {VALUE}", "NAME",
+                      name, "VALUE", readValue);
 
             inst.minCollected = readValue;
             inst.maxCollected = readValue;
@@ -215,16 +217,16 @@
         // Only provide subsequent output if new min/max established
         if (readValue < inst.minCollected)
         {
-            std::cerr << "Sensor " << name << ": Lowest reading=" << readValue
-                      << "\n";
+            lg2::info("Sensor name: {NAME}, Lowest reading: {VALUE}", "NAME",
+                      name, "VALUE", readValue);
 
             inst.minCollected = readValue;
         }
 
         if (readValue > inst.maxCollected)
         {
-            std::cerr << "Sensor " << name << ": Highest reading=" << readValue
-                      << "\n";
+            lg2::info("Sensor name: {NAME}, Highest reading: {VALUE}", "NAME",
+                      name, "VALUE", readValue);
 
             inst.maxCollected = readValue;
         }
@@ -293,7 +295,7 @@
 
             if (!iface)
             {
-                std::cout << "trying to set uninitialized interface\n";
+                lg2::info("trying to set uninitialized interface");
                 continue;
             }
 
@@ -331,7 +333,7 @@
         }
         if (!sensorInterface->initialize())
         {
-            std::cerr << "error initializing value interface\n";
+            lg2::error("error initializing value interface");
         }
 
         for (auto& thresIface : thresholdInterfaces)
@@ -340,7 +342,7 @@
             {
                 if (!thresIface->initialize(true))
                 {
-                    std::cerr << "Error initializing threshold interface \n";
+                    lg2::error("Error initializing threshold interface");
                 }
             }
         }
@@ -354,8 +356,8 @@
             valueMutabilityInterface->register_property("Mutable", true);
             if (!valueMutabilityInterface->initialize())
             {
-                std::cerr
-                    << "error initializing sensor value mutability interface\n";
+                lg2::error(
+                    "error initializing sensor value mutability interface");
                 valueMutabilityInterface = nullptr;
             }
         }
@@ -478,7 +480,7 @@
         errCount++;
         if (errCount == errorThreshold)
         {
-            std::cerr << "Sensor " << name << " reading error!\n";
+            lg2::error("Sensor name: {NAME}, reading error!", "NAME", name);
             markFunctional(false);
         }
     }
@@ -535,8 +537,8 @@
             if (interface &&
                 !(interface->set_property(dbusPropertyName, newValue)))
             {
-                std::cerr << "error setting property " << dbusPropertyName
-                          << " to " << newValue << "\n";
+                lg2::error("error setting property '{NAME}' to '{VALUE}'",
+                           "NAME", dbusPropertyName, "VALUE", newValue);
             }
         }
     }