ipmb: Convert logging to lg2

Change-Id: Ic7984c1ad6b5553e2ea2c7a9d09619051d181bd1
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/src/ipmb/IpmbSDRSensor.cpp b/src/ipmb/IpmbSDRSensor.cpp
index c23ddea..1952586 100644
--- a/src/ipmb/IpmbSDRSensor.cpp
+++ b/src/ipmb/IpmbSDRSensor.cpp
@@ -1,12 +1,12 @@
 #include "IpmbSDRSensor.hpp"
 
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 
 #include <cmath>
 #include <cstddef>
 #include <cstdint>
 #include <functional>
-#include <iostream>
 #include <memory>
 #include <string>
 #include <utility>
@@ -35,8 +35,8 @@
     const int status = std::get<0>(response);
     if (status != 0)
     {
-        std::cerr << "Error reading from IPMB SDR for host " << hostIndex
-                  << "\n";
+        lg2::error("Error reading from IPMB SDR for host '{INDEX}'", "INDEX",
+                   hostIndex);
         return false;
     }
     return true;
@@ -68,9 +68,9 @@
 
             if (data.size() < sdrInfoDataSize)
             {
-                std::cerr
-                    << " IPMB Get SDR Repository Info data is empty for host "
-                    << self->hostIndex << "\n";
+                lg2::error(
+                    "IPMB Get SDR Repository Info data is empty for host '{INDEX}'",
+                    "INDEX", self->hostIndex);
                 return;
             }
 
@@ -111,9 +111,9 @@
 
             if (data.size() < sdrReserveDataSize)
             {
-                std::cerr
-                    << " IPMB SDR Reserve Repository data is empty for host "
-                    << self->hostIndex << "\n";
+                lg2::error(
+                    "IPMB SDR Reserve Repository data is empty for host '{INDEX}'",
+                    "INDEX", self->hostIndex);
                 return;
             }
             uint8_t resrvIDLSB = data[0];
@@ -157,8 +157,8 @@
 
             if (data.size() < sdrSensorDataSize)
             {
-                std::cerr << "IPMB SDR sensor data is empty for host "
-                          << self->hostIndex << "\n";
+                lg2::error("IPMB SDR sensor data is empty for host '{INDEX}'",
+                           "INDEX", self->hostIndex);
                 return;
             }
 
diff --git a/src/ipmb/IpmbSensor.cpp b/src/ipmb/IpmbSensor.cpp
index fc6cc7a..658c875 100644
--- a/src/ipmb/IpmbSensor.cpp
+++ b/src/ipmb/IpmbSensor.cpp
@@ -27,6 +27,7 @@
 #include <boost/asio/io_context.hpp>
 #include <boost/asio/steady_timer.hpp>
 #include <boost/container/flat_map.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 #include <sdbusplus/message.hpp>
@@ -37,9 +38,11 @@
 #include <chrono>
 #include <cstddef>
 #include <cstdint>
+#include <iomanip>
 #include <iostream>
 #include <limits>
 #include <memory>
+#include <sstream>
 #include <stdexcept>
 #include <string>
 #include <tuple>
@@ -141,8 +144,8 @@
     const int& status = std::get<0>(response);
     if (ec || (status != 0))
     {
-        std::cerr << "Error setting init command for device: " << self->name
-                  << "\n";
+        lg2::error("Error setting init command for device: '{NAME}'", "NAME",
+                   self->name);
     }
 }
 
@@ -315,7 +318,7 @@
             {
                 if (errCount == 0U)
                 {
-                    std::cerr << "Invalid data length returned\n";
+                    lg2::error("Invalid data length returned");
                 }
                 return false;
             }
@@ -330,7 +333,7 @@
             {
                 if (errCount == 0U)
                 {
-                    std::cerr << "Invalid data length returned\n";
+                    lg2::error("Invalid data length returned");
                 }
                 return false;
             }
@@ -373,7 +376,7 @@
             {
                 if (errCount == 0U)
                 {
-                    std::cerr << "Invalid data length returned\n";
+                    lg2::error("Invalid data length returned");
                 }
                 return false;
             }
@@ -388,7 +391,7 @@
             {
                 if (errCount == 0U)
                 {
-                    std::cerr << "Invalid data length returned\n";
+                    lg2::error("Invalid data length returned");
                 }
                 return false;
             }
@@ -402,7 +405,7 @@
             {
                 if (errCount == 0U)
                 {
-                    std::cerr << "Invalid data length returned\n";
+                    lg2::error("Invalid data length returned");
                 }
                 return false;
             }
@@ -432,12 +435,13 @@
     const std::vector<uint8_t>& data = std::get<5>(response);
     if constexpr (debug)
     {
-        std::cout << name << ": ";
-        for (size_t d : data)
+        std::ostringstream tempStream;
+        for (int d : data)
         {
-            std::cout << d << " ";
+            tempStream << std::setfill('0') << std::setw(2) << std::hex << d
+                       << " ";
         }
-        std::cout << "\n";
+        lg2::info("'{NAME}': '{DATA}'", "NAME", name, "DATA", tempStream.str());
     }
     if (data.empty())
     {
@@ -541,7 +545,7 @@
     }
     else
     {
-        std::cerr << "Invalid class " << sensorClass << "\n";
+        lg2::error("Invalid class '{SENSOR}'", "SENSOR", sensorClass);
         return false;
     }
     return true;
@@ -596,14 +600,14 @@
 {
     if (!dbusConnection)
     {
-        std::cerr << "Connection not created\n";
+        lg2::error("Connection not created");
         return;
     }
     dbusConnection->async_method_call(
         [&](boost::system::error_code ec, const ManagedObjectType& resp) {
             if (ec)
             {
-                std::cerr << "Error contacting entity manager\n";
+                lg2::error("Error contacting entity manager");
                 return;
             }
             for (const auto& [path, interfaces] : resp)
@@ -620,8 +624,8 @@
                     if (!parseThresholdsFromConfig(interfaces,
                                                    sensorThresholds))
                     {
-                        std::cerr
-                            << "error populating thresholds " << name << "\n";
+                        lg2::error("error populating thresholds '{NAME}'",
+                                   "NAME", name);
                     }
                     uint8_t deviceAddress =
                         loadVariant<uint8_t>(cfg, "Address");
@@ -645,8 +649,8 @@
                     {
                         ipmbBusIndex = std::visit(VariantToUnsignedIntVisitor(),
                                                   findBusType->second);
-                        std::cerr << "Ipmb Bus Index for " << name << " is "
-                                  << static_cast<int>(ipmbBusIndex) << "\n";
+                        lg2::error("Ipmb Bus Index for '{NAME}' is '{INDEX}'",
+                                   "NAME", name, "INDEX", ipmbBusIndex);
                     }
 
                     /* Default sensor type is "temperature" */
@@ -686,7 +690,7 @@
 {
     if (message.is_method_error())
     {
-        std::cerr << "interfacesRemoved callback method error\n";
+        lg2::error("interfacesRemoved callback method error");
         return;
     }
 
diff --git a/src/ipmb/IpmbSensorMain.cpp b/src/ipmb/IpmbSensorMain.cpp
index 5e38dd2..6533338 100644
--- a/src/ipmb/IpmbSensorMain.cpp
+++ b/src/ipmb/IpmbSensorMain.cpp
@@ -7,6 +7,7 @@
 #include <boost/asio/post.hpp>
 #include <boost/asio/steady_timer.hpp>
 #include <boost/container/flat_map.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 #include <sdbusplus/bus.hpp>
@@ -18,7 +19,6 @@
 #include <cstddef>
 #include <cstdint>
 #include <functional>
-#include <iostream>
 #include <memory>
 #include <string>
 #include <variant>
@@ -122,7 +122,7 @@
                 createSensors(io, objectServer, sensors, systemBus);
                 if (sensors.empty())
                 {
-                    std::cout << "Configuration not detected\n";
+                    lg2::info("Configuration not detected");
                 }
             });
         };