mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
V2.x no std format (#3271)
* Removed SPDLOG_USE_STD_FORMAT * Removed SPDLOG_USE_STD_FORMAT * clang-format * Fix windows.yml ci * Fix ci
This commit is contained in:
@@ -21,10 +21,6 @@
|
||||
#include <version>
|
||||
#endif
|
||||
|
||||
#ifdef SPDLOG_USE_STD_FORMAT
|
||||
#include <format>
|
||||
#endif
|
||||
|
||||
#if defined(SPDLOG_SHARED_LIB)
|
||||
#if defined(_WIN32)
|
||||
#ifdef spdlog_EXPORTS
|
||||
@@ -41,15 +37,10 @@
|
||||
|
||||
#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_STRING(format_string) FMT_STRING(format_string)
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES)
|
||||
#include "fmt/xchar.h"
|
||||
#endif
|
||||
#else
|
||||
#define SPDLOG_FMT_RUNTIME(format_string) format_string
|
||||
#define SPDLOG_FMT_STRING(format_string) format_string
|
||||
#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)
|
||||
#include "fmt/xchar.h"
|
||||
#endif
|
||||
|
||||
#ifndef SPDLOG_FUNCTION
|
||||
@@ -98,29 +89,14 @@ using err_handler = std::function<void(const std::string &err_msg)>;
|
||||
using string_view_t = std::basic_string_view<char>;
|
||||
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;
|
||||
|
||||
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
|
||||
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
|
||||
|
||||
template <typename... 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_DEBUG 1
|
||||
#define SPDLOG_LEVEL_INFO 2
|
||||
@@ -231,27 +207,10 @@ namespace details {
|
||||
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept { return str; }
|
||||
#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>
|
||||
[[nodiscard]] constexpr fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) noexcept {
|
||||
return fmt;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
@@ -9,11 +9,6 @@
|
||||
#include "../common.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
|
||||
namespace spdlog {
|
||||
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());
|
||||
}
|
||||
|
||||
#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>
|
||||
inline void append_int(T n, memory_buf_t &dest) {
|
||||
fmt::format_int i(n);
|
||||
dest.append(i.data(), i.data() + i.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
constexpr unsigned int count_digits_fallback(T n) {
|
||||
@@ -66,19 +45,16 @@ constexpr unsigned int count_digits_fallback(T n) {
|
||||
template <typename T>
|
||||
inline unsigned int count_digits(T n) {
|
||||
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::
|
||||
// fmt 7.0.0 renamed the internal namespace to detail.
|
||||
// See: https://github.com/fmtlib/fmt/issues/1538
|
||||
#if FMT_VERSION < 70000
|
||||
// fmt 7.0.0 renamed the internal namespace to detail.
|
||||
// See: https://github.com/fmtlib/fmt/issues/1538
|
||||
#if FMT_VERSION < 70000
|
||||
internal
|
||||
#else
|
||||
#else
|
||||
detail
|
||||
#endif
|
||||
::count_digits(static_cast<count_type>(n)));
|
||||
#endif
|
||||
::count_digits(static_cast<count_type>(n)));
|
||||
}
|
||||
|
||||
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
|
||||
#ifdef SPDLOG_USE_STD_FORMAT
|
||||
std
|
||||
#else
|
||||
fmt
|
||||
#endif
|
||||
{
|
||||
namespace fmt {
|
||||
|
||||
template <typename T>
|
||||
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_lower = "0123456789abcdef";
|
||||
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();
|
||||
#endif
|
||||
|
||||
int size_per_line = static_cast<int>(the_range.size_per_line());
|
||||
auto start_of_line = the_range.get_begin();
|
||||
for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) {
|
||||
@@ -215,4 +203,4 @@ struct formatter<spdlog::details::dump_info<T>, char> {
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
} // namespace fmt
|
||||
|
@@ -5,14 +5,5 @@
|
||||
|
||||
#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"
|
||||
|
||||
#if defined(SPDLOG_USE_STD_FORMAT) // use std::format
|
||||
#include <format>
|
||||
#else
|
||||
#include "fmt/format.h"
|
||||
#endif
|
||||
#include "fmt/format.h"
|
||||
|
@@ -187,14 +187,9 @@ protected:
|
||||
void log_with_format_(source_loc loc, level lvl, const format_string_t<Args...> &fmt, Args &&...args) {
|
||||
assert(should_log(lvl));
|
||||
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;
|
||||
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())));
|
||||
#endif
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH(loc)
|
||||
}
|
||||
|
@@ -51,13 +51,7 @@ public:
|
||||
} // namespace spdlog
|
||||
|
||||
// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
|
||||
namespace
|
||||
#ifdef SPDLOG_USE_STD_FORMAT
|
||||
std
|
||||
#else
|
||||
fmt
|
||||
#endif
|
||||
{
|
||||
namespace fmt {
|
||||
|
||||
template <>
|
||||
struct formatter<spdlog::stopwatch> : formatter<double> {
|
||||
@@ -66,4 +60,4 @@ struct formatter<spdlog::stopwatch> : formatter<double> {
|
||||
return formatter<double>::format(sw.elapsed().count(), ctx);
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
} // namespace fmt
|
||||
|
Reference in New Issue
Block a user