wincolor_sink-inl.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. # include <spdlog/sinks/wincolor_sink.h>
  6. #endif
  7. #include <spdlog/details/windows_include.h>
  8. #include <wincon.h>
  9. #include <spdlog/common.h>
  10. #include <spdlog/pattern_formatter.h>
  11. namespace spdlog {
  12. namespace sinks {
  13. template<typename ConsoleMutex>
  14. SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode)
  15. : out_handle_(out_handle)
  16. , mutex_(ConsoleMutex::mutex())
  17. , formatter_(details::make_unique<spdlog::pattern_formatter>())
  18. {
  19. set_color_mode_impl(mode);
  20. // set level colors
  21. colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
  22. colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
  23. colors_[level::info] = FOREGROUND_GREEN; // green
  24. colors_[level::warn] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
  25. colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
  26. colors_[level::critical] =
  27. BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; // intense white on red background
  28. colors_[level::off] = 0;
  29. }
  30. template<typename ConsoleMutex>
  31. SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink()
  32. {
  33. this->flush();
  34. }
  35. // change the color for the given level
  36. template<typename ConsoleMutex>
  37. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, std::uint16_t color)
  38. {
  39. std::lock_guard<mutex_t> lock(mutex_);
  40. colors_[static_cast<size_t>(level)] = color;
  41. }
  42. template<typename ConsoleMutex>
  43. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
  44. {
  45. if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE)
  46. {
  47. return;
  48. }
  49. std::lock_guard<mutex_t> lock(mutex_);
  50. msg.color_range_start = 0;
  51. msg.color_range_end = 0;
  52. memory_buf_t formatted;
  53. formatter_->format(msg, formatted);
  54. if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
  55. {
  56. // before color range
  57. print_range_(formatted, 0, msg.color_range_start);
  58. // in color range
  59. auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(msg.level)]));
  60. print_range_(formatted, msg.color_range_start, msg.color_range_end);
  61. // reset to orig colors
  62. ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
  63. print_range_(formatted, msg.color_range_end, formatted.size());
  64. }
  65. else // print without colors if color range is invalid (or color is disabled)
  66. {
  67. write_to_file_(formatted);
  68. }
  69. }
  70. template<typename ConsoleMutex>
  71. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush()
  72. {
  73. // windows console always flushed?
  74. }
  75. template<typename ConsoleMutex>
  76. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
  77. {
  78. std::lock_guard<mutex_t> lock(mutex_);
  79. formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
  80. }
  81. template<typename ConsoleMutex>
  82. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
  83. {
  84. std::lock_guard<mutex_t> lock(mutex_);
  85. formatter_ = std::move(sink_formatter);
  86. }
  87. template<typename ConsoleMutex>
  88. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
  89. {
  90. std::lock_guard<mutex_t> lock(mutex_);
  91. set_color_mode_impl(mode);
  92. }
  93. template<typename ConsoleMutex>
  94. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode)
  95. {
  96. if (mode == color_mode::automatic)
  97. {
  98. // should do colors only if out_handle_ points to actual console.
  99. DWORD console_mode;
  100. bool in_console = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
  101. should_do_colors_ = in_console;
  102. }
  103. else
  104. {
  105. should_do_colors_ = mode == color_mode::always ? true : false;
  106. }
  107. }
  108. // set foreground color and return the orig console attributes (for resetting later)
  109. template<typename ConsoleMutex>
  110. std::uint16_t SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs)
  111. {
  112. CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
  113. if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info))
  114. {
  115. // just return white if failed getting console info
  116. return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  117. }
  118. // change only the foreground bits (lowest 4 bits)
  119. auto new_attribs = static_cast<WORD>(attribs) | (orig_buffer_info.wAttributes & 0xfff0);
  120. auto ignored = ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
  121. (void)(ignored);
  122. return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs
  123. }
  124. // print a range of formatted message to console
  125. template<typename ConsoleMutex>
  126. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
  127. {
  128. if (end > start)
  129. {
  130. auto size = static_cast<DWORD>(end - start);
  131. auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
  132. (void)(ignored);
  133. }
  134. }
  135. template<typename ConsoleMutex>
  136. void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted)
  137. {
  138. auto size = static_cast<DWORD>(formatted.size());
  139. DWORD bytes_written = 0;
  140. auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr);
  141. (void)(ignored);
  142. }
  143. // wincolor_stdout_sink
  144. template<typename ConsoleMutex>
  145. SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
  146. : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
  147. {}
  148. // wincolor_stderr_sink
  149. template<typename ConsoleMutex>
  150. SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
  151. : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
  152. {}
  153. } // namespace sinks
  154. } // namespace spdlog