mirror of
https://github.com/gabime/spdlog.git
synced 2025-03-01 03:35:50 +08:00
Refactor ansicolor_sink.cpp
This commit is contained in:
parent
e3f8349d0c
commit
c563b62aea
@ -70,11 +70,13 @@ public:
|
|||||||
static constexpr std::string_view bold_on_red = "\033[1m\033[41m";
|
static constexpr std::string_view bold_on_red = "\033[1m\033[41m";
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void sink_it_(const details::log_msg &msg) override;
|
|
||||||
void flush_() override;
|
|
||||||
FILE *target_file_;
|
FILE *target_file_;
|
||||||
bool should_do_colors_;
|
bool should_do_colors_;
|
||||||
std::array<std::string, levels_count> colors_;
|
std::array<std::string, levels_count> colors_;
|
||||||
|
|
||||||
|
void sink_it_(const details::log_msg &msg) override;
|
||||||
|
void flush_() override;
|
||||||
|
void set_color_mode_(color_mode mode);
|
||||||
void print_ccode_(const string_view_t color_code);
|
void print_ccode_(const string_view_t color_code);
|
||||||
void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
|
void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
|
||||||
static std::string to_string_(const string_view_t sv);
|
static std::string to_string_(const string_view_t sv);
|
||||||
|
@ -13,7 +13,7 @@ namespace sinks {
|
|||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) {
|
ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) {
|
||||||
set_color_mode(mode);
|
set_color_mode_(mode);
|
||||||
colors_.at(level_to_number(level::trace)) = to_string_(white);
|
colors_.at(level_to_number(level::trace)) = to_string_(white);
|
||||||
colors_.at(level_to_number(level::debug)) = to_string_(cyan);
|
colors_.at(level_to_number(level::debug)) = to_string_(cyan);
|
||||||
colors_.at(level_to_number(level::info)) = to_string_(green);
|
colors_.at(level_to_number(level::info)) = to_string_(green);
|
||||||
@ -37,22 +37,27 @@ bool ansicolor_sink<Mutex>::should_color() const {
|
|||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
set_color_mode_(mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
void ansicolor_sink<Mutex>::set_color_mode_(color_mode mode) {
|
||||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case color_mode::always:
|
case color_mode::always:
|
||||||
should_do_colors_ = true;
|
should_do_colors_ = true;
|
||||||
return;
|
return;
|
||||||
case color_mode::automatic:
|
case color_mode::automatic:
|
||||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||||
return;
|
return;
|
||||||
case color_mode::never:
|
case color_mode::never:
|
||||||
should_do_colors_ = false;
|
should_do_colors_ = false;
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
should_do_colors_ = false;
|
should_do_colors_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||||
// Wrap the originally formatted message in color codes.
|
// Wrap the originally formatted message in color codes.
|
||||||
|
Loading…
Reference in New Issue
Block a user