Switch to lg2 for logging

After enabling C++20, lg2 is preferred for logging. This patch replaces
all phosphor::logging::log calls to lg2 calls.

Tested:
Build pass.

Change-Id: Ic37bc36f43c2b3a1c61b1328af95e3a41c8d6d40
Signed-off-by: Jiaqing Zhao <jiaqing.zhao@intel.com>
diff --git a/user_mgr.cpp b/user_mgr.cpp
index e085b28..a2b6a76 100644
--- a/user_mgr.cpp
+++ b/user_mgr.cpp
@@ -33,7 +33,7 @@
 #include <boost/algorithm/string/split.hpp>
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/elog.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 #include <xyz/openbmc_project/User/Common/error.hpp>
 
@@ -138,8 +138,8 @@
     }
     if (!allowed)
     {
-        log<level::ERR>("Invalid group name; not in the allowed list",
-                        entry("GroupName=%s", groupName.c_str()));
+        lg2::error("Group name '{GROUP}' is not in the allowed list", "GROUP",
+                   groupName);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group Name"),
                               Argument::ARGUMENT_VALUE(groupName.c_str()));
     }
@@ -184,7 +184,7 @@
 {
     if (userName.empty())
     {
-        log<level::ERR>("User name is empty");
+        lg2::error("User name is empty");
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"),
                               Argument::ARGUMENT_VALUE("Null"));
     }
@@ -199,8 +199,7 @@
 {
     if (!isUserExist(userName))
     {
-        log<level::ERR>("User does not exist",
-                        entry("USER_NAME=%s", userName.c_str()));
+        lg2::error("User '{USERNAME}' does not exist", "USERNAME", userName);
         elog<UserNameDoesNotExist>();
     }
 }
@@ -212,8 +211,7 @@
         !std::regex_match(groupName.c_str(),
                           std::regex("[a-zA-z_][a-zA-Z_0-9]*")))
     {
-        log<level::ERR>("Invalid group name",
-                        entry("GroupName=%s", groupName.c_str()));
+        lg2::error("Invalid group name '{GROUP}'", "GROUP", groupName);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group Name"),
                               Argument::ARGUMENT_VALUE(groupName.c_str()));
     }
@@ -224,8 +222,7 @@
 {
     if (isUserExist(userName))
     {
-        log<level::ERR>("User already exists",
-                        entry("USER_NAME=%s", userName.c_str()));
+        lg2::error("User '{USERNAME}' already exists", "USERNAME", userName);
         elog<UserNameExists>();
     }
 }
@@ -238,8 +235,10 @@
     {
         if (userName.length() > ipmiMaxUserNameLen)
         {
-            log<level::ERR>("IPMI user name length limitation",
-                            entry("SIZE=%d", userName.length()));
+            lg2::error("User '{USERNAME}' exceeds IPMI username length limit "
+                       "({LENGTH} > {LIMIT})",
+                       "USERNAME", userName, "LENGTH", userName.length(),
+                       "LIMIT", ipmiMaxUserNameLen);
             elog<UserNameGroupFail>(
                 xyz::openbmc_project::User::Common::UserNameGroupFail::REASON(
                     "IPMI length"));
@@ -247,16 +246,17 @@
     }
     if (userName.length() > systemMaxUserNameLen)
     {
-        log<level::ERR>("User name length limitation",
-                        entry("SIZE=%d", userName.length()));
+        lg2::error("User '{USERNAME}' exceeds system username length limit "
+                   "({LENGTH} > {LIMIT})",
+                   "USERNAME", userName, "LENGTH", userName.length(), "LIMIT",
+                   systemMaxUserNameLen);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"),
                               Argument::ARGUMENT_VALUE("Invalid length"));
     }
     if (!std::regex_match(userName.c_str(),
                           std::regex("[a-zA-z_][a-zA-Z_0-9]*")))
     {
-        log<level::ERR>("Invalid user name",
-                        entry("USER_NAME=%s", userName.c_str()));
+        lg2::error("Invalid username '{USERNAME}'", "USERNAME", userName);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"),
                               Argument::ARGUMENT_VALUE("Invalid data"));
     }
@@ -270,10 +270,10 @@
     {
         if (getIpmiUsersCount() >= ipmiMaxUsers)
         {
-            log<level::ERR>("IPMI user limit reached");
+            lg2::error("IPMI user limit reached");
             elog<NoResource>(
                 xyz::openbmc_project::User::Common::NoResource::REASON(
-                    "ipmi user count reached"));
+                    "IPMI user limit reached"));
         }
     }
     else
@@ -281,10 +281,10 @@
         if (usersList.size() > 0 && (usersList.size() - getIpmiUsersCount()) >=
                                         (maxSystemUsers - ipmiMaxUsers))
         {
-            log<level::ERR>("Non-ipmi User limit reached");
+            lg2::error("Non-ipmi User limit reached");
             elog<NoResource>(
                 xyz::openbmc_project::User::Common::NoResource::REASON(
-                    "Non-ipmi user count reached"));
+                    "Non-ipmi user limit reached"));
         }
     }
     return;
@@ -295,7 +295,7 @@
     if (!priv.empty() &&
         (std::find(privMgr.begin(), privMgr.end(), priv) == privMgr.end()))
     {
-        log<level::ERR>("Invalid privilege");
+        lg2::error("Invalid privilege '{PRIVILEGE}'", "PRIVILEGE", priv);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege"),
                               Argument::ARGUMENT_VALUE(priv.c_str()));
     }
@@ -308,7 +308,7 @@
         if (std::find(groupsMgr.begin(), groupsMgr.end(), group) ==
             groupsMgr.end())
         {
-            log<level::ERR>("Invalid Group Name listed");
+            lg2::error("Invalid Group Name '{GROUPNAME}'", "GROUPNAME", group);
             elog<InvalidArgument>(Argument::ARGUMENT_NAME("GroupName"),
                                   Argument::ARGUMENT_VALUE(group.c_str()));
         }
@@ -370,7 +370,8 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Unable to create new user");
+        lg2::error("Unable to create new user '{USERNAME}'", "USERNAME",
+                   userName);
         elog<InternalFailure>();
     }
 
@@ -383,8 +384,7 @@
         userName, std::make_unique<phosphor::user::Users>(
                       bus, userObj.c_str(), groupNames, priv, enabled, *this));
 
-    log<level::INFO>("User created successfully",
-                     entry("USER_NAME=%s", userName.c_str()));
+    lg2::info("User '{USERNAME}' created successfully", "USERNAME", userName);
     return;
 }
 
@@ -399,15 +399,13 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("User delete failed",
-                        entry("USER_NAME=%s", userName.c_str()));
+        lg2::error("Delete User '{USERNAME}' failed", "USERNAME", userName);
         elog<InternalFailure>();
     }
 
     usersList.erase(userName);
 
-    log<level::INFO>("User deleted successfully",
-                     entry("USER_NAME=%s", userName.c_str()));
+    lg2::info("User '{USERNAME}' deleted successfully", "USERNAME", userName);
     return;
 }
 
@@ -416,8 +414,7 @@
     if (std::find(groupsMgr.begin(), groupsMgr.end(), groupName) ==
         groupsMgr.end())
     {
-        log<level::ERR>("Group already exists",
-                        entry("GROUP_NAME=%s", groupName.c_str()));
+        lg2::error("Group '{GROUP}' already exists", "GROUP", groupName);
         elog<GroupNameDoesNotExists>();
     }
     checkAndThrowsForGroupChangeAllowed(groupName);
@@ -432,15 +429,13 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Group delete failed",
-                        entry("GROUP_NAME=%s", groupName.c_str()));
+        lg2::error("Failed to delete group '{GROUP}'", "GROUP", groupName);
         elog<InternalFailure>();
     }
 
     groupsMgr.erase(std::find(groupsMgr.begin(), groupsMgr.end(), groupName));
     UserMgrIface::allGroups(groupsMgr);
-    log<level::INFO>("Group deleted successfully",
-                     entry("GROUP_NAME=%s", groupName.c_str()));
+    lg2::info("Successfully deleted group '{GROUP}'", "GROUP", groupName);
 }
 
 void UserMgr::checkCreateGroupConstraints(const std::string& groupName)
@@ -448,14 +443,13 @@
     if (std::find(groupsMgr.begin(), groupsMgr.end(), groupName) !=
         groupsMgr.end())
     {
-        log<level::ERR>("Group already exists",
-                        entry("GROUP_NAME=%s", groupName.c_str()));
+        lg2::error("Group '{GROUP}' already exists", "GROUP", groupName);
         elog<GroupNameExists>();
     }
     checkAndThrowForDisallowedGroupCreation(groupName);
     if (groupsMgr.size() >= maxSystemGroupCount)
     {
-        log<level::ERR>("Group limit reached");
+        lg2::error("Group limit reached");
         elog<NoResource>(xyz::openbmc_project::User::Common::NoResource::REASON(
             "Group limit reached"));
     }
@@ -470,8 +464,7 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Group create failed",
-                        entry("GROUP_NAME=%s", groupName.c_str()));
+        lg2::error("Failed to create group '{GROUP}'", "GROUP", groupName);
         elog<InternalFailure>();
     }
     groupsMgr.push_back(groupName);
@@ -492,8 +485,8 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("User rename failed",
-                        entry("USER_NAME=%s", userName.c_str()));
+        lg2::error("Rename '{USERNAME}' to '{NEWUSERNAME}' failed", "USERNAME",
+                   userName, "NEWUSERNAME", newUserName);
         elog<InternalFailure>();
     }
     const auto& user = usersList[userName];
@@ -556,16 +549,17 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Unable to modify user privilege / groups");
+        lg2::error(
+            "Unable to modify user privilege / groups for user '{USERNAME}'",
+            "USERNAME", userName);
         elog<InternalFailure>();
     }
 
-    log<level::INFO>("User groups / privilege updated successfully",
-                     entry("USER_NAME=%s", userName.c_str()));
     std::sort(groupNames.begin(), groupNames.end());
     usersList[userName]->setUserGroups(groupNames);
     usersList[userName]->setUserPrivilege(priv);
-    return;
+    lg2::info("User '{USERNAME}' groups / privilege updated successfully",
+              "USERNAME", userName);
 }
 
 uint8_t UserMgr::minPasswordLength(uint8_t value)
@@ -576,10 +570,9 @@
     }
     if (value < minPasswdLength)
     {
-        log<level::ERR>(("Attempting to set minPasswordLength to less than " +
-                         std::to_string(minPasswdLength))
-                            .c_str(),
-                        entry("SIZE=%d", value));
+        lg2::error("Attempting to set minPasswordLength to {VALUE}, less than "
+                   "{MINVALUE}",
+                   "VALUE", value, "MINVALUE", minPasswdLength);
         elog<InvalidArgument>(
             Argument::ARGUMENT_NAME("minPasswordLength"),
             Argument::ARGUMENT_VALUE(std::to_string(value).c_str()));
@@ -587,7 +580,8 @@
     if (setPamModuleArgValue(pamCrackLib, minPasswdLenProp,
                              std::to_string(value)) != success)
     {
-        log<level::ERR>("Unable to set minPasswordLength");
+        lg2::error("Unable to set minPasswordLength to {VALUE}", "VALUE",
+                   value);
         elog<InternalFailure>();
     }
     return AccountPolicyIface::minPasswordLength(value);
@@ -602,7 +596,8 @@
     if (setPamModuleArgValue(pamPWHistory, remOldPasswdCount,
                              std::to_string(value)) != success)
     {
-        log<level::ERR>("Unable to set rememberOldPasswordTimes");
+        lg2::error("Unable to set rememberOldPasswordTimes to {VALUE}", "VALUE",
+                   value);
         elog<InternalFailure>();
     }
     return AccountPolicyIface::rememberOldPasswordTimes(value);
@@ -617,7 +612,8 @@
     if (setPamModuleArgValue(pamTally2, maxFailedAttempt,
                              std::to_string(value)) != success)
     {
-        log<level::ERR>("Unable to set maxLoginAttemptBeforeLockout");
+        lg2::error("Unable to set maxLoginAttemptBeforeLockout to {VALUE}",
+                   "VALUE", value);
         elog<InternalFailure>();
     }
     return AccountPolicyIface::maxLoginAttemptBeforeLockout(value);
@@ -632,7 +628,8 @@
     if (setPamModuleArgValue(pamTally2, unlockTimeout, std::to_string(value)) !=
         success)
     {
-        log<level::ERR>("Unable to set accountUnlockTimeout");
+        lg2::error("Unable to set accountUnlockTimeout to {VALUE}", "VALUE",
+                   value);
         elog<InternalFailure>();
     }
     return AccountPolicyIface::accountUnlockTimeout(value);
@@ -654,8 +651,8 @@
     std::ifstream fileToRead(fileName, std::ios::in);
     if (!fileToRead.is_open())
     {
-        log<level::ERR>("Failed to open pam configuration file",
-                        entry("FILE_NAME=%s", fileName.c_str()));
+        lg2::error("Failed to open pam configuration file {FILENAME}",
+                   "FILENAME", fileName);
         return failure;
     }
     std::string line;
@@ -709,8 +706,8 @@
     std::ofstream fileToWrite(tmpFileName, std::ios::out);
     if (!fileToRead.is_open() || !fileToWrite.is_open())
     {
-        log<level::ERR>("Failed to open pam configuration /tmp file",
-                        entry("FILE_NAME=%s", fileName.c_str()));
+        lg2::error("Failed to open pam configuration file {FILENAME}",
+                   "FILENAME", fileName);
         return failure;
     }
     std::string line;
@@ -772,15 +769,14 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Unable to modify user enabled state");
+        lg2::error("Unable to modify user enabled state for '{USERNAME}'",
+                   "USERNAME", userName);
         elog<InternalFailure>();
     }
 
-    log<level::INFO>("User enabled/disabled state updated successfully",
-                     entry("USER_NAME=%s", userName.c_str()),
-                     entry("ENABLED=%d", enabled));
     usersList[userName]->setUserEnabled(enabled);
-    return;
+    lg2::info("User '{USERNAME}' has been {STATUS}", "USERNAME", userName,
+              "STATUS", enabled ? "Enabled" : "Disabled");
 }
 
 /**
@@ -811,7 +807,7 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Unable to read login failure counter");
+        lg2::error("Unable to read login failure counter");
         elog<InternalFailure>();
     }
 
@@ -831,8 +827,7 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Exception for userLockedForFailedAttempt",
-                        entry("WHAT=%s", e.what()));
+        lg2::error("Exception for userLockedForFailedAttempt: {ERR}", "ERR", e);
         elog<InternalFailure>();
     }
 
@@ -845,7 +840,7 @@
     // available
     if (splitWords.size() < 4)
     {
-        log<level::ERR>("Unable to read latest failure date/time");
+        lg2::error("Unable to read latest failure date/time");
         elog<InternalFailure>();
     }
 
@@ -858,7 +853,7 @@
     std::tm tmStruct = {};
     if (!strptime(failDateTime.c_str(), "%D %H:%M:%S", &tmStruct))
     {
-        log<level::ERR>("Failed to parse latest failure date/time");
+        lg2::error("Failed to parse latest failure date/time");
         elog<InternalFailure>();
     }
 
@@ -889,7 +884,7 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Unable to reset login failure counter");
+        lg2::error("Unable to reset login failure counter");
         elog<InternalFailure>();
     }
 
@@ -954,7 +949,7 @@
     phosphor::user::File passwd(passwdFileName, "r");
     if ((passwd)() == NULL)
     {
-        log<level::ERR>("Error opening the passwd file");
+        lg2::error("Error opening {FILENAME}", "FILENAME", passwdFileName);
         elog<InternalFailure>();
     }
 
@@ -1045,8 +1040,7 @@
     }
     else
     {
-        log<level::ERR>("Group not found",
-                        entry("GROUP=%s", groupName.c_str()));
+        lg2::error("Group '{GROUPNAME}' not found", "GROUPNAME", groupName);
         // Don't throw error, just return empty userList - fallback
     }
     return usersInGroup;
@@ -1071,15 +1065,13 @@
     }
     catch (const InternalFailure& e)
     {
-        log<level::ERR>("Unable to get the User Service",
-                        entry("WHAT=%s", e.what()));
+        lg2::error("Unable to get the User Service: {ERR}", "ERR", e);
         throw;
     }
     catch (const sdbusplus::exception_t& e)
     {
-        log<level::ERR>(
-            "Failed to excute method", entry("METHOD=%s", "GetManagedObjects"),
-            entry("PATH=%s", ldapMgrObjBasePath), entry("WHAT=%s", e.what()));
+        lg2::error("Failed to excute GetManagedObjects at {PATH}: {ERR}",
+                   "PATH", ldapMgrObjBasePath, "ERR", e);
         throw;
     }
     return objects;
@@ -1097,7 +1089,7 @@
 
     if (mapperResponseMsg.is_method_error())
     {
-        log<level::ERR>("Error in mapper call");
+        lg2::error("Error in mapper call");
         elog<InternalFailure>();
     }
 
@@ -1106,7 +1098,7 @@
 
     if (mapperResponse.begin() == mapperResponse.end())
     {
-        log<level::ERR>("Invalid response from mapper");
+        lg2::error("Invalid response from mapper");
         elog<InternalFailure>();
     }
 
@@ -1136,8 +1128,7 @@
         return pwd.pw_gid;
     }
 
-    log<level::ERR>("User noes not exist",
-                    entry("USER_NAME=%s", userName.c_str()));
+    lg2::error("User {USERNAME} does not exist", "USERNAME", userName);
     elog<UserNameDoesNotExist>();
 }
 
@@ -1167,8 +1158,8 @@
         buflen *= 2;
         buffer.resize(buflen);
 
-        log<level::DEBUG>("Increase buffer for getgrnam_r()",
-                          entry("BUFFER_LENGTH=%zu", buflen));
+        lg2::debug("Increase buffer for getgrnam_r() to {SIZE}", "SIZE",
+                   buflen);
 
         status = getgrnam_r(groupName.c_str(), &grp, buffer.data(),
                             buffer.size(), &grpPtr);
@@ -1194,13 +1185,12 @@
     }
     else if (status == ERANGE)
     {
-        log<level::ERR>("Group info requires too much memory",
-                        entry("GROUP_NAME=%s", groupName.c_str()));
+        lg2::error("Group info of {GROUP} requires too much memory", "GROUP",
+                   groupName);
     }
     else
     {
-        log<level::ERR>("Group does not exist",
-                        entry("GROUP_NAME=%s", groupName.c_str()));
+        lg2::error("Group {GROUP} does not exist", "GROUP", groupName);
     }
 
     return false;
@@ -1308,15 +1298,14 @@
             }
             else
             {
-                log<level::WARNING>("LDAP group privilege mapping does not "
-                                    "exist, default \"priv-user\" is used");
+                lg2::warning("LDAP group privilege mapping does not exist, "
+                             "default \"priv-user\" is used");
                 userInfo.emplace("UserPrivilege", "priv-user");
             }
         }
         catch (const std::bad_variant_access& e)
         {
-            log<level::ERR>("Error while accessing variant",
-                            entry("WHAT=%s", e.what()));
+            lg2::error("Error while accessing variant: {ERR}", "ERR", e);
             elog<InternalFailure>();
         }
         userInfo.emplace("RemoteUser", true);
@@ -1348,8 +1337,7 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Exception for MinPasswordLength",
-                            entry("WHAT=%s", e.what()));
+            lg2::error("Exception for MinPasswordLength: {ERR}", "ERR", e);
             throw;
         }
         AccountPolicyIface::minPasswordLength(value);
@@ -1374,8 +1362,8 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Exception for RememberOldPasswordTimes",
-                            entry("WHAT=%s", e.what()));
+            lg2::error("Exception for RememberOldPasswordTimes: {ERR}", "ERR",
+                       e);
             throw;
         }
         AccountPolicyIface::rememberOldPasswordTimes(value);
@@ -1399,8 +1387,8 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Exception for MaxLoginAttemptBeforLockout",
-                            entry("WHAT=%s", e.what()));
+            lg2::error("Exception for MaxLoginAttemptBeforLockout: {ERR}",
+                       "ERR", e);
             throw;
         }
         AccountPolicyIface::maxLoginAttemptBeforeLockout(value16);
@@ -1424,8 +1412,7 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Exception for AccountUnlockTimeout",
-                            entry("WHAT=%s", e.what()));
+            lg2::error("Exception for AccountUnlockTimeout: {ERR}", "ERR", e);
             throw;
         }
         AccountPolicyIface::accountUnlockTimeout(value32);