Removed SPDLOG_USE_STD_FORMAT

pull/3271/head
gabime 9 months ago
parent 9fea0e5fa8
commit d7c6cc13a5

@ -77,18 +77,9 @@ option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
# install options # install options
option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF) option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF)
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_USE_STD_FORMAT "Use std::format instead of fmt library." OFF)
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF)
option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)
if(SPDLOG_USE_STD_FORMAT AND CMAKE_CXX_STANDARD LESS 20)
message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT must be used with CMAKE_CXX_STANDARD >= 20")
endif()
if(SPDLOG_USE_STD_FORMAT AND SPDLOG_FMT_EXTERNAL)
message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT and SPDLOG_FMT_EXTERNAL are mutually exclusive")
endif()
# misc tweakme options # misc tweakme options
if(WIN32) if(WIN32)
@ -142,19 +133,16 @@ endif()
message(STATUS "spdlog version: ${SPDLOG_VERSION}") message(STATUS "spdlog version: ${SPDLOG_VERSION}")
message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE}) message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE})
message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS}) message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS})
message(STATUS "spdlog use std format: " ${SPDLOG_USE_STD_FORMAT})
message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL}) message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL})
# --------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------
# Find {fmt} library if not using std::format # Find {fmt} library
# --------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------
if(NOT SPDLOG_USE_STD_FORMAT) if (SPDLOG_FMT_EXTERNAL)
if (SPDLOG_FMT_EXTERNAL)
find_package(fmt REQUIRED) find_package(fmt REQUIRED)
message(STATUS "Using external fmt lib version: ${fmt_VERSION}") message(STATUS "Using external fmt lib version: ${fmt_VERSION}")
else() else()
include(cmake/fmtlib.cmake) include(cmake/fmtlib.cmake)
endif()
endif() endif()
# --------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------
@ -292,9 +280,7 @@ target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog PUBLIC Threads::Threads) target_link_libraries(spdlog PUBLIC Threads::Threads)
if(NOT SPDLOG_USE_STD_FORMAT) target_link_libraries(spdlog PUBLIC fmt::fmt)
target_link_libraries(spdlog PUBLIC fmt::fmt)
endif()
spdlog_enable_warnings(spdlog) spdlog_enable_warnings(spdlog)
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR}) set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d) set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d)

@ -161,8 +161,8 @@ void async_example() {
// {:p} - don't print the position on each line start. // {:p} - don't print the position on each line start.
// {:n} - don't split the output to lines. // {:n} - don't split the output to lines.
#if !defined SPDLOG_USE_STD_FORMAT || defined(_MSC_VER)
#include "spdlog/fmt/bin_to_hex.h" #include "spdlog/fmt/bin_to_hex.h"
void binary_example() { void binary_example() {
std::vector<char> buf; std::vector<char> buf;
for (int i = 0; i < 80; i++) { for (int i = 0; i < 80; i++) {
@ -177,26 +177,14 @@ void binary_example() {
// logger->info("hexdump style: {:a}", spdlog::to_hex(buf)); // logger->info("hexdump style: {:a}", spdlog::to_hex(buf));
// logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20)); // logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20));
} }
#else
void binary_example() {
// not supported with std::format yet
}
#endif
// Log a vector of numbers // Log a vector of numbers
#ifndef SPDLOG_USE_STD_FORMAT #include "fmt/ranges.h"
#include "fmt/ranges.h"
void vector_example() { void vector_example() {
std::vector<int> vec = {1, 2, 3}; std::vector<int> vec = {1, 2, 3};
spdlog::info("Vector example: {}", vec); spdlog::info("Vector example: {}", vec);
} }
#else
void vector_example() {}
#endif
// ! DSPDLOG_USE_STD_FORMAT
// Compile time log levels. // Compile time log levels.
// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE) // define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE)
void trace_example() { void trace_example() {
@ -250,7 +238,7 @@ struct my_type {
: i(i) {} : i(i) {}
}; };
#ifndef SPDLOG_USE_STD_FORMAT // when using fmtlib
template <> template <>
struct fmt::formatter<my_type> : fmt::formatter<std::string> { struct fmt::formatter<my_type> : fmt::formatter<std::string> {
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) { auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
@ -258,15 +246,6 @@ struct fmt::formatter<my_type> : fmt::formatter<std::string> {
} }
}; };
#else // when using std::format
template <>
struct std::formatter<my_type> : std::formatter<std::string> {
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
return format_to(ctx.out(), "[my_type i={}]", my.i);
}
};
#endif
void user_defined_example() { spdlog::info("user defined type: {}", my_type(14)); } void user_defined_example() { spdlog::info("user defined type: {}", my_type(14)); }
// Custom error handler. Will be triggered on log failure. // Custom error handler. Will be triggered on log failure.

@ -21,9 +21,6 @@
#include <version> #include <version>
#endif #endif
#ifdef SPDLOG_USE_STD_FORMAT
#include <format>
#endif
#if defined(SPDLOG_SHARED_LIB) #if defined(SPDLOG_SHARED_LIB)
#if defined(_WIN32) #if defined(_WIN32)
@ -41,16 +38,11 @@
#include "fmt/fmt.h" #include "fmt/fmt.h"
#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_RUNTIME(format_string) fmt::runtime(format_string)
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string) #define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
#if defined(SPDLOG_WCHAR_FILENAMES) #if defined(SPDLOG_WCHAR_FILENAMES)
#include "fmt/xchar.h" #include "fmt/xchar.h"
#endif #endif
#else
#define SPDLOG_FMT_RUNTIME(format_string) format_string
#define SPDLOG_FMT_STRING(format_string) format_string
#endif
#ifndef SPDLOG_FUNCTION #ifndef SPDLOG_FUNCTION
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__) #define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
@ -98,29 +90,14 @@ using err_handler = std::function<void(const std::string &err_msg)>;
using string_view_t = std::basic_string_view<char>; using string_view_t = std::basic_string_view<char>;
using wstring_view_t = std::basic_string_view<wchar_t>; using wstring_view_t = std::basic_string_view<wchar_t>;
#ifdef SPDLOG_USE_STD_FORMAT
namespace fmt_lib = std;
using memory_buf_t = std::string;
using wmemory_buf_t = std::wstring;
template <typename... Args>
#if __cpp_lib_format >= 202207L
using format_string_t = std::format_string<Args...>;
#else
using format_string_t = std::string_view;
#endif
#define SPDLOG_BUF_TO_STRING(x) x
#else // use fmt lib instead of std::format
namespace fmt_lib = fmt; namespace fmt_lib = fmt;
using memory_buf_t = fmt::basic_memory_buffer<char, 250>; using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
template <typename... Args> template <typename... Args>
using format_string_t = fmt::format_string<Args...>; using format_string_t = fmt::format_string<Args...>;
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
#define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
#endif // SPDLOG_USE_STD_FORMAT
#define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
#define SPDLOG_LEVEL_TRACE 0 #define SPDLOG_LEVEL_TRACE 0
#define SPDLOG_LEVEL_DEBUG 1 #define SPDLOG_LEVEL_DEBUG 1
#define SPDLOG_LEVEL_INFO 2 #define SPDLOG_LEVEL_INFO 2
@ -231,27 +208,10 @@ namespace details {
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept { return str; } [[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept { return str; }
#endif #endif
// convert format_string<...> to string_view depending on format lib versions
#if defined(SPDLOG_USE_STD_FORMAT)
#if __cpp_lib_format >= 202207L // std::format and __cpp_lib_format >= 202207L
template <typename T, typename... Args>
[[nodiscard]] constexpr std::basic_string_view<T> to_string_view(std::basic_format_string<T, Args...> fmt) noexcept {
return fmt.get();
}
#else // std::format and __cpp_lib_format < 202207L
template <typename T, typename... Args>
[[nodiscard]] constexpr std::basic_string_view<T> to_string_view(std::basic_format_string<T, Args...> fmt) noexcept {
return fmt;
}
#endif
#else // {fmt} version
template <typename T, typename... Args> template <typename T, typename... Args>
[[nodiscard]] constexpr fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) noexcept { [[nodiscard]] constexpr fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) noexcept {
return fmt; return fmt;
} }
#endif
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

@ -9,11 +9,6 @@
#include "../common.h" #include "../common.h"
#include "../fmt/fmt.h" #include "../fmt/fmt.h"
#ifdef SPDLOG_USE_STD_FORMAT
#include <charconv>
#include <limits>
#endif
// Some fmt helpers to efficiently format and pad ints and strings // Some fmt helpers to efficiently format and pad ints and strings
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -24,27 +19,11 @@ inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) {
dest.append(buf_ptr, buf_ptr + view.size()); dest.append(buf_ptr, buf_ptr + view.size());
} }
#ifdef SPDLOG_USE_STD_FORMAT
template <typename T>
inline void append_int(T n, memory_buf_t &dest) {
// Buffer should be large enough to hold all digits (digits10 + 1) and a sign
constexpr const auto BUF_SIZE = std::numeric_limits<T>::digits10 + 2;
char buf[BUF_SIZE];
auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10);
if (ec == std::errc()) {
dest.append(buf, ptr);
} else {
throw_spdlog_ex("Failed to format int", static_cast<int>(ec));
}
}
#else
template <typename T> template <typename T>
inline void append_int(T n, memory_buf_t &dest) { inline void append_int(T n, memory_buf_t &dest) {
fmt::format_int i(n); fmt::format_int i(n);
dest.append(i.data(), i.data() + i.size()); dest.append(i.data(), i.data() + i.size());
} }
#endif
template <typename T> template <typename T>
constexpr unsigned int count_digits_fallback(T n) { constexpr unsigned int count_digits_fallback(T n) {
@ -66,9 +45,7 @@ constexpr unsigned int count_digits_fallback(T n) {
template <typename T> template <typename T>
inline unsigned int count_digits(T n) { inline unsigned int count_digits(T n) {
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type; using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
#ifdef SPDLOG_USE_STD_FORMAT
return count_digits_fallback(static_cast<count_type>(n));
#else
return static_cast<unsigned int>(fmt:: return static_cast<unsigned int>(fmt::
// fmt 7.0.0 renamed the internal namespace to detail. // fmt 7.0.0 renamed the internal namespace to detail.
// See: https://github.com/fmtlib/fmt/issues/1538 // See: https://github.com/fmtlib/fmt/issues/1538
@ -78,7 +55,6 @@ inline unsigned int count_digits(T n) {
detail detail
#endif #endif
::count_digits(static_cast<count_type>(n))); ::count_digits(static_cast<count_type>(n)));
#endif
} }
inline void pad2(int n, memory_buf_t &dest) { inline void pad2(int n, memory_buf_t &dest) {

@ -88,13 +88,7 @@ inline details::dump_info<It> to_hex(const It range_begin, const It range_end, s
} // namespace spdlog } // namespace spdlog
namespace namespace fmt {
#ifdef SPDLOG_USE_STD_FORMAT
std
#else
fmt
#endif
{
template <typename T> template <typename T>
struct formatter<spdlog::details::dump_info<T>, char> { struct formatter<spdlog::details::dump_info<T>, char> {
@ -142,13 +136,7 @@ struct formatter<spdlog::details::dump_info<T>, char> {
constexpr const char *hex_upper = "0123456789ABCDEF"; constexpr const char *hex_upper = "0123456789ABCDEF";
constexpr const char *hex_lower = "0123456789abcdef"; constexpr const char *hex_lower = "0123456789abcdef";
const char *hex_chars = use_uppercase ? hex_upper : hex_lower; const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION < 60000
auto inserter = ctx.begin();
#else
auto inserter = ctx.out(); auto inserter = ctx.out();
#endif
int size_per_line = static_cast<int>(the_range.size_per_line()); int size_per_line = static_cast<int>(the_range.size_per_line());
auto start_of_line = the_range.get_begin(); auto start_of_line = the_range.get_begin();
for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) { for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) {

@ -5,14 +5,5 @@
#pragma once #pragma once
//
// Include a bundled header-only copy of fmtlib or an external one.
// By default, spdlog include its own copy.
//
#include "../spdlog_config.h" #include "../spdlog_config.h"
#include "fmt/format.h"
#if defined(SPDLOG_USE_STD_FORMAT) // use std::format
#include <format>
#else
#include "fmt/format.h"
#endif

@ -187,14 +187,9 @@ protected:
void log_with_format_(source_loc loc, level lvl, const format_string_t<Args...> &fmt, Args &&...args) { void log_with_format_(source_loc loc, level lvl, const format_string_t<Args...> &fmt, Args &&...args) {
assert(should_log(lvl)); assert(should_log(lvl));
SPDLOG_TRY { SPDLOG_TRY {
#ifdef SPDLOG_USE_STD_FORMAT
auto formatted = std::vformat(fmt, std::make_format_args(args...));
sink_it_(details::log_msg(loc, name_, lvl, formatted));
#else // use {fmt} lib
memory_buf_t buf; memory_buf_t buf;
fmt::vformat_to(std::back_inserter(buf), fmt, fmt::make_format_args(args...)); fmt::vformat_to(std::back_inserter(buf), fmt, fmt::make_format_args(args...));
sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()))); sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())));
#endif
} }
SPDLOG_LOGGER_CATCH(loc) SPDLOG_LOGGER_CATCH(loc)
} }

@ -51,12 +51,7 @@ public:
} // namespace spdlog } // namespace spdlog
// Support for fmt formatting (e.g. "{:012.9}" or just "{}") // Support for fmt formatting (e.g. "{:012.9}" or just "{}")
namespace namespace fmt
#ifdef SPDLOG_USE_STD_FORMAT
std
#else
fmt
#endif
{ {
template <> template <>

Loading…
Cancel
Save