Logger:error handling enhancements

This commit adds following enhancements in Logger:
- add a try-catch block in Logger::logMessage to catch exceptions
  locally. In case of any exception log message is redirected to journal
  so that it is not lost.
- Enable filestream exceptions before opening the filestream. This
  ensures that an exception is thrown if there is any error while
  opening the file.

Change-Id: I35df92c33ef0c15b9f440651e271f31dfa13d5b3
Signed-off-by: Souvik Roy <souvikroyofficial10@gmail.com>
diff --git a/vpd-manager/src/logger.cpp b/vpd-manager/src/logger.cpp
index cbf385a..742cf27 100644
--- a/vpd-manager/src/logger.cpp
+++ b/vpd-manager/src/logger.cpp
@@ -10,63 +10,72 @@
 void Logger::logMessage(std::string_view i_message,
                         const PlaceHolder& i_placeHolder,
                         const types::PelInfoTuple* i_pelTuple,
-                        const std::source_location& i_location)
+                        const std::source_location& i_location) noexcept
 {
     std::ostringstream l_log;
     l_log << "FileName: " << i_location.file_name() << ","
           << " Line: " << i_location.line() << " " << i_message;
 
-    if (i_placeHolder == PlaceHolder::COLLECTION)
+    try
     {
-#ifdef ENABLE_FILE_LOGGING
-        if (m_collectionLogger.get() == nullptr)
+        if (i_placeHolder == PlaceHolder::COLLECTION)
         {
-            initiateVpdCollectionLogging();
-
-            if (m_collectionLogger.get() != nullptr)
+#ifdef ENABLE_FILE_LOGGING
+            if (m_collectionLogger.get() == nullptr)
             {
-                // Log it to a specific place.
-                m_collectionLogger->logMessage(l_log.str());
+                initiateVpdCollectionLogging();
+
+                if (m_collectionLogger.get() != nullptr)
+                {
+                    // Log it to a specific place.
+                    m_collectionLogger->logMessage(l_log.str());
+                }
+                else
+                {
+                    std::cout << l_log.str() << std::endl;
+                }
             }
             else
             {
-                std::cout << l_log.str() << std::endl;
+                m_collectionLogger->logMessage(l_log.str());
             }
+#else
+            std::cout << l_log.str() << std::endl;
+#endif
+        }
+        else if (i_placeHolder == PlaceHolder::PEL)
+        {
+            if (i_pelTuple)
+            {
+                // LOG PEL
+                // This should call create PEL API from the event logger.
+                return;
+            }
+            std::cout << "Pel info tuple required to log PEL for message <" +
+                             l_log.str() + ">"
+                      << std::endl;
+        }
+        else if (i_placeHolder == PlaceHolder::VPD_WRITE)
+        {
+            if (!m_vpdWriteLogger)
+            {
+                m_vpdWriteLogger.reset(
+                    new SyncFileLogger("/var/lib/vpd/vpdWrite.log", 128));
+            }
+            m_vpdWriteLogger->logMessage(l_log.str());
         }
         else
         {
-            m_collectionLogger->logMessage(l_log.str());
+            // Default case, let it go to journal.
+            std::cout << l_log.str() << std::endl;
         }
-#else
-        std::cout << l_log.str() << std::endl;
-#endif
     }
-    else if (i_placeHolder == PlaceHolder::PEL)
+    catch (const std::exception& l_ex)
     {
-        if (i_pelTuple)
-        {
-            // LOG PEL
-            // This should call create PEL API from the event logger.
-            return;
-        }
-        std::cout << "Pel info tuple required to log PEL for message <" +
-                         l_log.str() + ">"
+        std::cout << "Failed to log message:[" + l_log.str() +
+                         "]. Error: " + std::string(l_ex.what())
                   << std::endl;
     }
-    else if (i_placeHolder == PlaceHolder::VPD_WRITE)
-    {
-        if (!m_vpdWriteLogger)
-        {
-            m_vpdWriteLogger.reset(
-                new SyncFileLogger("/var/lib/vpd/vpdWrite.log", 128));
-        }
-        m_vpdWriteLogger->logMessage(l_log.str());
-    }
-    else
-    {
-        // Default case, let it go to journal.
-        std::cout << l_log.str() << std::endl;
-    }
 }
 
 #ifdef ENABLE_FILE_LOGGING
@@ -319,10 +328,10 @@
         }
     }
 
-    // open the file in append mode
-    m_fileStream.open(m_filePath, std::ios::out | std::ios::ate);
     // enable exception mask to throw on badbit and failbit
     m_fileStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
+    // open the file in append mode
+    m_fileStream.open(m_filePath, std::ios::out | std::ios::ate);
 
     if (l_logFileExists)
     {