Agora  1.2.0
Agora project
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 
13 namespace spdlog {
14 namespace sinks {
15 
16 template<typename ConsoleMutex>
18  : target_file_(target_file)
19  , mutex_(ConsoleMutex::mutex())
20  , formatter_(details::make_unique<spdlog::pattern_formatter>())
21 
22 {
31 }
32 
33 template<typename ConsoleMutex>
35 {
36  std::lock_guard<mutex_t> lock(mutex_);
37  colors_[static_cast<size_t>(color_level)] = to_string_(color);
38 }
39 
40 template<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_[static_cast<size_t>(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 
68 template<typename ConsoleMutex>
70 {
71  std::lock_guard<mutex_t> lock(mutex_);
72  fflush(target_file_);
73 }
74 
75 template<typename ConsoleMutex>
77 {
78  std::lock_guard<mutex_t> lock(mutex_);
79  formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
80 }
81 
82 template<typename ConsoleMutex>
83 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
84 {
85  std::lock_guard<mutex_t> lock(mutex_);
86  formatter_ = std::move(sink_formatter);
87 }
88 
89 template<typename ConsoleMutex>
91 {
92  return should_do_colors_;
93 }
94 
95 template<typename ConsoleMutex>
97 {
98  switch (mode)
99  {
100  case color_mode::always:
101  should_do_colors_ = true;
102  return;
104  should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
105  return;
106  case color_mode::never:
107  should_do_colors_ = false;
108  return;
109  default:
110  should_do_colors_ = false;
111  }
112 }
113 
114 template<typename ConsoleMutex>
116 {
117  fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
118 }
119 
120 template<typename ConsoleMutex>
122 {
123  fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
124 }
125 
126 template<typename ConsoleMutex>
128 {
129  return std::string(sv.data(), sv.size());
130 }
131 
132 // ansicolor_stdout_sink
133 template<typename ConsoleMutex>
135  : ansicolor_sink<ConsoleMutex>(stdout, mode)
136 {}
137 
138 // ansicolor_stderr_sink
139 template<typename ConsoleMutex>
141  : ansicolor_sink<ConsoleMutex>(stderr, mode)
142 {}
143 
144 } // namespace sinks
145 } // namespace spdlog
fmt::v8::color
color
Definition: color.h:23
spdlog::details::os::in_terminal
SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
Definition: os-inl.h:447
spdlog::sinks::ansicolor_sink::white
const string_view_t white
Definition: ansicolor_sink.h:65
spdlog::sinks::ansicolor_sink::colors_
std::array< std::string, level::n_levels > colors_
Definition: ansicolor_sink.h:87
spdlog::sinks::ansicolor_sink::cyan
const string_view_t cyan
Definition: ansicolor_sink.h:64
spdlog::level::trace
@ trace
Definition: common.h:213
spdlog::sinks::ansicolor_sink::set_formatter
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
Definition: ansicolor_sink-inl.h:83
spdlog::level::off
@ off
Definition: common.h:219
spdlog::level::warn
@ warn
Definition: common.h:216
mm_gui.mode
string mode
Definition: mm_gui.py:105
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
spdlog::sinks::ansicolor_sink::flush
void flush() override
Definition: ansicolor_sink-inl.h:69
fmt::v8::basic_string_view
Definition: core.h:448
fmt::v8::basic_string_view::size
constexpr auto size() const -> size_t
Definition: core.h:495
spdlog::level::info
@ info
Definition: common.h:215
spdlog::level::level_enum
level_enum
Definition: common.h:211
spdlog::sinks::ansicolor_sink::to_string_
static std::string to_string_(const string_view_t &sv)
Definition: ansicolor_sink-inl.h:127
spdlog::details::log_msg::color_range_end
size_t color_range_end
Definition: log_msg.h:27
spdlog::level::debug
@ debug
Definition: common.h:214
spdlog::sinks::ansicolor_sink::should_color
bool should_color()
Definition: ansicolor_sink-inl.h:90
spdlog::pattern_formatter
Definition: pattern_formatter.h:77
spdlog::level::err
@ err
Definition: common.h:217
spdlog::details::os::is_color_terminal
SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT
Definition: os-inl.h:416
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog::sinks::ansicolor_sink::set_pattern
void set_pattern(const std::string &pattern) final
Definition: ansicolor_sink-inl.h:76
spdlog::sinks::ansicolor_sink::print_range_
void print_range_(const memory_buf_t &formatted, size_t start, size_t end)
Definition: ansicolor_sink-inl.h:121
spdlog
Definition: async.h:25
spdlog::color_mode::always
@ always
spdlog::sinks::ansicolor_sink::bold_on_red
const string_view_t bold_on_red
Definition: ansicolor_sink.h:80
spdlog::sinks::ansicolor_sink::yellow_bold
const string_view_t yellow_bold
Bold colors.
Definition: ansicolor_sink.h:78
SPDLOG_INLINE
#define SPDLOG_INLINE
Definition: common.h:42
os.h
spdlog::sinks::ansicolor_sink::print_ccode_
void print_ccode_(const string_view_t &color_code)
Definition: ansicolor_sink-inl.h:115
start
end start
Definition: inspect_agora_results.m:95
spdlog::details::make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: common.h:334
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:21
spdlog::sinks::ansicolor_sink::reset
const string_view_t reset
Definition: ansicolor_sink.h:48
spdlog::sinks::ansicolor_stderr_sink::ansicolor_stderr_sink
ansicolor_stderr_sink(color_mode mode=color_mode::automatic)
Definition: ansicolor_sink-inl.h:140
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
ansicolor_sink.h
fmt::v8::basic_string_view::data
constexpr auto data() const -> const Char *
Definition: core.h:492
spdlog::sinks::ansicolor_sink::red_bold
const string_view_t red_bold
Definition: ansicolor_sink.h:79
spdlog::sinks::ansicolor_sink::set_color_mode
void set_color_mode(color_mode mode)
Definition: ansicolor_sink-inl.h:96
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::ansicolor_sink::set_color
void set_color(level::level_enum color_level, string_view_t color)
Definition: ansicolor_sink-inl.h:34
pattern_formatter.h
fwrite
fwrite(fileID, pilot_f, 'float')
spdlog::sinks::ansicolor_sink::ansicolor_sink
ansicolor_sink(FILE *target_file, color_mode mode)
Definition: ansicolor_sink-inl.h:17
spdlog::sinks::ansicolor_sink
Definition: ansicolor_sink.h:25
spdlog::details::log_msg::color_range_start
size_t color_range_start
Definition: log_msg.h:26
spdlog::sinks::ansicolor_sink::green
const string_view_t green
Definition: ansicolor_sink.h:60
spdlog::sinks::ansicolor_sink::log
void log(const details::log_msg &msg) override
Definition: ansicolor_sink-inl.h:41
spdlog::color_mode::automatic
@ automatic
spdlog::level::critical
@ critical
Definition: common.h:218
spdlog::color_mode
color_mode
Definition: common.h:256
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::end
end_tag end(T &&...)
spdlog::color_mode::never
@ never
spdlog::sinks::ansicolor_stdout_sink::ansicolor_stdout_sink
ansicolor_stdout_sink(color_mode mode=color_mode::automatic)
Definition: ansicolor_sink-inl.h:134