Add consolute mutex support to wincolor sink

This commit is contained in:
Gabi Melman 2024-11-10 23:44:15 +02:00
parent bfe2116191
commit 3c4b1ceb36
2 changed files with 13 additions and 1 deletions

View File

@ -17,7 +17,7 @@ namespace spdlog {
namespace sinks { namespace sinks {
template <typename Mutex> template <typename Mutex>
SPDLOG_INLINE wincolor_sink<Mutex>::wincolor_sink(void *out_handle, color_mode mode) SPDLOG_INLINE wincolor_sink<Mutex>::wincolor_sink(void *out_handle, color_mode mode)
: out_handle_(out_handle) { : out_handle_(out_handle), console_mutex_(nullptr) {
set_color_mode_impl(mode); set_color_mode_impl(mode);
// set level colors // set level colors
colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
@ -45,6 +45,12 @@ void SPDLOG_INLINE wincolor_sink<Mutex>::set_color(level::level_enum level,
colors_[static_cast<size_t>(level)] = color; colors_[static_cast<size_t>(level)] = color;
} }
template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<Mutex>::set_console_mutex(std::mutex* mutex) {
std::lock_guard<Mutex> lock(base_t::mutex_);
console_mutex_ = mutex;
}
template <typename Mutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<Mutex>::sink_it_(const details::log_msg &msg) { void SPDLOG_INLINE wincolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) { if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) {
@ -54,6 +60,10 @@ void SPDLOG_INLINE wincolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
msg.color_range_end = 0; msg.color_range_end = 0;
memory_buf_t formatted; memory_buf_t formatted;
base_t::formatter_->format(msg, formatted); base_t::formatter_->format(msg, formatted);
auto console_lock = console_mutex_ != nullptr ? std::unique_lock<std::mutex>(*console_mutex_)
: std::unique_lock<std::mutex>();
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range // before color range
print_range_(formatted, 0, msg.color_range_start); print_range_(formatted, 0, msg.color_range_start);

View File

@ -30,11 +30,13 @@ public:
// change the color for the given level // change the color for the given level
void set_color(level::level_enum level, std::uint16_t color); void set_color(level::level_enum level, std::uint16_t color);
void set_color_mode(color_mode mode); void set_color_mode(color_mode mode);
void set_console_mutex(std::mutex *console_mutex);
protected: protected:
void *out_handle_; void *out_handle_;
bool should_do_colors_; bool should_do_colors_;
std::array<std::uint16_t, level::n_levels> colors_; std::array<std::uint16_t, level::n_levels> colors_;
std::mutex *console_mutex_;
void sink_it_(const details::log_msg &msg) final override; void sink_it_(const details::log_msg &msg) final override;
void flush_() final override; void flush_() final override;