spdlog
Loading...
Searching...
No Matches
ansicolor_sink-inl.h
Go to the documentation of this file.
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#ifndef SPDLOG_HEADER_ONLY
8#endif
9
11#include <spdlog/details/os.h>
12
13namespace spdlog {
14namespace sinks {
15
16template<typename ConsoleMutex>
18 : target_file_(target_file)
19 , mutex_(ConsoleMutex::mutex())
20 , formatter_(details::make_unique<spdlog::pattern_formatter>())
21
22{
23 set_color_mode(mode);
31}
32
33template<typename ConsoleMutex>
35{
36 std::lock_guard<mutex_t> lock(mutex_);
37 colors_[color_level] = to_string_(color);
38}
39
40template<typename ConsoleMutex>
42{
43 // Wrap the originally formatted message in color codes.
44 // If color is not supported in the terminal, log as is instead.
45 std::lock_guard<mutex_t> lock(mutex_);
46 msg.color_range_start = 0;
47 msg.color_range_end = 0;
48 memory_buf_t formatted;
49 formatter_->format(msg, formatted);
50 if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
51 {
52 // before color range
53 print_range_(formatted, 0, msg.color_range_start);
54 // in color range
55 print_ccode_(colors_[msg.level]);
56 print_range_(formatted, msg.color_range_start, msg.color_range_end);
57 print_ccode_(reset);
58 // after color range
59 print_range_(formatted, msg.color_range_end, formatted.size());
60 }
61 else // no color
62 {
63 print_range_(formatted, 0, formatted.size());
64 }
65 fflush(target_file_);
66}
67
68template<typename ConsoleMutex>
70{
71 std::lock_guard<mutex_t> lock(mutex_);
72 fflush(target_file_);
73}
74
75template<typename ConsoleMutex>
81
82template<typename ConsoleMutex>
84{
85 std::lock_guard<mutex_t> lock(mutex_);
86 formatter_ = std::move(sink_formatter);
87}
88
89template<typename ConsoleMutex>
91{
92 return should_do_colors_;
93}
94
95template<typename ConsoleMutex>
97{
98 switch (mode)
99 {
101 should_do_colors_ = true;
102 return;
104 should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
105 return;
107 should_do_colors_ = false;
108 return;
109 default:
110 should_do_colors_ = false;
111 }
112}
113
114template<typename ConsoleMutex>
116{
117 fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
118}
119
120template<typename ConsoleMutex>
121SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
122{
123 fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
124}
125
126template<typename ConsoleMutex>
131
132// ansicolor_stdout_sink
133template<typename ConsoleMutex>
137
138// ansicolor_stderr_sink
139template<typename ConsoleMutex>
143
144} // namespace sinks
145} // namespace spdlog
void set_pattern(const std::string &pattern) final
static std::string to_string_(const string_view_t &sv)
const string_view_t red_bold
void set_color(level::level_enum color_level, string_view_t color)
const string_view_t bold_on_red
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
std::array< std::string, level::n_levels > colors_
void set_color_mode(color_mode mode)
const string_view_t yellow_bold
Bold colors.
ansicolor_sink(FILE *target_file, color_mode mode)
void log(const details::log_msg &msg) override
void print_ccode_(const string_view_t &color_code)
void print_range_(const memory_buf_t &formatted, size_t start, size_t end)
ansicolor_stderr_sink(color_mode mode=color_mode::automatic)
ansicolor_stdout_sink(color_mode mode=color_mode::automatic)
color
Definition color.h:23
#define SPDLOG_INLINE
Definition common.h:33
SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT
Definition os-inl.h:405
SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
Definition os-inl.h:436
Definition async.h:25
fmt::basic_string_view< char > string_view_t
Definition common.h:114
color_mode
Definition common.h:207
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
level::level_enum level
Definition log_msg.h:21