fan: Convert logging to lg2

Change-Id: I622dff33087039c460ce64ea06634a0e07555230
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/src/fan/FanMain.cpp b/src/fan/FanMain.cpp
index cbd5173..98e41a0 100644
--- a/src/fan/FanMain.cpp
+++ b/src/fan/FanMain.cpp
@@ -28,6 +28,7 @@
 #include <boost/asio/steady_timer.hpp>
 #include <boost/container/flat_map.hpp>
 #include <boost/container/flat_set.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 #include <sdbusplus/bus.hpp>
@@ -42,7 +43,6 @@
 #include <fstream>
 #include <functional>
 #include <ios>
-#include <iostream>
 #include <map>
 #include <memory>
 #include <optional>
@@ -100,7 +100,7 @@
 
     if (!compatibleStream)
     {
-        std::cerr << "Error opening " << compatiblePath << "\n";
+        lg2::error("Error opening '{PATH}'", "PATH", compatiblePath);
         return FanTypes::i2c;
     }
 
@@ -125,7 +125,7 @@
     std::fstream enableFile(filePath, std::ios::in | std::ios::out);
     if (!enableFile.good())
     {
-        std::cerr << "Error read/write " << filePath << "\n";
+        lg2::error("Error read/write '{PATH}'", "PATH", filePath);
         return;
     }
 
@@ -149,7 +149,7 @@
     if (!findFiles(std::filesystem::path("/sys/class/hwmon"), R"(pwm\d+)",
                    pwmfanPaths))
     {
-        std::cerr << "No PWMs are found!\n";
+        lg2::error("No PWMs are found!");
         return false;
     }
     for (const auto& path : pwmfanPaths)
@@ -160,8 +160,8 @@
 
         if (ec)
         {
-            std::cerr << "read_symlink() failed: " << ec.message() << " ("
-                      << ec.value() << ")\n";
+            lg2::error("read_symlink() failed: '{ERROR_MESSAGE}'",
+                       "ERROR_MESSAGE", ec.message());
             continue;
         }
 
@@ -187,8 +187,8 @@
         /* PWM file not exist or error happened */
         if (ec)
         {
-            std::cerr << "exists() failed: " << ec.message() << " ("
-                      << ec.value() << ")\n";
+            lg2::error("exists() failed: '{ERROR_MESSAGE}'", "ERROR_MESSAGE",
+                       ec.message());
         }
         /* try search form pwm-fanX directory */
         return findPwmfanPath(pwm, pwmPath);
@@ -233,7 +233,7 @@
                                   const ManagedObjectType& managedObj) {
             if (ec)
             {
-                std::cerr << "Error calling entity manager \n";
+                lg2::error("Error calling entity manager");
                 return;
             }
             for (const auto& [path, interfaces] : managedObj)
@@ -246,7 +246,7 @@
                         auto findCount = cfg.find("AllowedFailures");
                         if (findCount == cfg.end())
                         {
-                            std::cerr << "Malformed redundancy record \n";
+                            lg2::error("Malformed redundancy record");
                             return;
                         }
                         std::vector<std::string> sensorList;
@@ -297,7 +297,7 @@
         if (!findFiles(std::filesystem::path("/sys/class/hwmon"),
                        R"(fan\d+_input)", paths))
         {
-            std::cerr << "No fan sensors in system\n";
+            lg2::error("No fan sensors in system");
             return;
         }
 
@@ -339,7 +339,8 @@
                 auto findIndex = baseConfiguration->second.find("Index");
                 if (findIndex == baseConfiguration->second.end())
                 {
-                    std::cerr << baseConfiguration->first << " missing index\n";
+                    lg2::error("'{INTERFACE}' missing index", "INTERFACE",
+                               baseConfiguration->first);
                     continue;
                 }
                 unsigned int configIndex = std::visit(
@@ -375,8 +376,8 @@
                     if (findBus == baseConfiguration->second.end() ||
                         findAddress == baseConfiguration->second.end())
                     {
-                        std::cerr << baseConfiguration->first
-                                  << " missing bus or address\n";
+                        lg2::error("'{INTERFACE}' missing bus or address",
+                                   "INTERFACE", baseConfiguration->first);
                         continue;
                     }
                     unsigned int configBus = std::visit(
@@ -393,8 +394,8 @@
             }
             if (sensorData == nullptr)
             {
-                std::cerr << "failed to find match for " << path.string()
-                          << "\n";
+                lg2::error("failed to find match for '{PATH}'", "PATH",
+                           path.string());
                 continue;
             }
 
@@ -402,8 +403,9 @@
 
             if (findSensorName == baseConfiguration->second.end())
             {
-                std::cerr << "could not determine configuration name for "
-                          << path.string() << "\n";
+                lg2::error(
+                    "could not determine configuration name for '{PATH}'",
+                    "PATH", path.string());
                 continue;
             }
             std::string sensorName =
@@ -433,8 +435,8 @@
             std::vector<thresholds::Threshold> sensorThresholds;
             if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
             {
-                std::cerr << "error populating thresholds for " << sensorName
-                          << "\n";
+                lg2::error("error populating thresholds for '{NAME}'", "NAME",
+                           sensorName);
             }
 
             auto presenceConfig =
@@ -451,7 +453,7 @@
                 if (findPinName == presenceConfig->second.end() ||
                     findPolarity == presenceConfig->second.end())
                 {
-                    std::cerr << "Malformed Presence Configuration\n";
+                    lg2::error("Malformed Presence Configuration");
                 }
                 else
                 {
@@ -486,10 +488,10 @@
                                 }
                                 else if (mType != "Event")
                                 {
-                                    std::cerr
-                                        << "Unsupported GPIO MonitorType of "
-                                        << mType << " for " << sensorName
-                                        << " (supported types: Polling, Event (default))\n";
+                                    lg2::error(
+                                        "Unsupported GPIO MonitorType of '{TYPE}' for '{NAME}', "
+                                        "supported types: Polling, Event default",
+                                        "TYPE", mType, "NAME", sensorName);
                                 }
                             }
                             try
@@ -512,17 +514,19 @@
                             }
                             catch (const std::system_error& e)
                             {
-                                std::cerr
-                                    << "Failed to create GPIO monitor object for "
-                                    << *pinName << " / " << sensorName << ": "
-                                    << e.what() << "\n";
+                                lg2::error(
+                                    "Failed to create GPIO monitor object for "
+                                    "'{PIN_NAME}' / '{SENSOR_NAME}': '{ERROR}'",
+                                    "PIN_NAME", *pinName, "SENSOR_NAME",
+                                    sensorName, "ERROR", e);
                             }
                         }
                     }
                     else
                     {
-                        std::cerr << "Malformed Presence pinName for sensor "
-                                  << sensorName << " \n";
+                        lg2::error(
+                            "Malformed Presence pinName for sensor '{NAME}'",
+                            "NAME", sensorName);
                     }
                 }
             }
@@ -557,8 +561,9 @@
                                             findPwm->second);
                     if (!findPwmPath(directory, pwm, pwmPath))
                     {
-                        std::cerr << "Connector for " << sensorName
-                                  << " no pwm channel found!\n";
+                        lg2::error(
+                            "Connector for '{NAME}' no pwm channel found!",
+                            "NAME", sensorName);
                         continue;
                     }
 
@@ -595,8 +600,8 @@
                 }
                 else
                 {
-                    std::cerr
-                        << "Connector for " << sensorName << " missing pwm!\n";
+                    lg2::error("Connector for '{NAME}' missing pwm!", "NAME",
+                               sensorName);
                 }
 
                 auto findLED = connector->second.find("LED");
@@ -606,8 +611,8 @@
                         std::get_if<std::string>(&(findLED->second));
                     if (ledName == nullptr)
                     {
-                        std::cerr
-                            << "Wrong format for LED of " << sensorName << "\n";
+                        lg2::error("Wrong format for LED of '{NAME}'", "NAME",
+                                   sensorName);
                     }
                     else
                     {
@@ -674,7 +679,7 @@
         [&](sdbusplus::message_t& message) {
             if (message.is_method_error())
             {
-                std::cerr << "callback method error\n";
+                lg2::error("callback method error");
                 return;
             }
             sensorsChanged->insert(message.get_path());
@@ -689,7 +694,7 @@
                 }
                 if (ec)
                 {
-                    std::cerr << "timer error\n";
+                    lg2::error("timer error");
                     return;
                 }
                 createSensors(io, objectServer, tachSensors, pwmSensors,
diff --git a/src/fan/PresenceGpio.cpp b/src/fan/PresenceGpio.cpp
index d917c82..6ac6fb9 100644
--- a/src/fan/PresenceGpio.cpp
+++ b/src/fan/PresenceGpio.cpp
@@ -19,9 +19,9 @@
 #include <boost/asio/io_context.hpp>
 #include <boost/asio/posix/stream_descriptor.hpp>
 #include <gpiod.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <chrono>
-#include <iostream>
 #include <memory>
 #include <stdexcept>
 #include <string>
@@ -37,7 +37,7 @@
     gpioLine = gpiod::find_line(gpioName);
     if (!gpioLine)
     {
-        std::cerr << "Error requesting gpio: " << gpioName << "\n";
+        lg2::error("Error requesting gpio: '{NAME}'", "NAME", gpioName);
         throw std::runtime_error("Failed to find GPIO " + gpioName);
     }
 }
@@ -74,15 +74,15 @@
     }
     catch (const std::system_error& e)
     {
-        std::cerr << "Error reading gpio " << gpioName << ": " << e.what()
-                  << "\n";
+        lg2::error("Error reading gpio '{NAME}': '{ERR}'", "NAME", gpioName,
+                   "ERR", e);
         throw std::runtime_error("Failed to read GPIO fd " + gpioName);
     }
 
     int gpioLineFd = gpioLine.event_get_fd();
     if (gpioLineFd < 0)
     {
-        std::cerr << "Failed to get " << gpioName << " fd\n";
+        lg2::error("Failed to get '{NAME}' fd", "NAME", gpioName);
         throw std::runtime_error("Failed to get GPIO fd " + gpioName);
     }
 
@@ -98,17 +98,19 @@
             std::shared_ptr<EventPresenceGpio> self = weakRef.lock();
             if (!self)
             {
-                std::cerr << "Failed to get lock for eventPresenceGpio: "
-                          << ec.message() << "\n";
+                lg2::error(
+                    "Failed to get lock for eventPresenceGpio: '{ERROR_MESSAGE}'",
+                    "ERROR_MESSAGE", ec.message());
                 return;
             }
             if (ec)
             {
                 if (ec != boost::system::errc::bad_file_descriptor)
                 {
-                    std::cerr
-                        << "Error on event presence device " << self->deviceName
-                        << ": " << ec.message() << "\n";
+                    lg2::error(
+                        "Error on event presence device '{NAME}': '{ERROR_MESSAGE}'",
+                        "NAME", self->deviceName, "ERROR_MESSAGE",
+                        ec.message());
                 }
                 return;
             }
@@ -138,8 +140,8 @@
     }
     catch (const std::system_error& e)
     {
-        std::cerr << "PollingPresenceGpio: Error reading gpio " << gpioName
-                  << ": " << e.what() << "\n";
+        lg2::error("PollingPresenceGpio: Error reading gpio '{NAME}': '{ERR}'",
+                   "NAME", gpioName, "ERR", e);
         status = false;
         throw std::runtime_error("Failed to get Polling GPIO fd " + gpioName);
     }
@@ -152,16 +154,18 @@
     std::shared_ptr<PollingPresenceGpio> self = weakRef.lock();
     if (!self)
     {
-        std::cerr << "Failed to get lock for pollingPresenceGpio: "
-                  << ec.message() << "\n";
+        lg2::error(
+            "Failed to get lock for pollingPresenceGpio: '{ERROR_MESSAGE}'",
+            "ERROR_MESSAGE", ec.message());
         return;
     }
     if (ec)
     {
         if (ec != boost::system::errc::bad_file_descriptor)
         {
-            std::cerr << "GPIO polling timer failed for " << self->gpioName
-                      << ": " << ec.what() << ")\n";
+            lg2::error(
+                "GPIO polling timer failed for '{NAME}': '{ERROR_MESSAGE}'",
+                "NAME", self->gpioName, "ERROR_MESSAGE", ec.message());
         }
         return;
     }
diff --git a/src/fan/TachSensor.cpp b/src/fan/TachSensor.cpp
index 10a34a4..c8a3c40 100644
--- a/src/fan/TachSensor.cpp
+++ b/src/fan/TachSensor.cpp
@@ -26,6 +26,7 @@
 #include <boost/asio/error.hpp>
 #include <boost/asio/io_context.hpp>
 #include <boost/asio/random_access_file.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 
@@ -33,7 +34,6 @@
 #include <chrono>
 #include <cstddef>
 #include <cstdint>
-#include <iostream>
 #include <memory>
 #include <optional>
 #include <string>
@@ -151,7 +151,8 @@
     if ((err == boost::system::errc::bad_file_descriptor) ||
         (err == boost::asio::error::misc_errors::not_found))
     {
-        std::cerr << "TachSensor " << name << " removed " << path << "\n";
+        lg2::error("TachSensor '{NAME}' removed '{PATH}'", "NAME", name, "PATH",
+                   path);
         return; // we're being destroyed
     }
     bool missing = false;