Update current entries if log file exists
This commit includes changes to check if a log file already exists
during vpd-manager service start and updates current number of entries
if it already exists. VPD related log files may already exist when
vpd-manager service starts and the current number of entries need to be
updated accordingly.
Change-Id: I2be5b34f68308696a3541693bbe96a866e7c83cd
Signed-off-by: Souvik Roy <souvikroyofficial10@gmail.com>
diff --git a/vpd-manager/include/logger.hpp b/vpd-manager/include/logger.hpp
index 59b7afd..9d3283e 100644
--- a/vpd-manager/include/logger.hpp
+++ b/vpd-manager/include/logger.hpp
@@ -71,15 +71,7 @@
* which the file will be rotated.
*/
ILogFileHandler(const std::filesystem::path& i_filePath,
- const size_t i_maxEntries) :
- m_filePath{i_filePath}, m_maxEntries{i_maxEntries}
- {
- // open the file in append mode
- m_fileStream.open(m_filePath, std::ios::out | std::ios::app);
-
- // enable exception mask to throw on badbit and failbit
- m_fileStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
- }
+ const size_t i_maxEntries);
/**
* @brief API to generate timestamp in string format.
diff --git a/vpd-manager/src/logger.cpp b/vpd-manager/src/logger.cpp
index 1e133f0..129da17 100644
--- a/vpd-manager/src/logger.cpp
+++ b/vpd-manager/src/logger.cpp
@@ -227,6 +227,37 @@
} // thread loop
}
+ILogFileHandler::ILogFileHandler(const std::filesystem::path& i_filePath,
+ const size_t i_maxEntries) :
+ m_filePath{i_filePath}, m_maxEntries{i_maxEntries}
+{
+ // check if log file already exists
+ std::error_code l_ec;
+ const bool l_logFileExists = std::filesystem::exists(m_filePath, l_ec);
+ if (l_ec)
+ {
+ Logger::getLoggerInstance()->logMessage(
+ "Failed to check if log file already exists. Error: " +
+ l_ec.message());
+ }
+
+ // open the file in append mode
+ m_fileStream.open(m_filePath, std::ios::out | std::ios::app);
+ // enable exception mask to throw on badbit and failbit
+ m_fileStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
+
+ if (l_logFileExists)
+ {
+ // log file already exists, check and update the number of entries
+ std::ifstream l_readFileStream{m_filePath};
+ for (std::string l_line; std::getline(l_readFileStream, l_line);
+ ++m_currentNumEntries)
+ {}
+
+ l_readFileStream.close();
+ }
+}
+
void ILogFileHandler::rotateFile(
[[maybe_unused]] const unsigned i_numEntriesToDelete)
{