Public Types | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex > Class Template Referencefinal

#include <ansicolor_sink.h>

Inheritance diagram for spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >:
spdlog::sinks::sink

Public Types

using mutex_t = typename ConsoleMutex::mutex_t
 

Public Member Functions

 ansicolor_sink ()
 
 ~ansicolor_sink () override=default
 
 ansicolor_sink (const ansicolor_sink &other)=delete
 
ansicolor_sinkoperator= (const ansicolor_sink &other)=delete
 
void set_color (level::level_enum color_level, const std::string &color)
 
void log (const details::log_msg &msg) override
 
void flush () override
 
void set_pattern (const std::string &pattern) final
 
void set_formatter (std::unique_ptr< spdlog::formatter > sink_formatter) override
 
- Public Member Functions inherited from spdlog::sinks::sink
 sink ()
 
 sink (std::unique_ptr< spdlog::pattern_formatter > formatter)
 
virtual ~sink ()=default
 
bool should_log (level::level_enum msg_level) const
 
void set_level (level::level_enum log_level)
 
level::level_enum level () const
 

Public Attributes

const std::string reset = "\033[m"
 Formatting codes. More...
 
const std::string bold = "\033[1m"
 
const std::string dark = "\033[2m"
 
const std::string underline = "\033[4m"
 
const std::string blink = "\033[5m"
 
const std::string reverse = "\033[7m"
 
const std::string concealed = "\033[8m"
 
const std::string clear_line = "\033[K"
 
const std::string black = "\033[30m"
 
const std::string red = "\033[31m"
 
const std::string green = "\033[32m"
 
const std::string yellow = "\033[33m"
 
const std::string blue = "\033[34m"
 
const std::string magenta = "\033[35m"
 
const std::string cyan = "\033[36m"
 
const std::string white = "\033[37m"
 
const std::string on_black = "\033[40m"
 Background colors. More...
 
const std::string on_red = "\033[41m"
 
const std::string on_green = "\033[42m"
 
const std::string on_yellow = "\033[43m"
 
const std::string on_blue = "\033[44m"
 
const std::string on_magenta = "\033[45m"
 
const std::string on_cyan = "\033[46m"
 
const std::string on_white = "\033[47m"
 

Private Member Functions

void print_ccode_ (const std::string &color_code)
 
void print_range_ (const fmt::memory_buffer &formatted, size_t start, size_t end)
 

Private Attributes

FILE * target_file_
 
mutex_tmutex_
 
bool should_do_colors_
 
std::unordered_map< level::level_enum, std::string, level::level_hashercolors_
 

Additional Inherited Members

- Protected Attributes inherited from spdlog::sinks::sink
level_t level_
 
std::unique_ptr< spdlog::formatterformatter_
 

Detailed Description

template<typename TargetStream, class ConsoleMutex>
class spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >

This sink prefixes the output with an ANSI escape sequence color code depending on the severity of the message. If no color terminal detected, omit the escape codes.

Definition at line 32 of file ansicolor_sink.h.

Member Typedef Documentation

template<typename TargetStream , class ConsoleMutex >
using spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::mutex_t = typename ConsoleMutex::mutex_t

Definition at line 35 of file ansicolor_sink.h.

Constructor & Destructor Documentation

template<typename TargetStream , class ConsoleMutex >
spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::ansicolor_sink ( )
inline

Definition at line 36 of file ansicolor_sink.h.

39 
40  {
47  colors_[level::critical] = bold + on_red;
49  }
const std::string reset
Formatting codes.
bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
Definition: os.h:410
bool is_color_terminal() SPDLOG_NOEXCEPT
Definition: os.h:388
std::unordered_map< level::level_enum, std::string, level::level_hasher > colors_
template<typename TargetStream , class ConsoleMutex >
spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::~ansicolor_sink ( )
overridedefault
template<typename TargetStream , class ConsoleMutex >
spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::ansicolor_sink ( const ansicolor_sink< TargetStream, ConsoleMutex > &  other)
delete

Member Function Documentation

template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::flush ( )
inlineoverridevirtual

Implements spdlog::sinks::sink.

Definition at line 118 of file ansicolor_sink.h.

119  {
120  std::lock_guard<mutex_t> lock(mutex_);
121  fflush(target_file_);
122  }
template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::log ( const details::log_msg msg)
inlineoverridevirtual

Implements spdlog::sinks::sink.

Definition at line 92 of file ansicolor_sink.h.

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);
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  }
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void msg(const char *fmt,...)
Definition: message.cpp:107
const std::string reset
Formatting codes.
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
void print_ccode_(const std::string &color_code)
std::unordered_map< level::level_enum, std::string, level::level_hasher > colors_
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
template<typename TargetStream , class ConsoleMutex >
ansicolor_sink& spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::operator= ( const ansicolor_sink< TargetStream, ConsoleMutex > &  other)
delete
template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::print_ccode_ ( const std::string color_code)
inlineprivate

Definition at line 137 of file ansicolor_sink.h.

138  {
139  fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
140  }
template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::print_range_ ( const fmt::memory_buffer formatted,
size_t  start,
size_t  end 
)
inlineprivate

Definition at line 141 of file ansicolor_sink.h.

142  {
143  fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
144  }
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::set_color ( level::level_enum  color_level,
const std::string color 
)
inline

Definition at line 56 of file ansicolor_sink.h.

57  {
58  std::lock_guard<mutex_t> lock(mutex_);
59  colors_[color_level] = color;
60  }
color
Definition: color.h:50
std::unordered_map< level::level_enum, std::string, level::level_hasher > colors_
template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::set_formatter ( std::unique_ptr< spdlog::formatter sink_formatter)
inlineoverridevirtual

Implements spdlog::sinks::sink.

Definition at line 130 of file ansicolor_sink.h.

131  {
132  std::lock_guard<mutex_t> lock(mutex_);
133  formatter_ = std::move(sink_formatter);
134  }
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
def move(depos, offset)
Definition: depos.py:107
template<typename TargetStream , class ConsoleMutex >
void spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::set_pattern ( const std::string pattern)
inlinefinalvirtual

Implements spdlog::sinks::sink.

Definition at line 124 of file ansicolor_sink.h.

125  {
126  std::lock_guard<mutex_t> lock(mutex_);
127  formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
128  }
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
std::string pattern
Definition: regex_t.cc:33

Member Data Documentation

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::black = "\033[30m"

Definition at line 73 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::blink = "\033[5m"

Definition at line 67 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::blue = "\033[34m"

Definition at line 77 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::bold = "\033[1m"

Definition at line 64 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::clear_line = "\033[K"

Definition at line 70 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
std::unordered_map<level::level_enum, std::string, level::level_hasher> spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::colors_
private

Definition at line 150 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::concealed = "\033[8m"

Definition at line 69 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::cyan = "\033[36m"

Definition at line 79 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::dark = "\033[2m"

Definition at line 65 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::green = "\033[32m"

Definition at line 75 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::magenta = "\033[35m"

Definition at line 78 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
mutex_t& spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::mutex_
private

Definition at line 147 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_black = "\033[40m"

Background colors.

Definition at line 83 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_blue = "\033[44m"

Definition at line 87 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_cyan = "\033[46m"

Definition at line 89 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_green = "\033[42m"

Definition at line 85 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_magenta = "\033[45m"

Definition at line 88 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_red = "\033[41m"

Definition at line 84 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_white = "\033[47m"

Definition at line 90 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::on_yellow = "\033[43m"

Definition at line 86 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::red = "\033[31m"

Definition at line 74 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::reset = "\033[m"

Formatting codes.

Definition at line 63 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::reverse = "\033[7m"

Definition at line 68 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
bool spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::should_do_colors_
private

Definition at line 149 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
FILE* spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::target_file_
private

Definition at line 146 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::underline = "\033[4m"

Definition at line 66 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::white = "\033[37m"

Definition at line 80 of file ansicolor_sink.h.

template<typename TargetStream , class ConsoleMutex >
const std::string spdlog::sinks::ansicolor_sink< TargetStream, ConsoleMutex >::yellow = "\033[33m"

Definition at line 76 of file ansicolor_sink.h.


The documentation for this class was generated from the following file: