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