Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
Added mutex for log just in case.
Browse files Browse the repository at this point in the history
  • Loading branch information
zit-hb committed Apr 25, 2015
1 parent d5f8e07 commit 083d51f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define LOG_H

#include <string>
#include <boost/thread.hpp>

#include "singleton.h"
#include "shared.h"
Expand Down Expand Up @@ -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_;
};
}

Expand Down
4 changes: 3 additions & 1 deletion include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "config.h"

void swd::log::open_file(std::string file) {
/* Use mutex to avoid race conditions. */
boost::unique_lock<boost::mutex> scoped_lock(mutex_);

/* Set the file. */
file_ = file;
}

Expand Down Expand Up @@ -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<boost::mutex> 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);
Expand Down

0 comments on commit 083d51f

Please sign in to comment.