ansicolor_sink.h 3.9 KB

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