implement locking properly with std::mutex and std::lock_guard<>

This commit is contained in:
Dennis Luxen 2014-05-08 15:25:06 +02:00
parent 50594febc7
commit 88a4bb4d12

View File

@ -43,8 +43,6 @@ enum LogLevel
logWARNING,
logDEBUG };
static std::mutex logger_mutex;
const char COL_RESET[] = "\x1b[0m";
const char RED[] = "\x1b[31m";
const char GREEN[] = "\x1b[32m";
@ -80,11 +78,18 @@ class SimpleLogger
public:
SimpleLogger() : level(logINFO) {}
std::mutex& get_mutex()
{
static std::mutex m;
return m;
}
std::ostringstream &Write(LogLevel l = logINFO)
{
std::lock_guard<std::mutex> lock(get_mutex());
try
{
std::lock_guard<std::mutex> lock(logger_mutex);
level = l;
os << "[";
switch (level)
@ -112,6 +117,7 @@ class SimpleLogger
virtual ~SimpleLogger()
{
std::lock_guard<std::mutex> lock(get_mutex());
if (!LogPolicy::GetInstance().IsMute())
{
const bool is_terminal = isatty(fileno(stdout));