implement locking properly with std::mutex and std::lock_guard<>
This commit is contained in:
parent
50594febc7
commit
88a4bb4d12
@ -43,8 +43,6 @@ enum LogLevel
|
|||||||
logWARNING,
|
logWARNING,
|
||||||
logDEBUG };
|
logDEBUG };
|
||||||
|
|
||||||
static std::mutex logger_mutex;
|
|
||||||
|
|
||||||
const char COL_RESET[] = "\x1b[0m";
|
const char COL_RESET[] = "\x1b[0m";
|
||||||
const char RED[] = "\x1b[31m";
|
const char RED[] = "\x1b[31m";
|
||||||
const char GREEN[] = "\x1b[32m";
|
const char GREEN[] = "\x1b[32m";
|
||||||
@ -80,11 +78,18 @@ class SimpleLogger
|
|||||||
public:
|
public:
|
||||||
SimpleLogger() : level(logINFO) {}
|
SimpleLogger() : level(logINFO) {}
|
||||||
|
|
||||||
|
std::mutex& get_mutex()
|
||||||
|
{
|
||||||
|
static std::mutex m;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
std::ostringstream &Write(LogLevel l = logINFO)
|
std::ostringstream &Write(LogLevel l = logINFO)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(get_mutex());
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_mutex);
|
|
||||||
level = l;
|
level = l;
|
||||||
os << "[";
|
os << "[";
|
||||||
switch (level)
|
switch (level)
|
||||||
@ -112,6 +117,7 @@ class SimpleLogger
|
|||||||
|
|
||||||
virtual ~SimpleLogger()
|
virtual ~SimpleLogger()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(get_mutex());
|
||||||
if (!LogPolicy::GetInstance().IsMute())
|
if (!LogPolicy::GetInstance().IsMute())
|
||||||
{
|
{
|
||||||
const bool is_terminal = isatty(fileno(stdout));
|
const bool is_terminal = isatty(fileno(stdout));
|
||||||
|
Loading…
Reference in New Issue
Block a user