Livox SDK API  V2.2.0
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 
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  {
41  should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
42  colors_[level::trace] = white;
43  colors_[level::debug] = cyan;
44  colors_[level::info] = green;
45  colors_[level::warn] = yellow + bold;
46  colors_[level::err] = red + bold;
47  colors_[level::critical] = bold + on_red;
48  colors_[level::off] = reset;
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 
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 
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);
100  if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
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 
137  {
138  return should_do_colors_;
139  }
140 
141 private:
142  void print_ccode_(const std::string &color_code)
143  {
144  fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
145  }
146  void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
147  {
148  fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
149  }
150 
151  FILE *target_file_;
152  mutex_t &mutex_;
153 
154  bool should_do_colors_;
155  std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
156 };
157 
160 
163 
164 } // namespace sinks
165 
166 } // namespace spdlog
const std::string on_black
Background colors.
~ansicolor_sink() override=default
const std::string reset
Formatting codes.
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:50
bool is_color_terminal() noexcept
Definition: os.h:388
std::size_t size() const
Definition: core.h:250
Definition: async.h:27
level::level_enum level
Definition: log_msg.h:42
bool in_terminal(FILE *file) noexcept
Definition: os.h:410
void set_pattern(const std::string &pattern) final
color
Definition: color.h:50
typename ConsoleMutex::mutex_t mutex_t
const std::string clear_line
void log(const details::log_msg &msg) override
void set_color(level::level_enum color_level, const std::string &color)
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
const std::string on_magenta
auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
ansicolor_sink & operator=(const ansicolor_sink &other)=delete