intrusion: Convert logging to lg2

Change-Id: I372adbb3ca3ee163df6243061fea37403b8bfa76
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/src/intrusion/ChassisIntrusionSensor.cpp b/src/intrusion/ChassisIntrusionSensor.cpp
index 9740007..445adbe 100644
--- a/src/intrusion/ChassisIntrusionSensor.cpp
+++ b/src/intrusion/ChassisIntrusionSensor.cpp
@@ -28,6 +28,7 @@
 #include <boost/asio/io_context.hpp>
 #include <boost/asio/posix/stream_descriptor.hpp>
 #include <gpiod.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 
 #include <chrono>
@@ -84,8 +85,8 @@
 
     if constexpr (debug)
     {
-        std::cout << "Update value from " << mValue << " to " << newValue
-                  << "\n";
+        lg2::info("Update value from '{VALUE}' to '{NEWVALUE}'", "VALUE",
+                  mValue, "NEWVALUE", newValue);
     }
 
     // Automatic Rearm mode allows direct update
@@ -129,12 +130,12 @@
     int32_t value = i2c_smbus_read_byte_data(mBusFd, statusReg);
     if constexpr (debug)
     {
-        std::cout << "Pch type: raw value is " << value << "\n";
+        lg2::info("Pch type: raw value is '{VALUE}'", "VALUE", value);
     }
 
     if (value < 0)
     {
-        std::cerr << "i2c_smbus_read_byte_data failed \n";
+        lg2::error("i2c_smbus_read_byte_data failed");
         return -1;
     }
 
@@ -143,7 +144,7 @@
 
     if constexpr (debug)
     {
-        std::cout << "Pch type: masked raw value is " << value << "\n";
+        lg2::info("Pch type: masked raw value is '{VALUE}'", "VALUE", value);
     }
     return value;
 }
@@ -159,14 +160,14 @@
         // case of being canceled
         if (ec == boost::asio::error::operation_aborted)
         {
-            std::cerr << "Timer of intrusion sensor is cancelled\n";
+            lg2::error("Timer of intrusion sensor is cancelled");
             return;
         }
 
         std::shared_ptr<ChassisIntrusionPchSensor> self = weakRef.lock();
         if (!self)
         {
-            std::cerr << "ChassisIntrusionSensor no self\n";
+            lg2::error("ChassisIntrusionSensor no self");
             return;
         }
 
@@ -192,7 +193,7 @@
     auto value = mGpioLine.get_value();
     if constexpr (debug)
     {
-        std::cout << "Gpio type: raw value is " << value << "\n";
+        lg2::info("Gpio type: raw value is '{VALUE}'", "VALUE", value);
     }
     return value;
 }
@@ -209,8 +210,7 @@
 
             if (ec)
             {
-                std::cerr
-                    << "Error on GPIO based intrusion sensor wait event\n";
+                lg2::error("Error on GPIO based intrusion sensor wait event");
             }
             else
             {
@@ -232,14 +232,14 @@
     std::fstream stream(mHwmonPath, std::ios::in | std::ios::out);
     if (!stream.good())
     {
-        std::cerr << "Error reading status at " << mHwmonPath << "\n";
+        lg2::error("Error reading status at '{PATH}'", "PATH", mHwmonPath);
         return -1;
     }
 
     std::string line;
     if (!std::getline(stream, line))
     {
-        std::cerr << "Error reading status at " << mHwmonPath << "\n";
+        lg2::error("Error reading status at '{PATH}'", "PATH", mHwmonPath);
         return -1;
     }
 
@@ -248,13 +248,13 @@
         value = std::stoi(line);
         if constexpr (debug)
         {
-            std::cout << "Hwmon type: raw value is " << value << "\n";
+            lg2::info("Hwmon type: raw value is '{VALUE}'", "VALUE", value);
         }
     }
     catch (const std::invalid_argument& e)
     {
-        std::cerr << "Error reading status at " << mHwmonPath << " : "
-                  << e.what() << "\n";
+        lg2::error("Error reading status at '{PATH}': '{ERR}'", "PATH",
+                   mHwmonPath, "ERR", e);
         return -1;
     }
 
@@ -275,14 +275,14 @@
         // case of being canceled
         if (ec == boost::asio::error::operation_aborted)
         {
-            std::cerr << "Timer of intrusion sensor is cancelled\n";
+            lg2::error("Timer of intrusion sensor is cancelled");
             return;
         }
 
         std::shared_ptr<ChassisIntrusionHwmonSensor> self = weakRef.lock();
         if (!self)
         {
-            std::cerr << "ChassisIntrusionSensor no self\n";
+            lg2::error("ChassisIntrusionSensor no self");
             return;
         }
 
@@ -457,8 +457,8 @@
 
     if (paths.size() > 1)
     {
-        std::cerr << "Found more than 1 hwmon file to read chassis intrusion"
-                  << " status. Taking the first one. \n";
+        lg2::error("Found more than 1 hwmon file to read chassis intrusion"
+                   " status. Taking the first one.");
     }
 
     // Expecting only one hwmon file for one given chassis
@@ -466,9 +466,9 @@
 
     if constexpr (debug)
     {
-        std::cout << "Found " << paths.size()
-                  << " paths for intrusion status \n"
-                  << " The first path is: " << mHwmonPath << "\n";
+        lg2::info(
+            "Found '{NUM_PATHS}' paths for intrusion status. The first path is: '{PATH}'",
+            "NUM_PATHS", paths.size(), "PATH", mHwmonPath);
     }
 }
 
@@ -482,7 +482,7 @@
     mPollTimer.cancel();
     if (close(mBusFd) < 0)
     {
-        std::cerr << "Failed to close fd " << std::to_string(mBusFd);
+        lg2::error("Failed to close fd '{FD}'", "FD", mBusFd);
     }
 }
 
diff --git a/src/intrusion/IntrusionSensorMain.cpp b/src/intrusion/IntrusionSensorMain.cpp
index bc096c4..4432ed5 100644
--- a/src/intrusion/IntrusionSensorMain.cpp
+++ b/src/intrusion/IntrusionSensorMain.cpp
@@ -37,7 +37,6 @@
 #include <filesystem>
 #include <fstream>
 #include <functional>
-#include <iostream>
 #include <map>
 #include <memory>
 #include <string>
@@ -71,7 +70,7 @@
     if (!getSensorConfiguration(sensorType, dbusConnection,
                                 sensorConfigurations, useCache))
     {
-        std::cerr << "error communicating to entity manager\n";
+        lg2::error("error communicating to entity manager");
         return;
     }
 
@@ -88,7 +87,7 @@
         auto sensorBase = sensorData->find(configInterfaceName(sensorType));
         if (sensorBase == sensorData->end())
         {
-            std::cerr << "error finding base configuration \n";
+            lg2::error("error finding base configuration");
             continue;
         }
 
@@ -102,7 +101,7 @@
             std::string rearmStr = std::get<std::string>(findRearm->second);
             if (rearmStr != "Automatic" && rearmStr != "Manual")
             {
-                std::cerr << "Wrong input for Rearm parameter\n";
+                lg2::error("Wrong input for Rearm parameter");
                 continue;
             }
             autoRearm = (rearmStr == "Automatic");
@@ -120,8 +119,7 @@
 
                 if (findGpioPolarity == baseConfiguration->second.end())
                 {
-                    std::cerr
-                        << "error finding gpio polarity in configuration \n";
+                    lg2::error("error finding gpio polarity in configuration");
                     continue;
                 }
 
@@ -135,21 +133,22 @@
                     pSensor->start();
                     if (debug)
                     {
-                        std::cout
-                            << "find chassis intrusion sensor polarity inverted "
-                               "flag is "
-                            << gpioInverted << "\n";
+                        lg2::info(
+                            "find chassis intrusion sensor polarity inverted flag is '{GPIO_INVERTED}'",
+                            "GPIO_INVERTED", gpioInverted);
                     }
                     return;
                 }
                 catch (const std::bad_variant_access& e)
                 {
-                    std::cerr << "invalid value for gpio info in config. \n";
+                    lg2::error("invalid value for gpio info in config.");
                     continue;
                 }
                 catch (const std::exception& e)
                 {
-                    std::cerr << e.what() << std::endl;
+                    lg2::error(
+                        "error creating chassis intrusion gpio sensor: '{ERROR}'",
+                        "ERROR", e);
                     continue;
                 }
             }
@@ -162,7 +161,7 @@
 
                 if (compatIterator == compatibleHwmonNames.end())
                 {
-                    std::cerr << "Hwmon Class string is not supported\n";
+                    lg2::error("Hwmon Class string is not supported");
                     continue;
                 }
 
@@ -177,7 +176,9 @@
                 }
                 catch (const std::exception& e)
                 {
-                    std::cerr << e.what() << std::endl;
+                    lg2::error(
+                        "error creating chassis intrusion hwmon sensor: '{ERROR}'",
+                        "ERROR", e);
                     continue;
                 }
             }
@@ -188,8 +189,7 @@
                 if (findBus == baseConfiguration->second.end() ||
                     findAddress == baseConfiguration->second.end())
                 {
-                    std::cerr
-                        << "error finding bus or address in configuration \n";
+                    lg2::error("error finding bus or address in configuration");
                     continue;
                 }
                 try
@@ -201,34 +201,35 @@
                     pSensor->start();
                     if (debug)
                     {
-                        std::cout
-                            << "find matched bus " << busId
-                            << ", matched slave addr " << slaveAddr << "\n";
+                        lg2::info(
+                            "find matched bus '{BUS}', matched slave addr '{ADDR}'",
+                            "BUS", busId, "ADDR", slaveAddr);
                     }
                     return;
                 }
                 catch (const std::bad_variant_access& e)
                 {
-                    std::cerr
-                        << "invalid value for bus or address in config. \n";
+                    lg2::error("invalid value for bus or address in config.");
                     continue;
                 }
                 catch (const std::exception& e)
                 {
-                    std::cerr << e.what() << std::endl;
+                    lg2::error(
+                        "error creating chassis intrusion pch sensor: '{ERROR}'",
+                        "ERROR", e);
                     continue;
                 }
             }
         }
     }
 
-    std::cerr << " Can't find matched I2C, GPIO or Hwmon configuration\n";
+    lg2::error("Can't find matched I2C, GPIO or Hwmon configuration");
 
     // Make sure nothing runs when there's failure in configuration for the
     // sensor after rescan
     if (pSensor)
     {
-        std::cerr << " Reset the occupied sensor pointer\n";
+        lg2::error("Reset the occupied sensor pointer");
         pSensor = nullptr;
     }
 }
@@ -273,8 +274,8 @@
                         lanInfoMap[*pEthIndex] = *pName;
                         if (debugLanLeash)
                         {
-                            std::cout << "find name of eth" << *pEthIndex
-                                      << " is " << *pName << "\n";
+                            lg2::info("find name of eth{ETH_INDEX} is '{NAME}'",
+                                      "ETH_INDEX", *pEthIndex, "NAME", *pName);
                         }
                     }
                 }
@@ -282,7 +283,7 @@
 
             if (lanInfoMap.empty())
             {
-                std::cerr << "can't find matched NIC name. \n";
+                lg2::error("can't find matched NIC name.");
             }
         });
 
@@ -306,7 +307,7 @@
         std::get_if<std::string>(&(findStateProperty->second));
     if (pState == nullptr)
     {
-        std::cerr << "invalid OperationalState \n";
+        lg2::error("invalid OperationalState");
         return;
     }
 
@@ -317,7 +318,7 @@
     size_t pos = pathName.find("/_");
     if (pos == std::string::npos || pathName.length() <= pos + 2)
     {
-        std::cerr << "unexpected path name " << pathName << "\n";
+        lg2::error("unexpected path name '{NAME}'", "NAME", pathName);
         return;
     }
     std::string suffixStr = pathName.substr(pos + 2);
@@ -325,7 +326,8 @@
     auto findEthNum = pathSuffixMap.find(suffixStr);
     if (findEthNum == pathSuffixMap.end())
     {
-        std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
+        lg2::error("unexpected eth for suffixStr '{SUFFIX}'", "SUFFIX",
+                   suffixStr);
         return;
     }
     int ethNum = findEthNum->second;
@@ -334,7 +336,8 @@
     auto findLanStatus = lanStatusMap.find(ethNum);
     if (findLanStatus == lanStatusMap.end())
     {
-        std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
+        lg2::error("unexpected eth{ETH_INDEX} is lanStatusMap", "ETH_INDEX",
+                   ethNum);
         return;
     }
     bool oldLanConnected = findLanStatus->second;
@@ -346,7 +349,8 @@
         auto findLanInfo = lanInfoMap.find(ethNum);
         if (findLanInfo == lanInfoMap.end())
         {
-            std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
+            lg2::error("unexpected eth{ETH_INDEX} is lanInfoMap", "ETH_INDEX",
+                       ethNum);
         }
         else
         {
@@ -356,11 +360,12 @@
 
     if (debugLanLeash)
     {
-        std::cout << "ethNum = " << ethNum << ", state = " << *pState
-                  << ", oldLanConnected = "
-                  << (oldLanConnected ? "true" : "false")
-                  << ", newLanConnected = "
-                  << (newLanConnected ? "true" : "false") << "\n";
+        lg2::info(
+            "ethNum = {ETH_INDEX}, state = {LAN_STATUS}, oldLanConnected = {OLD_LAN_CONNECTED}, "
+            "newLanConnected = {NEW_LAN_CONNECTED}",
+            "ETH_INDEX", ethNum, "LAN_STATUS", *pState, "OLD_LAN_CONNECTED",
+            (oldLanConnected ? "true" : "false"), "NEW_LAN_CONNECTED",
+            (newLanConnected ? "true" : "false"));
     }
 
     if (oldLanConnected != newLanConnected)
@@ -370,9 +375,9 @@
         const auto* strMsgId =
             newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost";
 
-        lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE",
-                  strState, "REDFISH_MESSAGE_ID", strMsgId,
-                  "REDFISH_MESSAGE_ARGS", strEthNum);
+        lg2::info("'{ETH_INFO}' LAN leash '{LAN_STATUS}'", "ETH_INFO",
+                  strEthNum, "LAN_STATUS", strState, "REDFISH_MESSAGE_ID",
+                  strMsgId, "REDFISH_MESSAGE_ARGS", strEthNum);
 
         lanStatusMap[ethNum] = newLanConnected;
     }
@@ -393,7 +398,7 @@
     if (!findFiles(std::filesystem::path("/sys/class/net/"),
                    R"(eth\d+/ifindex)", files))
     {
-        std::cerr << "No eth in system\n";
+        lg2::error("No eth in system");
         return false;
     }
 
@@ -402,12 +407,12 @@
     {
         if (debugLanLeash)
         {
-            std::cout << "Reading " << fileName << "\n";
+            lg2::info("Reading '{NAME}'", "NAME", fileName);
         }
         std::ifstream sysFile(fileName);
         if (!sysFile.good())
         {
-            std::cerr << "Failure reading " << fileName << "\n";
+            lg2::error("Failure reading '{NAME}'", "NAME", fileName);
             continue;
         }
         std::string line;
@@ -425,7 +430,8 @@
             ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum);
         if (r.ec != std::errc())
         {
-            std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
+            lg2::error("invalid ethNum string: '{ETH_INDEX}'", "ETH_INDEX",
+                       ethNumStr);
             continue;
         }
 
@@ -433,8 +439,9 @@
         pathSuffixMap[pathSuffix] = ethNum;
         if (debugLanLeash)
         {
-            std::cout << "ethNum = " << std::to_string(ethNum) << ", ifindex = "
-                      << line << ", pathSuffix = " << pathSuffix << "\n";
+            lg2::info(
+                "ethNum = {ETH_INDEX}, ifindex = {LINE}, pathSuffix = {PATH}",
+                "ETH_INDEX", ethNum, "LINE", line, "PATH", pathSuffix);
         }
 
         // init lan connected status from networkd
@@ -444,14 +451,14 @@
                 lanStatusMap[ethNum] = false;
                 if (ec)
                 {
-                    std::cerr
-                        << "Error reading init status of eth" << ethNum << "\n";
+                    lg2::error("Error reading init status of eth{ETH_INDEX}",
+                               "ETH_INDEX", ethNum);
                     return;
                 }
                 const std::string* pState = std::get_if<std::string>(&property);
                 if (pState == nullptr)
                 {
-                    std::cerr << "Unable to read lan status value\n";
+                    lg2::error("Unable to read lan status value");
                     return;
                 }
                 bool isLanConnected =
@@ -459,9 +466,10 @@
                      *pState == "degraded");
                 if (debugLanLeash)
                 {
-                    std::cout << "ethNum = " << std::to_string(ethNum)
-                              << ", init LAN status = "
-                              << (isLanConnected ? "true" : "false") << "\n";
+                    lg2::info(
+                        "ethNum = {ETH_INDEX}, init LAN status = {STATUS}",
+                        "ETH_INDEX", ethNum, "STATUS",
+                        (isLanConnected ? "true" : "false"));
                 }
                 lanStatusMap[ethNum] = isLanConnected;
             },
@@ -496,7 +504,7 @@
         [&](sdbusplus::message_t& message) {
             if (message.is_method_error())
             {
-                std::cerr << "callback method error\n";
+                lg2::error("callback method error");
                 return;
             }
             // this implicitly cancels the timer
@@ -507,7 +515,7 @@
                     // timer was cancelled
                     return;
                 }
-                std::cout << "rescan due to configuration change \n";
+                lg2::info("rescan due to configuration change");
                 createSensorsFromConfig(io, objServer, systemBus,
                                         intrusionSensor);
             });
@@ -536,7 +544,7 @@
             [&systemBus](sdbusplus::message_t& msg) {
                 if (msg.is_method_error())
                 {
-                    std::cerr << "callback method error\n";
+                    lg2::error("callback method error");
                     return;
                 }
                 getNicNameInfo(systemBus);