ansicolor_sink.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2017 spdlog authors.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #ifndef SPDLOG_H
9 #include "spdlog/spdlog.h"
10 #endif
11 
14 #include "spdlog/details/os.h"
15 #include "spdlog/sinks/sink.h"
16 
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <unordered_map>
21 
22 namespace spdlog {
23 namespace sinks {
24 
25 /**
26  * This sink prefixes the output with an ANSI escape sequence color code
27  * depending on the severity
28  * of the message.
29  * If no color terminal detected, omit the escape codes.
30  */
31 template<typename TargetStream, class ConsoleMutex>
32 class ansicolor_sink final : public sink
33 {
34 public:
35  using mutex_t = typename ConsoleMutex::mutex_t;
37  : target_file_(TargetStream::stream())
38  , mutex_(ConsoleMutex::mutex())
39 
40  {
47  colors_[level::critical] = bold + on_red;
49  }
50 
51  ~ansicolor_sink() override = default;
52 
53  ansicolor_sink(const ansicolor_sink &other) = delete;
54  ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
55 
56  void set_color(level::level_enum color_level, const std::string &color)
57  {
58  std::lock_guard<mutex_t> lock(mutex_);
59  colors_[color_level] = color;
60  }
61 
62  /// Formatting codes
63  const std::string reset = "\033[m";
64  const std::string bold = "\033[1m";
65  const std::string dark = "\033[2m";
66  const std::string underline = "\033[4m";
67  const std::string blink = "\033[5m";
68  const std::string reverse = "\033[7m";
69  const std::string concealed = "\033[8m";
70  const std::string clear_line = "\033[K";
71 
72  // Foreground colors
73  const std::string black = "\033[30m";
74  const std::string red = "\033[31m";
75  const std::string green = "\033[32m";
76  const std::string yellow = "\033[33m";
77  const std::string blue = "\033[34m";
78  const std::string magenta = "\033[35m";
79  const std::string cyan = "\033[36m";
80  const std::string white = "\033[37m";
81 
82  /// Background colors
83  const std::string on_black = "\033[40m";
84  const std::string on_red = "\033[41m";
85  const std::string on_green = "\033[42m";
86  const std::string on_yellow = "\033[43m";
87  const std::string on_blue = "\033[44m";
88  const std::string on_magenta = "\033[45m";
89  const std::string on_cyan = "\033[46m";
90  const std::string on_white = "\033[47m";
91 
92  void log(const details::log_msg &msg) override
93  {
94  // Wrap the originally formatted message in color codes.
95  // If color is not supported in the terminal, log as is instead.
96  std::lock_guard<mutex_t> lock(mutex_);
97 
98  fmt::memory_buffer formatted;
99  formatter_->format(msg, formatted);
101  {
102  // before color range
103  print_range_(formatted, 0, msg.color_range_start);
104  // in color range
105  print_ccode_(colors_[msg.level]);
106  print_range_(formatted, msg.color_range_start, msg.color_range_end);
107  print_ccode_(reset);
108  // after color range
109  print_range_(formatted, msg.color_range_end, formatted.size());
110  }
111  else // no color
112  {
113  print_range_(formatted, 0, formatted.size());
114  }
115  fflush(target_file_);
116  }
117 
118  void flush() override
119  {
120  std::lock_guard<mutex_t> lock(mutex_);
121  fflush(target_file_);
122  }
123 
124  void set_pattern(const std::string &pattern) final
125  {
126  std::lock_guard<mutex_t> lock(mutex_);
127  formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
128  }
129 
130  void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
131  {
132  std::lock_guard<mutex_t> lock(mutex_);
133  formatter_ = std::move(sink_formatter);
134  }
135 
136 private:
137  void print_ccode_(const std::string &color_code)
138  {
139  fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
140  }
141  void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
142  {
143  fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
144  }
145 
148 
150  std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
151 };
152 
155 
158 
159 } // namespace sinks
160 
161 } // namespace spdlog
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
const std::string on_black
Background colors.
~ansicolor_sink() override=default
void msg(const char *fmt,...)
Definition: message.cpp:107
const std::string reset
Formatting codes.
std::string string
Definition: nybbler.cc:12
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
Definition: async.h:27
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
level::level_enum level
Definition: log_msg.h:42
def move(depos, offset)
Definition: depos.py:107
void set_pattern(const std::string &pattern) final
bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
Definition: os.h:410
typename ConsoleMutex::mutex_t mutex_t
color
Definition: color.h:50
const std::string clear_line
bool is_color_terminal() SPDLOG_NOEXCEPT
Definition: os.h:388
std::string pattern
Definition: regex_t.cc:33
void print_ccode_(const std::string &color_code)
void log(const details::log_msg &msg) override
void set_color(level::level_enum color_level, const std::string &color)
std::unordered_map< level::level_enum, std::string, level::level_hasher > colors_
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
const std::string on_magenta
ansicolor_sink & operator=(const ansicolor_sink &other)=delete