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);