From f0fcc73f9252745e3a89d17336b969fd53212253 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 18 Jun 2019 23:45:18 +0300 Subject: [PATCH] Minor improvments to ansicolor sink --- include/spdlog/sinks/ansicolor_sink-inl.h | 12 ++-- include/spdlog/sinks/ansicolor_sink.h | 68 +++++++++++++---------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index dbb0c636..b5f6aff9 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -22,14 +22,14 @@ SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, co colors_[level::trace] = white; colors_[level::debug] = cyan; colors_[level::info] = green; - colors_[level::warn] = yellow + bold; - colors_[level::err] = red + bold; - colors_[level::critical] = bold + on_red; + colors_[level::warn] = yellow_bold; + colors_[level::err] = red_bold; + colors_[level::critical] = bold_on_red; colors_[level::off] = reset; } template -SPDLOG_INLINE void ansicolor_sink::set_color(level::level_enum color_level, const std::string &color) +SPDLOG_INLINE void ansicolor_sink::set_color(level::level_enum color_level, string_view_t color) { std::lock_guard lock(mutex_); colors_[color_level] = color; @@ -107,9 +107,9 @@ SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) } template -SPDLOG_INLINE void ansicolor_sink::print_ccode_(const std::string &color_code) +SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) { - fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); + fwrite(color_code.data(), sizeof(string_view_t::char_type), color_code.size(), target_file_); } template diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 09356e7a..4b7416ea 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -20,6 +20,7 @@ namespace sinks { * of the message. * If no color terminal detected, omit the escape codes. */ + template class ansicolor_sink : public sink { @@ -30,50 +31,57 @@ public: ansicolor_sink(const ansicolor_sink &other) = delete; ansicolor_sink &operator=(const ansicolor_sink &other) = delete; - void set_color(level::level_enum color_level, const std::string &color); + void set_color(level::level_enum color_level, string_view_t color); + void set_color_mode(color_mode mode); + bool should_color(); + void log(const details::log_msg &msg) override; void flush() override; void set_pattern(const std::string &pattern) final; void set_formatter(std::unique_ptr sink_formatter) override; - bool should_color(); - void set_color_mode(color_mode mode); - /// Formatting codes - const std::string reset = "\033[m"; - const std::string bold = "\033[1m"; - const std::string dark = "\033[2m"; - const std::string underline = "\033[4m"; - const std::string blink = "\033[5m"; - const std::string reverse = "\033[7m"; - const std::string concealed = "\033[8m"; - const std::string clear_line = "\033[K"; + + // Formatting codes + const string_view_t reset = "\033[m"; + const string_view_t bold = "\033[1m"; + const string_view_t dark = "\033[2m"; + const string_view_t underline = "\033[4m"; + const string_view_t blink = "\033[5m"; + const string_view_t reverse = "\033[7m"; + const string_view_t concealed = "\033[8m"; + const string_view_t clear_line = "\033[K"; // Foreground colors - const std::string black = "\033[30m"; - const std::string red = "\033[31m"; - const std::string green = "\033[32m"; - const std::string yellow = "\033[33m"; - const std::string blue = "\033[34m"; - const std::string magenta = "\033[35m"; - const std::string cyan = "\033[36m"; - const std::string white = "\033[37m"; + const string_view_t black = "\033[30m"; + const string_view_t red = "\033[31m"; + const string_view_t green = "\033[32m"; + const string_view_t yellow = "\033[33m"; + const string_view_t blue = "\033[34m"; + const string_view_t magenta = "\033[35m"; + const string_view_t cyan = "\033[36m"; + const string_view_t white = "\033[37m"; /// Background colors - const std::string on_black = "\033[40m"; - const std::string on_red = "\033[41m"; - const std::string on_green = "\033[42m"; - const std::string on_yellow = "\033[43m"; - const std::string on_blue = "\033[44m"; - const std::string on_magenta = "\033[45m"; - const std::string on_cyan = "\033[46m"; - const std::string on_white = "\033[47m"; + const string_view_t on_black = "\033[40m"; + const string_view_t on_red = "\033[41m"; + const string_view_t on_green = "\033[42m"; + const string_view_t on_yellow = "\033[43m"; + const string_view_t on_blue = "\033[44m"; + const string_view_t on_magenta = "\033[45m"; + const string_view_t on_cyan = "\033[46m"; + const string_view_t on_white = "\033[47m"; + + /// Bold colors + const string_view_t yellow_bold = "\033[33m\033[1m"; + const string_view_t red_bold = "\033[31m\033[1m"; + const string_view_t bold_on_red = "\033[1m\033[31m"; private: FILE *target_file_; mutex_t &mutex_; bool should_do_colors_; - std::unordered_map colors_; - void print_ccode_(const std::string &color_code); + std::unordered_map colors_; + void print_ccode_(const string_view_t&color_code); void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); };