bitz-server  2.0.0
ansicolor_sink.h
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 #include "../common.h"
9 #include "../details/os.h"
10 #include "base_sink.h"
11 
12 #include <string>
13 #include <unordered_map>
14 
15 namespace spdlog {
16 namespace sinks {
17 
23 template<class Mutex>
24 class ansicolor_sink : public base_sink<Mutex>
25 {
26 public:
27  explicit ansicolor_sink(FILE *file)
28  : target_file_(file)
29  {
30  should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
31  colors_[level::trace] = white;
32  colors_[level::debug] = cyan;
33  colors_[level::info] = green;
34  colors_[level::warn] = yellow + bold;
35  colors_[level::err] = red + bold;
36  colors_[level::critical] = bold + on_red;
37  colors_[level::off] = reset;
38  }
39 
40  ~ansicolor_sink() override
41  {
42  _flush();
43  }
44 
45  void set_color(level::level_enum color_level, const std::string &color)
46  {
47  std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
48  colors_[color_level] = color;
49  }
50 
52  const std::string reset = "\033[m";
53  const std::string bold = "\033[1m";
54  const std::string dark = "\033[2m";
55  const std::string underline = "\033[4m";
56  const std::string blink = "\033[5m";
57  const std::string reverse = "\033[7m";
58  const std::string concealed = "\033[8m";
59  const std::string clear_line = "\033[K";
60 
61  // Foreground colors
62  const std::string black = "\033[30m";
63  const std::string red = "\033[31m";
64  const std::string green = "\033[32m";
65  const std::string yellow = "\033[33m";
66  const std::string blue = "\033[34m";
67  const std::string magenta = "\033[35m";
68  const std::string cyan = "\033[36m";
69  const std::string white = "\033[37m";
70 
72  const std::string on_black = "\033[40m";
73  const std::string on_red = "\033[41m";
74  const std::string on_green = "\033[42m";
75  const std::string on_yellow = "\033[43m";
76  const std::string on_blue = "\033[44m";
77  const std::string on_magenta = "\033[45m";
78  const std::string on_cyan = "\033[46m";
79  const std::string on_white = "\033[47m";
80 
81 protected:
82  void _sink_it(const details::log_msg &msg) override
83  {
84  // Wrap the originally formatted message in color codes.
85  // If color is not supported in the terminal, log as is instead.
86  if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
87  {
88  // before color range
89  _print_range(msg, 0, msg.color_range_start);
90  // in color range
91  _print_ccode(colors_[msg.level]);
92  _print_range(msg, msg.color_range_start, msg.color_range_end);
93  _print_ccode(reset);
94  // after color range
95  _print_range(msg, msg.color_range_end, msg.formatted.size());
96  }
97  else
98  {
99  _print_range(msg, 0, msg.formatted.size());
100  }
101  _flush();
102  }
103 
104  void _flush() override
105  {
106  fflush(target_file_);
107  }
108 
109 private:
110  void _print_ccode(const std::string &color_code)
111  {
112  fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
113  }
114  void _print_range(const details::log_msg &msg, size_t start, size_t end)
115  {
116  fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
117  }
118  FILE *target_file_;
119  bool should_do_colors_;
120  std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
121 };
122 
123 template<class Mutex>
125 {
126 public:
128  : ansicolor_sink<Mutex>(stdout)
129  {
130  }
131 };
132 
135 
136 template<class Mutex>
138 {
139 public:
141  : ansicolor_sink<Mutex>(stderr)
142  {
143  }
144 };
145 
148 
149 } // namespace sinks
150 } // namespace spdlog
const std::string reset
Formatting codes.
Definition: ansicolor_sink.h:52
const Char * data() const FMT_NOEXCEPT
Definition: format.h:3280
Definition: ansicolor_sink.h:137
Definition: async_logger.h:26
std::size_t size() const
Definition: format.h:3271
Definition: log_msg.h:16
Definition: ansicolor_sink.h:124
Definition: ansicolor_sink.h:24
const std::string on_black
Background colors.
Definition: ansicolor_sink.h:72
Definition: base_sink.h:23