|
|
|
@ -19,6 +19,10 @@ SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(HANDLE out_handle, colo
|
|
|
|
|
, mutex_(ConsoleMutex::mutex())
|
|
|
|
|
, formatter_(details::make_unique<spdlog::pattern_formatter>())
|
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
@ -49,6 +53,12 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
|
|
|
|
|
std::lock_guard<mutex_t> 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
|
|
|
|
@ -68,6 +78,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename ConsoleMutex>
|
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush()
|
|
|
|
|
{
|
|
|
|
@ -109,6 +120,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
|
|
|
|
|
template<typename ConsoleMutex>
|
|
|
|
|
WORD SPDLOG_INLINE wincolor_sink<ConsoleMutex>::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;
|
|
|
|
@ -127,6 +139,29 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const fmt::memory_b
|
|
|
|
|
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|