Implement async file logger
This commit implements asynchronous, multi-thread safe file logging.
Certain log messages in the repo, for example VPD collection logs, need
to be logged from multiple threads in an asynchronous manner.
Change-Id: I1a7c28cc37f9e88f04e6af89cf4042d032c8cf90
Signed-off-by: Souvik Roy <souvikroyofficial10@gmail.com>
diff --git a/vpd-manager/include/logger.hpp b/vpd-manager/include/logger.hpp
index 1622426..59b7afd 100644
--- a/vpd-manager/include/logger.hpp
+++ b/vpd-manager/include/logger.hpp
@@ -2,6 +2,7 @@
#include "types.hpp"
+#include <condition_variable>
#include <filesystem>
#include <fstream>
#include <iostream>
@@ -196,6 +197,18 @@
*/
class AsyncFileLogger final : public ILogFileHandler
{
+ // queue for log messages
+ std::queue<std::string> m_messageQueue;
+
+ // mutex to control access to log message queue
+ std::mutex m_mutex;
+
+ // flag which indicates log worker thread if logging is finished
+ std::atomic_bool m_stopLogging{false};
+
+ // conditional variable to signal log worker thread
+ std::condition_variable m_cv;
+
/**
* @brief Constructor
* Private so that can't be initialized by class(es) other than friends.
@@ -231,6 +244,8 @@
/**
* @brief API to log a message to file
*
+ * This API logs given message to a file. This API is multi-thread safe.
+ *
* @param[in] i_message - Message to log
*
* @throw std::runtime_error
@@ -240,15 +255,16 @@
// destructor
~AsyncFileLogger()
{
- /* TODO
- - acquire lock
- - set log stop flag to true
- - notify log worker thread
- */
+ std::unique_lock<std::mutex> l_lock(m_mutex);
+
+ m_stopLogging = true;
+
if (m_fileStream.is_open())
{
m_fileStream.close();
}
+
+ m_cv.notify_one();
}
};