logging: switch to lg2

It is recommended to use `phosphor::lg2` to format log,
and the correct `CODE_LINE` and `CODE_FUNC` values
can be used in log tracking.

Tested By: Built CertificateManager successfully and Unit Test passes.

Change-Id: Ib2ff946febfe0335d4ecf5fa932683d3a0f117bb
Signed-off-by: Ravi Teja <raviteja28031990@gmail.com>
diff --git a/certificate.cpp b/certificate.cpp
index 52c3c2a..e3fe609 100644
--- a/certificate.cpp
+++ b/certificate.cpp
@@ -18,7 +18,7 @@
 
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/elog.hpp>
-#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <watch.hpp>
 #include <xyz/openbmc_project/Certs/error.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
@@ -40,10 +40,6 @@
 {
 namespace fs = std::filesystem;
 using ::phosphor::logging::elog;
-using ::phosphor::logging::entry;
-using ::phosphor::logging::level;
-using ::phosphor::logging::log;
-using ::phosphor::logging::report;
 using InvalidCertificateError =
     ::sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate;
 using ::phosphor::logging::xyz::openbmc_project::Certs::InvalidCertificate;
@@ -108,9 +104,9 @@
     }
     catch (const std::exception& e)
     {
-        log<level::ERR>("Failed to dump certificate", entry("ERR=%s", e.what()),
-                        entry("SRC_PEM=%s", pem.c_str()),
-                        entry("DST=%s", certFilePath.c_str()));
+        lg2::error(
+            "Failed to dump certificate, ERR:{ERR}, SRC_PEM:{SRC_PEM}, DST:{DST}",
+            "ERR", e, "SRC_PEM", pem, "DST", certFilePath);
         elog<InternalFailure>();
     }
 }
@@ -142,10 +138,9 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Failed to copy certificate",
-                            entry("ERR=%s", e.what()),
-                            entry("SRC=%s", certSrcFilePath.c_str()),
-                            entry("DST=%s", certFilePath.c_str()));
+            lg2::error(
+                "Failed to copy certificate, ERR:{ERR}, SRC:{SRC}, DST:{DST}",
+                "ERR", e, "SRC", certSrcFilePath, "DST", certFilePath);
             elog<InternalFailure>();
         }
     }
@@ -157,9 +152,9 @@
     char* filePath = tempnam(directoryPath.c_str(), nullptr);
     if (filePath == nullptr)
     {
-        log<level::ERR>(
-            "Error occurred while creating random certificate file path",
-            entry("DIR=%s", directoryPath.c_str()));
+        lg2::error(
+            "Error occurred while creating random certificate file path, DIR:{DIR}",
+            "DIR", directoryPath);
         elog<InternalFailure>();
     }
     std::string filePathStr(filePath);
@@ -188,8 +183,8 @@
         }
     }
 
-    log<level::ERR>("Authority certificate x509 file path already used",
-                    entry("DIR=%s", certDstDirPath.c_str()));
+    lg2::error("Authority certificate x509 file path already used, DIR:{DIR}",
+               "DIR", certDstDirPath);
     elog<InternalFailure>();
 }
 
@@ -291,8 +286,8 @@
 {
     if (!fs::remove(certFilePath))
     {
-        log<level::INFO>("Certificate file not found!",
-                         entry("PATH=%s", certFilePath.c_str()));
+        lg2::info("Certificate file not found! PATH:{PATH}", "PATH",
+                  certFilePath);
     }
 }
 
@@ -305,13 +300,13 @@
 {
     if (restore)
     {
-        log<level::DEBUG>("Certificate install ",
-                          entry("FILEPATH=%s", certSrcFilePath.c_str()));
+        lg2::debug("Certificate install, FILEPATH:{FILEPATH}", "FILEPATH",
+                   certSrcFilePath);
     }
     else
     {
-        log<level::INFO>("Certificate install ",
-                         entry("FILEPATH=%s", certSrcFilePath.c_str()));
+        lg2::info("Certificate install, FILEPATH:{FILEPATH}", "FILEPATH",
+                  certSrcFilePath);
     }
 
     // stop watch for user initiated certificate install
@@ -324,8 +319,7 @@
     fs::path file(certSrcFilePath);
     if (!fs::exists(file))
     {
-        log<level::ERR>("File is Missing",
-                        entry("FILE=%s", certSrcFilePath.c_str()));
+        lg2::error("File is Missing, FILE:{FILE}", "FILE", certSrcFilePath);
         elog<InternalFailure>();
     }
 
@@ -334,8 +328,7 @@
         if (fs::file_size(certSrcFilePath) == 0)
         {
             // file is empty
-            log<level::ERR>("File is empty",
-                            entry("FILE=%s", certSrcFilePath.c_str()));
+            lg2::error("File is empty, FILE:{FILE}", "FILE", certSrcFilePath);
             elog<InvalidCertificateError>(
                 InvalidCertificate::REASON("File is empty"));
         }
@@ -343,7 +336,8 @@
     catch (const fs::filesystem_error& e)
     {
         // Log Error message
-        log<level::ERR>(e.what(), entry("FILE=%s", certSrcFilePath.c_str()));
+        lg2::error("File is empty, FILE:{FILE}, ERR:{ERR}", "FILE",
+                   certSrcFilePath, "ERR", e);
         elog<InternalFailure>();
     }
 
@@ -360,8 +354,8 @@
     // Invoke type specific append private key function.
     if (auto it = appendKeyMap.find(certType); it == appendKeyMap.end())
     {
-        log<level::ERR>("Unsupported Type",
-                        entry("TYPE=%s", certificateTypeToString(certType)));
+        lg2::error("Unsupported Type, TYPE:{TYPE}", "TYPE",
+                   certificateTypeToString(certType));
         elog<InternalFailure>();
     }
     else
@@ -372,8 +366,8 @@
     // Invoke type specific compare keys function.
     if (auto it = typeFuncMap.find(certType); it == typeFuncMap.end())
     {
-        log<level::ERR>("Unsupported Type",
-                        entry("TYPE=%s", certificateTypeToString(certType)));
+        lg2::error("Unsupported Type, TYPE:{TYPE}", "TYPE",
+                   certificateTypeToString(certType));
         elog<InternalFailure>();
     }
     else
@@ -402,20 +396,18 @@
 {
     if (restore)
     {
-        log<level::DEBUG>("Certificate install ",
-                          entry("PEM_STR=%s", pem.data()));
+        lg2::debug("Certificate install, PEM_STR:{PEM_STR}", "PEM_STR", pem);
     }
     else
     {
-        log<level::INFO>("Certificate install ",
-                         entry("PEM_STR=%s", pem.data()));
+        lg2::info("Certificate install, PEM_STR:{PEM_STR} ", "PEM_STR", pem);
     }
 
     if (certType != CertificateType::authority)
     {
-        log<level::ERR>("Bulk install error: Unsupported Type; only authority "
-                        "supports bulk install",
-                        entry("TYPE=%s", certificateTypeToString(certType)));
+        lg2::error("Bulk install error: Unsupported Type; only authority "
+                   "supports bulk install, TYPE:{TYPE}",
+                   "TYPE", certificateTypeToString(certType));
         elog<InternalFailure>();
     }
 
@@ -482,10 +474,10 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Failed to create symlink for certificate",
-                            entry("ERR=%s", e.what()),
-                            entry("FILE=%s", certFilePath.c_str()),
-                            entry("SYMLINK=%s", certFileX509Path.c_str()));
+            lg2::error("Failed to create symlink for certificate, ERR:{ERR},"
+                       "FILE:{FILE}, SYMLINK:{SYMLINK}",
+                       "ERR", e, "FILE", certFilePath, "SYMLINK",
+                       certFileX509Path);
             elog<InternalFailure>();
         }
     }
@@ -574,8 +566,8 @@
     BIOMemPtr keyBio(BIO_new(BIO_s_file()), ::BIO_free);
     if (!keyBio)
     {
-        log<level::ERR>("Error occurred during BIO_s_file call",
-                        entry("FILE=%s", filePath.c_str()));
+        lg2::error("Error occurred during BIO_s_file call, FILE:{FILE}", "FILE",
+                   filePath);
         elog<InternalFailure>();
     }
     BIO_read_filename(keyBio.get(), filePath.c_str());
@@ -585,14 +577,14 @@
         ::EVP_PKEY_free);
     if (!priKey)
     {
-        log<level::INFO>("Private key not present in file",
-                         entry("FILE=%s", filePath.c_str()));
+        lg2::info("Private key not present in file, FILE:{FILE}", "FILE",
+                  filePath);
         fs::path privateKeyFile = fs::path(certInstallPath).parent_path();
         privateKeyFile = privateKeyFile / defaultPrivateKeyFileName;
         if (!fs::exists(privateKeyFile))
         {
-            log<level::ERR>("Private key file is not found",
-                            entry("FILE=%s", privateKeyFile.c_str()));
+            lg2::error("Private key file is not found, FILE:{FILE}", "FILE",
+                       privateKeyFile);
             elog<InternalFailure>();
         }
 
@@ -615,10 +607,9 @@
         }
         catch (const std::exception& e)
         {
-            log<level::ERR>("Failed to append private key",
-                            entry("ERR=%s", e.what()),
-                            entry("SRC=%s", privateKeyFile.c_str()),
-                            entry("DST=%s", filePath.c_str()));
+            lg2::error(
+                "Failed to append private key, ERR:{ERR}, SRC:{SRC}, DST:{DST}",
+                "ERR", e, "SRC", privateKeyFile, "DST", filePath);
             elog<InternalFailure>();
         }
     }
@@ -626,22 +617,22 @@
 
 bool Certificate::compareKeys(const std::string& filePath)
 {
-    log<level::INFO>("Certificate compareKeys",
-                     entry("FILEPATH=%s", filePath.c_str()));
+    lg2::info("Certificate compareKeys, FILEPATH:{FILEPATH}", "FILEPATH",
+              filePath);
     internal::X509Ptr cert(X509_new(), ::X509_free);
     if (!cert)
     {
-        log<level::ERR>("Error occurred during X509_new call",
-                        entry("FILE=%s", filePath.c_str()),
-                        entry("ERRCODE=%lu", ERR_get_error()));
+        lg2::error(
+            "Error occurred during X509_new call, FILE:{FILE}, ERRCODE:{ERRCODE}",
+            "FILE", filePath, "ERRCODE", ERR_get_error());
         elog<InternalFailure>();
     }
 
     BIOMemPtr bioCert(BIO_new_file(filePath.c_str(), "rb"), ::BIO_free);
     if (!bioCert)
     {
-        log<level::ERR>("Error occurred during BIO_new_file call",
-                        entry("FILE=%s", filePath.c_str()));
+        lg2::error("Error occurred during BIO_new_file call, FILE:{FILE}",
+                   "FILE", filePath);
         elog<InternalFailure>();
     }
 
@@ -651,9 +642,9 @@
     EVPPkeyPtr pubKey(X509_get_pubkey(cert.get()), ::EVP_PKEY_free);
     if (!pubKey)
     {
-        log<level::ERR>("Error occurred during X509_get_pubkey",
-                        entry("FILE=%s", filePath.c_str()),
-                        entry("ERRCODE=%lu", ERR_get_error()));
+        lg2::error(
+            "Error occurred during X509_get_pubkey, FILE:{FILE}, ERRCODE:{ERRCODE}",
+            "FILE", filePath, "ERRCODE", ERR_get_error());
         elog<InvalidCertificateError>(
             InvalidCertificate::REASON("Failed to get public key info"));
     }
@@ -661,8 +652,8 @@
     BIOMemPtr keyBio(BIO_new(BIO_s_file()), ::BIO_free);
     if (!keyBio)
     {
-        log<level::ERR>("Error occurred during BIO_s_file call",
-                        entry("FILE=%s", filePath.c_str()));
+        lg2::error("Error occurred during BIO_s_file call, FILE:{FILE}", "FILE",
+                   filePath);
         elog<InternalFailure>();
     }
     BIO_read_filename(keyBio.get(), filePath.c_str());
@@ -672,9 +663,9 @@
         ::EVP_PKEY_free);
     if (!priKey)
     {
-        log<level::ERR>("Error occurred during PEM_read_bio_PrivateKey",
-                        entry("FILE=%s", filePath.c_str()),
-                        entry("ERRCODE=%lu", ERR_get_error()));
+        lg2::error(
+            "Error occurred during PEM_read_bio_PrivateKey, FILE:{FILE}, ERRCODE:{ERRCODE}",
+            "FILE", filePath, "ERRCODE", ERR_get_error());
         elog<InvalidCertificateError>(
             InvalidCertificate::REASON("Failed to get private key info"));
     }
@@ -686,9 +677,9 @@
 #endif
     if (rc != 1)
     {
-        log<level::ERR>("Private key is not matching with Certificate",
-                        entry("FILE=%s", filePath.c_str()),
-                        entry("ERRCODE=%d", rc));
+        lg2::error(
+            "Private key is not matching with Certificate, FILE:{FILE}, ERRCODE:{ERRCODE}",
+            "FILE", filePath, "ERRCODE", rc);
         return false;
     }
     return true;