Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:58:15

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #include <spdlog/details/console_globals.h>
0007 #include <spdlog/details/null_mutex.h>
0008 #include <spdlog/sinks/sink.h>
0009 #include <memory>
0010 #include <mutex>
0011 #include <string>
0012 #include <array>
0013 
0014 namespace spdlog {
0015 namespace sinks {
0016 
0017 /**
0018  * This sink prefixes the output with an ANSI escape sequence color code
0019  * depending on the severity
0020  * of the message.
0021  * If no color terminal detected, omit the escape codes.
0022  */
0023 
0024 template<typename ConsoleMutex>
0025 class ansicolor_sink : public sink
0026 {
0027 public:
0028     using mutex_t = typename ConsoleMutex::mutex_t;
0029     ansicolor_sink(FILE *target_file, color_mode mode);
0030     ~ansicolor_sink() override = default;
0031 
0032     ansicolor_sink(const ansicolor_sink &other) = delete;
0033     ansicolor_sink(ansicolor_sink &&other) = delete;
0034 
0035     ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
0036     ansicolor_sink &operator=(ansicolor_sink &&other) = delete;
0037 
0038     void set_color(level::level_enum color_level, string_view_t color);
0039     void set_color_mode(color_mode mode);
0040     bool should_color();
0041 
0042     void log(const details::log_msg &msg) override;
0043     void flush() override;
0044     void set_pattern(const std::string &pattern) final;
0045     void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
0046 
0047     // Formatting codes
0048     const string_view_t reset = "\033[m";
0049     const string_view_t bold = "\033[1m";
0050     const string_view_t dark = "\033[2m";
0051     const string_view_t underline = "\033[4m";
0052     const string_view_t blink = "\033[5m";
0053     const string_view_t reverse = "\033[7m";
0054     const string_view_t concealed = "\033[8m";
0055     const string_view_t clear_line = "\033[K";
0056 
0057     // Foreground colors
0058     const string_view_t black = "\033[30m";
0059     const string_view_t red = "\033[31m";
0060     const string_view_t green = "\033[32m";
0061     const string_view_t yellow = "\033[33m";
0062     const string_view_t blue = "\033[34m";
0063     const string_view_t magenta = "\033[35m";
0064     const string_view_t cyan = "\033[36m";
0065     const string_view_t white = "\033[37m";
0066 
0067     /// Background colors
0068     const string_view_t on_black = "\033[40m";
0069     const string_view_t on_red = "\033[41m";
0070     const string_view_t on_green = "\033[42m";
0071     const string_view_t on_yellow = "\033[43m";
0072     const string_view_t on_blue = "\033[44m";
0073     const string_view_t on_magenta = "\033[45m";
0074     const string_view_t on_cyan = "\033[46m";
0075     const string_view_t on_white = "\033[47m";
0076 
0077     /// Bold colors
0078     const string_view_t yellow_bold = "\033[33m\033[1m";
0079     const string_view_t red_bold = "\033[31m\033[1m";
0080     const string_view_t bold_on_red = "\033[1m\033[41m";
0081 
0082 private:
0083     FILE *target_file_;
0084     mutex_t &mutex_;
0085     bool should_do_colors_;
0086     std::unique_ptr<spdlog::formatter> formatter_;
0087     std::array<std::string, level::n_levels> colors_;
0088     void print_ccode_(const string_view_t &color_code);
0089     void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
0090     static std::string to_string_(const string_view_t &sv);
0091 };
0092 
0093 template<typename ConsoleMutex>
0094 class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex>
0095 {
0096 public:
0097     explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic);
0098 };
0099 
0100 template<typename ConsoleMutex>
0101 class ansicolor_stderr_sink : public ansicolor_sink<ConsoleMutex>
0102 {
0103 public:
0104     explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic);
0105 };
0106 
0107 using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<details::console_mutex>;
0108 using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::console_nullmutex>;
0109 
0110 using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<details::console_mutex>;
0111 using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::console_nullmutex>;
0112 
0113 } // namespace sinks
0114 } // namespace spdlog
0115 
0116 #ifdef SPDLOG_HEADER_ONLY
0117 #    include "ansicolor_sink-inl.h"
0118 #endif