ensure stdout color sinks do not leak windows headers with SPDLOG_COMPILED_LIB
This commit is contained in:
		@@ -7,22 +7,30 @@
 | 
				
			|||||||
#include <spdlog/sinks/wincolor_sink.h>
 | 
					#include <spdlog/sinks/wincolor_sink.h>
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <spdlog/details/windows_include.h>
 | 
				
			||||||
 | 
					#include <wincon.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <spdlog/common.h>
 | 
					#include <spdlog/common.h>
 | 
				
			||||||
#include <spdlog/pattern_formatter.h>
 | 
					#include <spdlog/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(void *out_handle, color_mode mode)
 | 
				
			||||||
    : out_handle_(out_handle)
 | 
					    : BOLD(FOREGROUND_INTENSITY)
 | 
				
			||||||
 | 
					    , RED(FOREGROUND_RED)
 | 
				
			||||||
 | 
					    , GREEN(FOREGROUND_GREEN)
 | 
				
			||||||
 | 
					    , CYAN(FOREGROUND_GREEN | FOREGROUND_BLUE)
 | 
				
			||||||
 | 
					    , WHITE(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
 | 
				
			||||||
 | 
					    , YELLOW(FOREGROUND_RED | FOREGROUND_GREEN)
 | 
				
			||||||
 | 
					    , out_handle_(out_handle)
 | 
				
			||||||
    , mutex_(ConsoleMutex::mutex())
 | 
					    , mutex_(ConsoleMutex::mutex())
 | 
				
			||||||
    , formatter_(details::make_unique<spdlog::pattern_formatter>())
 | 
					    , formatter_(details::make_unique<spdlog::pattern_formatter>())
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    // check if out_handle is points to the actual console.
 | 
					    // check if out_handle is points to the actual console.
 | 
				
			||||||
    // ::GetConsoleMode() should return 0 if it is redirected or not valid console handle.
 | 
					    // ::GetConsoleMode() should return 0 if it is redirected or not valid console handle.
 | 
				
			||||||
    DWORD console_mode;
 | 
					    DWORD console_mode;
 | 
				
			||||||
    in_console_ = ::GetConsoleMode(out_handle, &console_mode) != 0;
 | 
					    in_console_ = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set_color_mode(mode);
 | 
					    set_color_mode(mode);
 | 
				
			||||||
    colors_[level::trace] = WHITE;
 | 
					    colors_[level::trace] = WHITE;
 | 
				
			||||||
@@ -42,7 +50,7 @@ SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// change the color for the given level
 | 
					// change the color for the given level
 | 
				
			||||||
template<typename ConsoleMutex>
 | 
					template<typename ConsoleMutex>
 | 
				
			||||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, WORD color)
 | 
					void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, std::uint16_t color)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    std::lock_guard<mutex_t> lock(mutex_);
 | 
					    std::lock_guard<mutex_t> lock(mutex_);
 | 
				
			||||||
    colors_[level] = color;
 | 
					    colors_[level] = color;
 | 
				
			||||||
@@ -67,10 +75,10 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
 | 
				
			|||||||
        print_range_(formatted, 0, msg.color_range_start);
 | 
					        print_range_(formatted, 0, msg.color_range_start);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // in color range
 | 
					        // in color range
 | 
				
			||||||
        auto orig_attribs = set_foreground_color_(colors_[msg.level]);
 | 
					        auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[msg.level]));
 | 
				
			||||||
        print_range_(formatted, msg.color_range_start, msg.color_range_end);
 | 
					        print_range_(formatted, msg.color_range_start, msg.color_range_end);
 | 
				
			||||||
        // reset to orig colors
 | 
					        // reset to orig colors
 | 
				
			||||||
        ::SetConsoleTextAttribute(out_handle_, orig_attribs);
 | 
					        ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
 | 
				
			||||||
        print_range_(formatted, msg.color_range_end, formatted.size());
 | 
					        print_range_(formatted, msg.color_range_end, formatted.size());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else // print without colors if color range is invalid (or color is disabled)
 | 
					    else // print without colors if color range is invalid (or color is disabled)
 | 
				
			||||||
@@ -118,16 +126,16 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// set foreground color and return the orig console attributes (for resetting later)
 | 
					// set foreground color and return the orig console attributes (for resetting later)
 | 
				
			||||||
template<typename ConsoleMutex>
 | 
					template<typename ConsoleMutex>
 | 
				
			||||||
WORD SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_foreground_color_(WORD attribs)
 | 
					std::uint16_t SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
 | 
					    CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
 | 
				
			||||||
    ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
 | 
					    ::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info);
 | 
				
			||||||
    WORD back_color = orig_buffer_info.wAttributes;
 | 
					    WORD back_color = orig_buffer_info.wAttributes;
 | 
				
			||||||
    // retrieve the current background color
 | 
					    // retrieve the current background color
 | 
				
			||||||
    back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
 | 
					    back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
 | 
				
			||||||
    // keep the background color unchanged
 | 
					    // keep the background color unchanged
 | 
				
			||||||
    ::SetConsoleTextAttribute(out_handle_, attribs | back_color);
 | 
					    ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(attribs) | back_color);
 | 
				
			||||||
    return orig_buffer_info.wAttributes; // return orig attribs
 | 
					    return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// print a range of formatted message to console
 | 
					// print a range of formatted message to console
 | 
				
			||||||
@@ -135,7 +143,7 @@ template<typename ConsoleMutex>
 | 
				
			|||||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
 | 
					void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    auto size = static_cast<DWORD>(end - start);
 | 
					    auto size = static_cast<DWORD>(end - start);
 | 
				
			||||||
    ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
 | 
					    ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename ConsoleMutex>
 | 
					template<typename ConsoleMutex>
 | 
				
			||||||
@@ -147,7 +155,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    auto size = static_cast<DWORD>(formatted.size());
 | 
					    auto size = static_cast<DWORD>(formatted.size());
 | 
				
			||||||
    DWORD bytes_written = 0;
 | 
					    DWORD bytes_written = 0;
 | 
				
			||||||
    bool ok = ::WriteFile(out_handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
 | 
					    bool ok = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr) != 0;
 | 
				
			||||||
    if (!ok)
 | 
					    if (!ok)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        throw_spdlog_ex("wincolor_sink: ::WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
 | 
					        throw_spdlog_ex("wincolor_sink: ::WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
 | 
				
			||||||
@@ -165,6 +173,5 @@ template<typename ConsoleMutex>
 | 
				
			|||||||
SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
 | 
					SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
 | 
				
			||||||
    : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
 | 
					    : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
 | 
				
			||||||
{}
 | 
					{}
 | 
				
			||||||
 | 
					 | 
				
			||||||
} // namespace sinks
 | 
					} // namespace sinks
 | 
				
			||||||
} // namespace spdlog
 | 
					} // namespace spdlog
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,9 +12,7 @@
 | 
				
			|||||||
#include <mutex>
 | 
					#include <mutex>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <array>
 | 
					#include <array>
 | 
				
			||||||
 | 
					#include <cstdint>
 | 
				
			||||||
#include <spdlog/details/windows_include.h>
 | 
					 | 
				
			||||||
#include <wincon.h>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace spdlog {
 | 
					namespace spdlog {
 | 
				
			||||||
namespace sinks {
 | 
					namespace sinks {
 | 
				
			||||||
@@ -26,21 +24,21 @@ template<typename ConsoleMutex>
 | 
				
			|||||||
class wincolor_sink : public sink
 | 
					class wincolor_sink : public sink
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    const WORD BOLD = FOREGROUND_INTENSITY;
 | 
					    const std::uint16_t BOLD;
 | 
				
			||||||
    const WORD RED = FOREGROUND_RED;
 | 
					    const std::uint16_t RED;
 | 
				
			||||||
    const WORD GREEN = FOREGROUND_GREEN;
 | 
					    const std::uint16_t GREEN;
 | 
				
			||||||
    const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
 | 
					    const std::uint16_t CYAN;
 | 
				
			||||||
    const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
 | 
					    const std::uint16_t WHITE;
 | 
				
			||||||
    const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
 | 
					    const std::uint16_t YELLOW;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    wincolor_sink(HANDLE out_handle, color_mode mode);
 | 
					    wincolor_sink(void *out_handle, color_mode mode);
 | 
				
			||||||
    ~wincolor_sink() override;
 | 
					    ~wincolor_sink() override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    wincolor_sink(const wincolor_sink &other) = delete;
 | 
					    wincolor_sink(const wincolor_sink &other) = delete;
 | 
				
			||||||
    wincolor_sink &operator=(const wincolor_sink &other) = delete;
 | 
					    wincolor_sink &operator=(const wincolor_sink &other) = delete;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // change the color for the given level
 | 
					    // change the color for the given level
 | 
				
			||||||
    void set_color(level::level_enum level, WORD color);
 | 
					    void set_color(level::level_enum level, std::uint16_t color);
 | 
				
			||||||
    void log(const details::log_msg &msg) final override;
 | 
					    void log(const details::log_msg &msg) final override;
 | 
				
			||||||
    void flush() final override;
 | 
					    void flush() final override;
 | 
				
			||||||
    void set_pattern(const std::string &pattern) override final;
 | 
					    void set_pattern(const std::string &pattern) override final;
 | 
				
			||||||
@@ -49,15 +47,15 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
    using mutex_t = typename ConsoleMutex::mutex_t;
 | 
					    using mutex_t = typename ConsoleMutex::mutex_t;
 | 
				
			||||||
    HANDLE out_handle_;
 | 
					    void *out_handle_;
 | 
				
			||||||
    mutex_t &mutex_;
 | 
					    mutex_t &mutex_;
 | 
				
			||||||
    bool in_console_;
 | 
					    bool in_console_;
 | 
				
			||||||
    bool should_do_colors_;
 | 
					    bool should_do_colors_;
 | 
				
			||||||
    std::unique_ptr<spdlog::formatter> formatter_;
 | 
					    std::unique_ptr<spdlog::formatter> formatter_;
 | 
				
			||||||
    std::array<WORD, level::n_levels> colors_;
 | 
					    std::array<std::uint16_t, level::n_levels> colors_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // set foreground color and return the orig console attributes (for resetting later)
 | 
					    // set foreground color and return the orig console attributes (for resetting later)
 | 
				
			||||||
    WORD set_foreground_color_(WORD attribs);
 | 
					    std::uint16_t set_foreground_color_(std::uint16_t attribs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // print a range of formatted message to console
 | 
					    // print a range of formatted message to console
 | 
				
			||||||
    void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
 | 
					    void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
 | 
				
			||||||
@@ -85,7 +83,6 @@ using wincolor_stdout_sink_st = wincolor_stdout_sink<details::console_nullmutex>
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
using wincolor_stderr_sink_mt = wincolor_stderr_sink<details::console_mutex>;
 | 
					using wincolor_stderr_sink_mt = wincolor_stderr_sink<details::console_mutex>;
 | 
				
			||||||
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::console_nullmutex>;
 | 
					using wincolor_stderr_sink_st = wincolor_stderr_sink<details::console_nullmutex>;
 | 
				
			||||||
 | 
					 | 
				
			||||||
} // namespace sinks
 | 
					} // namespace sinks
 | 
				
			||||||
} // namespace spdlog
 | 
					} // namespace spdlog
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
#include "includes.h"
 | 
					#include "includes.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifndef _WIN32
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
					#include <Windows.h>
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
#include <sys/types.h>
 | 
					#include <sys/types.h>
 | 
				
			||||||
#include <dirent.h>
 | 
					#include <dirent.h>
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user