mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-13 09:20:25 +08:00
Add console_mutex option to ansicolor sink
This commit is contained in:
parent
f7dcad8374
commit
c4922b1304
@ -15,7 +15,7 @@ namespace sinks {
|
|||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
SPDLOG_INLINE ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
SPDLOG_INLINE ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
||||||
: target_file_(target_file)
|
: target_file_(target_file), console_mutex_(nullptr)
|
||||||
{
|
{
|
||||||
set_color_mode_(mode);
|
set_color_mode_(mode);
|
||||||
colors_.at(level::trace) = to_string_(white);
|
colors_.at(level::trace) = to_string_(white);
|
||||||
@ -42,6 +42,10 @@ SPDLOG_INLINE void ansicolor_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_sink<Mutex>::formatter_->format(msg, formatted);
|
base_sink<Mutex>::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);
|
||||||
@ -60,6 +64,9 @@ SPDLOG_INLINE void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg)
|
|||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
SPDLOG_INLINE void ansicolor_sink<Mutex>::flush_() {
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::flush_() {
|
||||||
|
auto console_lock = console_mutex_ != nullptr ?
|
||||||
|
std::unique_lock<std::mutex>(*console_mutex_) :
|
||||||
|
std::unique_lock<std::mutex>();
|
||||||
fflush(target_file_);
|
fflush(target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +81,12 @@ SPDLOG_INLINE void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
|||||||
set_color_mode_(mode);
|
set_color_mode_(mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::set_console_mutex(std::mutex* mutex) {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
console_mutex_ = mutex;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
SPDLOG_INLINE void ansicolor_sink<Mutex>::set_color_mode_(color_mode mode) {
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::set_color_mode_(color_mode mode) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
@ -33,6 +34,7 @@ public:
|
|||||||
|
|
||||||
void set_color(level::level_enum color_level, string_view_t color);
|
void set_color(level::level_enum color_level, string_view_t color);
|
||||||
void set_color_mode(color_mode mode);
|
void set_color_mode(color_mode mode);
|
||||||
|
void set_console_mutex(std::mutex *console_mutex);
|
||||||
bool should_color();
|
bool should_color();
|
||||||
// Formatting codes
|
// Formatting codes
|
||||||
const string_view_t reset = "\033[m";
|
const string_view_t reset = "\033[m";
|
||||||
@ -73,6 +75,7 @@ protected:
|
|||||||
FILE *target_file_;
|
FILE *target_file_;
|
||||||
bool should_do_colors_;
|
bool should_do_colors_;
|
||||||
std::array<std::string, level::n_levels> colors_;
|
std::array<std::string, level::n_levels> colors_;
|
||||||
|
std::mutex *console_mutex_;
|
||||||
|
|
||||||
void sink_it_(const details::log_msg &msg) override;
|
void sink_it_(const details::log_msg &msg) override;
|
||||||
void flush_() override;
|
void flush_() override;
|
||||||
|
Loading…
Reference in New Issue
Block a user