From 44873cda8735eb9c092d0f9fea768fa2b534da28 Mon Sep 17 00:00:00 2001 From: Muhammed Galib Uludag Date: Thu, 4 Aug 2022 14:29:33 +0300 Subject: [PATCH] added compiler and feature check macros from fmt core header these macros used for adopting fmtlibs's color header --- include/spdlog/common-inl.h | 11 +++- include/spdlog/common.h | 126 +++++++++++++++++++++++++++++------- 2 files changed, 113 insertions(+), 24 deletions(-) diff --git a/include/spdlog/common-inl.h b/include/spdlog/common-inl.h index 9fd2bcb5..17e9092a 100644 --- a/include/spdlog/common-inl.h +++ b/include/spdlog/common-inl.h @@ -1,4 +1,4 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Copyright(c) 2015-present, Gabi Melman & spdlog and fmtlib contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) #pragma once @@ -49,6 +49,15 @@ SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG } } // namespace level +SPDLOG_INLINE void assert_fail(const char* file, int line, const char* message) { + // Use unchecked std::fprintf to avoid triggering another assertion when + // writing to stderr fails + std::fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message); + // Chosen instead of std::abort to satisfy Clang in CUDA mode during device + // code pass. + std::terminate(); +} + SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) : msg_(std::move(msg)) {} diff --git a/include/spdlog/common.h b/include/spdlog/common.h index f97fd48c..02807298 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -1,4 +1,4 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Copyright(c) 2015-present, Gabi Melman & and fmtlib contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) #pragma once @@ -44,30 +44,91 @@ #include -#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8 -# define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string) -# define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string) -# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) -# include +#ifndef SPDLOG_USE_STD_FORMAT +# if FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8 +# define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string) +# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +# include +# endif +# else +# define SPDLOG_FMT_RUNTIME(format_string) format_string # endif +#endif + +#ifdef __has_feature +# define SPDLOG_HAS_FEATURE(x) __has_feature(x) #else -# define SPDLOG_FMT_RUNTIME(format_string) format_string -# define SPDLOG_FMT_STRING(format_string) format_string +# define SPDLOG_HAS_FEATURE(x) 0 #endif -// visual studio up to 2013 does not support noexcept nor constexpr -#if defined(_MSC_VER) && (_MSC_VER < 1900) -# define SPDLOG_NOEXCEPT _NOEXCEPT -# define SPDLOG_CONSTEXPR -# define SPDLOG_CONSTEXPR_FUNC +// clang version definition +#if defined(__clang__) && !defined(__ibmxl__) +# define SPDLOG_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) #else +# define SPDLOG_CLANG_VERSION 0 +#endif + +// gcc version definition +#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && \ + !defined(__NVCOMPILER) +# define SPDLOG_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#else +# define SPDLOG_GCC_VERSION 0 +#endif + +// msvc version definition +#ifdef _MSC_VER +# define SPDLOG_MSC_VERSION _MSC_VER +# define SPDLOG_MSC_WARNING(...) __pragma(warning(__VA_ARGS__)) +#else +# define SPDLOG_MSC_VERSION 0 +# define SPDLOG_MSC_WARNING(...) +#endif + +// cpp version definition +#ifdef _MSVC_LANG +# define SPDLOG_CPLUSPLUS _MSVC_LANG +#else +# define SPDLOG_CPLUSPLUS __cplusplus +#endif + +// visual studio up to 2013 and gcc < 5 does not support noexcept nor constexpr +#if (SPDLOG_HAS_FEATURE(cxx_relaxed_constexpr) || \ + SPDLOG_MSC_VERSION > 1900 || \ + SPDLOG_GCC_VERSION >= 500) +# define SPDLOG_USE_CONSTEXPR 1 +#else +# define SPDLOG_USE_CONSTEXPR 0 +#endif + +#if SPDLOG_USE_CONSTEXPR # define SPDLOG_NOEXCEPT noexcept # define SPDLOG_CONSTEXPR constexpr -# if __cplusplus >= 201402L +# if SPDLOG_CPLUSPLUS >= 201402L # define SPDLOG_CONSTEXPR_FUNC constexpr # else # define SPDLOG_CONSTEXPR_FUNC # endif +#else +# define SPDLOG_NOEXCEPT _NOEXCEPT +# define SPDLOG_CONSTEXPR +# define SPDLOG_CONSTEXPR_FUNC +#endif + +// Check if constexpr std::char_traits<>::compare,length is supported. +#if defined(__GLIBCXX__) +# if SPDLOG_CPLUSPLUS >= 201703L && defined(_GLIBCXX_RELEASE) && \ + _GLIBCXX_RELEASE >= 7 // GCC 7+ libstdc++ has _GLIBCXX_RELEASE. +# define SPDLOG_CONSTEXPR_CHAR_TRAITS constexpr +# endif +#elif defined(_LIBCPP_VERSION) && SPDLOG_CPLUSPLUS >= 201703L && \ + _LIBCPP_VERSION >= 4000 +# define FMT_CONSTEXPR_CHAR_TRAITS constexpr +#elif SPDLOG_MSC_VERSION >= 1914 && SPDLOG_CPLUSPLUS >= 201703L +# define SPDLOG_CONSTEXPR_CHAR_TRAITS constexpr +#endif +#ifndef SPDLOG_CONSTEXPR_CHAR_TRAITS +# define SPDLOG_CONSTEXPR_CHAR_TRAITS #endif #if defined(__GNUC__) || defined(__clang__) @@ -89,6 +150,27 @@ # define SPDLOG_FUNCTION static_cast(__FUNCTION__) #endif +// Suppress "unused variable" warnings with the method described in +// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/. +// (void)var does not work on many Intel compilers. +template SPDLOG_CONSTEXPR_FUNC void ignore_unused(const T&...) {} + +SPDLOG_API void assert_fail(const char* file, int line, + const char* message); + +#ifndef SPDLOG_ASSERT +# ifdef NDEBUG +// SPDLOG_ASSERT is not empty to avoid -Werror=empty-body. +# define SPDLOG_ASSERT(condition, message) \ + ignore_unused((condition), (message)) +# else +# define SPDLOG_ASSERT(condition, message) \ + ((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \ + ? (void)0 \ + : ::spdlog::details::assert_fail(__FILE__, __LINE__, (message))) +# endif +#endif + #ifdef SPDLOG_NO_EXCEPTIONS # define SPDLOG_TRY # define SPDLOG_THROW(ex) \ @@ -147,7 +229,7 @@ using wmemory_buf_t = std::wstring; template using wformat_string_t = std::wstring_view; # endif -# define SPDLOG_BUF_TO_STRING(x) x + #else // use fmt lib instead of std::format namespace fmt_lib = fmt; @@ -175,7 +257,6 @@ using wmemory_buf_t = fmt::basic_memory_buffer; template using wformat_string_t = fmt::wformat_string; # endif -# define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x) #endif #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT @@ -308,17 +389,16 @@ struct source_loc struct file_event_handlers { - file_event_handlers() - : before_open(nullptr) - , after_open(nullptr) - , before_close(nullptr) - , after_close(nullptr) - {} - std::function before_open; std::function after_open; std::function before_close; std::function after_close; + file_event_handlers() + : before_open{nullptr} + , after_open{nullptr} + , before_close{nullptr} + , after_close{nullptr} + {} }; namespace details {