convert apphandler.cpp to use lg2

Signed-off-by: George Liu <liuxiwei@ieisystem.com>
Change-Id: Icb22dd3d46f85c7b5096f1edc84116cefcba677d
diff --git a/apphandler.cpp b/apphandler.cpp
index 6d57345..c7b1c37 100644
--- a/apphandler.cpp
+++ b/apphandler.cpp
@@ -21,7 +21,7 @@
 #include <ipmid/utils.hpp>
 #include <nlohmann/json.hpp>
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/message/types.hpp>
 #include <sys_info_param.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -119,9 +119,9 @@
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>("Failed to fetch redundancy object from dbus",
-                        entry("INTERFACE=%s", redundancyIntf),
-                        entry("ERRMSG=%s", e.what()));
+        lg2::error("Failed to fetch redundancy object from dbus, "
+                   "interface: {INTERFACE},  error: {ERROR}",
+                   "INTERFACE", redundancyIntf, "ERROR", e);
         elog<InternalFailure>();
     }
 
@@ -165,14 +165,14 @@
             }
             catch (const std::exception& e)
             {
-                log<level::ERR>(e.what());
+                lg2::error("error message: {ERROR}", "ERROR", e);
             }
         }
     }
 
     if (!objectFound)
     {
-        log<level::ERR>("Could not found an BMC software Object");
+        lg2::error("Could not found an BMC software Object");
         elog<InternalFailure>();
     }
 
@@ -326,15 +326,14 @@
         if (!acpi_state::isValidACPIState(
                 acpi_state::PowerStateType::sysPowerState, s))
         {
-            log<level::ERR>("set_acpi_power sys invalid input",
-                            entry("S=%x", s));
+            lg2::error("set_acpi_power sys invalid input, S: {S}", "S", s);
             return ipmi::responseParmOutOfRange();
         }
 
         // valid input
         if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
         {
-            log<level::DEBUG>("No change for system power state");
+            lg2::debug("No change for system power state");
         }
         else
         {
@@ -357,15 +356,15 @@
             }
             catch (const InternalFailure& e)
             {
-                log<level::ERR>("Failed in set ACPI system property",
-                                entry("EXCEPTION=%s", e.what()));
+                lg2::error("Failed in set ACPI system property: {ERROR}",
+                           "ERROR", e);
                 return ipmi::responseUnspecifiedError();
             }
         }
     }
     else
     {
-        log<level::DEBUG>("Do not change system power state");
+        lg2::debug("Do not change system power state");
     }
 
     if (devAcpiState & acpi_state::stateChanged)
@@ -375,15 +374,14 @@
         if (!acpi_state::isValidACPIState(
                 acpi_state::PowerStateType::devPowerState, s))
         {
-            log<level::ERR>("set_acpi_power dev invalid input",
-                            entry("S=%x", s));
+            lg2::error("set_acpi_power dev invalid input, S: {S}", "S", s);
             return ipmi::responseParmOutOfRange();
         }
 
         // valid input
         if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
         {
-            log<level::DEBUG>("No change for device power state");
+            lg2::debug("No change for device power state");
         }
         else
         {
@@ -406,15 +404,15 @@
             }
             catch (const InternalFailure& e)
             {
-                log<level::ERR>("Failed in set ACPI device property",
-                                entry("EXCEPTION=%s", e.what()));
+                lg2::error("Failed in set ACPI device property: {ERROR}",
+                           "ERROR", e);
                 return ipmi::responseUnspecifiedError();
             }
         }
     }
     else
     {
-        log<level::DEBUG>("Do not change device power state");
+        lg2::debug("Do not change device power state");
     }
     return ipmi::responseSuccess();
 }
@@ -645,7 +643,7 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>(e.what());
+            lg2::error("error message: {ERROR}", "ERROR", e);
         }
 
         if (r >= 0)
@@ -710,13 +708,13 @@
             }
             else
             {
-                log<level::ERR>("Device ID JSON parser failure");
+                lg2::error("Device ID JSON parser failure");
                 return ipmi::responseUnspecifiedError();
             }
         }
         else
         {
-            log<level::ERR>("Device ID file not found");
+            lg2::error("Device ID file not found");
             return ipmi::responseUnspecifiedError();
         }
     }
@@ -858,10 +856,9 @@
                                                        objectInfo);
     if (ec.value())
     {
-        log<level::ERR>("Failed to locate System UUID object",
-                        entry("INTERFACE=%s", uuidInterface),
-                        entry("ERROR=%s", ec.message().c_str()));
-        return ipmi::responseUnspecifiedError();
+        lg2::error("Failed to locate System UUID object, "
+                   "interface: {INTERFACE}, error: {ERROR}",
+                   "INTERFACE", uuidInterface, "ERROR", ec.message());
     }
 
     // Read UUID property value from bmcObject
@@ -871,10 +868,11 @@
                                uuidInterface, uuidProperty, rfc4122Uuid);
     if (ec.value())
     {
-        log<level::ERR>("Failed in reading BMC UUID property",
-                        entry("INTERFACE=%s", uuidInterface),
-                        entry("PROPERTY=%s", uuidProperty),
-                        entry("ERROR=%s", ec.message().c_str()));
+        lg2::error("Failed to read System UUID property, "
+                   "interface: {INTERFACE}, property: {PROPERTY}, "
+                   "error: {ERROR}",
+                   "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
+                   "ERROR", ec.message());
         return ipmi::responseUnspecifiedError();
     }
     std::array<uint8_t, 16> uuid;
@@ -885,10 +883,11 @@
     }
     catch (const InvalidArgument& e)
     {
-        log<level::ERR>("Failed in parsing BMC UUID property",
-                        entry("INTERFACE=%s", uuidInterface),
-                        entry("PROPERTY=%s", uuidProperty),
-                        entry("VALUE=%s", rfc4122Uuid.c_str()));
+        lg2::error("Failed in parsing BMC UUID property, "
+                   "interface: {INTERFACE}, property: {PROPERTY}, "
+                   "value: {VALUE}, error: {ERROR}",
+                   "INTERFACE", uuidInterface, "PROPERTY", uuidProperty,
+                   "VALUE", rfc4122Uuid, "ERROR", e);
         return ipmi::responseUnspecifiedError();
     }
     return ipmi::responseSuccess(uuid);
@@ -925,10 +924,11 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed in getting session state property",
-                        entry("service=%s", service.c_str()),
-                        entry("object path=%s", obj.c_str()),
-                        entry("interface=%s", session::sessionIntf));
+        lg2::error("Failed in getting session state property, "
+                   "service: {SERVICE}, object path: {OBJECT_PATH}, "
+                   "interface: {INTERFACE}, error: {ERROR}",
+                   "SERVICE", service, "OBJECT_PATH", obj, "INTERFACE",
+                   session::sessionIntf, "ERROR", e);
         return ipmi::ccUnspecifiedError;
     }
 
@@ -991,9 +991,9 @@
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>("Failed to fetch object from dbus",
-                        entry("INTERFACE=%s", session::sessionIntf),
-                        entry("ERRMSG=%s", e.what()));
+        lg2::error("Failed to fetch object from dbus, "
+                   "interface: {INTERFACE}, error: {ERROR}",
+                   "INTERFACE", session::sessionIntf, "ERROR", e);
         return ipmi::responseUnspecifiedError();
     }
 
@@ -1117,11 +1117,11 @@
         ctx, service, objPath, session::sessionIntf, "State", sessionState);
     if (ec)
     {
-        log<level::ERR>("Failed to fetch state property ",
-                        entry("SERVICE=%s", service.c_str()),
-                        entry("OBJECTPATH=%s", objPath.c_str()),
-                        entry("INTERFACE=%s", session::sessionIntf),
-                        entry("ERRMSG=%s", ec.message().c_str()));
+        lg2::error("Failed to fetch state property, service: {SERVICE}, "
+                   "object path: {OBJECTPATH}, interface: {INTERFACE}, "
+                   "error: {ERROR}",
+                   "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
+                   session::sessionIntf, "ERROR", ec.message());
         return ipmi::ccUnspecifiedError;
     }
     return ipmi::ccSuccess;
@@ -1166,11 +1166,11 @@
 
     if (ec)
     {
-        log<level::ERR>("Failed to fetch state property ",
-                        entry("SERVICE=%s", service.c_str()),
-                        entry("OBJECTPATH=%s", objPath.c_str()),
-                        entry("INTERFACE=%s", session::sessionIntf),
-                        entry("ERRMSG=%s", ec.message().c_str()));
+        lg2::error("Failed to fetch state property, service: {SERVICE}, "
+                   "object path: {OBJECTPATH}, interface: {INTERFACE}, "
+                   "error: {ERROR}",
+                   "SERVICE", service, "OBJECTPATH", objPath, "INTERFACE",
+                   session::sessionIntf, "ERROR", ec.message());
         return ipmi::ccUnspecifiedError;
     }
 
@@ -1224,9 +1224,9 @@
         ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
     if (ec)
     {
-        log<level::ERR>("Failed to fetch object from dbus",
-                        entry("INTERFACE=%s", session::sessionIntf),
-                        entry("ERRMSG=%s", ec.message().c_str()));
+        lg2::error("Failed to fetch object from dbus, "
+                   "interface: {INTERFACE}, error: {ERROR}",
+                   "INTERFACE", session::sessionIntf, "ERROR", ec.message());
         return ipmi::responseUnspecifiedError();
     }
 
@@ -1451,8 +1451,7 @@
         // only following 2 states are supported
         if (data1 > setInProgress)
         {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "illegal SetInProgress status");
+            lg2::error("illegal SetInProgress status");
             return ipmi::responseInvalidFieldRequest();
         }
 
@@ -1546,9 +1545,8 @@
 
     if (!jsonFile.good())
     {
-        log<level::WARNING>(
-            "i2c allow list file not found!",
-            entry("FILE_NAME: %s", i2cControllerWRAllowlistFile));
+        lg2::warning("i2c allow list file not found! file name: {FILE_NAME}",
+                     "FILE_NAME", i2cControllerWRAllowlistFile);
         return false;
     }
 
@@ -1558,9 +1556,9 @@
     }
     catch (const nlohmann::json::parse_error& e)
     {
-        log<level::ERR>("Corrupted i2c allow list config file",
-                        entry("FILE_NAME: %s", i2cControllerWRAllowlistFile),
-                        entry("MSG: %s", e.what()));
+        lg2::error("Corrupted i2c allow list config file, "
+                   "file name: {FILE_NAME}, error: {ERROR}",
+                   "FILE_NAME", i2cControllerWRAllowlistFile, "ERROR", e);
         return false;
     }
 
@@ -1594,9 +1592,10 @@
             nlohmann::json filter = it.value();
             if (filter.is_null())
             {
-                log<level::ERR>(
-                    "Corrupted I2C controller write read allowlist config file",
-                    entry("FILE_NAME: %s", i2cControllerWRAllowlistFile));
+                lg2::error(
+                    "Corrupted I2C controller write read allowlist config file, "
+                    "file name: {FILE_NAME}",
+                    "FILE_NAME", i2cControllerWRAllowlistFile);
                 return false;
             }
             const std::vector<uint8_t>& writeData =
@@ -1605,8 +1604,8 @@
                 convertStringToData(filter[cmdMaskStr].get<std::string>());
             if (writeDataMask.size() != writeData.size())
             {
-                log<level::ERR>("I2C controller write read allowlist filter "
-                                "mismatch for command & mask size");
+                lg2::error("I2C controller write read allowlist filter "
+                           "mismatch for command & mask size");
                 return false;
             }
             allowlist.push_back(
@@ -1622,16 +1621,16 @@
         }
         if (allowlist.size() != filters.size())
         {
-            log<level::ERR>(
+            lg2::error(
                 "I2C controller write read allowlist filter size mismatch");
             return false;
         }
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>(
-            "I2C controller write read allowlist unexpected exception",
-            entry("ERROR=%s", e.what()));
+        lg2::error("I2C controller write read allowlist "
+                   "unexpected exception: {ERROR}",
+                   "ERROR", e);
         return false;
     }
     return true;
@@ -1679,8 +1678,7 @@
 #else
 static bool populateI2CControllerWRAllowlist()
 {
-    log<level::INFO>(
-        "I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
+    lg2::info("I2C_WHITELIST_CHECK is disabled, do not populate allowlist");
     return true;
 }
 #endif // ENABLE_I2C_WHITELIST_CHECK
@@ -1711,18 +1709,17 @@
     const size_t writeCount = writeData.size();
     if (!readCount && !writeCount)
     {
-        log<level::ERR>(
-            "Controller write read command: Read & write count are 0");
+        lg2::error("Controller write read command: Read & write count are 0");
         return ipmi::responseInvalidFieldRequest();
     }
 #ifdef ENABLE_I2C_WHITELIST_CHECK
     if (!isCmdAllowlisted(static_cast<uint8_t>(busId),
                           static_cast<uint8_t>(targetAddr), writeData))
     {
-        log<level::ERR>("Controller write read request blocked!",
-                        entry("BUS=%d", static_cast<uint8_t>(busId)),
-                        entry("ADDR=0x%x", static_cast<uint8_t>(targetAddr)));
-        return ipmi::responseInvalidFieldRequest();
+        lg2::error("Controller write read request blocked!, "
+                   "bus: {BUS}, addr: {ADDR}",
+                   "BUS", static_cast<uint8_t>(busId), "ADDR", lg2::hex,
+                   static_cast<uint8_t>(targetAddr));
     }
 #endif // ENABLE_I2C_WHITELIST_CHECK
     std::vector<uint8_t> readBuf(readCount);