Implement padded log entry based log rotation

This commit implements log file rotation. If the number of entries in a
log file crosses a specified threshold, the write position is wrapped to
the beginning of the file so that the size of the file and subsequently
the size of BMC dump doesn't keep increasing infinitely. Each log entry
is padded or trimmed to ensure they are of fixed size.

Change-Id: I61bf0431adef9482180bf3463a96f7e0d6aea023
Signed-off-by: Souvik Roy <souvikroyofficial10@gmail.com>
diff --git a/vpd-manager/src/logger.cpp b/vpd-manager/src/logger.cpp
index 129da17..b73fe1d 100644
--- a/vpd-manager/src/logger.cpp
+++ b/vpd-manager/src/logger.cpp
@@ -140,11 +140,31 @@
 {
     try
     {
-        if (++m_currentNumEntries > m_maxEntries)
+        if (m_currentNumEntries >= m_maxEntries)
         {
             rotateFile();
         }
-        m_fileStream << timestamp() << " : " << i_message << std::endl;
+
+        std::string l_timeStampedMsg{
+            timestamp() + " : " + std::string(i_message)};
+
+        // check size of message and pad/trim as required
+        if (l_timeStampedMsg.length() > m_logEntrySize)
+        {
+            l_timeStampedMsg.resize(m_logEntrySize);
+        }
+        else if (l_timeStampedMsg.length() < m_logEntrySize)
+        {
+            constexpr char l_padChar{' '};
+            l_timeStampedMsg.append(m_logEntrySize - l_timeStampedMsg.length(),
+                                    l_padChar);
+        }
+
+        // write the message to file
+        m_fileStream << l_timeStampedMsg << std::endl;
+
+        // increment number of entries only if write to file is successful
+        ++m_currentNumEntries;
     }
     catch (const std::exception& l_ex)
     {
@@ -242,7 +262,7 @@
     }
 
     // open the file in append mode
-    m_fileStream.open(m_filePath, std::ios::out | std::ios::app);
+    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);
 
@@ -258,15 +278,6 @@
     }
 }
 
-void ILogFileHandler::rotateFile(
-    [[maybe_unused]] const unsigned i_numEntriesToDelete)
-{
-    /* TODO:
-        - delete specified number of oldest entries from beginning of file
-        - rewrite file to move existing logs to beginning of file
-    */
-    m_currentNumEntries = m_maxEntries - i_numEntriesToDelete;
-}
 namespace logging
 {
 void logMessage(std::string_view message, const std::source_location& location)