From 05cac05c06c1a0f20fe7d0f08eec0f984bf909f0 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 14 Jul 2019 18:35:59 +0300 Subject: [PATCH] clang_format --- include/spdlog/sinks/wincolor_sink-inl.h | 291 +++++++++++------------ include/spdlog/sinks/wincolor_sink.h | 9 +- 2 files changed, 148 insertions(+), 152 deletions(-) diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 30dea867..4258be9f 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -11,168 +11,165 @@ #include "spdlog/details/pattern_formatter.h" namespace spdlog { - namespace sinks { +namespace sinks { - template - SPDLOG_INLINE wincolor_sink::wincolor_sink(HANDLE out_handle, color_mode mode) - : out_handle_(out_handle) - , mutex_(ConsoleMutex::mutex()) - , formatter_(details::make_unique()) - { - // check if out_handle is points to the actual console. - // ::GetConsoleMode() should return 0 if it is redirected or not valid console handle. - DWORD console_mode; - in_console_ = ::GetConsoleMode(out_handle, &console_mode) != 0; - set_color_mode(mode); - colors_[level::trace] = WHITE; - colors_[level::debug] = CYAN; - colors_[level::info] = GREEN; - colors_[level::warn] = YELLOW | BOLD; - colors_[level::err] = RED | BOLD; // red bold - colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background - colors_[level::off] = 0; - } +template +SPDLOG_INLINE wincolor_sink::wincolor_sink(HANDLE out_handle, color_mode mode) + : out_handle_(out_handle) + , mutex_(ConsoleMutex::mutex()) + , formatter_(details::make_unique()) +{ + // check if out_handle is points to the actual console. + // ::GetConsoleMode() should return 0 if it is redirected or not valid console handle. + DWORD console_mode; + in_console_ = ::GetConsoleMode(out_handle, &console_mode) != 0; + set_color_mode(mode); + colors_[level::trace] = WHITE; + colors_[level::debug] = CYAN; + colors_[level::info] = GREEN; + colors_[level::warn] = YELLOW | BOLD; + colors_[level::err] = RED | BOLD; // red bold + colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background + colors_[level::off] = 0; +} - template - SPDLOG_INLINE wincolor_sink::~wincolor_sink() - { - this->flush(); - } +template +SPDLOG_INLINE wincolor_sink::~wincolor_sink() +{ + this->flush(); +} - // change the color for the given level - template - void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, WORD color) - { - std::lock_guard lock(mutex_); - colors_[level] = color; - } +// change the color for the given level +template +void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, WORD color) +{ + std::lock_guard lock(mutex_); + colors_[level] = color; +} - template - void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) - { - std::lock_guard lock(mutex_); - fmt::memory_buffer formatted; - formatter_->format(msg, formatted); - if (!in_console_) - { - write_to_file_(formatted); - return; - } - - if (should_do_colors_ && msg.color_range_end > msg.color_range_start) - { - // before color range - print_range_(formatted, 0, msg.color_range_start); +template +void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) +{ + std::lock_guard lock(mutex_); + fmt::memory_buffer formatted; + formatter_->format(msg, formatted); + if (!in_console_) + { + write_to_file_(formatted); + return; + } - // 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_, - orig_attribs); // reset to orig colors - // after color range - print_range_(formatted, msg.color_range_end, formatted.size()); - } - else // print without colors if color range is invalid (or color is disabled) - { - print_range_(formatted, 0, formatted.size()); - } - } + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) + { + // before color range + print_range_(formatted, 0, msg.color_range_start); + // 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_, + orig_attribs); // reset to orig colors + // after color range + print_range_(formatted, msg.color_range_end, formatted.size()); + } + else // print without colors if color range is invalid (or color is disabled) + { + print_range_(formatted, 0, formatted.size()); + } +} - template - void SPDLOG_INLINE wincolor_sink::flush() - { - // windows console always flushed? - } +template +void SPDLOG_INLINE wincolor_sink::flush() +{ + // windows console always flushed? +} - template - void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) - { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); - } +template +void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) +{ + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); +} - template - void SPDLOG_INLINE wincolor_sink::set_formatter(std::unique_ptr sink_formatter) - { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); - } +template +void SPDLOG_INLINE wincolor_sink::set_formatter(std::unique_ptr sink_formatter) +{ + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); +} - template - void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) - { - switch (mode) - { - case color_mode::always: - case color_mode::automatic: - should_do_colors_ = true; - break; - case color_mode::never: - should_do_colors_ = false; - break; - default: - should_do_colors_ = true; - } - } +template +void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) +{ + switch (mode) + { + case color_mode::always: + case color_mode::automatic: + should_do_colors_ = true; + break; + case color_mode::never: + should_do_colors_ = false; + break; + default: + should_do_colors_ = true; + } +} - // set color and return the orig console attributes (for resetting later) - template - WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) - { - in_console_ - CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; - ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); - WORD back_color = orig_buffer_info.wAttributes; - // retrieve the current background color - back_color &= static_cast(~(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 +WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) +{ + in_console_ CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; + ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); + WORD back_color = orig_buffer_info.wAttributes; + // retrieve the current background color + back_color &= static_cast(~(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 +} - // print a range of formatted message to console - template - void SPDLOG_INLINE wincolor_sink::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) - { - auto size = static_cast(end - start); - ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); - } +// print a range of formatted message to console +template +void SPDLOG_INLINE wincolor_sink::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) +{ + auto size = static_cast(end - start); + ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); +} +template +void SPDLOG_INLINE wincolor_sink::write_to_file_(const fmt::memory_buffer &formatted) +{ + auto size = static_cast(formatted.size()); + if (size == 0) + { + return; + } - template - void SPDLOG_INLINE wincolor_sink::write_to_file_(const fmt::memory_buffer &formatted) - { - auto size = static_cast(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); +} - 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 +SPDLOG_INLINE wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) + : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) +{} - // wincolor_stdout_sink - template - SPDLOG_INLINE wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) - {} +// wincolor_stderr_sink +template +SPDLOG_INLINE wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) + : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) +{} - // wincolor_stderr_sink - template - SPDLOG_INLINE wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) - {} - - } // namespace sinks +} // namespace sinks } // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 3b736ed9..e9578bc0 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -49,8 +49,8 @@ protected: using mutex_t = typename ConsoleMutex::mutex_t; HANDLE out_handle_; mutex_t &mutex_; - bool in_console_; - bool should_do_colors_; + bool in_console_; + bool should_do_colors_; std::unique_ptr formatter_; std::unordered_map colors_; @@ -59,9 +59,8 @@ protected: // print a range of formatted message to console 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); - + // in case we are redirected to file (not in console mode) + void write_to_file_(const fmt::memory_buffer &formatted); }; template