logging: switch to lg2

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: If28e6c9c7ae4eae5d17d28e055c90a04750834ac
diff --git a/dbusUtils.hpp b/dbusUtils.hpp
index f718e6d..8aed268 100644
--- a/dbusUtils.hpp
+++ b/dbusUtils.hpp
@@ -1,5 +1,4 @@
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 const char* propIntf = "org.freedesktop.DBus.Properties";
@@ -10,7 +9,6 @@
 const char* methodGetObject = "GetObject";
 const char* methodGet = "Get";
 
-using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 
 using Value = std::variant<int64_t, double, std::string, bool>;
diff --git a/meson.build b/meson.build
index a6cc5a5..5ff07fb 100644
--- a/meson.build
+++ b/meson.build
@@ -58,7 +58,6 @@
             'sdeventplus',
             fallback: ['sdeventplus', 'sdeventplus_dep'],
         ),
-        dependency('fmt'),
         exprtk,
         nlohmann_json,
     ],
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index 0f27019..ce69291 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -2,9 +2,7 @@
 
 #include "config.hpp"
 
-#include <fmt/format.h>
-
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdeventplus/event.hpp>
 
 #include <fstream>
@@ -20,7 +18,7 @@
     "xyz.openbmc_project.Configuration.ModifiedMedian"};
 static constexpr auto defaultHysteresis = 0;
 
-using namespace phosphor::logging;
+PHOSPHOR_LOG2_USING_WITH_FLAGS;
 
 int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error*)
 {
@@ -90,8 +88,7 @@
     }
     catch (const std::exception& ex)
     {
-        log<level::ERR>("Failed to parse association",
-                        entry("EX=%s", ex.what()));
+        error("Failed to parse association: {ERROR}", "ERROR", ex);
     }
     return assocs;
 }
@@ -120,8 +117,7 @@
     }
     else if (required)
     {
-        log<level::ERR>("Required field missing in config",
-                        entry("NAME=%s", name.c_str()));
+        error("Required field {NAME} missing in config", "NAME", name);
         throw std::invalid_argument("Required field missing in config");
     }
     return std::numeric_limits<U>::quiet_NaN();
@@ -332,17 +328,15 @@
     exprtk::parser<double> parser{};
     if (!parser.compile(exprStr, expression))
     {
-        log<level::ERR>("Expression compilation failed");
+        error("Expression compilation failed");
 
         for (std::size_t i = 0; i < parser.error_count(); ++i)
         {
-            auto error = parser.get_error(i);
-
-            log<level::ERR>(
-                fmt::format(
-                    "Position: {} Type: {} Message: {}", error.token.position,
-                    exprtk::parser_error::to_str(error.mode), error.diagnostic)
-                    .c_str());
+            auto err = parser.get_error(i);
+            error("Error parsing token at {POSITION}: {ERROR}", "POSITION",
+                  err.token.position, "TYPE",
+                  exprtk::parser_error::to_str(err.mode), "ERROR",
+                  err.diagnostic);
         }
         throw std::runtime_error("Expression compilation failed");
     }
@@ -634,16 +628,15 @@
     std::ifstream jsonFile(configFile);
     if (!jsonFile.is_open())
     {
-        log<level::ERR>("config JSON file not found",
-                        entry("FILENAME=%s", configFile.c_str()));
+        error("config JSON file {FILENAME} not found", "FILENAME", configFile);
         return {};
     }
 
     auto data = Json::parse(jsonFile, nullptr, false);
     if (data.is_discarded())
     {
-        log<level::ERR>("config readings JSON parser failure",
-                        entry("FILENAME=%s", configFile.c_str()));
+        error("config readings JSON parser failure with {FILENAME}", "FILENAME",
+              configFile);
         throw std::exception{};
     }
 
@@ -688,7 +681,7 @@
     auto eventHandler = [this](sdbusplus::message::message& message) {
         if (message.is_method_error())
         {
-            log<level::ERR>("Callback method error");
+            error("Callback method error");
             return;
         }
         this->propertiesChanged(message);
@@ -710,7 +703,7 @@
 {
     if (calculationIface.empty())
     {
-        log<level::ERR>("No calculation type supplied");
+        error("No calculation type supplied");
         return;
     }
     auto objects = getObjectsFromDBus();
@@ -729,14 +722,12 @@
         }
         if (name.empty())
         {
-            log<level::ERR>(
-                "Virtual Sensor name not found in entity manager config");
+            error("Virtual Sensor name not found in entity manager config");
             continue;
         }
         if (virtualSensorsMap.contains(name))
         {
-            log<level::ERR>("A virtual sensor with this name already exists",
-                            entry("NAME=%s", name.c_str()));
+            error("A virtual sensor named {NAME} already exists", "NAME", name);
             continue;
         }
 
@@ -758,8 +749,8 @@
         sensorType = getSensorTypeFromUnit(sensorUnit);
         if (sensorType.empty())
         {
-            log<level::ERR>("Sensor unit is not supported",
-                            entry("TYPE=%s", sensorUnit.c_str()));
+            error("Sensor unit type {TYPE} is not supported", "TYPE",
+                  sensorUnit);
             continue;
         }
 
@@ -770,8 +761,8 @@
             auto virtualSensorPtr = std::make_unique<VirtualSensor>(
                 bus, virtObjPath.c_str(), interfaceMap, name, sensorType,
                 calculationIface);
-            log<level::INFO>("Added a new virtual sensor",
-                             entry("NAME=%s", name.c_str()));
+            info("Added a new virtual sensor: {NAME} {TYPE}", "NAME", name,
+                 "TYPE", sensorType);
             virtualSensorPtr->updateVirtualSensor();
 
             /* Initialize unit value for virtual sensor */
@@ -791,8 +782,7 @@
                 message.read(path);
                 if (static_cast<const std::string&>(path) == objpath)
                 {
-                    log<level::INFO>("Removed a virtual sensor",
-                                     entry("NAME=%s", name.c_str()));
+                    info("Removed a virtual sensor: {NAME}", "NAME", name);
                     virtualSensorsMap.erase(name);
                 }
             };
@@ -807,8 +797,7 @@
         }
         catch (std::invalid_argument& ia)
         {
-            log<level::ERR>("Failed to set up virtual sensor",
-                            entry("Error=%s", ia.what()));
+            error("Failed to set up virtual sensor: {ERROR}", "ERROR", ia);
         }
     }
 }
@@ -840,13 +829,13 @@
 
                 if (desc.contains("Type"))
                 {
-                    auto path = "xyz.openbmc_project.Configuration." +
-                                desc.value("Type", "");
+                    auto type = desc.value("Type", "");
+                    auto path = "xyz.openbmc_project.Configuration." + type;
+
                     if (!isCalculationType(path))
                     {
-                        log<level::ERR>(
-                            "Invalid calculation type supplied\n",
-                            entry("TYPE=%s", desc.value("Type", "").c_str()));
+                        error("Invalid calculation type {TYPE} supplied.",
+                              "TYPE", type);
                         continue;
                     }
                     createVirtualSensorsFromDBus(path);
@@ -862,16 +851,15 @@
             {
                 if (unitMap.find(sensorType) == unitMap.end())
                 {
-                    log<level::ERR>("Sensor type is not supported",
-                                    entry("TYPE=%s", sensorType.c_str()));
+                    error("Sensor type {TYPE} is not supported", "TYPE",
+                          sensorType);
                 }
                 else
                 {
                     if (virtualSensorsMap.find(name) != virtualSensorsMap.end())
                     {
-                        log<level::ERR>(
-                            "A virtual sensor with this name already exists",
-                            entry("TYPE=%s", name.c_str()));
+                        error("A virtual sensor named {NAME} already exists",
+                              "NAME", name);
                         continue;
                     }
                     auto objPath = sensorDbusPath + sensorType + "/" + name;
@@ -879,8 +867,7 @@
                     auto virtualSensorPtr = std::make_unique<VirtualSensor>(
                         bus, objPath.c_str(), j, name);
 
-                    log<level::INFO>("Added a new virtual sensor",
-                                     entry("NAME=%s", name.c_str()));
+                    info("Added a new virtual sensor: {NAME}", "NAME", name);
                     virtualSensorPtr->updateVirtualSensor();
 
                     /* Initialize unit value for virtual sensor */
@@ -893,13 +880,14 @@
             }
             else
             {
-                log<level::ERR>("Sensor type or name not found in config file");
+                error(
+                    "Sensor type ({TYPE}) or name ({NAME}) not found in config file",
+                    "NAME", name, "TYPE", sensorType);
             }
         }
         else
         {
-            log<level::ERR>(
-                "Descriptor for new virtual sensor not found in config file");
+            error("Descriptor for new virtual sensor not found in config file");
         }
     }
 }
diff --git a/virtualSensor.hpp b/virtualSensor.hpp
index 3b47fd6..cdf1d97 100644
--- a/virtualSensor.hpp
+++ b/virtualSensor.hpp
@@ -2,9 +2,8 @@
 #include "exprtkTools.hpp"
 #include "thresholds.hpp"
 
-#include <fmt/format.h>
-
 #include <nlohmann/json.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 #include <xyz/openbmc_project/Association/Definitions/server.hpp>
 #include <xyz/openbmc_project/Sensor/Value/server.hpp>
@@ -17,6 +16,8 @@
 namespace virtualSensor
 {
 
+PHOSPHOR_LOG2_USING_WITH_FLAGS;
+
 using BasicVariantType =
     std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
                  int16_t, uint16_t, uint8_t, bool, std::vector<std::string>>;
@@ -205,16 +206,16 @@
         {
             if (!alarmHigh)
             {
-                constexpr auto msg =
-                    "ASSERT: {} has exceeded the {} high threshold";
-                log<level::ERR>(fmt::format(msg, name, tname).c_str());
+                error("ASSERT: sensor {SENSOR} is above the upper threshold "
+                      "{THRESHOLD}.",
+                      "SENSOR", name, "THRESHOLD", tname);
                 threshold->alarmHighSignalAsserted(value);
             }
             else
             {
-                constexpr auto msg =
-                    "DEASSERT: {} is under the {} high threshold";
-                log<level::INFO>(fmt::format(msg, name, tname).c_str());
+                info("DEASSERT: sensor {SENSOR} is under the upper threshold "
+                     "{THRESHOLD}.",
+                     "SENSOR", name, "THRESHOLD", tname);
                 threshold->alarmHighSignalDeasserted(value);
             }
             threshold->alarmHigh(!alarmHigh);
@@ -227,15 +228,16 @@
         {
             if (!alarmLow)
             {
-                constexpr auto msg = "ASSERT: {} is under the {} low threshold";
-                log<level::ERR>(fmt::format(msg, name, tname).c_str());
+                error("ASSERT: sensor {SENSOR} is below the lower threshold "
+                      "{THRESHOLD}.",
+                      "SENSOR", name, "THRESHOLD", tname);
                 threshold->alarmLowSignalAsserted(value);
             }
             else
             {
-                constexpr auto msg =
-                    "DEASSERT: {} is above the {} low threshold";
-                log<level::INFO>(fmt::format(msg, name, tname).c_str());
+                info("DEASSERT: sensor {SENSOR} is above the lower threshold "
+                     "{THRESHOLD}.",
+                     "SENSOR", name, "THRESHOLD", tname);
                 threshold->alarmLowSignalDeasserted(value);
             }
             threshold->alarmLow(!alarmLow);