pHAL: Enabled environment variable based pHAL logging

pHAL repos logging level is initialised based on repository
specific environment variables. If the environment variable
is not set by the user, default logging level ( INFO only)
will be getting initialised.

Environment variable names:
 - PDBG_LOG pdbg library
 - IPL_LOG  ipl library
 - LIBEKB_LOG libekb library

Refer individual repository for logging level details.

Example: To set pdbg log level to PDBG_DEBUG
  export PDBG_LOG=4
  systemctl import-environment PDBG_LOG

Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
Change-Id: I3f9f35c0b5d23c5635001379bd330d56c2a468b7
diff --git a/phalerror/phal_error.cpp b/phalerror/phal_error.cpp
index 698886b..152c013 100644
--- a/phalerror/phal_error.cpp
+++ b/phalerror/phal_error.cpp
@@ -8,9 +8,11 @@
 #include <libekb.H>

 #include <libipl.H>

 

+#include <cstdlib>

 #include <iomanip>

 #include <phosphor-logging/elog.hpp>

 #include <sstream>

+#include <string>

 

 namespace openpower

 {

@@ -89,12 +91,31 @@
 }

 } // namespace detail

 

+static inline uint8_t getLogLevelFromEnv(const char* env, const uint8_t dValue)

+{

+    auto logLevel = dValue;

+    try

+    {

+        if (const char* env_p = std::getenv(env))

+        {

+            logLevel = std::stoi(env_p);

+        }

+    }

+    catch (std::exception& e)

+    {

+        log<level::ERR>(("Conversion Failure"), entry("ENVIRONMENT=%s", env),

+                        entry("EXCEPTION=%s", e.what()));

+    }

+    return logLevel;

+}

+

 void addBootErrorCallbacks()

 {

-    // set log level to info

-    pdbg_set_loglevel(PDBG_INFO);

-    libekb_set_loglevel(LIBEKB_LOG_INF);

-    ipl_set_loglevel(IPL_INFO);

+    // Get individual phal repos log level from environment variable

+    // and update the  log level.

+    pdbg_set_loglevel(getLogLevelFromEnv("PDBG_LOG", PDBG_INFO));

+    libekb_set_loglevel(getLogLevelFromEnv("LIBEKB_LOG", LIBEKB_LOG_IMP));

+    ipl_set_loglevel(getLogLevelFromEnv("IPL_LOG", IPL_INFO));

 

     // add callback for debug traces

     pdbg_set_logfunc(detail::pDBGLogTraceCallbackHelper);

@@ -104,5 +125,6 @@
     // add callback for ipl failures

     ipl_set_error_callback_func(detail::processBootErrorCallback);

 }

+

 } // namespace pel

 } // namespace openpower