external: Convert logging to lg2

Change-Id: I3d426c85be0a6fef0c4600ec784a56952871e4c1
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/src/external/ExternalSensor.cpp b/src/external/ExternalSensor.cpp
index 506823a..92f0f0f 100644
--- a/src/external/ExternalSensor.cpp
+++ b/src/external/ExternalSensor.cpp
@@ -5,13 +5,13 @@
 #include "Utils.hpp"
 #include "sensor.hpp"
 
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 
 #include <chrono>
 #include <cstddef>
 #include <functional>
-#include <iostream>
 #include <limits>
 #include <memory>
 #include <stdexcept>
@@ -65,13 +65,13 @@
 
     if constexpr (debug)
     {
-        std::cerr << "ExternalSensor " << name << " constructed: path "
-                  << configurationPath << ", type " << objectType << ", min "
-                  << minReading << ", max " << maxReading << ", timeout "
-                  << std::chrono::duration_cast<std::chrono::microseconds>(
-                         writeTimeout)
-                         .count()
-                  << " us\n";
+        lg2::error(
+            "ExternalSensor '{NAME}' constructed: path '{PATH}', type '{TYPE}', "
+            "min '{MIN}', max '{MAX}', timeout '{TIMEOUT}' us",
+            "NAME", name, "PATH", objectPath, "TYPE", objectType, "MIN",
+            minReading, "MAX", maxReading, "TIMEOUT",
+            std::chrono::duration_cast<std::chrono::microseconds>(writeTimeout)
+                .count());
     }
 }
 
@@ -95,7 +95,7 @@
         }
         if constexpr (debug)
         {
-            std::cerr << "ExternalSensor receive ignored, sensor gone\n";
+            lg2::error("ExternalSensor receive ignored, sensor gone");
         }
     };
 }
@@ -114,7 +114,7 @@
 
     if constexpr (debug)
     {
-        std::cerr << "ExternalSensor " << name << " destructed\n";
+        lg2::error("ExternalSensor '{NAME}' destructed", "NAME", name);
     }
 }
 
@@ -147,8 +147,9 @@
 {
     if (!writeAlive)
     {
-        std::cerr << "ExternalSensor " << name
-                  << " online, receiving first value " << value << "\n";
+        lg2::error(
+            "ExternalSensor '{NAME}' online, receiving first value '{VALUE}'",
+            "NAME", name, "VALUE", value);
     }
 
     writeLast = now;
@@ -159,7 +160,7 @@
 {
     writeAlive = false;
 
-    std::cerr << "ExternalSensor " << name << " offline, timed out\n";
+    lg2::error("ExternalSensor '{NAME}' offline, timed out", "NAME", name);
 
     // Take back control of this sensor from the external override,
     // as the external source has timed out.
@@ -190,7 +191,8 @@
 {
     if constexpr (debug)
     {
-        std::cerr << "ExternalSensor " << name << " received " << value << "\n";
+        lg2::error("ExternalSensor '{NAME}' received '{VALUE}'", "NAME", name,
+                   "VALUE", value);
     }
 
     auto now = std::chrono::steady_clock::now();
diff --git a/src/external/ExternalSensorMain.cpp b/src/external/ExternalSensorMain.cpp
index 9bd7a4d..c8b21b4 100644
--- a/src/external/ExternalSensorMain.cpp
+++ b/src/external/ExternalSensorMain.cpp
@@ -9,6 +9,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/match.hpp>
@@ -20,7 +21,6 @@
 #include <chrono>
 #include <cmath>
 #include <functional>
-#include <iostream>
 #include <memory>
 #include <string>
 #include <utility>
@@ -119,7 +119,7 @@
     {
         if constexpr (debug)
         {
-            std::cerr << "Next ExternalSensor timer idle\n";
+            lg2::error("Next ExternalSensor timer idle");
         }
 
         return;
@@ -133,8 +133,9 @@
             // Cancellation is normal, as timer is dynamically rescheduled
             if (err != boost::asio::error::operation_aborted)
             {
-                std::cerr << "ExternalSensor timer scheduling problem: "
-                          << err.message() << "\n";
+                lg2::error(
+                    "ExternalSensor timer scheduling problem: {ERROR_MESSAGE}",
+                    "ERROR_MESSAGE", err.message());
             }
             return;
         }
@@ -144,11 +145,10 @@
 
     if constexpr (debug)
     {
-        std::cerr << "Next ExternalSensor timer "
-                  << std::chrono::duration_cast<std::chrono::microseconds>(
-                         nextCheck)
-                         .count()
-                  << " us\n";
+        lg2::error(
+            "Next ExternalSensor timer '{VALUE}' us", "VALUE",
+            std::chrono::duration_cast<std::chrono::microseconds>(nextCheck)
+                .count());
     }
 }
 
@@ -163,7 +163,7 @@
 {
     if constexpr (debug)
     {
-        std::cerr << "ExternalSensor considering creating sensors\n";
+        lg2::error("ExternalSensor considering creating sensors");
     }
 
     auto getter = std::make_shared<GetSensorConfiguration>(
@@ -182,8 +182,8 @@
                     sensorData.find(configInterfaceName(sensorType));
                 if (sensorBase == sensorData.end())
                 {
-                    std::cerr << "Base configuration not found for "
-                              << interfacePath << "\n";
+                    lg2::error("Base configuration not found for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
 
@@ -195,32 +195,32 @@
                 auto minFound = baseConfigMap.find("MinValue");
                 if (minFound == baseConfigMap.end())
                 {
-                    std::cerr << "MinValue parameter not found for "
-                              << interfacePath << "\n";
+                    lg2::error("MinValue parameter not found for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
                 double minValue =
                     std::visit(VariantToDoubleVisitor(), minFound->second);
                 if (!std::isfinite(minValue))
                 {
-                    std::cerr << "MinValue parameter not parsed for "
-                              << interfacePath << "\n";
+                    lg2::error("MinValue parameter not parsed for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
 
                 auto maxFound = baseConfigMap.find("MaxValue");
                 if (maxFound == baseConfigMap.end())
                 {
-                    std::cerr << "MaxValue parameter not found for "
-                              << interfacePath << "\n";
+                    lg2::error("MaxValue parameter not found for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
                 double maxValue =
                     std::visit(VariantToDoubleVisitor(), maxFound->second);
                 if (!std::isfinite(maxValue))
                 {
-                    std::cerr << "MaxValue parameter not parsed for "
-                              << interfacePath << "\n";
+                    lg2::error("MaxValue parameter not parsed for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
 
@@ -235,8 +235,8 @@
                 }
                 if (!std::isfinite(timeoutSecs) || (timeoutSecs < 0.0))
                 {
-                    std::cerr << "Timeout parameter not parsed for "
-                              << interfacePath << "\n";
+                    lg2::error("Timeout parameter not parsed for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
 
@@ -247,32 +247,32 @@
                 auto nameFound = baseConfigMap.find("Name");
                 if (nameFound == baseConfigMap.end())
                 {
-                    std::cerr << "Name parameter not found for "
-                              << interfacePath << "\n";
+                    lg2::error("Name parameter not found for '{PATH}'", "PATH",
+                               interfacePath);
                     continue;
                 }
                 sensorName =
                     std::visit(VariantToStringVisitor(), nameFound->second);
                 if (sensorName.empty())
                 {
-                    std::cerr << "Name parameter not parsed for "
-                              << interfacePath << "\n";
+                    lg2::error("Name parameter not parsed for '{PATH}'", "PATH",
+                               interfacePath);
                     continue;
                 }
 
                 auto unitsFound = baseConfigMap.find("Units");
                 if (unitsFound == baseConfigMap.end())
                 {
-                    std::cerr << "Units parameter not found for "
-                              << interfacePath << "\n";
+                    lg2::error("Units parameter not found for '{PATH}'", "PATH",
+                               interfacePath);
                     continue;
                 }
                 sensorUnits =
                     std::visit(VariantToStringVisitor(), unitsFound->second);
                 if (sensorUnits.empty())
                 {
-                    std::cerr << "Units parameter not parsed for "
-                              << interfacePath << "\n";
+                    lg2::error("Units parameter not parsed for '{PATH}'",
+                               "PATH", interfacePath);
                     continue;
                 }
 
@@ -295,8 +295,9 @@
                             found = true;
                             if constexpr (debug)
                             {
-                                std::cerr << "ExternalSensor " << sensorName
-                                          << " change found\n";
+                                lg2::error(
+                                    "ExternalSensor '{NAME}' change found",
+                                    "NAME", sensorName);
                             }
                             break;
                         }
@@ -310,8 +311,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);
                 }
 
                 PowerState readState = getPowerState(baseConfigMap);
@@ -331,8 +332,8 @@
 
                 if constexpr (debug)
                 {
-                    std::cerr
-                        << "ExternalSensor " << sensorName << " created\n";
+                    lg2::error("ExternalSensor '{NAME}' created", "NAME",
+                               sensorName);
                 }
             }
         });
@@ -344,7 +345,7 @@
 {
     if constexpr (debug)
     {
-        std::cerr << "ExternalSensor service starting up\n";
+        lg2::error("ExternalSensor service starting up");
     }
 
     boost::asio::io_context io;
@@ -371,7 +372,7 @@
          &reaperTimer](sdbusplus::message_t& message) mutable {
             if (message.is_method_error())
             {
-                std::cerr << "callback method error\n";
+                lg2::error("callback method error");
                 return;
             }
 
@@ -379,8 +380,8 @@
             sensorsChanged->insert(messagePath);
             if constexpr (debug)
             {
-                std::cerr << "ExternalSensor change event received: "
-                          << messagePath << "\n";
+                lg2::error("ExternalSensor change event received: '{PATH}'",
+                           "PATH", messagePath);
             }
 
             // this implicitly cancels the timer
@@ -393,8 +394,8 @@
                     {
                         if (ec != boost::asio::error::operation_aborted)
                         {
-                            std::cerr
-                                << "callback error: " << ec.message() << "\n";
+                            lg2::error("callback error: '{ERROR_MESSAGE}'",
+                                       "ERROR_MESSAGE", ec.message());
                         }
                         return;
                     }
@@ -410,7 +411,7 @@
 
     if constexpr (debug)
     {
-        std::cerr << "ExternalSensor service entering main loop\n";
+        lg2::error("ExternalSensor service entering main loop");
     }
 
     io.run();