| 1 | // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
|---|---|
| 2 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <array> |
| 7 | #include <memory> |
| 8 | #include <mutex> |
| 9 | #include <spdlog/details/console_globals.h> |
| 10 | #include <spdlog/details/null_mutex.h> |
| 11 | #include <spdlog/sinks/sink.h> |
| 12 | #include <string> |
| 13 | |
| 14 | namespace spdlog { |
| 15 | namespace sinks { |
| 16 | |
| 17 | /** |
| 18 | * This sink prefixes the output with an ANSI escape sequence color code |
| 19 | * depending on the severity |
| 20 | * of the message. |
| 21 | * If no color terminal detected, omit the escape codes. |
| 22 | */ |
| 23 | |
| 24 | template <typename ConsoleMutex> |
| 25 | class ansicolor_sink : public sink { |
| 26 | public: |
| 27 | using mutex_t = typename ConsoleMutex::mutex_t; |
| 28 | ansicolor_sink(FILE *target_file, color_mode mode); |
| 29 | ~ansicolor_sink() override = default; |
| 30 | |
| 31 | ansicolor_sink(const ansicolor_sink &other) = delete; |
| 32 | ansicolor_sink(ansicolor_sink &&other) = delete; |
| 33 | |
| 34 | ansicolor_sink &operator=(const ansicolor_sink &other) = delete; |
| 35 | ansicolor_sink &operator=(ansicolor_sink &&other) = delete; |
| 36 | |
| 37 | void set_color(level::level_enum color_level, string_view_t color); |
| 38 | void set_color_mode(color_mode mode); |
| 39 | bool should_color() const; |
| 40 | |
| 41 | void log(const details::log_msg &msg) override; |
| 42 | void flush() override; |
| 43 | void set_pattern(const std::string &pattern) final override; |
| 44 | void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override; |
| 45 | |
| 46 | // Formatting codes |
| 47 | const string_view_t reset = "\033[m"; |
| 48 | const string_view_t bold = "\033[1m"; |
| 49 | const string_view_t dark = "\033[2m"; |
| 50 | const string_view_t underline = "\033[4m"; |
| 51 | const string_view_t blink = "\033[5m"; |
| 52 | const string_view_t reverse = "\033[7m"; |
| 53 | const string_view_t concealed = "\033[8m"; |
| 54 | const string_view_t clear_line = "\033[K"; |
| 55 | |
| 56 | // Foreground colors |
| 57 | const string_view_t black = "\033[30m"; |
| 58 | const string_view_t red = "\033[31m"; |
| 59 | const string_view_t green = "\033[32m"; |
| 60 | const string_view_t yellow = "\033[33m"; |
| 61 | const string_view_t blue = "\033[34m"; |
| 62 | const string_view_t magenta = "\033[35m"; |
| 63 | const string_view_t cyan = "\033[36m"; |
| 64 | const string_view_t white = "\033[37m"; |
| 65 | |
| 66 | /// Background colors |
| 67 | const string_view_t on_black = "\033[40m"; |
| 68 | const string_view_t on_red = "\033[41m"; |
| 69 | const string_view_t on_green = "\033[42m"; |
| 70 | const string_view_t on_yellow = "\033[43m"; |
| 71 | const string_view_t on_blue = "\033[44m"; |
| 72 | const string_view_t on_magenta = "\033[45m"; |
| 73 | const string_view_t on_cyan = "\033[46m"; |
| 74 | const string_view_t on_white = "\033[47m"; |
| 75 | |
| 76 | /// Bold colors |
| 77 | const string_view_t yellow_bold = "\033[33m\033[1m"; |
| 78 | const string_view_t red_bold = "\033[31m\033[1m"; |
| 79 | const string_view_t bold_on_red = "\033[1m\033[41m"; |
| 80 | |
| 81 | private: |
| 82 | FILE *target_file_; |
| 83 | mutex_t &mutex_; |
| 84 | bool should_do_colors_; |
| 85 | std::unique_ptr<spdlog::formatter> formatter_; |
| 86 | std::array<std::string, level::n_levels> colors_; |
| 87 | void set_color_mode_(color_mode mode); |
| 88 | void print_ccode_(const string_view_t &color_code) const; |
| 89 | void print_range_(const memory_buf_t &formatted, size_t start, size_t end) const; |
| 90 | static std::string to_string_(const string_view_t &sv); |
| 91 | }; |
| 92 | |
| 93 | template <typename ConsoleMutex> |
| 94 | class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex> { |
| 95 | public: |
| 96 | explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); |
| 97 | }; |
| 98 | |
| 99 | template <typename ConsoleMutex> |
| 100 | class ansicolor_stderr_sink : public ansicolor_sink<ConsoleMutex> { |
| 101 | public: |
| 102 | explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); |
| 103 | }; |
| 104 | |
| 105 | using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<details::console_mutex>; |
| 106 | using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::console_nullmutex>; |
| 107 | |
| 108 | using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<details::console_mutex>; |
| 109 | using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::console_nullmutex>; |
| 110 | |
| 111 | } // namespace sinks |
| 112 | } // namespace spdlog |
| 113 | |
| 114 | #ifdef SPDLOG_HEADER_ONLY |
| 115 | #include "ansicolor_sink-inl.h" |
| 116 | #endif |
| 117 |