Mutex protect set_color_mode()

This commit is contained in:
gabime 2019-05-10 17:41:21 +03:00
parent 3b0e7b4d0d
commit 3ce9ac74a6
2 changed files with 23 additions and 10 deletions

View File

@ -38,7 +38,7 @@ public:
, mutex_(ConsoleMutex::mutex())
{
set_color_mode(mode);
set_color_mode_(mode);
colors_[level::trace] = white;
colors_[level::debug] = cyan;
colors_[level::info] = green;
@ -139,22 +139,28 @@ public:
}
void set_color_mode(color_mode mode)
{
std::lock_guard<mutex_t> lock(mutex_);
set_color_mode_(mode);
}
private:
void set_color_mode_(color_mode mode)
{
switch (mode)
{
case color_mode::always:
should_do_colors_ = true;
return;
break;
case color_mode::automatic:
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
return;
break;
case color_mode::never:
should_do_colors_ = false;
return;
break;
}
}
private:
void print_ccode_(const std::string &color_code)
{
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);

View File

@ -41,7 +41,7 @@ public:
: out_handle_(OutHandle::handle())
, mutex_(ConsoleMutex::mutex())
{
set_color_mode(mode);
set_color_mode_(mode);
colors_[level::trace] = WHITE;
colors_[level::debug] = CYAN;
colors_[level::info] = GREEN;
@ -108,21 +108,28 @@ public:
}
void set_color_mode(color_mode mode)
{
std::lock_guard<mutex_t> lock(mutex_);
set_color_mode_(mode);
}
private:
using mutex_t = typename ConsoleMutex::mutex_t;
void set_color_mode_(color_mode mode)
{
switch (mode)
{
case color_mode::always:
case color_mode::automatic:
should_do_colors_ = true;
return;
break
case color_mode::never:
should_do_colors_ = false;
return;
break
}
}
private:
using mutex_t = typename ConsoleMutex::mutex_t;
// set color and return the orig console attributes (for resetting later)
WORD set_console_attribs(WORD attribs)
{