From 99e519cf0fbaaa44a441e477c287911b893f0d6a Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 18 Jun 2019 12:32:51 +0300 Subject: [PATCH] Simplify color sinks --- cygwin/CPackConfig.cmake | 82 +++++++++++++++++++++ cygwin/CPackSourceConfig.cmake | 89 +++++++++++++++++++++++ cygwin/spdlogConfig.cmake | 15 ++++ cygwin/spdlogConfigVersion.cmake | 46 ++++++++++++ include/spdlog/common.h | 14 ++++ include/spdlog/details/console_globals.h | 40 ---------- include/spdlog/sinks/ansicolor_sink-inl.h | 63 ++++++++++------ include/spdlog/sinks/ansicolor_sink.h | 30 ++++++-- include/spdlog/sinks/wincolor_sink-inl.h | 56 ++++++++------ include/spdlog/sinks/wincolor_sink.h | 26 +++++-- src/spdlog.cpp | 16 ++-- 11 files changed, 371 insertions(+), 106 deletions(-) create mode 100644 cygwin/CPackConfig.cmake create mode 100644 cygwin/CPackSourceConfig.cmake create mode 100644 cygwin/spdlogConfig.cmake create mode 100644 cygwin/spdlogConfigVersion.cmake diff --git a/cygwin/CPackConfig.cmake b/cygwin/CPackConfig.cmake new file mode 100644 index 00000000..13833133 --- /dev/null +++ b/cygwin/CPackConfig.cmake @@ -0,0 +1,82 @@ +# This file will be configured to contain variables for CPack. These variables +# should be set in the CMake list file of the project before CPack module is +# included. The list of available CPACK_xxx variables and their associated +# documentation may be obtained using +# cpack --help-variable-list +# +# Some variables are common to all generators (e.g. CPACK_PACKAGE_NAME) +# and some are specific to a generator +# (e.g. CPACK_NSIS_EXTRA_INSTALL_COMMANDS). The generator specific variables +# usually begin with CPACK__xxxx. + + +SET(CPACK_BINARY_7Z "") +SET(CPACK_BINARY_BUNDLE "") +SET(CPACK_BINARY_CYGWIN "") +SET(CPACK_BINARY_DEB "") +SET(CPACK_BINARY_DRAGNDROP "") +SET(CPACK_BINARY_IFW "") +SET(CPACK_BINARY_NSIS "") +SET(CPACK_BINARY_OSXX11 "") +SET(CPACK_BINARY_PACKAGEMAKER "") +SET(CPACK_BINARY_RPM "") +SET(CPACK_BINARY_STGZ "") +SET(CPACK_BINARY_TBZ2 "") +SET(CPACK_BINARY_TGZ "") +SET(CPACK_BINARY_TXZ "") +SET(CPACK_BINARY_TZ "") +SET(CPACK_BINARY_WIX "") +SET(CPACK_BINARY_ZIP "") +SET(CPACK_CMAKE_GENERATOR "Unix Makefiles") +SET(CPACK_COMPONENT_UNSPECIFIED_HIDDEN "TRUE") +SET(CPACK_COMPONENT_UNSPECIFIED_REQUIRED "TRUE") +SET(CPACK_GENERATOR "TGZ;ZIP") +SET(CPACK_INCLUDE_TOPLEVEL_DIRECTORY "0") +SET(CPACK_INSTALL_CMAKE_PROJECTS "/cygdrive/e/devel/spdlog/cygwin;spdlog;ALL;.") +SET(CPACK_INSTALL_PREFIX "/usr/local") +SET(CPACK_MODULE_PATH "") +SET(CPACK_NSIS_DISPLAY_NAME "spdlog 1.4.0") +SET(CPACK_NSIS_INSTALLER_ICON_CODE "") +SET(CPACK_NSIS_INSTALLER_MUI_ICON_CODE "") +SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES") +SET(CPACK_NSIS_PACKAGE_NAME "spdlog 1.4.0") +SET(CPACK_OUTPUT_CONFIG_FILE "/cygdrive/e/devel/spdlog/cygwin/CPackConfig.cmake") +SET(CPACK_PACKAGE_CONTACT "Gabi Melman ") +SET(CPACK_PACKAGE_DEFAULT_LOCATION "/") +SET(CPACK_PACKAGE_DESCRIPTION_FILE "/usr/share/cmake-3.6.2/Templates/CPack.GenericDescription.txt") +SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "spdlog built using CMake") +SET(CPACK_PACKAGE_FILE_NAME "spdlog-1.4.0-CYGWIN") +SET(CPACK_PACKAGE_INSTALL_DIRECTORY "spdlog 1.4.0") +SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "spdlog 1.4.0") +SET(CPACK_PACKAGE_NAME "spdlog") +SET(CPACK_PACKAGE_RELOCATABLE "true") +SET(CPACK_PACKAGE_VENDOR "Gabi Melman") +SET(CPACK_PACKAGE_VERSION "1.4.0") +SET(CPACK_PACKAGE_VERSION_MAJOR "1") +SET(CPACK_PACKAGE_VERSION_MINOR "4") +SET(CPACK_PACKAGE_VERSION_PATCH "0") +SET(CPACK_PROJECT_URL "https://github.com/gabime/spdlog") +SET(CPACK_RESOURCE_FILE_LICENSE "/usr/share/cmake-3.6.2/Templates/CPack.GenericLicense.txt") +SET(CPACK_RESOURCE_FILE_README "/usr/share/cmake-3.6.2/Templates/CPack.GenericDescription.txt") +SET(CPACK_RESOURCE_FILE_WELCOME "/usr/share/cmake-3.6.2/Templates/CPack.GenericWelcome.txt") +SET(CPACK_SET_DESTDIR "OFF") +SET(CPACK_SOURCE_7Z "") +SET(CPACK_SOURCE_CYGWIN "ON") +SET(CPACK_SOURCE_GENERATOR "CygwinSource") +SET(CPACK_SOURCE_OUTPUT_CONFIG_FILE "/cygdrive/e/devel/spdlog/cygwin/CPackSourceConfig.cmake") +SET(CPACK_SOURCE_TBZ2 "") +SET(CPACK_SOURCE_TGZ "") +SET(CPACK_SOURCE_TXZ "") +SET(CPACK_SOURCE_TZ "") +SET(CPACK_SOURCE_ZIP "") +SET(CPACK_SYSTEM_NAME "CYGWIN") +SET(CPACK_TOPLEVEL_TAG "CYGWIN") +SET(CPACK_WIX_SIZEOF_VOID_P "8") + +if(NOT CPACK_PROPERTIES_FILE) + set(CPACK_PROPERTIES_FILE "/cygdrive/e/devel/spdlog/cygwin/CPackProperties.cmake") +endif() + +if(EXISTS ${CPACK_PROPERTIES_FILE}) + include(${CPACK_PROPERTIES_FILE}) +endif() diff --git a/cygwin/CPackSourceConfig.cmake b/cygwin/CPackSourceConfig.cmake new file mode 100644 index 00000000..6b869677 --- /dev/null +++ b/cygwin/CPackSourceConfig.cmake @@ -0,0 +1,89 @@ +# This file will be configured to contain variables for CPack. These variables +# should be set in the CMake list file of the project before CPack module is +# included. The list of available CPACK_xxx variables and their associated +# documentation may be obtained using +# cpack --help-variable-list +# +# Some variables are common to all generators (e.g. CPACK_PACKAGE_NAME) +# and some are specific to a generator +# (e.g. CPACK_NSIS_EXTRA_INSTALL_COMMANDS). The generator specific variables +# usually begin with CPACK__xxxx. + + +SET(CPACK_BINARY_7Z "") +SET(CPACK_BINARY_BUNDLE "") +SET(CPACK_BINARY_CYGWIN "") +SET(CPACK_BINARY_DEB "") +SET(CPACK_BINARY_DRAGNDROP "") +SET(CPACK_BINARY_IFW "") +SET(CPACK_BINARY_NSIS "") +SET(CPACK_BINARY_OSXX11 "") +SET(CPACK_BINARY_PACKAGEMAKER "") +SET(CPACK_BINARY_RPM "") +SET(CPACK_BINARY_STGZ "") +SET(CPACK_BINARY_TBZ2 "") +SET(CPACK_BINARY_TGZ "") +SET(CPACK_BINARY_TXZ "") +SET(CPACK_BINARY_TZ "") +SET(CPACK_BINARY_WIX "") +SET(CPACK_BINARY_ZIP "") +SET(CPACK_CMAKE_GENERATOR "Unix Makefiles") +SET(CPACK_COMPONENT_UNSPECIFIED_HIDDEN "TRUE") +SET(CPACK_COMPONENT_UNSPECIFIED_REQUIRED "TRUE") +SET(CPACK_GENERATOR "CygwinSource") +SET(CPACK_IGNORE_FILES "/CVS/;/\\.svn/;/\\.bzr/;/\\.hg/;/\\.git/;\\.swp\$;\\.#;/#") +SET(CPACK_INCLUDE_TOPLEVEL_DIRECTORY "0") +SET(CPACK_INSTALLED_DIRECTORIES "/cygdrive/e/devel/spdlog;/") +SET(CPACK_INSTALL_CMAKE_PROJECTS "") +SET(CPACK_INSTALL_PREFIX "/usr/local") +SET(CPACK_MODULE_PATH "") +SET(CPACK_NSIS_DISPLAY_NAME "spdlog 1.4.0") +SET(CPACK_NSIS_INSTALLER_ICON_CODE "") +SET(CPACK_NSIS_INSTALLER_MUI_ICON_CODE "") +SET(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES") +SET(CPACK_NSIS_PACKAGE_NAME "spdlog 1.4.0") +SET(CPACK_OUTPUT_CONFIG_FILE "/cygdrive/e/devel/spdlog/cygwin/CPackConfig.cmake") +SET(CPACK_PACKAGE_CONTACT "Gabi Melman ") +SET(CPACK_PACKAGE_DEFAULT_LOCATION "/") +SET(CPACK_PACKAGE_DESCRIPTION_FILE "/usr/share/cmake-3.6.2/Templates/CPack.GenericDescription.txt") +SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "spdlog built using CMake") +SET(CPACK_PACKAGE_FILE_NAME "spdlog-1.4.0-Source") +SET(CPACK_PACKAGE_INSTALL_DIRECTORY "spdlog 1.4.0") +SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "spdlog 1.4.0") +SET(CPACK_PACKAGE_NAME "spdlog") +SET(CPACK_PACKAGE_RELOCATABLE "true") +SET(CPACK_PACKAGE_VENDOR "Gabi Melman") +SET(CPACK_PACKAGE_VERSION "1.4.0") +SET(CPACK_PACKAGE_VERSION_MAJOR "1") +SET(CPACK_PACKAGE_VERSION_MINOR "4") +SET(CPACK_PACKAGE_VERSION_PATCH "0") +SET(CPACK_PROJECT_URL "https://github.com/gabime/spdlog") +SET(CPACK_RESOURCE_FILE_LICENSE "/usr/share/cmake-3.6.2/Templates/CPack.GenericLicense.txt") +SET(CPACK_RESOURCE_FILE_README "/usr/share/cmake-3.6.2/Templates/CPack.GenericDescription.txt") +SET(CPACK_RESOURCE_FILE_WELCOME "/usr/share/cmake-3.6.2/Templates/CPack.GenericWelcome.txt") +SET(CPACK_SET_DESTDIR "OFF") +SET(CPACK_SOURCE_7Z "") +SET(CPACK_SOURCE_CYGWIN "ON") +SET(CPACK_SOURCE_GENERATOR "CygwinSource") +SET(CPACK_SOURCE_IGNORE_FILES "/CVS/;/\\.svn/;/\\.bzr/;/\\.hg/;/\\.git/;\\.swp\$;\\.#;/#") +SET(CPACK_SOURCE_INSTALLED_DIRECTORIES "/cygdrive/e/devel/spdlog;/") +SET(CPACK_SOURCE_OUTPUT_CONFIG_FILE "/cygdrive/e/devel/spdlog/cygwin/CPackSourceConfig.cmake") +SET(CPACK_SOURCE_PACKAGE_FILE_NAME "spdlog-1.4.0-Source") +SET(CPACK_SOURCE_TBZ2 "") +SET(CPACK_SOURCE_TGZ "") +SET(CPACK_SOURCE_TOPLEVEL_TAG "CYGWIN-Source") +SET(CPACK_SOURCE_TXZ "") +SET(CPACK_SOURCE_TZ "") +SET(CPACK_SOURCE_ZIP "") +SET(CPACK_STRIP_FILES "") +SET(CPACK_SYSTEM_NAME "CYGWIN") +SET(CPACK_TOPLEVEL_TAG "CYGWIN-Source") +SET(CPACK_WIX_SIZEOF_VOID_P "8") + +if(NOT CPACK_PROPERTIES_FILE) + set(CPACK_PROPERTIES_FILE "/cygdrive/e/devel/spdlog/cygwin/CPackProperties.cmake") +endif() + +if(EXISTS ${CPACK_PROPERTIES_FILE}) + include(${CPACK_PROPERTIES_FILE}) +endif() diff --git a/cygwin/spdlogConfig.cmake b/cygwin/spdlogConfig.cmake new file mode 100644 index 00000000..6cdd5446 --- /dev/null +++ b/cygwin/spdlogConfig.cmake @@ -0,0 +1,15 @@ +# Copyright(c) 2019 spdlog authors +# Distributed under the MIT License (http://opensource.org/licenses/MIT) + +find_package(Threads REQUIRED) + +set(SPDLOG_FMT_EXTERNAL OFF) +set(config_targets_file spdlogConfigTargets.cmake) + +if(SPDLOG_FMT_EXTERNAL) + include(CMakeFindDependencyMacro) + find_dependency(fmt CONFIG) +endif() + + +include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}") diff --git a/cygwin/spdlogConfigVersion.cmake b/cygwin/spdlogConfigVersion.cmake new file mode 100644 index 00000000..66f87f7e --- /dev/null +++ b/cygwin/spdlogConfigVersion.cmake @@ -0,0 +1,46 @@ +# This is a basic version file for the Config-mode of find_package(). +# It is used by write_basic_package_version_file() as input file for configure_file() +# to create a version-file which can be installed along a config.cmake file. +# +# The created file sets PACKAGE_VERSION_EXACT if the current version string and +# the requested version string are exactly the same and it sets +# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version, +# but only if the requested major version is the same as the current one. +# The variable CVF_VERSION must be set before calling configure_file(). + + +set(PACKAGE_VERSION "1.4.0") + +if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) + set(PACKAGE_VERSION_COMPATIBLE FALSE) +else() + + if("1.4.0" MATCHES "^([0-9]+)\\.") + set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}") + else() + set(CVF_VERSION_MAJOR "1.4.0") + endif() + + if(PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR) + set(PACKAGE_VERSION_COMPATIBLE TRUE) + else() + set(PACKAGE_VERSION_COMPATIBLE FALSE) + endif() + + if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) + set(PACKAGE_VERSION_EXACT TRUE) + endif() +endif() + + +# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it: +if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "") + return() +endif() + +# check that the installed version has the same 32/64bit-ness as the one which is currently searching: +if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8") + math(EXPR installedBits "8 * 8") + set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)") + set(PACKAGE_VERSION_UNSUITABLE TRUE) +endif() diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 82f1629b..c18e5c66 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -14,11 +14,25 @@ #include #include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX // prevent windows redefining min/max +#endif + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include +#endif //_WIN32 + + #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) #include #include #endif + #ifdef SPDLOG_COMPILED_LIB #undef SPDLOG_HEADER_ONLY #define SPDLOG_INLINE diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h index 9fe8adc8..dcc82a62 100644 --- a/include/spdlog/details/console_globals.h +++ b/include/spdlog/details/console_globals.h @@ -4,51 +4,11 @@ #pragma once #include "spdlog/details/null_mutex.h" -#include #include -#ifdef _WIN32 - -#ifndef NOMINMAX -#define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include -#endif namespace spdlog { namespace details { -struct console_stdout -{ - static std::FILE *stream() - { - return stdout; - } -#ifdef _WIN32 - static HANDLE handle() - { - return ::GetStdHandle(STD_OUTPUT_HANDLE); - } -#endif -}; - -struct console_stderr -{ - static std::FILE *stream() - { - return stderr; - } -#ifdef _WIN32 - static HANDLE handle() - { - return ::GetStdHandle(STD_ERROR_HANDLE); - } -#endif -}; struct console_mutex { diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 2acb0a2d..dbb0c636 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -9,9 +9,12 @@ #include "spdlog/details/os.h" -template -SPDLOG_INLINE spdlog::sinks::ansicolor_sink::ansicolor_sink(color_mode mode) - : target_file_(TargetStream::stream()) +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) + : target_file_(target_file) , mutex_(ConsoleMutex::mutex()) { @@ -25,16 +28,15 @@ SPDLOG_INLINE spdlog::sinks::ansicolor_sink::ansicol colors_[level::off] = reset; } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::set_color( - level::level_enum color_level, const std::string &color) +template +SPDLOG_INLINE void ansicolor_sink::set_color(level::level_enum color_level, const std::string &color) { std::lock_guard lock(mutex_); colors_[color_level] = color; } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::log(const details::log_msg &msg) +template +SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg) { // Wrap the originally formatted message in color codes. // If color is not supported in the terminal, log as is instead. @@ -60,36 +62,35 @@ SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::lo fflush(target_file_); } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::flush() +template +SPDLOG_INLINE void ansicolor_sink::flush() { std::lock_guard lock(mutex_); fflush(target_file_); } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::set_pattern(const std::string &pattern) +template +SPDLOG_INLINE void ansicolor_sink::set_pattern(const std::string &pattern) { std::lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::set_formatter( - std::unique_ptr sink_formatter) +template +SPDLOG_INLINE void ansicolor_sink::set_formatter(std::unique_ptr sink_formatter) { std::lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } -template -SPDLOG_INLINE bool spdlog::sinks::ansicolor_sink::should_color() +template +SPDLOG_INLINE bool ansicolor_sink::should_color() { return should_do_colors_; } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::set_color_mode(color_mode mode) +template +SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) { switch (mode) { @@ -105,15 +106,29 @@ SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::se } } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::print_ccode_(const std::string &color_code) +template +SPDLOG_INLINE void ansicolor_sink::print_ccode_(const std::string &color_code) { fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); } -template -SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::print_range_( - const fmt::memory_buffer &formatted, size_t start, size_t end) +template +SPDLOG_INLINE void ansicolor_sink::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) { fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); } + +// ansicolor_stdout_sink +template +SPDLOG_INLINE ansicolor_stdout_sink::ansicolor_stdout_sink(color_mode mode) + : ansicolor_sink(stdout, mode) +{} + +// ansicolor_stderr_sink +template +SPDLOG_INLINE ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) + : ansicolor_sink(stderr, mode) +{} + +} // namespace sinks +} // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index a25fecb8..283df48e 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -20,12 +20,12 @@ namespace sinks { * of the message. * If no color terminal detected, omit the escape codes. */ -template -class ansicolor_sink final : public sink +template +class ansicolor_sink : public sink { public: using mutex_t = typename ConsoleMutex::mutex_t; - explicit ansicolor_sink(color_mode mode = color_mode::automatic); + ansicolor_sink(FILE* target_file, color_mode mode); ~ansicolor_sink() override = default; ansicolor_sink(const ansicolor_sink &other) = delete; @@ -77,11 +77,27 @@ private: void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); }; -using ansicolor_stdout_sink_mt = ansicolor_sink; -using ansicolor_stdout_sink_st = ansicolor_sink; -using ansicolor_stderr_sink_mt = ansicolor_sink; -using ansicolor_stderr_sink_st = ansicolor_sink; +template +class ansicolor_stdout_sink : public ansicolor_sink +{ +public: + explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); +}; + +template +class ansicolor_stderr_sink : public ansicolor_sink +{ +public: + explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); +}; + + +using ansicolor_stdout_sink_mt = ansicolor_stdout_sink; +using ansicolor_stdout_sink_st = ansicolor_stdout_sink; + +using ansicolor_stderr_sink_mt = ansicolor_stderr_sink; +using ansicolor_stderr_sink_st = ansicolor_stderr_sink; } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 1611e2c2..6ee2c724 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -12,9 +12,9 @@ namespace spdlog { namespace sinks { -template -SPDLOG_INLINE wincolor_sink::wincolor_sink(color_mode mode) - : out_handle_(TargetStream::handle()) +template +SPDLOG_INLINE wincolor_sink::wincolor_sink(HANDLE out_handle, color_mode mode) + : out_handle_(out_handle) , mutex_(ConsoleMutex::mutex()) { set_color_mode(mode); @@ -27,22 +27,22 @@ SPDLOG_INLINE wincolor_sink::wincolor_sink(color_mod colors_[level::off] = 0; } -template -SPDLOG_INLINE wincolor_sink::~wincolor_sink() +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) +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) +template +void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); fmt::memory_buffer formatted; @@ -66,28 +66,28 @@ void SPDLOG_INLINE wincolor_sink::log(const details: } } -template -void SPDLOG_INLINE wincolor_sink::flush() +template +void SPDLOG_INLINE wincolor_sink::flush() { // windows console always flushed? } -template -void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &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) +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) +template +void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) { switch (mode) { @@ -104,8 +104,8 @@ void SPDLOG_INLINE wincolor_sink::set_color_mode(col } // set color and return the orig console attributes (for resetting later) -template -WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) +template +WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) { CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); @@ -118,12 +118,26 @@ WORD SPDLOG_INLINE wincolor_sink::set_console_attrib } // 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) +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); } + +// 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) +{} + + } // 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 d8b2b715..82a551e2 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -20,7 +20,7 @@ namespace sinks { * Windows color console sink. Uses WriteConsoleA to write to the console with * colors */ -template +template class wincolor_sink : public sink { public: @@ -31,7 +31,7 @@ public: const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; - wincolor_sink(color_mode mode = color_mode::automatic); + wincolor_sink(HANDLE out_handle, color_mode mode); ~wincolor_sink() override; wincolor_sink(const wincolor_sink &other) = delete; @@ -58,11 +58,25 @@ private: void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); }; -using wincolor_stdout_sink_mt = wincolor_sink; -using wincolor_stdout_sink_st = wincolor_sink; +template +class wincolor_stdout_sink : public wincolor_sink +{ +public: + explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); +}; -using wincolor_stderr_sink_mt = wincolor_sink; -using wincolor_stderr_sink_st = wincolor_sink; +template +class wincolor_stderr_sink : public wincolor_sink +{ +public: + explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); +}; + +using wincolor_stdout_sink_mt = wincolor_stdout_sink; +using wincolor_stdout_sink_st = wincolor_stdout_sink; + +using wincolor_stderr_sink_mt = wincolor_stderr_sink; +using wincolor_stderr_sink_st = wincolor_stderr_sink; } // namespace sinks } // namespace spdlog diff --git a/src/spdlog.cpp b/src/spdlog.cpp index d888dc4f..e5194dab 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -45,16 +45,16 @@ template class spdlog::details::mpmc_blocking_queue; #ifdef _WIN32 #include "spdlog/sinks/wincolor_sink-inl.h" -template class spdlog::sinks::wincolor_sink; -template class spdlog::sinks::wincolor_sink; -template class spdlog::sinks::wincolor_sink; -template class spdlog::sinks::wincolor_sink; +template class spdlog::sinks::wincolor_stdout_sink; +template class spdlog::sinks::wincolor_stdout_sink; +template class spdlog::sinks::wincolor_stderr_sink; +template class spdlog::sinks::wincolor_stderr_sink; #else #include "spdlog/sinks/ansicolor_sink-inl.h" -template class spdlog::sinks::ansicolor_sink; -template class spdlog::sinks::ansicolor_sink; -template class spdlog::sinks::ansicolor_sink; -template class spdlog::sinks::ansicolor_sink; +template class spdlog::sinks::ansicolor_stdout_sink; +template class spdlog::sinks::ansicolor_stdout_sink; +template class spdlog::sinks::ansicolor_stderr_sink; +template class spdlog::sinks::ansicolor_stderr_sink; #endif #include "spdlog/sinks/stdout_color_sinks-inl.h"