Commit 724713ac authored by gabime's avatar gabime

Merge branch 'v1.x' of https://github.com/gabime/spdlog into v1.x

parents 2512ac1e 72f3d529
...@@ -11,133 +11,168 @@ ...@@ -11,133 +11,168 @@
#include "spdlog/details/pattern_formatter.h" #include "spdlog/details/pattern_formatter.h"
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<typename ConsoleMutex> template<typename ConsoleMutex>
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(HANDLE out_handle, color_mode mode) SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(HANDLE out_handle, color_mode mode)
: out_handle_(out_handle) : out_handle_(out_handle)
, mutex_(ConsoleMutex::mutex()) , mutex_(ConsoleMutex::mutex())
, formatter_(details::make_unique<spdlog::pattern_formatter>()) , formatter_(details::make_unique<spdlog::pattern_formatter>())
{ {
set_color_mode(mode); // check if out_handle is points to the actual console.
colors_[level::trace] = WHITE; // ::GetConsoleMode() should return 0 if it is redirected or not valid console handle.
colors_[level::debug] = CYAN; DWORD console_mode;
colors_[level::info] = GREEN; in_console_ = ::GetConsoleMode(out_handle, &console_mode) != 0;
colors_[level::warn] = YELLOW | BOLD; set_color_mode(mode);
colors_[level::err] = RED | BOLD; // red bold colors_[level::trace] = WHITE;
colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background colors_[level::debug] = CYAN;
colors_[level::off] = 0; colors_[level::info] = GREEN;
} colors_[level::warn] = YELLOW | BOLD;
colors_[level::err] = RED | BOLD; // red bold
template<typename ConsoleMutex> colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink() colors_[level::off] = 0;
{ }
this->flush();
} template<typename ConsoleMutex>
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink()
// change the color for the given level {
template<typename ConsoleMutex> this->flush();
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, WORD color) }
{
std::lock_guard<mutex_t> lock(mutex_); // change the color for the given level
colors_[level] = color; template<typename ConsoleMutex>
} void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, WORD color)
{
template<typename ConsoleMutex> std::lock_guard<mutex_t> lock(mutex_);
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) colors_[level] = color;
{ }
std::lock_guard<mutex_t> lock(mutex_);
fmt::memory_buffer formatted; template<typename ConsoleMutex>
formatter_->format(msg, formatted); void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
{ std::lock_guard<mutex_t> lock(mutex_);
// before color range fmt::memory_buffer formatted;
print_range_(formatted, 0, msg.color_range_start); formatter_->format(msg, formatted);
if (!in_console_)
// in color range {
auto orig_attribs = set_console_attribs(colors_[msg.level]); write_to_file_(formatted);
print_range_(formatted, msg.color_range_start, msg.color_range_end); return;
::SetConsoleTextAttribute(out_handle_, }
orig_attribs); // reset to orig colors
// after color range if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
print_range_(formatted, msg.color_range_end, formatted.size()); {
} // before color range
else // print without colors if color range is invalid (or color is disabled) print_range_(formatted, 0, msg.color_range_start);
{
print_range_(formatted, 0, formatted.size()); // in color range
} auto orig_attribs = set_console_attribs(colors_[msg.level]);
} print_range_(formatted, msg.color_range_start, msg.color_range_end);
::SetConsoleTextAttribute(out_handle_,
template<typename ConsoleMutex> orig_attribs); // reset to orig colors
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush() // after color range
{ print_range_(formatted, msg.color_range_end, formatted.size());
// windows console always flushed? }
} else // print without colors if color range is invalid (or color is disabled)
{
template<typename ConsoleMutex> print_range_(formatted, 0, formatted.size());
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) }
{ }
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
} template<typename ConsoleMutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush()
template<typename ConsoleMutex> {
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) // windows console always flushed?
{ }
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter); template<typename ConsoleMutex>
} void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
{
template<typename ConsoleMutex> std::lock_guard<mutex_t> lock(mutex_);
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
{ }
switch (mode)
{ template<typename ConsoleMutex>
case color_mode::always: void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
case color_mode::automatic: {
should_do_colors_ = true; std::lock_guard<mutex_t> lock(mutex_);
break; formatter_ = std::move(sink_formatter);
case color_mode::never: }
should_do_colors_ = false;
break; template<typename ConsoleMutex>
default: void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
should_do_colors_ = true; {
} switch (mode)
} {
case color_mode::always:
// set color and return the orig console attributes (for resetting later) case color_mode::automatic:
template<typename ConsoleMutex> should_do_colors_ = true;
WORD SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_console_attribs(WORD attribs) break;
{ case color_mode::never:
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; should_do_colors_ = false;
::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); break;
WORD back_color = orig_buffer_info.wAttributes; default:
// retrieve the current background color should_do_colors_ = true;
back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)); }
// keep the background color unchanged }
::SetConsoleTextAttribute(out_handle_, attribs | back_color);
return orig_buffer_info.wAttributes; // return orig attribs // set color and return the orig console attributes (for resetting later)
} template<typename ConsoleMutex>
WORD SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_console_attribs(WORD attribs)
// print a range of formatted message to console {
template<typename ConsoleMutex> in_console_
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
{ ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
auto size = static_cast<DWORD>(end - start); WORD back_color = orig_buffer_info.wAttributes;
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); // retrieve the current background color
} back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
// keep the background color unchanged
// wincolor_stdout_sink ::SetConsoleTextAttribute(out_handle_, attribs | back_color);
template<typename ConsoleMutex> return orig_buffer_info.wAttributes; // return orig attribs
SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode) }
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
{} // print a range of formatted message to console
template<typename ConsoleMutex>
// wincolor_stderr_sink void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
template<typename ConsoleMutex> {
SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode) auto size = static_cast<DWORD>(end - start);
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
{} }
} // namespace sinks
template<typename ConsoleMutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const fmt::memory_buffer &formatted)
{
auto size = static_cast<DWORD>(formatted.size());
if (size == 0)
{
return;
}
DWORD total_written = 0;
do
{
DWORD bytes_written = 0;
bool ok = WriteFile(out_handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
if (!ok || bytes_written == 0)
{
throw spdlog_ex("wincolor_sink: write_to_file_ failed. GetLastError(): " + std::to_string(::GetLastError()));
}
total_written += bytes_written;
} while (total_written < size);
}
// wincolor_stdout_sink
template<typename ConsoleMutex>
SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
{}
// wincolor_stderr_sink
template<typename ConsoleMutex>
SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
{}
} // namespace sinks
} // namespace spdlog } // namespace spdlog
\ No newline at end of file
...@@ -49,7 +49,8 @@ protected: ...@@ -49,7 +49,8 @@ protected:
using mutex_t = typename ConsoleMutex::mutex_t; using mutex_t = typename ConsoleMutex::mutex_t;
HANDLE out_handle_; HANDLE out_handle_;
mutex_t &mutex_; mutex_t &mutex_;
bool should_do_colors_; bool in_console_;
bool should_do_colors_;
std::unique_ptr<spdlog::formatter> formatter_; std::unique_ptr<spdlog::formatter> formatter_;
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_; std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
...@@ -57,6 +58,10 @@ protected: ...@@ -57,6 +58,10 @@ protected:
WORD set_console_attribs(WORD attribs); WORD set_console_attribs(WORD attribs);
// print a range of formatted message to console // print a range of formatted message to console
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
//in case we are redirected to file (not in console mode)
void write_to_file_(const fmt::memory_buffer &formatted);
}; };
template<typename ConsoleMutex> template<typename ConsoleMutex>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment