Minor improvments to ansicolor sink

This commit is contained in:
gabime 2019-06-18 23:45:18 +03:00
parent a340b3812c
commit f0fcc73f92
2 changed files with 44 additions and 36 deletions

View File

@ -22,14 +22,14 @@ SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::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<typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, const std::string &color)
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color)
{
std::lock_guard<mutex_t> lock(mutex_);
colors_[color_level] = color;
@ -107,9 +107,9 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
}
template<typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const std::string &color_code)
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::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<typename ConsoleMutex>

View File

@ -20,6 +20,7 @@ namespace sinks {
* of the message.
* If no color terminal detected, omit the escape codes.
*/
template<typename ConsoleMutex>
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<spdlog::formatter> 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<level::level_enum, std::string, level::level_hasher> colors_;
void print_ccode_(const std::string &color_code);
std::unordered_map<level::level_enum, string_view_t, level::level_hasher> colors_;
void print_ccode_(const string_view_t&color_code);
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
};