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/include/logger.hpp b/vpd-manager/include/logger.hpp
index 9d3283e..8dc8b55 100644
--- a/vpd-manager/include/logger.hpp
+++ b/vpd-manager/include/logger.hpp
@@ -49,18 +49,24 @@
// current number of log entries in file
size_t m_currentNumEntries{0};
+ // number of chars in a single log entry
+ size_t m_logEntrySize{512};
+
/**
* @brief API to rotate file.
*
- * This API rotates the logs within a file by deleting specified number of
- * oldest entries.
+ * This API rotates the logs within a file by repositioning the write file
+ * pointer to beginning of file. Rotation is achieved by overwriting the
+ * oldest log entries starting from the top of the file.
*
- * @param[in] i_numEntriesToDelete - Number of entries to delete.
- *
- * @throw std::runtime_error
+ * @throw std::ios_base::failure
*/
- virtual void rotateFile(
- [[maybe_unused]] const unsigned i_numEntriesToDelete = 5);
+ virtual inline void rotateFile()
+ {
+ // reset file pointer to beginning of file
+ m_fileStream.seekp(0);
+ m_currentNumEntries = 0;
+ }
/**
* @brief Constructor.
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)