Public Member Functions | Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex > Class Template Reference

#include <wincolor_sink.h>

Inheritance diagram for spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >:
spdlog::sinks::sink

Public Member Functions

 wincolor_sink ()
 
 ~wincolor_sink () override
 
 wincolor_sink (const wincolor_sink &other)=delete
 
wincolor_sinkoperator= (const wincolor_sink &other)=delete
 
void set_color (level::level_enum level, WORD color)
 
void log (const details::log_msg &msg) final override
 
void flush () final override
 
void set_pattern (const std::string &pattern) override final
 
void set_formatter (std::unique_ptr< spdlog::formatter > sink_formatter) override final
 
- 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 WORD BOLD = FOREGROUND_INTENSITY
 
const WORD RED = FOREGROUND_RED
 
const WORD GREEN = FOREGROUND_GREEN
 
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE
 
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
 
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN
 

Private Types

using mutex_t = typename ConsoleMutex::mutex_t
 

Private Member Functions

WORD set_console_attribs (WORD attribs)
 
void print_range_ (const fmt::memory_buffer &formatted, size_t start, size_t end)
 

Private Attributes

HANDLE out_handle_
 
mutex_tmutex_
 
std::unordered_map< level::level_enum, WORD, 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 OutHandle, typename ConsoleMutex>
class spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >

Definition at line 30 of file wincolor_sink.h.

Member Typedef Documentation

template<typename OutHandle , typename ConsoleMutex >
using spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::mutex_t = typename ConsoleMutex::mutex_t
private

Definition at line 110 of file wincolor_sink.h.

Constructor & Destructor Documentation

template<typename OutHandle , typename ConsoleMutex >
spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::wincolor_sink ( )
inline

Definition at line 40 of file wincolor_sink.h.

41  : out_handle_(OutHandle::handle())
43  {
48  colors_[level::err] = RED | BOLD; // red bold
49  colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
50  colors_[level::off] = 0;
51  }
std::unordered_map< level::level_enum, WORD, level::level_hasher > colors_
template<typename OutHandle , typename ConsoleMutex >
spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::~wincolor_sink ( )
inlineoverride

Definition at line 53 of file wincolor_sink.h.

54  {
55  this->flush();
56  }
void flush() final override
Definition: wincolor_sink.h:92
template<typename OutHandle , typename ConsoleMutex >
spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::wincolor_sink ( const wincolor_sink< OutHandle, ConsoleMutex > &  other)
delete

Member Function Documentation

template<typename OutHandle , typename ConsoleMutex >
void spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::flush ( )
inlinefinaloverridevirtual

Implements spdlog::sinks::sink.

Definition at line 92 of file wincolor_sink.h.

93  {
94  // windows console always flushed?
95  }
template<typename OutHandle , typename ConsoleMutex >
void spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::log ( const details::log_msg msg)
inlinefinaloverridevirtual

Implements spdlog::sinks::sink.

Definition at line 68 of file wincolor_sink.h.

69  {
70  std::lock_guard<mutex_t> lock(mutex_);
71  fmt::memory_buffer formatted;
72  formatter_->format(msg, formatted);
73  if (msg.color_range_end > msg.color_range_start)
74  {
75  // before color range
76  print_range_(formatted, 0, msg.color_range_start);
77 
78  // in color range
79  auto orig_attribs = set_console_attribs(colors_[msg.level]);
80  print_range_(formatted, msg.color_range_start, msg.color_range_end);
81  ::SetConsoleTextAttribute(out_handle_,
82  orig_attribs); // reset to orig colors
83  // after color range
84  print_range_(formatted, msg.color_range_end, formatted.size());
85  }
86  else // print without colors if color range is invalid
87  {
88  print_range_(formatted, 0, formatted.size());
89  }
90  }
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void msg(const char *fmt,...)
Definition: message.cpp:107
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
std::unordered_map< level::level_enum, WORD, level::level_hasher > colors_
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
WORD set_console_attribs(WORD attribs)
template<typename OutHandle , typename ConsoleMutex >
wincolor_sink& spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::operator= ( const wincolor_sink< OutHandle, ConsoleMutex > &  other)
delete
template<typename OutHandle , typename ConsoleMutex >
void spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::print_range_ ( const fmt::memory_buffer formatted,
size_t  start,
size_t  end 
)
inlineprivate

Definition at line 125 of file wincolor_sink.h.

126  {
127  auto size = static_cast<DWORD>(end - start);
128  ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
129  }
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:87
template<typename OutHandle , typename ConsoleMutex >
void spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::set_color ( level::level_enum  level,
WORD  color 
)
inline

Definition at line 62 of file wincolor_sink.h.

63  {
64  std::lock_guard<mutex_t> lock(mutex_);
65  colors_[level] = color;
66  }
std::unordered_map< level::level_enum, WORD, level::level_hasher > colors_
level::level_enum level() const
Definition: sink.h:45
color
Definition: color.h:50
template<typename OutHandle , typename ConsoleMutex >
WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::set_console_attribs ( WORD  attribs)
inlineprivate

Definition at line 112 of file wincolor_sink.h.

113  {
114  CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
115  ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
116  WORD back_color = orig_buffer_info.wAttributes;
117  // retrieve the current background color
118  back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
119  // keep the background color unchanged
120  ::SetConsoleTextAttribute(out_handle_, attribs | back_color);
121  return orig_buffer_info.wAttributes; // return orig attribs
122  }
#define WORD
template<typename OutHandle , typename ConsoleMutex >
void spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::set_formatter ( std::unique_ptr< spdlog::formatter sink_formatter)
inlinefinaloverridevirtual

Implements spdlog::sinks::sink.

Definition at line 103 of file wincolor_sink.h.

104  {
105  std::lock_guard<mutex_t> lock(mutex_);
106  formatter_ = std::move(sink_formatter);
107  }
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
def move(depos, offset)
Definition: depos.py:107
template<typename OutHandle , typename ConsoleMutex >
void spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::set_pattern ( const std::string pattern)
inlinefinaloverridevirtual

Implements spdlog::sinks::sink.

Definition at line 97 of file wincolor_sink.h.

98  {
99  std::lock_guard<mutex_t> lock(mutex_);
100  formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
101  }
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
std::string pattern
Definition: regex_t.cc:33

Member Data Documentation

template<typename OutHandle , typename ConsoleMutex >
const WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::BOLD = FOREGROUND_INTENSITY

Definition at line 33 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
std::unordered_map<level::level_enum, WORD, level::level_hasher> spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::colors_
private

Definition at line 133 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
const WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE

Definition at line 36 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
const WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::GREEN = FOREGROUND_GREEN

Definition at line 35 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
mutex_t& spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::mutex_
private

Definition at line 132 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
HANDLE spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::out_handle_
private

Definition at line 131 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
const WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::RED = FOREGROUND_RED

Definition at line 34 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
const WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE

Definition at line 37 of file wincolor_sink.h.

template<typename OutHandle , typename ConsoleMutex >
const WORD spdlog::sinks::wincolor_sink< OutHandle, ConsoleMutex >::YELLOW = FOREGROUND_RED | FOREGROUND_GREEN

Definition at line 38 of file wincolor_sink.h.


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