From 083d51ffd80f2e4690eb74b8e3cb99afa21258f8 Mon Sep 17 00:00:00 2001 From: zit-hb Date: Sat, 25 Apr 2015 14:16:42 +0200 Subject: [PATCH] Added mutex for log just in case. --- include/log.h | 13 +++++++++++++ include/storage.h | 4 +++- src/log.cpp | 7 +++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/log.h b/include/log.h index ba2e912..9197a08 100644 --- a/include/log.h +++ b/include/log.h @@ -33,6 +33,7 @@ #define LOG_H #include +#include #include "singleton.h" #include "shared.h" @@ -70,8 +71,20 @@ namespace swd { void send(swd::log_level level, std::string message); private: + /** + * @brief Get the current date and time as string. + */ std::string get_current_time(); + + /** + * @brief The log file. If empty stderr is used instead. + */ std::string file_; + + /** + * @brief Mutex for output. + */ + boost::mutex mutex_; }; } diff --git a/include/storage.h b/include/storage.h index 45e29c1..9c71fb0 100644 --- a/include/storage.h +++ b/include/storage.h @@ -101,7 +101,9 @@ namespace swd { */ bool stop_; - /* @brief Notify consumer threads on new requests in the queue. */ + /** + * @brief Notify consumer threads on new requests in the queue. + */ boost::condition_variable cond_; }; } diff --git a/src/log.cpp b/src/log.cpp index 62c9976..f93426e 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -37,6 +37,10 @@ #include "config.h" void swd::log::open_file(std::string file) { + /* Use mutex to avoid race conditions. */ + boost::unique_lock scoped_lock(mutex_); + + /* Set the file. */ file_ = file; } @@ -70,6 +74,9 @@ void swd::log::send(swd::log_level level, std::string message) { line << "\t" << message << "\n"; + /* Use mutex to avoid race conditions. */ + boost::unique_lock scoped_lock(mutex_); + /* Write the final line into a log file or stderr. */ if (!file_.empty()) { std::ofstream out_file(file_.c_str(), std::ios_base::app);