logging: switch networkd static_library to lg2

This commit switches logging to lg2 in the following files -
1. src/network_manager.cpp
2. src/util.cpp
3. src/ethernet_interface.cpp

Tested: Build passes with "ninja -C build".
Change-Id: Ib2492116ecf3f41f9cd0146dc4b5389a59bce87c
Signed-off-by: Jagpal Singh Gill <paligill@gmail.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 5e61cbd..c5d6bdc 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -15,7 +15,7 @@
 #include <algorithm>
 #include <filesystem>
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <stdplus/raw.hpp>
 #include <stdplus/zstring.hpp>
 #include <string>
@@ -56,8 +56,8 @@
     }
     catch (const std::exception& e)
     {
-        auto err = fmt::format("{} failed on {}: {}", msg, intf, e.what());
-        log<level::ERR>(err.c_str(), entry("INTERFACE=%s", intf.c_str()));
+        lg2::error("{MSG} failed on {INTERFACE_NAME}: {ERROR}", "MSG", msg,
+                   "INTERFACE_NAME", intf, "ERROR", e);
     }
     return fallback;
 }
@@ -195,8 +195,8 @@
 {
     if (!info.mac || !info.addr)
     {
-        auto msg = fmt::format("Missing neighbor mac on {}\n", interfaceName());
-        log<level::ERR>(msg.c_str());
+        lg2::error("Missing neighbor mac on {INTERFACE_NAME}", "INTERFACE_NAME",
+                   interfaceName());
         return;
     }
 
@@ -233,8 +233,8 @@
     }
     catch (const std::exception& e)
     {
-        auto msg = fmt::format("Invalid IP `{}`: {}\n", ipaddress, e.what());
-        log<level::ERR>(msg.c_str(), entry("ADDRESS=%s", ipaddress.c_str()));
+        lg2::error("Invalid IP {IP_ADDRESS}: {ERROR}", "IP_ADDRESS", ipaddress,
+                   "ERROR", e);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"),
                               Argument::ARGUMENT_VALUE(ipaddress.c_str()));
     }
@@ -245,10 +245,8 @@
     }
     catch (const std::exception& e)
     {
-        auto msg = fmt::format("Invalid prefix length `{}`: {}\n", prefixLength,
-                               e.what());
-        log<level::ERR>(msg.c_str(),
-                        entry("PREFIXLENGTH=%" PRIu8, prefixLength));
+        lg2::error("Invalid prefix length {PREFIXLENGTH}: {ERROR}",
+                   "PREFIXLENGTH", prefixLength, "ERROR", e);
         elog<InvalidArgument>(
             Argument::ARGUMENT_NAME("prefixLength"),
             Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str()));
@@ -287,9 +285,8 @@
     }
     catch (const std::exception& e)
     {
-        auto msg =
-            fmt::format("Not a valid IP address `{}`: {}", ipAddress, e.what());
-        log<level::ERR>(msg.c_str(), entry("ADDRESS=%s", ipAddress.c_str()));
+        lg2::error("Not a valid IP address {IP_ADDRESS}: {ERROR}", "IP_ADDRESS",
+                   ipAddress, "ERROR", e);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipAddress"),
                               Argument::ARGUMENT_VALUE(ipAddress.c_str()));
     }
@@ -301,10 +298,8 @@
     }
     catch (const std::exception& e)
     {
-        auto msg = fmt::format("Not a valid MAC address `{}`: {}", macAddress,
-                               e.what());
-        log<level::ERR>(msg.c_str(),
-                        entry("MACADDRESS=%s", macAddress.c_str()));
+        lg2::error("Not a valid MAC address {MAC_ADDRESS}: {ERROR}",
+                   "MAC_ADDRESS", macAddress, "ERROR", e);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("macAddress"),
                               Argument::ARGUMENT_VALUE(macAddress.c_str()));
     }
@@ -443,9 +438,8 @@
         }
         catch (const std::exception& e)
         {
-            auto msg =
-                fmt::format("Not a valid IP address `{}`: {}", ip, e.what());
-            log<level::ERR>(msg.c_str()), entry("ADDRESS=%s", ip.c_str());
+            lg2::error("Not a valid IP address {IP_ADDRESS}: {ERROR}",
+                       "IP_ADDRESS", ip, "ERROR", e);
             elog<InvalidArgument>(Argument::ARGUMENT_NAME("StaticNameserver"),
                                   Argument::ARGUMENT_VALUE(ip.c_str()));
         }
@@ -459,7 +453,7 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Exception processing DNS entries");
+        lg2::error("Exception processing DNS entries: {ERROR}", "ERROR", e);
     }
     return EthernetInterfaceIntf::staticNameServers();
 }
@@ -496,8 +490,9 @@
     }
     catch (const sdbusplus::exception::SdBusError& e)
     {
-        log<level::ERR>(
-            "Failed to get NTP server information from Systemd-Timesyncd");
+        lg2::error("Failed to get NTP server information from "
+                   "Systemd-Timesyncd: {ERROR}",
+                   "ERROR", e);
     }
 
     return servers;
@@ -538,7 +533,9 @@
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>("Failed to get DNS information from Systemd-Resolved");
+        lg2::error(
+            "Failed to get DNS information from Systemd-Resolved: {ERROR}",
+            "ERROR", e);
     }
     auto tupleVector = std::get_if<type>(&name);
     for (auto i = tupleVector->begin(); i != tupleVector->end(); ++i)
@@ -558,7 +555,7 @@
     if (manager.get().interfaces.find(intfName) !=
         manager.get().interfaces.end())
     {
-        log<level::ERR>("VLAN already exists", entry("VLANID=%u", id));
+        lg2::error("VLAN {VLAN_ID} already exists", "VLAN_ID", id);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("VLANId"),
                               Argument::ARGUMENT_VALUE(idStr.c_str()));
     }
@@ -614,7 +611,7 @@
     }
     catch (InternalFailure& e)
     {
-        log<level::ERR>("Exception processing NTP entries");
+        lg2::error("Exception processing NTP entries: {ERROR}", "ERROR", e);
     }
     return EthernetInterfaceIntf::staticNTPServers();
 }
@@ -740,15 +737,14 @@
     auto path =
         config::pathForIntfConf(manager.get().getConfDir(), interfaceName());
     config.writeFile(path);
-    auto msg = fmt::format("Wrote networkd file: {}", path.native());
-    log<level::INFO>(msg.c_str(), entry("FILE=%s", path.c_str()));
+    lg2::info("Wrote networkd file: {FILE_PATH}", "FILE_PATH", path);
 }
 
 std::string EthernetInterface::macAddress([[maybe_unused]] std::string value)
 {
     if (vlan)
     {
-        log<level::ERR>("Tried to set MAC address on VLAN");
+        lg2::error("Tried to set MAC address on VLAN");
         elog<InternalFailure>();
     }
 #ifdef PERSIST_MAC
@@ -759,15 +755,15 @@
     }
     catch (const std::invalid_argument&)
     {
-        log<level::ERR>("MACAddress is not valid.",
-                        entry("MAC=%s", value.c_str()));
+        lg2::error("MAC Address {MAC_ADDRESS} is not valid", "MAC_ADDRESS",
+                   value);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
                               Argument::ARGUMENT_VALUE(value.c_str()));
     }
     if (!mac_address::isUnicast(newMAC))
     {
-        log<level::ERR>("MACAddress is not valid.",
-                        entry("MAC=%s", value.c_str()));
+        lg2::error("MAC Address {MAC_ADDRESS} is not valid", "MAC_ADDRESS",
+                   value);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
                               Argument::ARGUMENT_VALUE(value.c_str()));
     }
@@ -836,8 +832,8 @@
     }
     catch (const std::exception& e)
     {
-        auto msg = fmt::format("Invalid v4 GW `{}`: {}", gateway, e.what());
-        log<level::ERR>(msg.c_str(), entry("GATEWAY=%s", gateway.c_str()));
+        lg2::error("Invalid v4 GW {GATEWAY}: {ERROR}", "GATEWAY", gateway,
+                   "ERROR", e);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"),
                               Argument::ARGUMENT_VALUE(gateway.c_str()));
     }
@@ -865,8 +861,8 @@
     }
     catch (const std::exception& e)
     {
-        auto msg = fmt::format("Invalid v6 GW `{}`: {}", gateway, e.what());
-        log<level::ERR>(msg.c_str(), entry("GATEWAY=%s", gateway.c_str()));
+        lg2::error("Invalid v6 GW {GATEWAY}: {ERROR}", "GATEWAY", gateway,
+                   "ERROR", e);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"),
                               Argument::ARGUMENT_VALUE(gateway.c_str()));
     }
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index 060242c..ffcae48 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -12,7 +12,7 @@
 
 #include <filesystem>
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/message.hpp>
 #include <stdplus/pinned.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -70,11 +70,8 @@
             }
             catch (const std::exception& e)
             {
-                log<level::ERR>(
-                    fmt::format("AdministrativeState match parsing failed: {}",
-                                e.what())
-                        .c_str(),
-                    entry("ERROR=%s", e.what()));
+                lg2::error("AdministrativeState match parsing failed: {ERROR}",
+                           "ERROR", e);
             }
         })
 {
@@ -87,8 +84,8 @@
             }
             catch (const std::exception& ex)
             {
-                log<level::ERR>("Failed executing reload hook, ignoring",
-                                entry("ERR=%s", ex.what()));
+                lg2::error("Failed executing reload hook, ignoring: {ERROR}",
+                           "ERROR", ex);
             }
         }
         reloadPreHooks.clear();
@@ -98,12 +95,11 @@
                 .new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
                                  NETWORKD_INTERFACE, "Reload")
                 .call();
-            log<level::INFO>("Reloaded systemd-networkd");
+            lg2::info("Reloaded systemd-networkd");
         }
         catch (const sdbusplus::exception_t& ex)
         {
-            log<level::ERR>("Failed to reload configuration",
-                            entry("ERR=%s", ex.what()));
+            lg2::error("Failed to reload configuration: {ERROR}", "ERROR", ex);
             reloadPostHooks.clear();
         }
         for (auto& hook : reloadPostHooks)
@@ -114,8 +110,8 @@
             }
             catch (const std::exception& ex)
             {
-                log<level::ERR>("Failed executing reload hook, ignoring",
-                                entry("ERR=%s", ex.what()));
+                lg2::error("Failed executing reload hook, ignoring: {ERROR}",
+                           "ERROR", ex);
             }
         }
         reloadPostHooks.clear();
@@ -189,9 +185,8 @@
     }
     if (!info.intf.name)
     {
-        auto msg = fmt::format("Can't create interface without name: {}",
-                               info.intf.idx);
-        log<level::ERR>(msg.c_str(), entry("IFIDX=%u", info.intf.idx));
+        lg2::error("Can't create interface without name: {IFIDX}", "IFIDX",
+                   info.intf.idx);
         return;
     }
     config::Parser config(config::pathForIntfConf(confDir, *info.intf.name));
@@ -220,8 +215,8 @@
             if (!ignored.contains(*info.name))
             {
                 ignored.emplace(*info.name);
-                auto msg = fmt::format("Ignoring interface {}\n", *info.name);
-                log<level::INFO>(msg.c_str());
+                lg2::info("Ignoring interface {INTERFACE_NAME}",
+                          "INTERFACE_NAME", *info.name);
             }
             ignoredIntf.emplace(info.idx);
             return;
@@ -403,8 +398,7 @@
     }
     else if (!ignoredIntf.contains(ifidx))
     {
-        auto msg = fmt::format("Interface `{}` not found for gw", ifidx);
-        log<level::ERR>(msg.c_str(), entry("IFIDX=%u", ifidx));
+        lg2::error("Interface {IFIDX} not found for gw", "IFIDX", ifidx);
     }
 }
 
@@ -470,7 +464,7 @@
 {
     if (id == 0 || id >= 4095)
     {
-        log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
+        lg2::error("VLAN ID {VLAN_ID} is not valid", "VLAN_ID", id);
         elog<InvalidArgument>(
             Argument::ARGUMENT_NAME("VLANId"),
             Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
@@ -493,7 +487,7 @@
         std::error_code ec;
         std::filesystem::remove(dirent.path(), ec);
     }
-    log<level::INFO>("Network data purged.");
+    lg2::info("Network data purged.");
 }
 
 // Need to merge the below function with the code which writes the
diff --git a/src/util.cpp b/src/util.cpp
index 14a1cb3..0b9c1e2 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -11,7 +11,7 @@
 
 #include <cctype>
 #include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <string>
 #include <string_view>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -41,7 +41,7 @@
     else if (pid < 0)
     {
         auto error = errno;
-        log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
+        lg2::error("Error occurred during fork: {ERRNO}", "ERRNO", error);
         elog<InternalFailure>();
     }
     else if (pid > 0)
@@ -65,9 +65,8 @@
                 fmt::format_to(fmt::appender(buf), " `{}`", args[i]);
             }
             buf.push_back('\0');
-            log<level::ERR>("Unable to execute the command",
-                            entry("CMD=%s", buf.data()),
-                            entry("STATUS=%d", status));
+            lg2::error("Unable to execute the command {CMD}: {STATUS}", "CMD",
+                       buf.data(), "STATUS", status);
             elog<InternalFailure>();
         }
     }
@@ -174,17 +173,15 @@
     else if (auto str = config.map.getLastValueString(section, key);
              str == nullptr)
     {
-        auto err = fmt::format("Unable to get the value of {}[{}] from {}",
-                               section, key, config.getFilename().native());
-        log<level::NOTICE>(err.c_str(),
-                           entry("FILE=%s", config.getFilename().c_str()));
+        lg2::notice("Unable to get the value of {SECTION}[{KEY}] from {FILE}",
+                    "SECTION", section, "KEY", key, "FILE",
+                    config.getFilename());
     }
     else if (auto val = fun(*str); !val)
     {
-        auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section,
-                               key, config.getFilename().native(), *str);
-        log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()),
-                           entry("FILE=%s", config.getFilename().c_str()));
+        lg2::notice("Invalid value of {SECTION}[{KEY}] from {FILE}: {VALUE}",
+                    "SECTION", section, "KEY", key, "FILE",
+                    config.getFilename(), "VALUE", *str);
     }
     else
     {