Upgraded spdlog to v1.11.0

This commit is contained in:
Bartek Kryza
2023-06-03 18:00:54 +02:00
parent a78839eecf
commit 12f129a6fe
72 changed files with 6797 additions and 4117 deletions

View File

@@ -35,7 +35,7 @@ template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
struct async_factory_impl struct async_factory_impl
{ {
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
{ {
auto &registry_inst = details::registry::instance(); auto &registry_inst = details::registry::instance();
@@ -61,28 +61,34 @@ using async_factory = async_factory_impl<async_overflow_policy::block>;
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>; using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args) inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
{ {
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...); return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
} }
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args) inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
{ {
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...); return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
} }
// set global thread pool. // set global thread pool.
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) inline void init_thread_pool(
size_t q_size, size_t thread_count, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
{ {
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start); auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
details::registry::instance().set_tp(std::move(tp)); details::registry::instance().set_tp(std::move(tp));
} }
// set global thread pool. inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
{
init_thread_pool(q_size, thread_count, on_thread_start, [] {});
}
inline void init_thread_pool(size_t q_size, size_t thread_count) inline void init_thread_pool(size_t q_size, size_t thread_count)
{ {
init_thread_pool(q_size, thread_count, [] {}); init_thread_pool(
q_size, thread_count, [] {}, [] {});
} }
// get the global thread pool. // get the global thread pool.

View File

@@ -62,7 +62,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg
{ {
sink->log(msg); sink->log(msg);
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(msg.source)
} }
} }
@@ -80,7 +80,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
{ {
sink->flush(); sink->flush();
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(source_loc())
} }
} }

View File

@@ -34,7 +34,7 @@ SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG
{ {
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
if (it != std::end(level_string_views)) if (it != std::end(level_string_views))
return static_cast<level::level_enum>(it - std::begin(level_string_views)); return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it));
// check also for "warn" and "err" before giving up.. // check also for "warn" and "err" before giving up..
if (name == "warn") if (name == "warn")
@@ -55,9 +55,13 @@ SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg)
SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno)
{ {
#ifdef SPDLOG_USE_STD_FORMAT
msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what();
#else
memory_buf_t outbuf; memory_buf_t outbuf;
fmt::format_system_error(outbuf, last_errno, msg.c_str()); fmt::format_system_error(outbuf, last_errno, msg.c_str());
msg_ = fmt::to_string(outbuf); msg_ = fmt::to_string(outbuf);
#endif
} }
SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT

View File

@@ -14,16 +14,25 @@
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <functional> #include <functional>
#include <cstdio>
#ifdef SPDLOG_USE_STD_FORMAT
# include <string_view>
#endif
#ifdef SPDLOG_COMPILED_LIB #ifdef SPDLOG_COMPILED_LIB
# undef SPDLOG_HEADER_ONLY # undef SPDLOG_HEADER_ONLY
# if defined(_WIN32) && defined(SPDLOG_SHARED_LIB) # if defined(SPDLOG_SHARED_LIB)
# ifdef spdlog_EXPORTS # if defined(_WIN32)
# define SPDLOG_API __declspec(dllexport) # ifdef spdlog_EXPORTS
# else # define SPDLOG_API __declspec(dllexport)
# define SPDLOG_API __declspec(dllimport) # else // !spdlog_EXPORTS
# define SPDLOG_API __declspec(dllimport)
# endif
# else // !defined(_WIN32)
# define SPDLOG_API __attribute__((visibility("default")))
# endif # endif
# else // !defined(_WIN32) || !defined(SPDLOG_SHARED_LIB) # else // !defined(SPDLOG_SHARED_LIB)
# define SPDLOG_API # define SPDLOG_API
# endif # endif
# define SPDLOG_INLINE # define SPDLOG_INLINE
@@ -35,23 +44,30 @@
#include <spdlog/fmt/fmt.h> #include <spdlog/fmt/fmt.h>
// backward compatibility with fmt versions older than 8 #if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8
#if FMT_VERSION >= 80000
# 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)
# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) # if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
# include <spdlog/fmt/xchar.h> # include <spdlog/fmt/xchar.h>
# endif # endif
#else #else
# define SPDLOG_FMT_RUNTIME(format_string) format_string # define SPDLOG_FMT_RUNTIME(format_string) format_string
# define SPDLOG_FMT_STRING(format_string) format_string
#endif #endif
// visual studio upto 2013 does not support noexcept nor constexpr // visual studio up to 2013 does not support noexcept nor constexpr
#if defined(_MSC_VER) && (_MSC_VER < 1900) #if defined(_MSC_VER) && (_MSC_VER < 1900)
# define SPDLOG_NOEXCEPT _NOEXCEPT # define SPDLOG_NOEXCEPT _NOEXCEPT
# define SPDLOG_CONSTEXPR # define SPDLOG_CONSTEXPR
# define SPDLOG_CONSTEXPR_FUNC
#else #else
# define SPDLOG_NOEXCEPT noexcept # define SPDLOG_NOEXCEPT noexcept
# define SPDLOG_CONSTEXPR constexpr # define SPDLOG_CONSTEXPR constexpr
# if __cplusplus >= 201402L
# define SPDLOG_CONSTEXPR_FUNC constexpr
# else
# define SPDLOG_CONSTEXPR_FUNC
# endif
#endif #endif
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
@@ -111,10 +127,35 @@ using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr<sinks::sink>; using sink_ptr = std::shared_ptr<sinks::sink>;
using sinks_init_list = std::initializer_list<sink_ptr>; using sinks_init_list = std::initializer_list<sink_ptr>;
using err_handler = std::function<void(const std::string &err_msg)>; using err_handler = std::function<void(const std::string &err_msg)>;
#ifdef SPDLOG_USE_STD_FORMAT
namespace fmt_lib = std;
using string_view_t = std::string_view;
using memory_buf_t = std::string;
template<typename... Args>
using format_string_t = std::string_view;
template<class T, class Char = char>
struct is_convertible_to_basic_format_string : std::integral_constant<bool, std::is_convertible<T, std::basic_string_view<Char>>::value>
{};
# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
using wstring_view_t = std::wstring_view;
using wmemory_buf_t = std::wstring;
template<typename... Args>
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;
using string_view_t = fmt::basic_string_view<char>; using string_view_t = fmt::basic_string_view<char>;
using wstring_view_t = fmt::basic_string_view<wchar_t>;
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>
using format_string_t = fmt::format_string<Args...>;
template<class T> template<class T>
using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type; using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
@@ -127,6 +168,16 @@ struct is_convertible_to_basic_format_string
std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt::basic_runtime<Char>>::value> std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt::basic_runtime<Char>>::value>
{}; {};
# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
using wstring_view_t = fmt::basic_string_view<wchar_t>;
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
template<typename... Args>
using wformat_string_t = fmt::wformat_string<Args...>;
# endif
# define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
#endif
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
# ifndef _WIN32 # ifndef _WIN32
# error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows # error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
@@ -158,7 +209,7 @@ using level_t = std::atomic<int>;
// Log level enum // Log level enum
namespace level { namespace level {
enum level_enum enum level_enum : int
{ {
trace = SPDLOG_LEVEL_TRACE, trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG, debug = SPDLOG_LEVEL_DEBUG,
@@ -255,19 +306,53 @@ struct source_loc
const char *funcname{nullptr}; const char *funcname{nullptr};
}; };
struct file_event_handlers
{
file_event_handlers()
: before_open(nullptr)
, after_open(nullptr)
, before_close(nullptr)
, after_close(nullptr)
{}
std::function<void(const filename_t &filename)> before_open;
std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
std::function<void(const filename_t &filename)> after_close;
};
namespace details { namespace details {
// make_unique support for pre c++14 // make_unique support for pre c++14
#if __cplusplus >= 201402L // C++14 and beyond #if __cplusplus >= 201402L // C++14 and beyond
using std::enable_if_t;
using std::make_unique; using std::make_unique;
#else #else
template<bool B, class T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T, typename... Args> template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&...args) std::unique_ptr<T> make_unique(Args &&... args)
{ {
static_assert(!std::is_array<T>::value, "arrays not supported"); static_assert(!std::is_array<T>::value, "arrays not supported");
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
} }
#endif #endif
// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
template<typename T, typename U, enable_if_t<!std::is_same<T, U>::value, int> = 0>
constexpr T conditional_static_cast(U value)
{
return static_cast<T>(value);
}
template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
constexpr T conditional_static_cast(U value)
{
return value;
}
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@@ -121,6 +121,11 @@ public:
return overrun_counter_; return overrun_counter_;
} }
void reset_overrun_counter()
{
overrun_counter_ = 0;
}
private: private:
// copy from other&& and reset it to disabled state // copy from other&& and reset it to disabled state
void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT

View File

@@ -20,6 +20,10 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
SPDLOG_INLINE file_helper::file_helper(const file_event_handlers &event_handlers)
: event_handlers_(event_handlers)
{}
SPDLOG_INLINE file_helper::~file_helper() SPDLOG_INLINE file_helper::~file_helper()
{ {
close(); close();
@@ -33,6 +37,10 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
auto *mode = SPDLOG_FILENAME_T("ab"); auto *mode = SPDLOG_FILENAME_T("ab");
auto *trunc_mode = SPDLOG_FILENAME_T("wb"); auto *trunc_mode = SPDLOG_FILENAME_T("wb");
if (event_handlers_.before_open)
{
event_handlers_.before_open(filename_);
}
for (int tries = 0; tries < open_tries_; ++tries) for (int tries = 0; tries < open_tries_; ++tries)
{ {
// create containing folder if not exists already. // create containing folder if not exists already.
@@ -52,6 +60,10 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
} }
if (!os::fopen_s(&fd_, fname, mode)) if (!os::fopen_s(&fd_, fname, mode))
{ {
if (event_handlers_.after_open)
{
event_handlers_.after_open(filename_, fd_);
}
return; return;
} }
@@ -72,15 +84,28 @@ SPDLOG_INLINE void file_helper::reopen(bool truncate)
SPDLOG_INLINE void file_helper::flush() SPDLOG_INLINE void file_helper::flush()
{ {
std::fflush(fd_); if (std::fflush(fd_) != 0)
{
throw_spdlog_ex("Failed flush to file " + os::filename_to_str(filename_), errno);
}
} }
SPDLOG_INLINE void file_helper::close() SPDLOG_INLINE void file_helper::close()
{ {
if (fd_ != nullptr) if (fd_ != nullptr)
{ {
if (event_handlers_.before_close)
{
event_handlers_.before_close(filename_, fd_);
}
std::fclose(fd_); std::fclose(fd_);
fd_ = nullptr; fd_ = nullptr;
if (event_handlers_.after_close)
{
event_handlers_.after_close(filename_);
}
} }
} }

View File

@@ -16,7 +16,8 @@ namespace details {
class SPDLOG_API file_helper class SPDLOG_API file_helper
{ {
public: public:
explicit file_helper() = default; file_helper() = default;
explicit file_helper(const file_event_handlers &event_handlers);
file_helper(const file_helper &) = delete; file_helper(const file_helper &) = delete;
file_helper &operator=(const file_helper &) = delete; file_helper &operator=(const file_helper &) = delete;
@@ -50,6 +51,7 @@ private:
const unsigned int open_interval_ = 10; const unsigned int open_interval_ = 10;
std::FILE *fd_{nullptr}; std::FILE *fd_{nullptr};
filename_t filename_; filename_t filename_;
file_event_handlers event_handlers_;
}; };
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@@ -8,6 +8,11 @@
#include <spdlog/fmt/fmt.h> #include <spdlog/fmt/fmt.h>
#include <spdlog/common.h> #include <spdlog/common.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,26 +29,73 @@ 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
SPDLOG_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>
SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n)
{
// taken from fmt: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/format.h#L899-L912
unsigned int count = 1;
for (;;)
{
// Integer division is slow so do it for a group of four digits instead
// of for every digit. The idea comes from the talk by Alexandrescu
// "Three Optimization Tips for C++". See speed-test for a comparison.
if (n < 10)
return count;
if (n < 100)
return count + 1;
if (n < 1000)
return count + 2;
if (n < 10000)
return count + 3;
n /= 10000u;
count += 4;
}
}
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
#if FMT_VERSION < 70000 # if FMT_VERSION < 70000
internal internal
#else # else
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)
@@ -55,7 +107,7 @@ inline void pad2(int n, memory_buf_t &dest)
} }
else // unlikely, but just in case, let fmt deal with it else // unlikely, but just in case, let fmt deal with it
{ {
fmt::format_to(std::back_inserter(dest), SPDLOG_FMT_RUNTIME("{:02}"), n); fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n);
} }
} }

View File

@@ -49,7 +49,7 @@ public:
push_cv_.notify_one(); push_cv_.notify_one();
} }
// try to dequeue item. if no item found. wait upto timeout and try again // try to dequeue item. if no item found. wait up to timeout and try again
// Return true, if succeeded dequeue item, false otherwise // Return true, if succeeded dequeue item, false otherwise
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
{ {
@@ -87,7 +87,7 @@ public:
push_cv_.notify_one(); push_cv_.notify_one();
} }
// try to dequeue item. if no item found. wait upto timeout and try again // try to dequeue item. if no item found. wait up to timeout and try again
// Return true, if succeeded dequeue item, false otherwise // Return true, if succeeded dequeue item, false otherwise
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
{ {
@@ -116,6 +116,12 @@ public:
return q_.size(); return q_.size();
} }
void reset_overrun_counter()
{
std::unique_lock<std::mutex> lock(queue_mutex_);
q_.reset_overrun_counter();
}
private: private:
std::mutex queue_mutex_; std::mutex queue_mutex_;
std::condition_variable push_cv_; std::condition_variable push_cv_;

View File

@@ -13,10 +13,6 @@ struct null_mutex
{ {
void lock() const {} void lock() const {}
void unlock() const {} void unlock() const {}
bool try_lock() const
{
return true;
}
}; };
struct null_atomic_int struct null_atomic_int

View File

@@ -46,7 +46,7 @@
# include <sys/syscall.h> //Use gettid() syscall under linux to get thread id # include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
# elif defined(_AIX) # elif defined(_AIX)
# include <pthread.h> // for pthread_getthreadid_np # include <pthread.h> // for pthread_getthrds_np
# elif defined(__DragonFly__) || defined(__FreeBSD__) # elif defined(__DragonFly__) || defined(__FreeBSD__)
# include <pthread_np.h> // for pthread_getthreadid_np # include <pthread_np.h> // for pthread_getthreadid_np
@@ -145,7 +145,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
if (fd == -1) if (fd == -1)
{ {
return false; return true;
} }
*fp = ::fdopen(fd, mode.c_str()); *fp = ::fdopen(fd, mode.c_str());
if (*fp == nullptr) if (*fp == nullptr)
@@ -230,8 +230,8 @@ SPDLOG_INLINE size_t filesize(FILE *f)
# endif # endif
#else // unix #else // unix
// OpenBSD doesn't compile with :: before the fileno(..) // OpenBSD and AIX doesn't compile with :: before the fileno(..)
# if defined(__OpenBSD__) # if defined(__OpenBSD__) || defined(_AIX)
int fd = fileno(f); int fd = fileno(f);
# else # else
int fd = ::fileno(f); int fd = ::fileno(f);
@@ -305,7 +305,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
// + difference in years * 365 */ // + difference in years * 365 */
+ (long int)(local_year - gmt_year) * 365); + static_cast<long int>(local_year - gmt_year) * 365);
long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour); long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour);
long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min); long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
@@ -336,7 +336,14 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT
# define SYS_gettid __NR_gettid # define SYS_gettid __NR_gettid
# endif # endif
return static_cast<size_t>(::syscall(SYS_gettid)); return static_cast<size_t>(::syscall(SYS_gettid));
#elif defined(_AIX) || defined(__DragonFly__) || defined(__FreeBSD__) #elif defined(_AIX)
struct __pthrdsinfo buf;
int reg_size = 0;
pthread_t pt = pthread_self();
int retval = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &buf, sizeof(buf), NULL, &reg_size);
int tid = (!retval) ? buf.__pi_tid : 0;
return static_cast<size_t>(tid);
#elif defined(__DragonFly__) || defined(__FreeBSD__)
return static_cast<size_t>(::pthread_getthreadid_np()); return static_cast<size_t>(::pthread_getthreadid_np());
#elif defined(__NetBSD__) #elif defined(__NetBSD__)
return static_cast<size_t>(::_lwp_self()); return static_cast<size_t>(::_lwp_self());
@@ -381,7 +388,7 @@ SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
{ {
memory_buf_t buf; memory_buf_t buf;
wstr_to_utf8buf(filename, buf); wstr_to_utf8buf(filename, buf);
return fmt::to_string(buf); return SPDLOG_BUF_TO_STRING(buf);
} }
#else #else
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
@@ -394,9 +401,9 @@ SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT
{ {
#ifdef _WIN32 #ifdef _WIN32
return static_cast<int>(::GetCurrentProcessId()); return conditional_static_cast<int>(::GetCurrentProcessId());
#else #else
return static_cast<int>(::getpid()); return conditional_static_cast<int>(::getpid());
#endif #endif
} }
@@ -476,7 +483,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target)
} }
} }
throw_spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); throw_spdlog_ex(fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
} }
SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target)
@@ -511,7 +518,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target)
} }
} }
throw_spdlog_ex(fmt::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
} }
#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) #endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
@@ -531,7 +538,7 @@ static SPDLOG_INLINE bool mkdir_(const filename_t &path)
// create the given directory - and all directories leading to it // create the given directory - and all directories leading to it
// return true on success or if the directory already exists // return true on success or if the directory already exists
SPDLOG_INLINE bool create_dir(filename_t path) SPDLOG_INLINE bool create_dir(const filename_t &path)
{ {
if (path_exists(path)) if (path_exists(path))
{ {
@@ -570,7 +577,7 @@ SPDLOG_INLINE bool create_dir(filename_t path)
// "abc/" => "abc" // "abc/" => "abc"
// "abc" => "" // "abc" => ""
// "abc///" => "abc//" // "abc///" => "abc//"
SPDLOG_INLINE filename_t dir_name(filename_t path) SPDLOG_INLINE filename_t dir_name(const filename_t &path)
{ {
auto pos = path.find_last_of(folder_seps_filename); auto pos = path.find_last_of(folder_seps_filename);
return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; return pos != filename_t::npos ? path.substr(0, pos) : filename_t{};

View File

@@ -99,11 +99,11 @@ SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
// "abc/" => "abc" // "abc/" => "abc"
// "abc" => "" // "abc" => ""
// "abc///" => "abc//" // "abc///" => "abc//"
SPDLOG_API filename_t dir_name(filename_t path); SPDLOG_API filename_t dir_name(const filename_t &path);
// Create a dir from the given path. // Create a dir from the given path.
// Return true if succeeded or if this dir already exists. // Return true if succeeded or if this dir already exists.
SPDLOG_API bool create_dir(filename_t path); SPDLOG_API bool create_dir(const filename_t &path);
// non thread safe, cross platform getenv/getenv_s // non thread safe, cross platform getenv/getenv_s
// return empty string if field not found // return empty string if field not found

View File

@@ -10,27 +10,6 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
SPDLOG_INLINE periodic_worker::periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval)
{
active_ = (interval > std::chrono::seconds::zero());
if (!active_)
{
return;
}
worker_thread_ = std::thread([this, callback_fun, interval]() {
for (;;)
{
std::unique_lock<std::mutex> lock(this->mutex_);
if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
{
return; // active_ == false, so exit this thread
}
callback_fun();
}
});
}
// stop the worker thread and join it // stop the worker thread and join it
SPDLOG_INLINE periodic_worker::~periodic_worker() SPDLOG_INLINE periodic_worker::~periodic_worker()
{ {

View File

@@ -20,7 +20,27 @@ namespace details {
class SPDLOG_API periodic_worker class SPDLOG_API periodic_worker
{ {
public: public:
periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval); template<typename Rep, typename Period>
periodic_worker(const std::function<void()> &callback_fun, std::chrono::duration<Rep, Period> interval)
{
active_ = (interval > std::chrono::duration<Rep, Period>::zero());
if (!active_)
{
return;
}
worker_thread_ = std::thread([this, callback_fun, interval]() {
for (;;)
{
std::unique_lock<std::mutex> lock(this->mutex_);
if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
{
return; // active_ == false, so exit this thread
}
callback_fun();
}
});
}
periodic_worker(const periodic_worker &) = delete; periodic_worker(const periodic_worker &) = delete;
periodic_worker &operator=(const periodic_worker &) = delete; periodic_worker &operator=(const periodic_worker &) = delete;
// stop the worker thread and join it // stop the worker thread and join it

View File

@@ -188,13 +188,6 @@ SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
flush_level_ = log_level; flush_level_ = log_level;
} }
SPDLOG_INLINE void registry::flush_every(std::chrono::seconds interval)
{
std::lock_guard<std::mutex> lock(flusher_mutex_);
auto clbk = [this]() { this->flush_all(); };
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
}
SPDLOG_INLINE void registry::set_error_handler(err_handler handler) SPDLOG_INLINE void registry::set_error_handler(err_handler handler)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);

View File

@@ -9,6 +9,7 @@
// This class is thread safe // This class is thread safe
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/details/periodic_worker.h>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@@ -22,7 +23,6 @@ class logger;
namespace details { namespace details {
class thread_pool; class thread_pool;
class periodic_worker;
class SPDLOG_API registry class SPDLOG_API registry
{ {
@@ -61,7 +61,13 @@ public:
void flush_on(level::level_enum log_level); void flush_on(level::level_enum log_level);
void flush_every(std::chrono::seconds interval); template<typename Rep, typename Period>
void flush_every(std::chrono::duration<Rep, Period> interval)
{
std::lock_guard<std::mutex> lock(flusher_mutex_);
auto clbk = [this]() { this->flush_all(); };
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
}
void set_error_handler(err_handler handler); void set_error_handler(err_handler handler);

View File

@@ -13,7 +13,7 @@ class logger;
struct synchronous_factory struct synchronous_factory
{ {
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args) static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
{ {
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...); auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink)); auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));

View File

@@ -25,20 +25,6 @@ class tcp_client
{ {
SOCKET socket_ = INVALID_SOCKET; SOCKET socket_ = INVALID_SOCKET;
static bool winsock_initialized_()
{
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
return false;
}
else
{
closesocket(s);
return true;
}
}
static void init_winsock_() static void init_winsock_()
{ {
WSADATA wsaData; WSADATA wsaData;
@@ -52,13 +38,24 @@ class tcp_client
static void throw_winsock_error_(const std::string &msg, int last_error) static void throw_winsock_error_(const std::string &msg, int last_error)
{ {
char buf[512]; char buf[512];
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error, ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL); MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
throw_spdlog_ex(fmt::format("tcp_sink - {}: {}", msg, buf)); throw_spdlog_ex(fmt_lib::format("tcp_sink - {}: {}", msg, buf));
} }
public: public:
tcp_client()
{
init_winsock_();
}
~tcp_client()
{
close();
::WSACleanup();
}
bool is_connected() const bool is_connected() const
{ {
return socket_ != INVALID_SOCKET; return socket_ != INVALID_SOCKET;
@@ -68,7 +65,6 @@ public:
{ {
::closesocket(socket_); ::closesocket(socket_);
socket_ = INVALID_SOCKET; socket_ = INVALID_SOCKET;
WSACleanup();
} }
SOCKET fd() const SOCKET fd() const
@@ -76,20 +72,9 @@ public:
return socket_; return socket_;
} }
~tcp_client()
{
close();
}
// try to connect or throw on failure // try to connect or throw on failure
void connect(const std::string &host, int port) void connect(const std::string &host, int port)
{ {
// initialize winsock if needed
if (!winsock_initialized_())
{
init_winsock_();
}
if (is_connected()) if (is_connected())
{ {
close(); close();

View File

@@ -67,8 +67,7 @@ public:
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
if (rv != 0) if (rv != 0)
{ {
auto msg = fmt::format("::getaddrinfo failed: {}", gai_strerror(rv)); throw_spdlog_ex(fmt_lib::format("::getaddrinfo failed: {}", gai_strerror(rv)));
throw_spdlog_ex(msg);
} }
// Try each address until we successfully connect(2). // Try each address until we successfully connect(2).

View File

@@ -13,7 +13,8 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start) SPDLOG_INLINE thread_pool::thread_pool(
size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
: q_(q_max_items) : q_(q_max_items)
{ {
if (threads_n == 0 || threads_n > 1000) if (threads_n == 0 || threads_n > 1000)
@@ -23,15 +24,21 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std
} }
for (size_t i = 0; i < threads_n; i++) for (size_t i = 0; i < threads_n; i++)
{ {
threads_.emplace_back([this, on_thread_start] { threads_.emplace_back([this, on_thread_start, on_thread_stop] {
on_thread_start(); on_thread_start();
this->thread_pool::worker_loop_(); this->thread_pool::worker_loop_();
on_thread_stop();
}); });
} }
} }
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start)
: thread_pool(q_max_items, threads_n, on_thread_start, [] {})
{}
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
: thread_pool(q_max_items, threads_n, [] {}) : thread_pool(
q_max_items, threads_n, [] {}, [] {})
{} {}
// message all threads to terminate gracefully join them // message all threads to terminate gracefully join them
@@ -68,6 +75,11 @@ size_t SPDLOG_INLINE thread_pool::overrun_counter()
return q_.overrun_counter(); return q_.overrun_counter();
} }
void SPDLOG_INLINE thread_pool::reset_overrun_counter()
{
q_.reset_overrun_counter();
}
size_t SPDLOG_INLINE thread_pool::queue_size() size_t SPDLOG_INLINE thread_pool::queue_size()
{ {
return q_.size(); return q_.size();

View File

@@ -84,10 +84,11 @@ public:
using item_type = async_msg; using item_type = async_msg;
using q_type = details::mpmc_blocking_queue<item_type>; using q_type = details::mpmc_blocking_queue<item_type>;
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start, std::function<void()> on_thread_stop);
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start); thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
thread_pool(size_t q_max_items, size_t threads_n); thread_pool(size_t q_max_items, size_t threads_n);
// message all threads to terminate gracefully join them // message all threads to terminate gracefully and join them
~thread_pool(); ~thread_pool();
thread_pool(const thread_pool &) = delete; thread_pool(const thread_pool &) = delete;
@@ -96,6 +97,7 @@ public:
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy); void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy); void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
size_t overrun_counter(); size_t overrun_counter();
void reset_overrun_counter();
size_t queue_size(); size_t queue_size();
private: private:

View File

@@ -0,0 +1,111 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
// Helper RAII over winsock udp client socket.
// Will throw on construction if socket creation failed.
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#include <spdlog/details/windows_include.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
namespace spdlog {
namespace details {
class udp_client
{
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in addr_ = {0};
static void init_winsock_()
{
WSADATA wsaData;
auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rv != 0)
{
throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
}
}
static void throw_winsock_error_(const std::string &msg, int last_error)
{
char buf[512];
::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf));
}
void cleanup_()
{
if (socket_ != INVALID_SOCKET)
{
::closesocket(socket_);
}
socket_ = INVALID_SOCKET;
::WSACleanup();
}
public:
udp_client(const std::string &host, uint16_t port)
{
init_winsock_();
addr_.sin_family = PF_INET;
addr_.sin_port = htons(port);
addr_.sin_addr.s_addr = INADDR_ANY;
if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
{
int last_error = ::WSAGetLastError();
::WSACleanup();
throw_winsock_error_("error: Invalid address!", last_error);
}
socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
if (socket_ == INVALID_SOCKET)
{
int last_error = ::WSAGetLastError();
::WSACleanup();
throw_winsock_error_("error: Create Socket failed", last_error);
}
int option_value = TX_BUFFER_SIZE;
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0)
{
int last_error = ::WSAGetLastError();
cleanup_();
throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
}
}
~udp_client()
{
cleanup_();
}
SOCKET fd() const
{
return socket_;
}
void send(const char *data, size_t n_bytes)
{
socklen_t tolen = sizeof(struct sockaddr);
if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1)
{
throw_spdlog_ex("sendto(2) failed", errno);
}
}
};
} // namespace details
} // namespace spdlog

94
thirdparty/spdlog/details/udp_client.h vendored Normal file
View File

@@ -0,0 +1,94 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
// Helper RAII over unix udp client socket.
// Will throw on construction if the socket creation failed.
#ifdef _WIN32
# error "include udp_client-windows.h instead"
#endif
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/udp.h>
#include <string>
namespace spdlog {
namespace details {
class udp_client
{
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
int socket_ = -1;
struct sockaddr_in sockAddr_;
void cleanup_()
{
if (socket_ != -1)
{
::close(socket_);
socket_ = -1;
}
}
public:
udp_client(const std::string &host, uint16_t port)
{
socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
if (socket_ < 0)
{
throw_spdlog_ex("error: Create Socket Failed!");
}
int option_value = TX_BUFFER_SIZE;
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0)
{
cleanup_();
throw_spdlog_ex("error: setsockopt(SO_SNDBUF) Failed!");
}
sockAddr_.sin_family = AF_INET;
sockAddr_.sin_port = htons(port);
if (::inet_aton(host.c_str(), &sockAddr_.sin_addr) == 0)
{
cleanup_();
throw_spdlog_ex("error: Invalid address!");
}
::memset(sockAddr_.sin_zero, 0x00, sizeof(sockAddr_.sin_zero));
}
~udp_client()
{
cleanup_();
}
int fd() const
{
return socket_;
}
// Send exactly n_bytes of the given data.
// On error close the connection and throw.
void send(const char *data, size_t n_bytes)
{
ssize_t toslen = 0;
socklen_t tolen = sizeof(struct sockaddr);
if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1)
{
throw_spdlog_ex("sendto(2) failed", errno);
}
}
};
} // namespace details
} // namespace spdlog

View File

@@ -8,9 +8,19 @@
#include <cctype> #include <cctype>
#include <spdlog/common.h> #include <spdlog/common.h>
#if defined(__has_include)
# if __has_include(<version>)
# include <version>
# endif
#endif
#if __cpp_lib_span >= 202002L
# include <span>
#endif
// //
// Support for logging binary data as hex // Support for logging binary data as hex
// format flags, any combination of the followng: // format flags, any combination of the following:
// {:X} - print in uppercase. // {:X} - print in uppercase.
// {:s} - don't separate each byte with space. // {:s} - don't separate each byte with space.
// {:p} - don't print the position on each line start. // {:p} - don't print the position on each line start.
@@ -39,11 +49,12 @@ public:
, size_per_line_(size_per_line) , size_per_line_(size_per_line)
{} {}
It begin() const // do not use begin() and end() to avoid collision with fmt/ranges
It get_begin() const
{ {
return begin_; return begin_;
} }
It end() const It get_end() const
{ {
return end_; return end_;
} }
@@ -67,6 +78,20 @@ inline details::dump_info<typename Container::const_iterator> to_hex(const Conta
return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line); return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
} }
#if __cpp_lib_span >= 202002L
template<typename Value, size_t Extent>
inline details::dump_info<typename std::span<Value, Extent>::iterator> to_hex(
const std::span<Value, Extent> &container, size_t size_per_line = 32)
{
using Container = std::span<Value, Extent>;
static_assert(sizeof(typename Container::value_type) == 1, "sizeof(Container::value_type) != 1");
using Iter = typename Container::iterator;
return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
}
#endif
// create dump_info from ranges // create dump_info from ranges
template<typename It> template<typename It>
inline details::dump_info<It> to_hex(const It range_begin, const It range_end, size_t size_per_line = 32) inline details::dump_info<It> to_hex(const It range_begin, const It range_end, size_t size_per_line = 32)
@@ -76,10 +101,16 @@ inline details::dump_info<It> to_hex(const It range_begin, const It range_end, s
} // namespace spdlog } // namespace spdlog
namespace fmt { namespace
#ifdef SPDLOG_USE_STD_FORMAT
std
#else
fmt
#endif
{
template<typename T> template<typename T>
struct formatter<spdlog::details::dump_info<T>> struct formatter<spdlog::details::dump_info<T>, char>
{ {
const char delimiter = ' '; const char delimiter = ' ';
bool put_newlines = true; bool put_newlines = true;
@@ -90,7 +121,7 @@ struct formatter<spdlog::details::dump_info<T>>
// parse the format string flags // parse the format string flags
template<typename ParseContext> template<typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) SPDLOG_CONSTEXPR_FUNC auto parse(ParseContext &ctx) -> decltype(ctx.begin())
{ {
auto it = ctx.begin(); auto it = ctx.begin();
while (it != ctx.end() && *it != '}') while (it != ctx.end() && *it != '}')
@@ -131,21 +162,21 @@ struct formatter<spdlog::details::dump_info<T>>
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef"; SPDLOG_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 FMT_VERSION < 60000 #if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION < 60000
auto inserter = ctx.begin(); auto inserter = ctx.begin();
#else #else
auto inserter = ctx.out(); auto inserter = ctx.out();
#endif #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.begin(); auto start_of_line = the_range.get_begin();
for (auto i = the_range.begin(); i != the_range.end(); i++) for (auto i = the_range.get_begin(); i != the_range.get_end(); i++)
{ {
auto ch = static_cast<unsigned char>(*i); auto ch = static_cast<unsigned char>(*i);
if (put_newlines && (i == the_range.begin() || i - start_of_line >= size_per_line)) if (put_newlines && (i == the_range.get_begin() || i - start_of_line >= size_per_line))
{ {
if (show_ascii && i != the_range.begin()) if (show_ascii && i != the_range.get_begin())
{ {
*inserter++ = delimiter; *inserter++ = delimiter;
*inserter++ = delimiter; *inserter++ = delimiter;
@@ -156,7 +187,7 @@ struct formatter<spdlog::details::dump_info<T>>
} }
} }
put_newline(inserter, static_cast<size_t>(i - the_range.begin())); put_newline(inserter, static_cast<size_t>(i - the_range.get_begin()));
// put first byte without delimiter in front of it // put first byte without delimiter in front of it
*inserter++ = hex_chars[(ch >> 4) & 0x0f]; *inserter++ = hex_chars[(ch >> 4) & 0x0f];
@@ -175,9 +206,9 @@ struct formatter<spdlog::details::dump_info<T>>
} }
if (show_ascii) // add ascii to last line if (show_ascii) // add ascii to last line
{ {
if (the_range.end() - the_range.begin() > size_per_line) if (the_range.get_end() - the_range.get_begin() > size_per_line)
{ {
auto blank_num = size_per_line - (the_range.end() - start_of_line); auto blank_num = size_per_line - (the_range.get_end() - start_of_line);
while (blank_num-- > 0) while (blank_num-- > 0)
{ {
*inserter++ = delimiter; *inserter++ = delimiter;
@@ -190,7 +221,7 @@ struct formatter<spdlog::details::dump_info<T>>
} }
*inserter++ = delimiter; *inserter++ = delimiter;
*inserter++ = delimiter; *inserter++ = delimiter;
for (auto j = start_of_line; j != the_range.end(); j++) for (auto j = start_of_line; j != the_range.get_end(); j++)
{ {
auto pc = static_cast<unsigned char>(*j); auto pc = static_cast<unsigned char>(*j);
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.'; *inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
@@ -210,8 +241,8 @@ struct formatter<spdlog::details::dump_info<T>>
if (put_positions) if (put_positions)
{ {
fmt::format_to(inserter, "{:04X}: ", pos); spdlog::fmt_lib::format_to(inserter, SPDLOG_FMT_STRING("{:04X}: "), pos);
} }
} }
}; };
} // namespace fmt } // namespace std

View File

@@ -95,10 +95,10 @@ class dynamic_format_arg_store
}; };
template <typename T> template <typename T>
using stored_type = conditional_t<detail::is_string<T>::value && using stored_type = conditional_t<
!has_formatter<T, Context>::value && std::is_convertible<T, std::basic_string<char_type>>::value &&
!detail::is_reference_wrapper<T>::value, !detail::is_reference_wrapper<T>::value,
std::basic_string<char_type>, T>; std::basic_string<char_type>, T>;
// Storage of basic_format_arg must be contiguous. // Storage of basic_format_arg must be contiguous.
std::vector<basic_format_arg<Context>> data_; std::vector<basic_format_arg<Context>> data_;
@@ -143,6 +143,8 @@ class dynamic_format_arg_store
} }
public: public:
constexpr dynamic_format_arg_store() = default;
/** /**
\rst \rst
Adds an argument into the dynamic store for later passing to a formatting Adds an argument into the dynamic store for later passing to a formatting

File diff suppressed because it is too large Load Diff

View File

@@ -10,13 +10,6 @@
#include "format.h" #include "format.h"
// __declspec(deprecated) is broken in some MSVC versions.
#if FMT_MSC_VER
# define FMT_DEPRECATED_NONMSVC
#else
# define FMT_DEPRECATED_NONMSVC FMT_DEPRECATED
#endif
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
FMT_MODULE_EXPORT_BEGIN FMT_MODULE_EXPORT_BEGIN
@@ -185,9 +178,13 @@ enum class terminal_color : uint8_t {
enum class emphasis : uint8_t { enum class emphasis : uint8_t {
bold = 1, bold = 1,
italic = 1 << 1, faint = 1 << 1,
underline = 1 << 2, italic = 1 << 2,
strikethrough = 1 << 3 underline = 1 << 3,
blink = 1 << 4,
reverse = 1 << 5,
conceal = 1 << 6,
strikethrough = 1 << 7,
}; };
// rgb is a struct for red, green and blue colors. // rgb is a struct for red, green and blue colors.
@@ -210,17 +207,16 @@ FMT_BEGIN_DETAIL_NAMESPACE
// color is a struct of either a rgb color or a terminal color. // color is a struct of either a rgb color or a terminal color.
struct color_type { struct color_type {
FMT_CONSTEXPR color_type() FMT_NOEXCEPT : is_rgb(), value{} {} FMT_CONSTEXPR color_type() noexcept : is_rgb(), value{} {}
FMT_CONSTEXPR color_type(color rgb_color) FMT_NOEXCEPT : is_rgb(true), FMT_CONSTEXPR color_type(color rgb_color) noexcept : is_rgb(true), value{} {
value{} {
value.rgb_color = static_cast<uint32_t>(rgb_color); value.rgb_color = static_cast<uint32_t>(rgb_color);
} }
FMT_CONSTEXPR color_type(rgb rgb_color) FMT_NOEXCEPT : is_rgb(true), value{} { FMT_CONSTEXPR color_type(rgb rgb_color) noexcept : is_rgb(true), value{} {
value.rgb_color = (static_cast<uint32_t>(rgb_color.r) << 16) | value.rgb_color = (static_cast<uint32_t>(rgb_color.r) << 16) |
(static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b; (static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b;
} }
FMT_CONSTEXPR color_type(terminal_color term_color) FMT_NOEXCEPT : is_rgb(), FMT_CONSTEXPR color_type(terminal_color term_color) noexcept
value{} { : is_rgb(), value{} {
value.term_color = static_cast<uint8_t>(term_color); value.term_color = static_cast<uint8_t>(term_color);
} }
bool is_rgb; bool is_rgb;
@@ -235,10 +231,8 @@ FMT_END_DETAIL_NAMESPACE
/** A text style consisting of foreground and background colors and emphasis. */ /** A text style consisting of foreground and background colors and emphasis. */
class text_style { class text_style {
public: public:
FMT_CONSTEXPR text_style(emphasis em = emphasis()) FMT_NOEXCEPT FMT_CONSTEXPR text_style(emphasis em = emphasis()) noexcept
: set_foreground_color(), : set_foreground_color(), set_background_color(), ems(em) {}
set_background_color(),
ems(em) {}
FMT_CONSTEXPR text_style& operator|=(const text_style& rhs) { FMT_CONSTEXPR text_style& operator|=(const text_style& rhs) {
if (!set_foreground_color) { if (!set_foreground_color) {
@@ -269,44 +263,32 @@ class text_style {
return lhs |= rhs; return lhs |= rhs;
} }
FMT_DEPRECATED_NONMSVC FMT_CONSTEXPR text_style& operator&=( FMT_CONSTEXPR bool has_foreground() const noexcept {
const text_style& rhs) {
return and_assign(rhs);
}
FMT_DEPRECATED_NONMSVC friend FMT_CONSTEXPR text_style
operator&(text_style lhs, const text_style& rhs) {
return lhs.and_assign(rhs);
}
FMT_CONSTEXPR bool has_foreground() const FMT_NOEXCEPT {
return set_foreground_color; return set_foreground_color;
} }
FMT_CONSTEXPR bool has_background() const FMT_NOEXCEPT { FMT_CONSTEXPR bool has_background() const noexcept {
return set_background_color; return set_background_color;
} }
FMT_CONSTEXPR bool has_emphasis() const FMT_NOEXCEPT { FMT_CONSTEXPR bool has_emphasis() const noexcept {
return static_cast<uint8_t>(ems) != 0; return static_cast<uint8_t>(ems) != 0;
} }
FMT_CONSTEXPR detail::color_type get_foreground() const FMT_NOEXCEPT { FMT_CONSTEXPR detail::color_type get_foreground() const noexcept {
FMT_ASSERT(has_foreground(), "no foreground specified for this style"); FMT_ASSERT(has_foreground(), "no foreground specified for this style");
return foreground_color; return foreground_color;
} }
FMT_CONSTEXPR detail::color_type get_background() const FMT_NOEXCEPT { FMT_CONSTEXPR detail::color_type get_background() const noexcept {
FMT_ASSERT(has_background(), "no background specified for this style"); FMT_ASSERT(has_background(), "no background specified for this style");
return background_color; return background_color;
} }
FMT_CONSTEXPR emphasis get_emphasis() const FMT_NOEXCEPT { FMT_CONSTEXPR emphasis get_emphasis() const noexcept {
FMT_ASSERT(has_emphasis(), "no emphasis specified for this style"); FMT_ASSERT(has_emphasis(), "no emphasis specified for this style");
return ems; return ems;
} }
private: private:
FMT_CONSTEXPR text_style(bool is_foreground, FMT_CONSTEXPR text_style(bool is_foreground,
detail::color_type text_color) FMT_NOEXCEPT detail::color_type text_color) noexcept
: set_foreground_color(), : set_foreground_color(), set_background_color(), ems() {
set_background_color(),
ems() {
if (is_foreground) { if (is_foreground) {
foreground_color = text_color; foreground_color = text_color;
set_foreground_color = true; set_foreground_color = true;
@@ -316,36 +298,9 @@ class text_style {
} }
} }
// DEPRECATED! friend FMT_CONSTEXPR text_style fg(detail::color_type foreground) noexcept;
FMT_CONSTEXPR text_style& and_assign(const text_style& rhs) {
if (!set_foreground_color) {
set_foreground_color = rhs.set_foreground_color;
foreground_color = rhs.foreground_color;
} else if (rhs.set_foreground_color) {
if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb)
FMT_THROW(format_error("can't AND a terminal color"));
foreground_color.value.rgb_color &= rhs.foreground_color.value.rgb_color;
}
if (!set_background_color) { friend FMT_CONSTEXPR text_style bg(detail::color_type background) noexcept;
set_background_color = rhs.set_background_color;
background_color = rhs.background_color;
} else if (rhs.set_background_color) {
if (!background_color.is_rgb || !rhs.background_color.is_rgb)
FMT_THROW(format_error("can't AND a terminal color"));
background_color.value.rgb_color &= rhs.background_color.value.rgb_color;
}
ems = static_cast<emphasis>(static_cast<uint8_t>(ems) &
static_cast<uint8_t>(rhs.ems));
return *this;
}
friend FMT_CONSTEXPR_DECL text_style fg(detail::color_type foreground)
FMT_NOEXCEPT;
friend FMT_CONSTEXPR_DECL text_style bg(detail::color_type background)
FMT_NOEXCEPT;
detail::color_type foreground_color; detail::color_type foreground_color;
detail::color_type background_color; detail::color_type background_color;
@@ -355,17 +310,16 @@ class text_style {
}; };
/** Creates a text style from the foreground (text) color. */ /** Creates a text style from the foreground (text) color. */
FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) FMT_NOEXCEPT { FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) noexcept {
return text_style(true, foreground); return text_style(true, foreground);
} }
/** Creates a text style from the background color. */ /** Creates a text style from the background color. */
FMT_CONSTEXPR inline text_style bg(detail::color_type background) FMT_NOEXCEPT { FMT_CONSTEXPR inline text_style bg(detail::color_type background) noexcept {
return text_style(false, background); return text_style(false, background);
} }
FMT_CONSTEXPR inline text_style operator|(emphasis lhs, FMT_CONSTEXPR inline text_style operator|(emphasis lhs, emphasis rhs) noexcept {
emphasis rhs) FMT_NOEXCEPT {
return text_style(lhs) | rhs; return text_style(lhs) | rhs;
} }
@@ -373,7 +327,7 @@ FMT_BEGIN_DETAIL_NAMESPACE
template <typename Char> struct ansi_color_escape { template <typename Char> struct ansi_color_escape {
FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color, FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color,
const char* esc) FMT_NOEXCEPT { const char* esc) noexcept {
// If we have a terminal color, we need to output another escape code // If we have a terminal color, we need to output another escape code
// sequence. // sequence.
if (!text_color.is_rgb) { if (!text_color.is_rgb) {
@@ -408,17 +362,19 @@ template <typename Char> struct ansi_color_escape {
to_esc(color.b, buffer + 15, 'm'); to_esc(color.b, buffer + 15, 'm');
buffer[19] = static_cast<Char>(0); buffer[19] = static_cast<Char>(0);
} }
FMT_CONSTEXPR ansi_color_escape(emphasis em) FMT_NOEXCEPT { FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept {
uint8_t em_codes[4] = {}; uint8_t em_codes[num_emphases] = {};
uint8_t em_bits = static_cast<uint8_t>(em); if (has_emphasis(em, emphasis::bold)) em_codes[0] = 1;
if (em_bits & static_cast<uint8_t>(emphasis::bold)) em_codes[0] = 1; if (has_emphasis(em, emphasis::faint)) em_codes[1] = 2;
if (em_bits & static_cast<uint8_t>(emphasis::italic)) em_codes[1] = 3; if (has_emphasis(em, emphasis::italic)) em_codes[2] = 3;
if (em_bits & static_cast<uint8_t>(emphasis::underline)) em_codes[2] = 4; if (has_emphasis(em, emphasis::underline)) em_codes[3] = 4;
if (em_bits & static_cast<uint8_t>(emphasis::strikethrough)) if (has_emphasis(em, emphasis::blink)) em_codes[4] = 5;
em_codes[3] = 9; if (has_emphasis(em, emphasis::reverse)) em_codes[5] = 7;
if (has_emphasis(em, emphasis::conceal)) em_codes[6] = 8;
if (has_emphasis(em, emphasis::strikethrough)) em_codes[7] = 9;
size_t index = 0; size_t index = 0;
for (int i = 0; i < 4; ++i) { for (size_t i = 0; i < num_emphases; ++i) {
if (!em_codes[i]) continue; if (!em_codes[i]) continue;
buffer[index++] = static_cast<Char>('\x1b'); buffer[index++] = static_cast<Char>('\x1b');
buffer[index++] = static_cast<Char>('['); buffer[index++] = static_cast<Char>('[');
@@ -427,66 +383,76 @@ template <typename Char> struct ansi_color_escape {
} }
buffer[index++] = static_cast<Char>(0); buffer[index++] = static_cast<Char>(0);
} }
FMT_CONSTEXPR operator const Char*() const FMT_NOEXCEPT { return buffer; } FMT_CONSTEXPR operator const Char*() const noexcept { return buffer; }
FMT_CONSTEXPR const Char* begin() const FMT_NOEXCEPT { return buffer; } FMT_CONSTEXPR const Char* begin() const noexcept { return buffer; }
FMT_CONSTEXPR_CHAR_TRAITS const Char* end() const FMT_NOEXCEPT { FMT_CONSTEXPR_CHAR_TRAITS const Char* end() const noexcept {
return buffer + std::char_traits<Char>::length(buffer); return buffer + std::char_traits<Char>::length(buffer);
} }
private: private:
Char buffer[7u + 3u * 4u + 1u]; static constexpr size_t num_emphases = 8;
Char buffer[7u + 3u * num_emphases + 1u];
static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out, static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out,
char delimiter) FMT_NOEXCEPT { char delimiter) noexcept {
out[0] = static_cast<Char>('0' + c / 100); out[0] = static_cast<Char>('0' + c / 100);
out[1] = static_cast<Char>('0' + c / 10 % 10); out[1] = static_cast<Char>('0' + c / 10 % 10);
out[2] = static_cast<Char>('0' + c % 10); out[2] = static_cast<Char>('0' + c % 10);
out[3] = static_cast<Char>(delimiter); out[3] = static_cast<Char>(delimiter);
} }
static FMT_CONSTEXPR bool has_emphasis(emphasis em, emphasis mask) noexcept {
return static_cast<uint8_t>(em) & static_cast<uint8_t>(mask);
}
}; };
template <typename Char> template <typename Char>
FMT_CONSTEXPR ansi_color_escape<Char> make_foreground_color( FMT_CONSTEXPR ansi_color_escape<Char> make_foreground_color(
detail::color_type foreground) FMT_NOEXCEPT { detail::color_type foreground) noexcept {
return ansi_color_escape<Char>(foreground, "\x1b[38;2;"); return ansi_color_escape<Char>(foreground, "\x1b[38;2;");
} }
template <typename Char> template <typename Char>
FMT_CONSTEXPR ansi_color_escape<Char> make_background_color( FMT_CONSTEXPR ansi_color_escape<Char> make_background_color(
detail::color_type background) FMT_NOEXCEPT { detail::color_type background) noexcept {
return ansi_color_escape<Char>(background, "\x1b[48;2;"); return ansi_color_escape<Char>(background, "\x1b[48;2;");
} }
template <typename Char> template <typename Char>
FMT_CONSTEXPR ansi_color_escape<Char> make_emphasis(emphasis em) FMT_NOEXCEPT { FMT_CONSTEXPR ansi_color_escape<Char> make_emphasis(emphasis em) noexcept {
return ansi_color_escape<Char>(em); return ansi_color_escape<Char>(em);
} }
template <typename Char> template <typename Char> inline void fputs(const Char* chars, FILE* stream) {
inline void fputs(const Char* chars, FILE* stream) FMT_NOEXCEPT { int result = std::fputs(chars, stream);
std::fputs(chars, stream); if (result < 0)
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
} }
template <> template <> inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) {
inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) FMT_NOEXCEPT { int result = std::fputws(chars, stream);
std::fputws(chars, stream); if (result < 0)
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
} }
template <typename Char> inline void reset_color(FILE* stream) FMT_NOEXCEPT { template <typename Char> inline void reset_color(FILE* stream) {
fputs("\x1b[0m", stream); fputs("\x1b[0m", stream);
} }
template <> inline void reset_color<wchar_t>(FILE* stream) FMT_NOEXCEPT { template <> inline void reset_color<wchar_t>(FILE* stream) {
fputs(L"\x1b[0m", stream); fputs(L"\x1b[0m", stream);
} }
template <typename Char> template <typename Char> inline void reset_color(buffer<Char>& buffer) {
inline void reset_color(buffer<Char>& buffer) FMT_NOEXCEPT {
auto reset_color = string_view("\x1b[0m"); auto reset_color = string_view("\x1b[0m");
buffer.append(reset_color.begin(), reset_color.end()); buffer.append(reset_color.begin(), reset_color.end());
} }
template <typename T> struct styled_arg {
const T& value;
text_style style;
};
template <typename Char> template <typename Char>
void vformat_to(buffer<Char>& buf, const text_style& ts, void vformat_to(buffer<Char>& buf, const text_style& ts,
basic_string_view<Char> format_str, basic_string_view<Char> format_str,
@@ -517,9 +483,13 @@ template <typename S, typename Char = char_t<S>>
void vprint(std::FILE* f, const text_style& ts, const S& format, void vprint(std::FILE* f, const text_style& ts, const S& format,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args) {
basic_memory_buffer<Char> buf; basic_memory_buffer<Char> buf;
detail::vformat_to(buf, ts, to_string_view(format), args); detail::vformat_to(buf, ts, detail::to_string_view(format), args);
buf.push_back(Char(0)); if (detail::is_utf8()) {
detail::fputs(buf.data(), f); detail::print(f, basic_string_view<Char>(buf.begin(), buf.size()));
} else {
buf.push_back(Char(0));
detail::fputs(buf.data(), f);
}
} }
/** /**
@@ -538,7 +508,7 @@ template <typename S, typename... Args,
void print(std::FILE* f, const text_style& ts, const S& format_str, void print(std::FILE* f, const text_style& ts, const S& format_str,
const Args&... args) { const Args&... args) {
vprint(f, ts, format_str, vprint(f, ts, format_str,
fmt::make_args_checked<Args...>(format_str, args...)); fmt::make_format_args<buffer_context<char_t<S>>>(args...));
} }
/** /**
@@ -563,7 +533,7 @@ inline std::basic_string<Char> vformat(
const text_style& ts, const S& format_str, const text_style& ts, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args) {
basic_memory_buffer<Char> buf; basic_memory_buffer<Char> buf;
detail::vformat_to(buf, ts, to_string_view(format_str), args); detail::vformat_to(buf, ts, detail::to_string_view(format_str), args);
return fmt::to_string(buf); return fmt::to_string(buf);
} }
@@ -582,8 +552,8 @@ inline std::basic_string<Char> vformat(
template <typename S, typename... Args, typename Char = char_t<S>> template <typename S, typename... Args, typename Char = char_t<S>>
inline std::basic_string<Char> format(const text_style& ts, const S& format_str, inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
const Args&... args) { const Args&... args) {
return fmt::vformat(ts, to_string_view(format_str), return fmt::vformat(ts, detail::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str, args...)); fmt::make_format_args<buffer_context<Char>>(args...));
} }
/** /**
@@ -617,8 +587,62 @@ template <typename OutputIt, typename S, typename... Args,
inline auto format_to(OutputIt out, const text_style& ts, const S& format_str, inline auto format_to(OutputIt out, const text_style& ts, const S& format_str,
Args&&... args) -> Args&&... args) ->
typename std::enable_if<enable, OutputIt>::type { typename std::enable_if<enable, OutputIt>::type {
return vformat_to(out, ts, to_string_view(format_str), return vformat_to(out, ts, detail::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str, args...)); fmt::make_format_args<buffer_context<char_t<S>>>(args...));
}
template <typename T, typename Char>
struct formatter<detail::styled_arg<T>, Char> : formatter<T, Char> {
template <typename FormatContext>
auto format(const detail::styled_arg<T>& arg, FormatContext& ctx) const
-> decltype(ctx.out()) {
const auto& ts = arg.style;
const auto& value = arg.value;
auto out = ctx.out();
bool has_style = false;
if (ts.has_emphasis()) {
has_style = true;
auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
out = std::copy(emphasis.begin(), emphasis.end(), out);
}
if (ts.has_foreground()) {
has_style = true;
auto foreground =
detail::make_foreground_color<Char>(ts.get_foreground());
out = std::copy(foreground.begin(), foreground.end(), out);
}
if (ts.has_background()) {
has_style = true;
auto background =
detail::make_background_color<Char>(ts.get_background());
out = std::copy(background.begin(), background.end(), out);
}
out = formatter<T, Char>::format(value, ctx);
if (has_style) {
auto reset_color = string_view("\x1b[0m");
out = std::copy(reset_color.begin(), reset_color.end(), out);
}
return out;
}
};
/**
\rst
Returns an argument that will be formatted using ANSI escape sequences,
to be used in a formatting function.
**Example**::
fmt::print("Elapsed time: {0:.2f} seconds",
fmt::styled(1.23, fmt::fg(fmt::color::green) |
fmt::bg(fmt::color::blue)));
\endrst
*/
template <typename T>
FMT_CONSTEXPR auto styled(const T& value, text_style ts)
-> detail::styled_arg<remove_cvref_t<T>> {
return detail::styled_arg<remove_cvref_t<T>>{value, ts};
} }
FMT_MODULE_EXPORT_END FMT_MODULE_EXPORT_END

View File

@@ -13,48 +13,9 @@
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
// An output iterator that counts the number of objects written to it and
// discards them.
class counting_iterator {
private:
size_t count_;
public:
using iterator_category = std::output_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = void;
using _Unchecked_type = counting_iterator; // Mark iterator as checked.
struct value_type {
template <typename T> void operator=(const T&) {}
};
counting_iterator() : count_(0) {}
size_t count() const { return count_; }
counting_iterator& operator++() {
++count_;
return *this;
}
counting_iterator operator++(int) {
auto it = *this;
++*this;
return it;
}
friend counting_iterator operator+(counting_iterator it, difference_type n) {
it.count_ += static_cast<size_t>(n);
return it;
}
value_type operator*() const { return {}; }
};
template <typename Char, typename InputIt> template <typename Char, typename InputIt>
inline counting_iterator copy_str(InputIt begin, InputIt end, FMT_CONSTEXPR inline counting_iterator copy_str(InputIt begin, InputIt end,
counting_iterator it) { counting_iterator it) {
return it + (end - begin); return it + (end - begin);
} }
@@ -75,8 +36,7 @@ template <typename OutputIt> class truncating_iterator_base {
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using pointer = void; using pointer = void;
using reference = void; using reference = void;
using _Unchecked_type = FMT_UNCHECKED_ITERATOR(truncating_iterator_base);
truncating_iterator_base; // Mark iterator as checked.
OutputIt base() const { return out_; } OutputIt base() const { return out_; }
size_t count() const { return count_; } size_t count() const { return count_; }
@@ -156,19 +116,19 @@ struct is_compiled_string : std::is_base_of<compiled_string, S> {};
std::string s = fmt::format(FMT_COMPILE("{}"), 42); std::string s = fmt::format(FMT_COMPILE("{}"), 42);
\endrst \endrst
*/ */
#ifdef __cpp_if_constexpr #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
# define FMT_COMPILE(s) \ # define FMT_COMPILE(s) \
FMT_STRING_IMPL(s, fmt::detail::compiled_string, explicit) FMT_STRING_IMPL(s, fmt::detail::compiled_string, explicit)
#else #else
# define FMT_COMPILE(s) FMT_STRING(s) # define FMT_COMPILE(s) FMT_STRING(s)
#endif #endif
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS #if FMT_USE_NONTYPE_TEMPLATE_ARGS
template <typename Char, size_t N, template <typename Char, size_t N,
fmt::detail_exported::fixed_string<Char, N> Str> fmt::detail_exported::fixed_string<Char, N> Str>
struct udl_compiled_string : compiled_string { struct udl_compiled_string : compiled_string {
using char_type = Char; using char_type = Char;
constexpr operator basic_string_view<char_type>() const { explicit constexpr operator basic_string_view<char_type>() const {
return {Str.data, N - 1}; return {Str.data, N - 1};
} }
}; };
@@ -179,7 +139,7 @@ const T& first(const T& value, const Tail&...) {
return value; return value;
} }
#ifdef __cpp_if_constexpr #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
template <typename... Args> struct type_list {}; template <typename... Args> struct type_list {};
// Returns a reference to the argument at index N from [first, rest...]. // Returns a reference to the argument at index N from [first, rest...].
@@ -190,7 +150,7 @@ constexpr const auto& get([[maybe_unused]] const T& first,
if constexpr (N == 0) if constexpr (N == 0)
return first; return first;
else else
return get<N - 1>(rest...); return detail::get<N - 1>(rest...);
} }
template <typename Char, typename... Args> template <typename Char, typename... Args>
@@ -202,7 +162,8 @@ constexpr int get_arg_index_by_name(basic_string_view<Char> name,
template <int N, typename> struct get_type_impl; template <int N, typename> struct get_type_impl;
template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> { template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> {
using type = remove_cvref_t<decltype(get<N>(std::declval<Args>()...))>; using type =
remove_cvref_t<decltype(detail::get<N>(std::declval<Args>()...))>;
}; };
template <int N, typename T> template <int N, typename T>
@@ -242,7 +203,7 @@ template <typename Char> struct code_unit {
// This ensures that the argument type is convertible to `const T&`. // This ensures that the argument type is convertible to `const T&`.
template <typename T, int N, typename... Args> template <typename T, int N, typename... Args>
constexpr const T& get_arg_checked(const Args&... args) { constexpr const T& get_arg_checked(const Args&... args) {
const auto& arg = get<N>(args...); const auto& arg = detail::get<N>(args...);
if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) { if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) {
return arg.value; return arg.value;
} else { } else {
@@ -289,7 +250,7 @@ template <typename Char> struct runtime_named_field {
constexpr OutputIt format(OutputIt out, const Args&... args) const { constexpr OutputIt format(OutputIt out, const Args&... args) const {
bool found = (try_format_argument(out, name, args) || ...); bool found = (try_format_argument(out, name, args) || ...);
if (!found) { if (!found) {
throw format_error("argument with specified name is not found"); FMT_THROW(format_error("argument with specified name is not found"));
} }
return out; return out;
} }
@@ -376,10 +337,11 @@ template <typename T, typename Char>
constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str, constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
size_t pos, int next_arg_id) { size_t pos, int next_arg_id) {
str.remove_prefix(pos); str.remove_prefix(pos);
auto ctx = basic_format_parse_context<Char>(str, {}, next_arg_id); auto ctx = compile_parse_context<Char>(str, max_value<int>(), nullptr, {},
next_arg_id);
auto f = formatter<T, Char>(); auto f = formatter<T, Char>();
auto end = f.parse(ctx); auto end = f.parse(ctx);
return {f, pos + fmt::detail::to_unsigned(end - str.data()) + 1, return {f, pos + fmt::detail::to_unsigned(end - str.data()),
next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()}; next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
} }
@@ -399,7 +361,9 @@ template <typename Char> struct arg_id_handler {
return 0; return 0;
} }
constexpr void on_error(const char* message) { throw format_error(message); } constexpr void on_error(const char* message) {
FMT_THROW(format_error(message));
}
}; };
template <typename Char> struct parse_arg_id_result { template <typename Char> struct parse_arg_id_result {
@@ -433,13 +397,20 @@ constexpr auto parse_replacement_field_then_tail(S format_str) {
return parse_tail<Args, END_POS + 1, NEXT_ID>( return parse_tail<Args, END_POS + 1, NEXT_ID>(
field<char_type, typename field_type<T>::type, ARG_INDEX>(), field<char_type, typename field_type<T>::type, ARG_INDEX>(),
format_str); format_str);
} else if constexpr (c == ':') { } else if constexpr (c != ':') {
FMT_THROW(format_error("expected ':'"));
} else {
constexpr auto result = parse_specs<typename field_type<T>::type>( constexpr auto result = parse_specs<typename field_type<T>::type>(
str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID); str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
return parse_tail<Args, result.end, result.next_arg_id>( if constexpr (result.end >= str.size() || str[result.end] != '}') {
spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{ FMT_THROW(format_error("expected '}'"));
result.fmt}, return 0;
format_str); } else {
return parse_tail<Args, result.end + 1, result.next_arg_id>(
spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
result.fmt},
format_str);
}
} }
} }
@@ -451,7 +422,7 @@ constexpr auto compile_format_string(S format_str) {
constexpr auto str = basic_string_view<char_type>(format_str); constexpr auto str = basic_string_view<char_type>(format_str);
if constexpr (str[POS] == '{') { if constexpr (str[POS] == '{') {
if constexpr (POS + 1 == str.size()) if constexpr (POS + 1 == str.size())
throw format_error("unmatched '{' in format string"); FMT_THROW(format_error("unmatched '{' in format string"));
if constexpr (str[POS + 1] == '{') { if constexpr (str[POS + 1] == '{') {
return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str); return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
} else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') { } else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') {
@@ -500,7 +471,7 @@ constexpr auto compile_format_string(S format_str) {
} }
} else if constexpr (str[POS] == '}') { } else if constexpr (str[POS] == '}') {
if constexpr (POS + 1 == str.size()) if constexpr (POS + 1 == str.size())
throw format_error("unmatched '}' in format string"); FMT_THROW(format_error("unmatched '}' in format string"));
return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str); return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
} else { } else {
constexpr auto end = parse_text(str, POS + 1); constexpr auto end = parse_text(str, POS + 1);
@@ -527,12 +498,12 @@ constexpr auto compile(S format_str) {
return result; return result;
} }
} }
#endif // __cpp_if_constexpr #endif // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
} // namespace detail } // namespace detail
FMT_MODULE_EXPORT_BEGIN FMT_MODULE_EXPORT_BEGIN
#ifdef __cpp_if_constexpr #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
template <typename CompiledFormat, typename... Args, template <typename CompiledFormat, typename... Args,
typename Char = typename CompiledFormat::char_type, typename Char = typename CompiledFormat::char_type,
@@ -570,10 +541,11 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
constexpr auto compiled = detail::compile<Args...>(S()); constexpr auto compiled = detail::compile<Args...>(S());
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>, if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
detail::unknown_format>()) { detail::unknown_format>()) {
return format(static_cast<basic_string_view<typename S::char_type>>(S()), return fmt::format(
std::forward<Args>(args)...); static_cast<basic_string_view<typename S::char_type>>(S()),
std::forward<Args>(args)...);
} else { } else {
return format(compiled, std::forward<Args>(args)...); return fmt::format(compiled, std::forward<Args>(args)...);
} }
} }
@@ -583,11 +555,11 @@ FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
constexpr auto compiled = detail::compile<Args...>(S()); constexpr auto compiled = detail::compile<Args...>(S());
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>, if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
detail::unknown_format>()) { detail::unknown_format>()) {
return format_to(out, return fmt::format_to(
static_cast<basic_string_view<typename S::char_type>>(S()), out, static_cast<basic_string_view<typename S::char_type>>(S()),
std::forward<Args>(args)...); std::forward<Args>(args)...);
} else { } else {
return format_to(out, compiled, std::forward<Args>(args)...); return fmt::format_to(out, compiled, std::forward<Args>(args)...);
} }
} }
#endif #endif
@@ -596,22 +568,24 @@ template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
const S& format_str, Args&&... args) { const S& format_str, Args&&... args) {
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), format_str, auto it = fmt::format_to(detail::truncating_iterator<OutputIt>(out, n),
std::forward<Args>(args)...); format_str, std::forward<Args>(args)...);
return {it.base(), it.count()}; return {it.base(), it.count()};
} }
template <typename S, typename... Args, template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
size_t formatted_size(const S& format_str, const Args&... args) { FMT_CONSTEXPR20 size_t formatted_size(const S& format_str,
return format_to(detail::counting_iterator(), format_str, args...).count(); const Args&... args) {
return fmt::format_to(detail::counting_iterator(), format_str, args...)
.count();
} }
template <typename S, typename... Args, template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
void print(std::FILE* f, const S& format_str, const Args&... args) { void print(std::FILE* f, const S& format_str, const Args&... args) {
memory_buffer buffer; memory_buffer buffer;
format_to(std::back_inserter(buffer), format_str, args...); fmt::format_to(std::back_inserter(buffer), format_str, args...);
detail::print(f, {buffer.data(), buffer.size()}); detail::print(f, {buffer.data(), buffer.size()});
} }
@@ -621,14 +595,12 @@ void print(const S& format_str, const Args&... args) {
print(stdout, format_str, args...); print(stdout, format_str, args...);
} }
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS #if FMT_USE_NONTYPE_TEMPLATE_ARGS
inline namespace literals { inline namespace literals {
template <detail_exported::fixed_string Str> template <detail_exported::fixed_string Str> constexpr auto operator""_cf() {
constexpr detail::udl_compiled_string< using char_t = remove_cvref_t<decltype(Str.data[0])>;
remove_cvref_t<decltype(Str.data[0])>, return detail::udl_compiled_string<char_t, sizeof(Str.data) / sizeof(char_t),
sizeof(Str.data) / sizeof(decltype(Str.data[0])), Str> Str>();
operator""_cf() {
return {};
} }
} // namespace literals } // namespace literals
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,27 @@
Copyright (c) 2012 - present, Victor Zverovich
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--- Optional exception to the license ---
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into a machine-executable object form of such
source code, you may redistribute such embedded portions in such object form
without including the above copyright and permission notices.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -9,10 +9,8 @@
#define FMT_OS_H_ #define FMT_OS_H_
#include <cerrno> #include <cerrno>
#include <clocale> // locale_t
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> // strtod_l
#include <system_error> // std::system_error #include <system_error> // std::system_error
#if defined __APPLE__ || defined(__FreeBSD__) #if defined __APPLE__ || defined(__FreeBSD__)
@@ -21,17 +19,20 @@
#include "format.h" #include "format.h"
#ifndef FMT_USE_FCNTL
// UWP doesn't provide _pipe. // UWP doesn't provide _pipe.
#if FMT_HAS_INCLUDE("winapifamily.h") # if FMT_HAS_INCLUDE("winapifamily.h")
# include <winapifamily.h> # include <winapifamily.h>
#endif # endif
#if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \ # if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \
defined(__linux__)) && \ defined(__linux__)) && \
(!defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) (!defined(WINAPI_FAMILY) || \
# include <fcntl.h> // for O_RDONLY (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
# define FMT_USE_FCNTL 1 # include <fcntl.h> // for O_RDONLY
#else # define FMT_USE_FCNTL 1
# define FMT_USE_FCNTL 0 # else
# define FMT_USE_FCNTL 0
# endif
#endif #endif
#ifndef FMT_POSIX #ifndef FMT_POSIX
@@ -138,7 +139,7 @@ template <typename Char> struct formatter<std::error_code, Char> {
}; };
#ifdef _WIN32 #ifdef _WIN32
FMT_API const std::error_category& system_category() FMT_NOEXCEPT; FMT_API const std::error_category& system_category() noexcept;
FMT_BEGIN_DETAIL_NAMESPACE FMT_BEGIN_DETAIL_NAMESPACE
// A converter from UTF-16 to UTF-8. // A converter from UTF-16 to UTF-8.
@@ -162,7 +163,7 @@ class utf16_to_utf8 {
}; };
FMT_API void format_windows_error(buffer<char>& out, int error_code, FMT_API void format_windows_error(buffer<char>& out, int error_code,
const char* message) FMT_NOEXCEPT; const char* message) noexcept;
FMT_END_DETAIL_NAMESPACE FMT_END_DETAIL_NAMESPACE
FMT_API std::system_error vwindows_error(int error_code, string_view format_str, FMT_API std::system_error vwindows_error(int error_code, string_view format_str,
@@ -204,10 +205,9 @@ std::system_error windows_error(int error_code, string_view message,
// Reports a Windows error without throwing an exception. // Reports a Windows error without throwing an exception.
// Can be used to report errors from destructors. // Can be used to report errors from destructors.
FMT_API void report_windows_error(int error_code, FMT_API void report_windows_error(int error_code, const char* message) noexcept;
const char* message) FMT_NOEXCEPT;
#else #else
inline const std::error_category& system_category() FMT_NOEXCEPT { inline const std::error_category& system_category() noexcept {
return std::system_category(); return std::system_category();
} }
#endif // _WIN32 #endif // _WIN32
@@ -234,13 +234,13 @@ class buffered_file {
void operator=(const buffered_file&) = delete; void operator=(const buffered_file&) = delete;
// Constructs a buffered_file object which doesn't represent any file. // Constructs a buffered_file object which doesn't represent any file.
buffered_file() FMT_NOEXCEPT : file_(nullptr) {} buffered_file() noexcept : file_(nullptr) {}
// Destroys the object closing the file it represents if any. // Destroys the object closing the file it represents if any.
FMT_API ~buffered_file() FMT_NOEXCEPT; FMT_API ~buffered_file() noexcept;
public: public:
buffered_file(buffered_file&& other) FMT_NOEXCEPT : file_(other.file_) { buffered_file(buffered_file&& other) noexcept : file_(other.file_) {
other.file_ = nullptr; other.file_ = nullptr;
} }
@@ -258,11 +258,9 @@ class buffered_file {
FMT_API void close(); FMT_API void close();
// Returns the pointer to a FILE object representing this file. // Returns the pointer to a FILE object representing this file.
FILE* get() const FMT_NOEXCEPT { return file_; } FILE* get() const noexcept { return file_; }
// We place parentheses around fileno to workaround a bug in some versions FMT_API int descriptor() const;
// of MinGW that define fileno as a macro.
FMT_API int(fileno)() const;
void vprint(string_view format_str, format_args args) { void vprint(string_view format_str, format_args args) {
fmt::vprint(file_, format_str, args); fmt::vprint(file_, format_str, args);
@@ -276,12 +274,12 @@ class buffered_file {
#if FMT_USE_FCNTL #if FMT_USE_FCNTL
// A file. Closed file is represented by a file object with descriptor -1. // A file. Closed file is represented by a file object with descriptor -1.
// Methods that are not declared with FMT_NOEXCEPT may throw // Methods that are not declared with noexcept may throw
// fmt::system_error in case of failure. Note that some errors such as // fmt::system_error in case of failure. Note that some errors such as
// closing the file multiple times will cause a crash on Windows rather // closing the file multiple times will cause a crash on Windows rather
// than an exception. You can get standard behavior by overriding the // than an exception. You can get standard behavior by overriding the
// invalid parameter handler with _set_invalid_parameter_handler. // invalid parameter handler with _set_invalid_parameter_handler.
class file { class FMT_API file {
private: private:
int fd_; // File descriptor. int fd_; // File descriptor.
@@ -300,16 +298,16 @@ class file {
}; };
// Constructs a file object which doesn't represent any file. // Constructs a file object which doesn't represent any file.
file() FMT_NOEXCEPT : fd_(-1) {} file() noexcept : fd_(-1) {}
// Opens a file and constructs a file object representing this file. // Opens a file and constructs a file object representing this file.
FMT_API file(cstring_view path, int oflag); file(cstring_view path, int oflag);
public: public:
file(const file&) = delete; file(const file&) = delete;
void operator=(const file&) = delete; void operator=(const file&) = delete;
file(file&& other) FMT_NOEXCEPT : fd_(other.fd_) { other.fd_ = -1; } file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; }
// Move assignment is not noexcept because close may throw. // Move assignment is not noexcept because close may throw.
file& operator=(file&& other) { file& operator=(file&& other) {
@@ -320,43 +318,43 @@ class file {
} }
// Destroys the object closing the file it represents if any. // Destroys the object closing the file it represents if any.
FMT_API ~file() FMT_NOEXCEPT; ~file() noexcept;
// Returns the file descriptor. // Returns the file descriptor.
int descriptor() const FMT_NOEXCEPT { return fd_; } int descriptor() const noexcept { return fd_; }
// Closes the file. // Closes the file.
FMT_API void close(); void close();
// Returns the file size. The size has signed type for consistency with // Returns the file size. The size has signed type for consistency with
// stat::st_size. // stat::st_size.
FMT_API long long size() const; long long size() const;
// Attempts to read count bytes from the file into the specified buffer. // Attempts to read count bytes from the file into the specified buffer.
FMT_API size_t read(void* buffer, size_t count); size_t read(void* buffer, size_t count);
// Attempts to write count bytes from the specified buffer to the file. // Attempts to write count bytes from the specified buffer to the file.
FMT_API size_t write(const void* buffer, size_t count); size_t write(const void* buffer, size_t count);
// Duplicates a file descriptor with the dup function and returns // Duplicates a file descriptor with the dup function and returns
// the duplicate as a file object. // the duplicate as a file object.
FMT_API static file dup(int fd); static file dup(int fd);
// Makes fd be the copy of this file descriptor, closing fd first if // Makes fd be the copy of this file descriptor, closing fd first if
// necessary. // necessary.
FMT_API void dup2(int fd); void dup2(int fd);
// Makes fd be the copy of this file descriptor, closing fd first if // Makes fd be the copy of this file descriptor, closing fd first if
// necessary. // necessary.
FMT_API void dup2(int fd, std::error_code& ec) FMT_NOEXCEPT; void dup2(int fd, std::error_code& ec) noexcept;
// Creates a pipe setting up read_end and write_end file objects for reading // Creates a pipe setting up read_end and write_end file objects for reading
// and writing respectively. // and writing respectively.
FMT_API static void pipe(file& read_end, file& write_end); static void pipe(file& read_end, file& write_end);
// Creates a buffered_file object associated with this file and detaches // Creates a buffered_file object associated with this file and detaches
// this file object from the file. // this file object from the file.
FMT_API buffered_file fdopen(const char* mode); buffered_file fdopen(const char* mode);
}; };
// Returns the memory page size. // Returns the memory page size.
@@ -390,23 +388,26 @@ struct ostream_params {
: ostream_params(params...) { : ostream_params(params...) {
this->buffer_size = bs.value; this->buffer_size = bs.value;
} }
// Intel has a bug that results in failure to deduce a constructor
// for empty parameter packs.
# if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000
ostream_params(int new_oflag) : oflag(new_oflag) {}
ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {}
# endif
}; };
FMT_END_DETAIL_NAMESPACE FMT_END_DETAIL_NAMESPACE
constexpr detail::buffer_size buffer_size; // Added {} below to work around default constructor error known to
// occur in Xcode versions 7.2.1 and 8.2.1.
constexpr detail::buffer_size buffer_size{};
/** A fast output stream which is not thread-safe. */ /** A fast output stream which is not thread-safe. */
class FMT_API ostream final : private detail::buffer<char> { class FMT_API ostream final : private detail::buffer<char> {
private: private:
file file_; file file_;
void flush() {
if (size() == 0) return;
file_.write(data(), size());
clear();
}
void grow(size_t) override; void grow(size_t) override;
ostream(cstring_view path, const detail::ostream_params& params) ostream(cstring_view path, const detail::ostream_params& params)
@@ -426,6 +427,12 @@ class FMT_API ostream final : private detail::buffer<char> {
delete[] data(); delete[] data();
} }
void flush() {
if (size() == 0) return;
file_.write(data(), size());
clear();
}
template <typename... T> template <typename... T>
friend ostream output_file(cstring_view path, T... params); friend ostream output_file(cstring_view path, T... params);
@@ -450,7 +457,7 @@ class FMT_API ostream final : private detail::buffer<char> {
* ``<integer>``: Flags passed to `open * ``<integer>``: Flags passed to `open
<https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html>`_ <https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html>`_
(``file::WRONLY | file::CREATE`` by default) (``file::WRONLY | file::CREATE | file::TRUNC`` by default)
* ``buffer_size=<integer>``: Output buffer size * ``buffer_size=<integer>``: Output buffer size
**Example**:: **Example**::
@@ -465,50 +472,6 @@ inline ostream output_file(cstring_view path, T... params) {
} }
#endif // FMT_USE_FCNTL #endif // FMT_USE_FCNTL
#ifdef FMT_LOCALE
// A "C" numeric locale.
class locale {
private:
# ifdef _WIN32
using locale_t = _locale_t;
static void freelocale(locale_t loc) { _free_locale(loc); }
static double strtod_l(const char* nptr, char** endptr, _locale_t loc) {
return _strtod_l(nptr, endptr, loc);
}
# endif
locale_t locale_;
public:
using type = locale_t;
locale(const locale&) = delete;
void operator=(const locale&) = delete;
locale() {
# ifndef _WIN32
locale_ = FMT_SYSTEM(newlocale(LC_NUMERIC_MASK, "C", nullptr));
# else
locale_ = _create_locale(LC_NUMERIC, "C");
# endif
if (!locale_) FMT_THROW(system_error(errno, "cannot create locale"));
}
~locale() { freelocale(locale_); }
type get() const { return locale_; }
// Converts string to floating-point number and advances str past the end
// of the parsed input.
double strtod(const char*& str) const {
char* end = nullptr;
double result = strtod_l(str, &end, locale_);
str = end;
return result;
}
};
using Locale FMT_DEPRECATED_ALIAS = locale;
#endif // FMT_LOCALE
FMT_MODULE_EXPORT_END FMT_MODULE_EXPORT_END
FMT_END_NAMESPACE FMT_END_NAMESPACE

View File

@@ -8,79 +8,33 @@
#ifndef FMT_OSTREAM_H_ #ifndef FMT_OSTREAM_H_
#define FMT_OSTREAM_H_ #define FMT_OSTREAM_H_
#include <fstream>
#include <ostream> #include <ostream>
#if defined(_WIN32) && defined(__GLIBCXX__)
# include <ext/stdio_filebuf.h>
# include <ext/stdio_sync_filebuf.h>
#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
# include <__std_stream>
#endif
#include "format.h" #include "format.h"
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
template <typename Char> class basic_printf_parse_context;
template <typename OutputIt, typename Char> class basic_printf_context; template <typename OutputIt, typename Char> class basic_printf_context;
namespace detail { namespace detail {
template <class Char> class formatbuf : public std::basic_streambuf<Char> { // Checks if T has a user-defined operator<<.
private: template <typename T, typename Char, typename Enable = void>
using int_type = typename std::basic_streambuf<Char>::int_type; class is_streamable {
using traits_type = typename std::basic_streambuf<Char>::traits_type;
buffer<Char>& buffer_;
public:
formatbuf(buffer<Char>& buf) : buffer_(buf) {}
protected:
// The put-area is actually always empty. This makes the implementation
// simpler and has the advantage that the streambuf and the buffer are always
// in sync and sputc never writes into uninitialized memory. The obvious
// disadvantage is that each call to sputc always results in a (virtual) call
// to overflow. There is no disadvantage here for sputn since this always
// results in a call to xsputn.
int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
if (!traits_type::eq_int_type(ch, traits_type::eof()))
buffer_.push_back(static_cast<Char>(ch));
return ch;
}
std::streamsize xsputn(const Char* s, std::streamsize count) FMT_OVERRIDE {
buffer_.append(s, s + count);
return count;
}
};
struct converter {
template <typename T, FMT_ENABLE_IF(is_integral<T>::value)> converter(T);
};
template <typename Char> struct test_stream : std::basic_ostream<Char> {
private:
void_t<> operator<<(converter);
};
// Hide insertion operators for built-in types.
template <typename Char, typename Traits>
void_t<> operator<<(std::basic_ostream<Char, Traits>&, Char);
template <typename Char, typename Traits>
void_t<> operator<<(std::basic_ostream<Char, Traits>&, char);
template <typename Traits>
void_t<> operator<<(std::basic_ostream<char, Traits>&, char);
template <typename Traits>
void_t<> operator<<(std::basic_ostream<char, Traits>&, signed char);
template <typename Traits>
void_t<> operator<<(std::basic_ostream<char, Traits>&, unsigned char);
// Checks if T has a user-defined operator<< (e.g. not a member of
// std::ostream).
template <typename T, typename Char> class is_streamable {
private: private:
template <typename U> template <typename U>
static bool_constant<!std::is_same<decltype(std::declval<test_stream<Char>&>() static auto test(int)
<< std::declval<U>()), -> bool_constant<sizeof(std::declval<std::basic_ostream<Char>&>()
void_t<>>::value> << std::declval<U>()) != 0>;
test(int);
template <typename> static std::false_type test(...); template <typename> static auto test(...) -> std::false_type;
using result = decltype(test<T>(0)); using result = decltype(test<T>(0));
@@ -90,7 +44,68 @@ template <typename T, typename Char> class is_streamable {
static const bool value = result::value; static const bool value = result::value;
}; };
// Formatting of built-in types and arrays is intentionally disabled because
// it's handled by standard (non-ostream) formatters.
template <typename T, typename Char>
struct is_streamable<
T, Char,
enable_if_t<
std::is_arithmetic<T>::value || std::is_array<T>::value ||
std::is_pointer<T>::value || std::is_same<T, char8_type>::value ||
std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
std::is_same<T, std_string_view<Char>>::value ||
(std::is_convertible<T, int>::value && !std::is_enum<T>::value)>>
: std::false_type {};
// Generate a unique explicit instantion in every translation unit using a tag
// type in an anonymous namespace.
namespace {
struct file_access_tag {};
} // namespace
template <class Tag, class BufType, FILE* BufType::*FileMemberPtr>
class file_access {
friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
};
#if FMT_MSC_VERSION
template class file_access<file_access_tag, std::filebuf,
&std::filebuf::_Myfile>;
auto get_file(std::filebuf&) -> FILE*;
#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
template class file_access<file_access_tag, std::__stdoutbuf<char>,
&std::__stdoutbuf<char>::__file_>;
auto get_file(std::__stdoutbuf<char>&) -> FILE*;
#endif
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
#if FMT_MSC_VERSION
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
if (FILE* f = get_file(*buf)) return write_console(f, data);
#elif defined(_WIN32) && defined(__GLIBCXX__)
auto* rdbuf = os.rdbuf();
FILE* c_file;
if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
c_file = fbuf->file();
else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
c_file = fbuf->file();
else
return false;
if (c_file) return write_console(c_file, data);
#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
if (auto* buf = dynamic_cast<std::__stdoutbuf<char>*>(os.rdbuf()))
if (FILE* f = get_file(*buf)) return write_console(f, data);
#else
ignore_unused(os, data);
#endif
return false;
}
inline bool write_ostream_unicode(std::wostream&,
fmt::basic_string_view<wchar_t>) {
return false;
}
// Write the content of buf to os. // Write the content of buf to os.
// It is a separate function rather than a part of vprint to simplify testing.
template <typename Char> template <typename Char>
void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) { void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
const Char* buf_data = buf.data(); const Char* buf_data = buf.data();
@@ -108,55 +123,86 @@ void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
template <typename Char, typename T> template <typename Char, typename T>
void format_value(buffer<Char>& buf, const T& value, void format_value(buffer<Char>& buf, const T& value,
locale_ref loc = locale_ref()) { locale_ref loc = locale_ref()) {
formatbuf<Char> format_buf(buf); auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
std::basic_ostream<Char> output(&format_buf); auto&& output = std::basic_ostream<Char>(&format_buf);
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
if (loc) output.imbue(loc.get<std::locale>()); if (loc) output.imbue(loc.get<std::locale>());
#endif #endif
output << value; output << value;
output.exceptions(std::ios_base::failbit | std::ios_base::badbit); output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
buf.try_resize(buf.size());
} }
template <typename T> struct streamed_view { const T& value; };
} // namespace detail
// Formats an object of type T that has an overloaded ostream operator<<.
template <typename Char>
struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
void set_debug_format() = delete;
template <typename T, typename OutputIt>
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
-> OutputIt {
auto buffer = basic_memory_buffer<Char>();
format_value(buffer, value, ctx.locale());
return formatter<basic_string_view<Char>, Char>::format(
{buffer.data(), buffer.size()}, ctx);
}
};
using ostream_formatter = basic_ostream_formatter<char>;
template <typename T, typename Char>
struct formatter<detail::streamed_view<T>, Char>
: basic_ostream_formatter<Char> {
template <typename OutputIt>
auto format(detail::streamed_view<T> view,
basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
return basic_ostream_formatter<Char>::format(view.value, ctx);
}
};
/**
\rst
Returns a view that formats `value` via an ostream ``operator<<``.
**Example**::
fmt::print("Current thread id: {}\n",
fmt::streamed(std::this_thread::get_id()));
\endrst
*/
template <typename T>
auto streamed(const T& value) -> detail::streamed_view<T> {
return {value};
}
namespace detail {
// Formats an object of type T that has an overloaded ostream operator<<. // Formats an object of type T that has an overloaded ostream operator<<.
template <typename T, typename Char> template <typename T, typename Char>
struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>> struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
: private formatter<basic_string_view<Char>, Char> { : basic_ostream_formatter<Char> {
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx) using basic_ostream_formatter<Char>::format;
-> decltype(ctx.begin()) {
return formatter<basic_string_view<Char>, Char>::parse(ctx);
}
template <typename ParseCtx,
FMT_ENABLE_IF(std::is_same<
ParseCtx, basic_printf_parse_context<Char>>::value)>
auto parse(ParseCtx& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
template <typename OutputIt>
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx)
-> OutputIt {
basic_memory_buffer<Char> buffer;
format_value(buffer, value, ctx.locale());
basic_string_view<Char> str(buffer.data(), buffer.size());
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
}
template <typename OutputIt>
auto format(const T& value, basic_printf_context<OutputIt, Char>& ctx)
-> OutputIt {
basic_memory_buffer<Char> buffer;
format_value(buffer, value, ctx.locale());
return std::copy(buffer.begin(), buffer.end(), ctx.out());
}
}; };
inline void vprint_directly(std::ostream& os, string_view format_str,
format_args args) {
auto buffer = memory_buffer();
detail::vformat_to(buffer, format_str, args);
detail::write_buffer(os, buffer);
}
} // namespace detail } // namespace detail
FMT_MODULE_EXPORT FMT_MODULE_EXPORT template <typename Char>
template <typename Char> void vprint(std::basic_ostream<Char>& os,
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str, basic_string_view<type_identity_t<Char>> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args) {
basic_memory_buffer<Char> buffer; auto buffer = basic_memory_buffer<Char>();
detail::vformat_to(buffer, format_str, args); detail::vformat_to(buffer, format_str, args);
if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return;
detail::write_buffer(os, buffer); detail::write_buffer(os, buffer);
} }
@@ -169,13 +215,23 @@ void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
fmt::print(cerr, "Don't {}!", "panic"); fmt::print(cerr, "Don't {}!", "panic");
\endrst \endrst
*/ */
FMT_MODULE_EXPORT FMT_MODULE_EXPORT template <typename... T>
template <typename S, typename... Args, void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>> const auto& vargs = fmt::make_format_args(args...);
void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) { if (detail::is_utf8())
vprint(os, to_string_view(format_str), vprint(os, fmt, vargs);
fmt::make_args_checked<Args...>(format_str, args...)); else
detail::vprint_directly(os, fmt, vargs);
} }
FMT_MODULE_EXPORT
template <typename... Args>
void print(std::wostream& os,
basic_format_string<wchar_t, type_identity_t<Args>...> fmt,
Args&&... args) {
vprint(os, fmt, fmt::make_format_args<buffer_context<wchar_t>>(args...));
}
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif // FMT_OSTREAM_H_ #endif // FMT_OSTREAM_H_

View File

@@ -10,7 +10,6 @@
#include <algorithm> // std::max #include <algorithm> // std::max
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
#include <ostream>
#include "format.h" #include "format.h"
@@ -233,7 +232,7 @@ class printf_arg_formatter : public arg_formatter<Char> {
OutputIt write_null_pointer(bool is_string = false) { OutputIt write_null_pointer(bool is_string = false) {
auto s = this->specs; auto s = this->specs;
s.type = 0; s.type = presentation_type::none;
return write_bytes(this->out, is_string ? "(null)" : "(nil)", s); return write_bytes(this->out, is_string ? "(null)" : "(nil)", s);
} }
@@ -249,8 +248,10 @@ class printf_arg_formatter : public arg_formatter<Char> {
// std::is_same instead. // std::is_same instead.
if (std::is_same<T, Char>::value) { if (std::is_same<T, Char>::value) {
format_specs fmt_specs = this->specs; format_specs fmt_specs = this->specs;
if (fmt_specs.type && fmt_specs.type != 'c') if (fmt_specs.type != presentation_type::none &&
fmt_specs.type != presentation_type::chr) {
return (*this)(static_cast<int>(value)); return (*this)(static_cast<int>(value));
}
fmt_specs.sign = sign::none; fmt_specs.sign = sign::none;
fmt_specs.alt = false; fmt_specs.alt = false;
fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types. fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types.
@@ -271,13 +272,13 @@ class printf_arg_formatter : public arg_formatter<Char> {
/** Formats a null-terminated C string. */ /** Formats a null-terminated C string. */
OutputIt operator()(const char* value) { OutputIt operator()(const char* value) {
if (value) return base::operator()(value); if (value) return base::operator()(value);
return write_null_pointer(this->specs.type != 'p'); return write_null_pointer(this->specs.type != presentation_type::pointer);
} }
/** Formats a null-terminated wide C string. */ /** Formats a null-terminated wide C string. */
OutputIt operator()(const wchar_t* value) { OutputIt operator()(const wchar_t* value) {
if (value) return base::operator()(value); if (value) return base::operator()(value);
return write_null_pointer(this->specs.type != 'p'); return write_null_pointer(this->specs.type != presentation_type::pointer);
} }
OutputIt operator()(basic_string_view<Char> value) { OutputIt operator()(basic_string_view<Char> value) {
@@ -490,13 +491,13 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
// Parse type. // Parse type.
if (it == end) FMT_THROW(format_error("invalid format string")); if (it == end) FMT_THROW(format_error("invalid format string"));
specs.type = static_cast<char>(*it++); char type = static_cast<char>(*it++);
if (arg.is_integral()) { if (arg.is_integral()) {
// Normalize type. // Normalize type.
switch (specs.type) { switch (type) {
case 'i': case 'i':
case 'u': case 'u':
specs.type = 'd'; type = 'd';
break; break;
case 'c': case 'c':
visit_format_arg( visit_format_arg(
@@ -505,6 +506,9 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
break; break;
} }
} }
specs.type = parse_presentation_type(type);
if (specs.type == presentation_type::none)
parse_ctx.on_error("invalid type specifier");
start = it; start = it;
@@ -556,7 +560,7 @@ inline auto vsprintf(
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args) basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
vprintf(buffer, to_string_view(fmt), args); vprintf(buffer, detail::to_string_view(fmt), args);
return to_string(buffer); return to_string(buffer);
} }
@@ -573,7 +577,8 @@ template <typename S, typename... T,
typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>> typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>>
inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> { inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> {
using context = basic_printf_context_t<Char>; using context = basic_printf_context_t<Char>;
return vsprintf(to_string_view(fmt), fmt::make_format_args<context>(args...)); return vsprintf(detail::to_string_view(fmt),
fmt::make_format_args<context>(args...));
} }
template <typename S, typename Char = char_t<S>> template <typename S, typename Char = char_t<S>>
@@ -582,7 +587,7 @@ inline auto vfprintf(
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args) basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
-> int { -> int {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
vprintf(buffer, to_string_view(fmt), args); vprintf(buffer, detail::to_string_view(fmt), args);
size_t size = buffer.size(); size_t size = buffer.size();
return std::fwrite(buffer.data(), sizeof(Char), size, f) < size return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
? -1 ? -1
@@ -601,7 +606,7 @@ inline auto vfprintf(
template <typename S, typename... T, typename Char = char_t<S>> template <typename S, typename... T, typename Char = char_t<S>>
inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int { inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int {
using context = basic_printf_context_t<Char>; using context = basic_printf_context_t<Char>;
return vfprintf(f, to_string_view(fmt), return vfprintf(f, detail::to_string_view(fmt),
fmt::make_format_args<context>(args...)); fmt::make_format_args<context>(args...));
} }
@@ -610,7 +615,7 @@ inline auto vprintf(
const S& fmt, const S& fmt,
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args) basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
-> int { -> int {
return vfprintf(stdout, to_string_view(fmt), args); return vfprintf(stdout, detail::to_string_view(fmt), args);
} }
/** /**
@@ -625,27 +630,10 @@ inline auto vprintf(
template <typename S, typename... T, FMT_ENABLE_IF(detail::is_string<S>::value)> template <typename S, typename... T, FMT_ENABLE_IF(detail::is_string<S>::value)>
inline auto printf(const S& fmt, const T&... args) -> int { inline auto printf(const S& fmt, const T&... args) -> int {
return vprintf( return vprintf(
to_string_view(fmt), detail::to_string_view(fmt),
fmt::make_format_args<basic_printf_context_t<char_t<S>>>(args...)); fmt::make_format_args<basic_printf_context_t<char_t<S>>>(args...));
} }
template <typename S, typename Char = char_t<S>>
FMT_DEPRECATED auto vfprintf(
std::basic_ostream<Char>& os, const S& fmt,
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
-> int {
basic_memory_buffer<Char> buffer;
vprintf(buffer, to_string_view(fmt), args);
os.write(buffer.data(), static_cast<std::streamsize>(buffer.size()));
return static_cast<int>(buffer.size());
}
template <typename S, typename... T, typename Char = char_t<S>>
FMT_DEPRECATED auto fprintf(std::basic_ostream<Char>& os, const S& fmt,
const T&... args) -> int {
return vfprintf(os, to_string_view(fmt),
fmt::make_format_args<basic_printf_context_t<Char>>(args...));
}
FMT_MODULE_EXPORT_END FMT_MODULE_EXPORT_END
FMT_END_NAMESPACE FMT_END_NAMESPACE

File diff suppressed because it is too large Load Diff

View File

@@ -5,11 +5,10 @@
// //
// For the license information refer to format.h. // For the license information refer to format.h.
#ifndef FMT_WCHAR_H_ #ifndef FMT_XCHAR_H_
#define FMT_WCHAR_H_ #define FMT_XCHAR_H_
#include <cwchar> #include <cwchar>
#include <tuple>
#include "format.h" #include "format.h"
@@ -30,9 +29,11 @@ using wmemory_buffer = basic_memory_buffer<wchar_t>;
#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
// Workaround broken conversion on older gcc. // Workaround broken conversion on older gcc.
template <typename... Args> using wformat_string = wstring_view; template <typename... Args> using wformat_string = wstring_view;
inline auto runtime(wstring_view s) -> wstring_view { return s; }
#else #else
template <typename... Args> template <typename... Args>
using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>; using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
inline auto runtime(wstring_view s) -> basic_runtime<wchar_t> { return {{s}}; }
#endif #endif
template <> struct is_char<wchar_t> : std::true_type {}; template <> struct is_char<wchar_t> : std::true_type {};
@@ -47,12 +48,7 @@ constexpr format_arg_store<wformat_context, Args...> make_wformat_args(
} }
inline namespace literals { inline namespace literals {
constexpr auto operator"" _format(const wchar_t* s, size_t n) #if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
-> detail::udl_formatter<wchar_t> {
return {{s, n}};
}
#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) { constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
return {s}; return {s};
} }
@@ -87,13 +83,19 @@ auto vformat(basic_string_view<Char> format_str,
return to_string(buffer); return to_string(buffer);
} }
template <typename... T>
auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
}
// Pass char_t as a default template parameter instead of using // Pass char_t as a default template parameter instead of using
// std::basic_string<char_t<S>> to reduce the symbol size. // std::basic_string<char_t<S>> to reduce the symbol size.
template <typename S, typename... Args, typename Char = char_t<S>, template <typename S, typename... Args, typename Char = char_t<S>,
FMT_ENABLE_IF(!std::is_same<Char, char>::value)> FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
!std::is_same<Char, wchar_t>::value)>
auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> { auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> {
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...); return vformat(detail::to_string_view(format_str),
return vformat(to_string_view(format_str), vargs); fmt::make_format_args<buffer_context<Char>>(args...));
} }
template <typename Locale, typename S, typename Char = char_t<S>, template <typename Locale, typename S, typename Char = char_t<S>,
@@ -103,7 +105,7 @@ inline auto vformat(
const Locale& loc, const S& format_str, const Locale& loc, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
return detail::vformat(loc, to_string_view(format_str), args); return detail::vformat(loc, detail::to_string_view(format_str), args);
} }
template <typename Locale, typename S, typename... Args, template <typename Locale, typename S, typename... Args,
@@ -112,8 +114,8 @@ template <typename Locale, typename S, typename... Args,
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline auto format(const Locale& loc, const S& format_str, Args&&... args) inline auto format(const Locale& loc, const S& format_str, Args&&... args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
return detail::vformat(loc, to_string_view(format_str), return detail::vformat(loc, detail::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str, args...)); fmt::make_format_args<buffer_context<Char>>(args...));
} }
template <typename OutputIt, typename S, typename Char = char_t<S>, template <typename OutputIt, typename S, typename Char = char_t<S>,
@@ -123,7 +125,7 @@ auto vformat_to(OutputIt out, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> OutputIt { -> OutputIt {
auto&& buf = detail::get_buffer<Char>(out); auto&& buf = detail::get_buffer<Char>(out);
detail::vformat_to(buf, to_string_view(format_str), args); detail::vformat_to(buf, detail::to_string_view(format_str), args);
return detail::get_iterator(buf); return detail::get_iterator(buf);
} }
@@ -132,18 +134,8 @@ template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt { inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt {
const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...); return vformat_to(out, detail::to_string_view(fmt),
return vformat_to(out, to_string_view(fmt), vargs); fmt::make_format_args<buffer_context<Char>>(args...));
}
template <typename S, typename... Args, typename Char, size_t SIZE,
typename Allocator, FMT_ENABLE_IF(detail::is_string<S>::value)>
FMT_DEPRECATED auto format_to(basic_memory_buffer<Char, SIZE, Allocator>& buf,
const S& format_str, Args&&... args) ->
typename buffer_context<Char>::iterator {
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
detail::vformat_to(buf, to_string_view(format_str), vargs, {});
return detail::buffer_appender<Char>(buf);
} }
template <typename Locale, typename S, typename OutputIt, typename... Args, template <typename Locale, typename S, typename OutputIt, typename... Args,
@@ -155,7 +147,8 @@ inline auto vformat_to(
OutputIt out, const Locale& loc, const S& format_str, OutputIt out, const Locale& loc, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt { basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
auto&& buf = detail::get_buffer<Char>(out); auto&& buf = detail::get_buffer<Char>(out);
vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc)); vformat_to(buf, detail::to_string_view(format_str), args,
detail::locale_ref(loc));
return detail::get_iterator(buf); return detail::get_iterator(buf);
} }
@@ -167,8 +160,8 @@ template <
inline auto format_to(OutputIt out, const Locale& loc, const S& format_str, inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
Args&&... args) -> Args&&... args) ->
typename std::enable_if<enable, OutputIt>::type { typename std::enable_if<enable, OutputIt>::type {
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...); return vformat_to(out, loc, to_string_view(format_str),
return vformat_to(out, loc, to_string_view(format_str), vargs); fmt::make_format_args<buffer_context<Char>>(args...));
} }
template <typename OutputIt, typename Char, typename... Args, template <typename OutputIt, typename Char, typename... Args,
@@ -190,16 +183,16 @@ template <typename OutputIt, typename S, typename... Args,
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline auto format_to_n(OutputIt out, size_t n, const S& fmt, inline auto format_to_n(OutputIt out, size_t n, const S& fmt,
const Args&... args) -> format_to_n_result<OutputIt> { const Args&... args) -> format_to_n_result<OutputIt> {
const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...); return vformat_to_n(out, n, detail::to_string_view(fmt),
return vformat_to_n(out, n, to_string_view(fmt), vargs); fmt::make_format_args<buffer_context<Char>>(args...));
} }
template <typename S, typename... Args, typename Char = char_t<S>, template <typename S, typename... Args, typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)> FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
inline auto formatted_size(const S& fmt, Args&&... args) -> size_t { inline auto formatted_size(const S& fmt, Args&&... args) -> size_t {
detail::counting_buffer<Char> buf; detail::counting_buffer<Char> buf;
const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...); detail::vformat_to(buf, detail::to_string_view(fmt),
detail::vformat_to(buf, to_string_view(fmt), vargs); fmt::make_format_args<buffer_context<Char>>(args...));
return buf.count(); return buf.count();
} }
@@ -217,11 +210,11 @@ inline void vprint(wstring_view fmt, wformat_args args) {
template <typename... T> template <typename... T>
void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) { void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
return vprint(f, wstring_view(fmt), make_wformat_args(args...)); return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
} }
template <typename... T> void print(wformat_string<T...> fmt, T&&... args) { template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
return vprint(wstring_view(fmt), make_wformat_args(args...)); return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
} }
/** /**
@@ -233,4 +226,4 @@ template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
FMT_MODULE_EXPORT_END FMT_MODULE_EXPORT_END
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif // FMT_WCHAR_H_ #endif // FMT_XCHAR_H_

View File

@@ -8,13 +8,15 @@
// include bundled or external copy of fmtlib's chrono support // include bundled or external copy of fmtlib's chrono support
// //
#if !defined(SPDLOG_FMT_EXTERNAL) #if !defined(SPDLOG_USE_STD_FORMAT)
# ifdef SPDLOG_HEADER_ONLY # if !defined(SPDLOG_FMT_EXTERNAL)
# ifndef FMT_HEADER_ONLY # ifdef SPDLOG_HEADER_ONLY
# define FMT_HEADER_ONLY # ifndef FMT_HEADER_ONLY
# define FMT_HEADER_ONLY
# endif
# endif # endif
# include <spdlog/fmt/bundled/chrono.h>
# else
# include <fmt/chrono.h>
# endif # endif
# include <spdlog/fmt/bundled/chrono.h>
#else
# include <fmt/chrono.h>
#endif #endif

View File

@@ -5,16 +5,18 @@
#pragma once #pragma once
// //
// include bundled or external copy of fmtlib's ostream support // include bundled or external copy of fmtlib's compile-time support
// //
#if !defined(SPDLOG_FMT_EXTERNAL) #if !defined(SPDLOG_USE_STD_FORMAT)
# ifdef SPDLOG_HEADER_ONLY # if !defined(SPDLOG_FMT_EXTERNAL)
# ifndef FMT_HEADER_ONLY # ifdef SPDLOG_HEADER_ONLY
# define FMT_HEADER_ONLY # ifndef FMT_HEADER_ONLY
# define FMT_HEADER_ONLY
# endif
# endif # endif
# include <spdlog/fmt/bundled/compile.h>
# else
# include <fmt/compile.h>
# endif # endif
# include <spdlog/fmt/bundled/compile.h>
#else
# include <fmt/compile.h>
#endif #endif

View File

@@ -10,7 +10,9 @@
// By default spdlog include its own copy. // By default spdlog include its own copy.
// //
#if !defined(SPDLOG_FMT_EXTERNAL) #if defined(SPDLOG_USE_STD_FORMAT) // SPDLOG_USE_STD_FORMAT is defined - use std::format
# include <format>
#elif !defined(SPDLOG_FMT_EXTERNAL)
# if !defined(SPDLOG_COMPILED_LIB) && !defined(FMT_HEADER_ONLY) # if !defined(SPDLOG_COMPILED_LIB) && !defined(FMT_HEADER_ONLY)
# define FMT_HEADER_ONLY # define FMT_HEADER_ONLY
# endif # endif
@@ -19,8 +21,12 @@
# endif # endif
// enable the 'n' flag in for backward compatibility with fmt 6.x // enable the 'n' flag in for backward compatibility with fmt 6.x
# define FMT_DEPRECATED_N_SPECIFIER # define FMT_DEPRECATED_N_SPECIFIER
// enable ostream formatting for backward compatibility with fmt 8.x
# define FMT_DEPRECATED_OSTREAM
# include <spdlog/fmt/bundled/core.h> # include <spdlog/fmt/bundled/core.h>
# include <spdlog/fmt/bundled/format.h> # include <spdlog/fmt/bundled/format.h>
#else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib #else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib
# include <fmt/core.h> # include <fmt/core.h>
# include <fmt/format.h> # include <fmt/format.h>

View File

@@ -8,13 +8,15 @@
// include bundled or external copy of fmtlib's ostream support // include bundled or external copy of fmtlib's ostream support
// //
#if !defined(SPDLOG_FMT_EXTERNAL) #if !defined(SPDLOG_USE_STD_FORMAT)
# ifdef SPDLOG_HEADER_ONLY # if !defined(SPDLOG_FMT_EXTERNAL)
# ifndef FMT_HEADER_ONLY # ifdef SPDLOG_HEADER_ONLY
# define FMT_HEADER_ONLY # ifndef FMT_HEADER_ONLY
# define FMT_HEADER_ONLY
# endif
# endif # endif
# include <spdlog/fmt/bundled/ostream.h>
# else
# include <fmt/ostream.h>
# endif # endif
# include <spdlog/fmt/bundled/ostream.h>
#else
# include <fmt/ostream.h>
#endif #endif

22
thirdparty/spdlog/fmt/ranges.h vendored Normal file
View File

@@ -0,0 +1,22 @@
//
// Copyright(c) 2016 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
//
// include bundled or external copy of fmtlib's ranges support
//
#if !defined(SPDLOG_USE_STD_FORMAT)
# if !defined(SPDLOG_FMT_EXTERNAL)
# ifdef SPDLOG_HEADER_ONLY
# ifndef FMT_HEADER_ONLY
# define FMT_HEADER_ONLY
# endif
# endif
# include <spdlog/fmt/bundled/ranges.h>
# else
# include <fmt/ranges.h>
# endif
#endif

View File

@@ -5,16 +5,18 @@
#pragma once #pragma once
// //
// include bundled or external copy of fmtlib's ostream support // include bundled or external copy of fmtlib's xchar support
// //
#if !defined(SPDLOG_FMT_EXTERNAL) #if !defined(SPDLOG_USE_STD_FORMAT)
# ifdef SPDLOG_HEADER_ONLY # if !defined(SPDLOG_FMT_EXTERNAL)
# ifndef FMT_HEADER_ONLY # ifdef SPDLOG_HEADER_ONLY
# define FMT_HEADER_ONLY # ifndef FMT_HEADER_ONLY
# define FMT_HEADER_ONLY
# endif
# endif # endif
# include <spdlog/fmt/bundled/xchar.h>
# else
# include <fmt/xchar.h>
# endif # endif
# include <spdlog/fmt/bundled/xchar.h>
#else
# include <fmt/xchar.h>
#endif #endif

View File

@@ -11,4 +11,8 @@ namespace sinks {
class sink; class sink;
} }
namespace level {
enum level_enum : int;
}
} // namespace spdlog } // namespace spdlog

View File

@@ -185,7 +185,7 @@ SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
{ {
sink->log(msg); sink->log(msg);
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(msg.source)
} }
} }
@@ -203,7 +203,7 @@ SPDLOG_INLINE void logger::flush_()
{ {
sink->flush(); sink->flush();
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(source_loc())
} }
} }

View File

@@ -28,10 +28,17 @@
#include <vector> #include <vector>
#ifndef SPDLOG_NO_EXCEPTIONS #ifndef SPDLOG_NO_EXCEPTIONS
# define SPDLOG_LOGGER_CATCH() \ # define SPDLOG_LOGGER_CATCH(location) \
catch (const std::exception &ex) \ catch (const std::exception &ex) \
{ \ { \
err_handler_(ex.what()); \ if (location.filename) \
{ \
err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), location.filename, location.line)); \
} \
else \
{ \
err_handler_(ex.what()); \
} \
} \ } \
catch (...) \ catch (...) \
{ \ { \
@@ -39,7 +46,7 @@
throw; \ throw; \
} }
#else #else
# define SPDLOG_LOGGER_CATCH() # define SPDLOG_LOGGER_CATCH(location)
#endif #endif
namespace spdlog { namespace spdlog {
@@ -78,13 +85,13 @@ public:
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
template<typename... Args> template<typename... Args>
void log(source_loc loc, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args) void log(source_loc loc, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{ {
log_(loc, lvl, fmt, std::forward<Args>(args)...); log_(loc, lvl, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args) void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{ {
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...); log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
} }
@@ -95,14 +102,7 @@ public:
log(source_loc{}, lvl, msg); log(source_loc{}, lvl, msg);
} }
// T can be statically converted to string_view // T cannot be statically converted to format string (including string_view/wstring_view)
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, int>::type = 0>
void log(source_loc loc, level::level_enum lvl, const T &msg)
{
log(loc, lvl, string_view_t{msg});
}
// T cannot be statically converted to format string (including string_view)
template<class T, typename std::enable_if<!is_convertible_to_any_format_string<const T &>::value, int>::type = 0> template<class T, typename std::enable_if<!is_convertible_to_any_format_string<const T &>::value, int>::type = 0>
void log(source_loc loc, level::level_enum lvl, const T &msg) void log(source_loc loc, level::level_enum lvl, const T &msg)
{ {
@@ -141,86 +141,121 @@ public:
} }
template<typename... Args> template<typename... Args>
void trace(fmt::format_string<Args...> fmt, Args &&...args) void trace(format_string_t<Args...> fmt, Args &&... args)
{ {
log(level::trace, fmt, std::forward<Args>(args)...); log(level::trace, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void debug(fmt::format_string<Args...> fmt, Args &&...args) void debug(format_string_t<Args...> fmt, Args &&... args)
{ {
log(level::debug, fmt, std::forward<Args>(args)...); log(level::debug, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void info(fmt::format_string<Args...> fmt, Args &&...args) void info(format_string_t<Args...> fmt, Args &&... args)
{ {
log(level::info, fmt, std::forward<Args>(args)...); log(level::info, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void warn(fmt::format_string<Args...> fmt, Args &&...args) void warn(format_string_t<Args...> fmt, Args &&... args)
{ {
log(level::warn, fmt, std::forward<Args>(args)...); log(level::warn, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void error(fmt::format_string<Args...> fmt, Args &&...args) void error(format_string_t<Args...> fmt, Args &&... args)
{ {
log(level::err, fmt, std::forward<Args>(args)...); log(level::err, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void critical(fmt::format_string<Args...> fmt, Args &&...args) void critical(format_string_t<Args...> fmt, Args &&... args)
{ {
log(level::critical, fmt, std::forward<Args>(args)...); log(level::critical, fmt, std::forward<Args>(args)...);
} }
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args> template<typename... Args>
void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args) void log(source_loc loc, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
{
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
{ {
log_(loc, lvl, fmt, std::forward<Args>(args)...); log_(loc, lvl, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void trace(fmt::wformat_string<Args...> fmt, Args &&...args) void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
{
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, wstring_view_t msg)
{
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled)
{
return;
}
memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(msg.data(), msg.size()), buf);
details::log_msg log_msg(log_time, loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg, log_enabled, traceback_enabled);
}
void log(source_loc loc, level::level_enum lvl, wstring_view_t msg)
{
bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled)
{
return;
}
memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(msg.data(), msg.size()), buf);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg, log_enabled, traceback_enabled);
}
void log(level::level_enum lvl, wstring_view_t msg)
{
log(source_loc{}, lvl, msg);
}
template<typename... Args>
void trace(wformat_string_t<Args...> fmt, Args &&... args)
{ {
log(level::trace, fmt, std::forward<Args>(args)...); log(level::trace, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void debug(fmt::wformat_string<Args...> fmt, Args &&...args) void debug(wformat_string_t<Args...> fmt, Args &&... args)
{ {
log(level::debug, fmt, std::forward<Args>(args)...); log(level::debug, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void info(fmt::wformat_string<Args...> fmt, Args &&...args) void info(wformat_string_t<Args...> fmt, Args &&... args)
{ {
log(level::info, fmt, std::forward<Args>(args)...); log(level::info, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void warn(fmt::wformat_string<Args...> fmt, Args &&...args) void warn(wformat_string_t<Args...> fmt, Args &&... args)
{ {
log(level::warn, fmt, std::forward<Args>(args)...); log(level::warn, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void error(fmt::wformat_string<Args...> fmt, Args &&...args) void error(wformat_string_t<Args...> fmt, Args &&... args)
{ {
log(level::err, fmt, std::forward<Args>(args)...); log(level::err, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
void critical(fmt::wformat_string<Args...> fmt, Args &&...args) void critical(wformat_string_t<Args...> fmt, Args &&... args)
{ {
log(level::critical, fmt, std::forward<Args>(args)...); log(level::critical, fmt, std::forward<Args>(args)...);
} }
@@ -284,6 +319,10 @@ public:
// each sink will get a separate instance of the formatter object. // each sink will get a separate instance of the formatter object.
void set_formatter(std::unique_ptr<formatter> f); void set_formatter(std::unique_ptr<formatter> f);
// set formatting for the sinks in this logger.
// equivalent to
// set_formatter(make_unique<pattern_formatter>(pattern, time_type))
// Note: each sink will get a new instance of a formatter object, replacing the old one.
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
// backtrace support. // backtrace support.
@@ -318,7 +357,7 @@ protected:
// common implementation for after templated public api has been resolved // common implementation for after templated public api has been resolved
template<typename... Args> template<typename... Args>
void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args) void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&... args)
{ {
bool log_enabled = should_log(lvl); bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled(); bool traceback_enabled = tracer_.enabled();
@@ -329,16 +368,21 @@ protected:
SPDLOG_TRY SPDLOG_TRY
{ {
memory_buf_t buf; memory_buf_t buf;
fmt::detail::vformat_to(buf, fmt, fmt::make_format_args(args...)); #ifdef SPDLOG_USE_STD_FORMAT
fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(std::forward<Args>(args)...));
#else
fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(std::forward<Args>(args)...));
#endif
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg, log_enabled, traceback_enabled); log_it_(log_msg, log_enabled, traceback_enabled);
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(loc)
} }
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args> template<typename... Args>
void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args) void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&... args)
{ {
bool log_enabled = should_log(lvl); bool log_enabled = should_log(lvl);
bool traceback_enabled = tracer_.enabled(); bool traceback_enabled = tracer_.enabled();
@@ -349,14 +393,20 @@ protected:
SPDLOG_TRY SPDLOG_TRY
{ {
// format to wmemory_buffer and convert to utf8 // format to wmemory_buffer and convert to utf8
fmt::wmemory_buffer wbuf; wmemory_buf_t wbuf;
fmt::detail::vformat_to(wbuf, fmt, fmt::make_format_args<fmt::wformat_context>(args...)); # ifdef SPDLOG_USE_STD_FORMAT
fmt_lib::vformat_to(
std::back_inserter(wbuf), fmt, fmt_lib::make_format_args<fmt_lib::wformat_context>(std::forward<Args>(args)...));
# else
fmt::vformat_to(std::back_inserter(wbuf), fmt, fmt::make_format_args<fmt::wformat_context>(std::forward<Args>(args)...));
# endif
memory_buf_t buf; memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg, log_enabled, traceback_enabled); log_it_(log_msg, log_enabled, traceback_enabled);
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(loc)
} }
// T can be statically converted to wstring_view, and no formatting needed. // T can be statically converted to wstring_view, and no formatting needed.
@@ -376,7 +426,7 @@ protected:
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
log_it_(log_msg, log_enabled, traceback_enabled); log_it_(log_msg, log_enabled, traceback_enabled);
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH(loc)
} }
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT

View File

@@ -766,6 +766,7 @@ public:
{ {
if (msg.source.empty()) if (msg.source.empty())
{ {
ScopedPadder p(0, padinfo_, dest);
return; return;
} }
@@ -800,6 +801,7 @@ public:
{ {
if (msg.source.empty()) if (msg.source.empty())
{ {
ScopedPadder p(0, padinfo_, dest);
return; return;
} }
size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) : 0; size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) : 0;
@@ -846,6 +848,7 @@ public:
{ {
if (msg.source.empty()) if (msg.source.empty())
{ {
ScopedPadder p(0, padinfo_, dest);
return; return;
} }
auto filename = basename(msg.source.filename); auto filename = basename(msg.source.filename);
@@ -867,6 +870,7 @@ public:
{ {
if (msg.source.empty()) if (msg.source.empty())
{ {
ScopedPadder p(0, padinfo_, dest);
return; return;
} }
@@ -889,6 +893,7 @@ public:
{ {
if (msg.source.empty()) if (msg.source.empty())
{ {
ScopedPadder p(0, padinfo_, dest);
return; return;
} }
size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.funcname) : 0; size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.funcname) : 0;
@@ -1019,6 +1024,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(
: pattern_(std::move(pattern)) : pattern_(std::move(pattern))
, eol_(std::move(eol)) , eol_(std::move(eol))
, pattern_time_type_(time_type) , pattern_time_type_(time_type)
, need_localtime_(false)
, last_log_secs_(0) , last_log_secs_(0)
, custom_handlers_(std::move(custom_user_flags)) , custom_handlers_(std::move(custom_user_flags))
{ {
@@ -1031,6 +1037,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type,
: pattern_("%+") : pattern_("%+")
, eol_(std::move(eol)) , eol_(std::move(eol))
, pattern_time_type_(time_type) , pattern_time_type_(time_type)
, need_localtime_(true)
, last_log_secs_(0) , last_log_secs_(0)
{ {
std::memset(&cached_tm_, 0, sizeof(cached_tm_)); std::memset(&cached_tm_, 0, sizeof(cached_tm_));
@@ -1044,16 +1051,25 @@ SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const
{ {
cloned_custom_formatters[it.first] = it.second->clone(); cloned_custom_formatters[it.first] = it.second->clone();
} }
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); auto cloned = details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters));
cloned->need_localtime(need_localtime_);
#if defined(__GNUC__) && __GNUC__ < 5
return std::move(cloned);
#else
return cloned;
#endif
} }
SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest)
{ {
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch()); if (need_localtime_)
if (secs != last_log_secs_)
{ {
cached_tm_ = get_time_(msg); const auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
last_log_secs_ = secs; if (secs != last_log_secs_)
{
cached_tm_ = get_time_(msg);
last_log_secs_ = secs;
}
} }
for (auto &f : formatters_) for (auto &f : formatters_)
@@ -1067,9 +1083,15 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory
SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern) SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern)
{ {
pattern_ = std::move(pattern); pattern_ = std::move(pattern);
need_localtime_ = false;
compile_pattern_(pattern_); compile_pattern_(pattern_);
} }
SPDLOG_INLINE void pattern_formatter::need_localtime(bool need)
{
need_localtime_ = need;
}
SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
{ {
if (pattern_time_type_ == pattern_time_type::local) if (pattern_time_type_ == pattern_time_type::local)
@@ -1097,6 +1119,7 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
{ {
case ('+'): // default formatter case ('+'): // default formatter
formatters_.push_back(details::make_unique<details::full_formatter>(padding)); formatters_.push_back(details::make_unique<details::full_formatter>(padding));
need_localtime_ = true;
break; break;
case 'n': // logger name case 'n': // logger name
@@ -1121,60 +1144,74 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
case ('a'): // weekday case ('a'): // weekday
formatters_.push_back(details::make_unique<details::a_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::a_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('A'): // short weekday case ('A'): // short weekday
formatters_.push_back(details::make_unique<details::A_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::A_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('b'): case ('b'):
case ('h'): // month case ('h'): // month
formatters_.push_back(details::make_unique<details::b_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::b_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('B'): // short month case ('B'): // short month
formatters_.push_back(details::make_unique<details::B_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::B_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('c'): // datetime case ('c'): // datetime
formatters_.push_back(details::make_unique<details::c_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::c_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('C'): // year 2 digits case ('C'): // year 2 digits
formatters_.push_back(details::make_unique<details::C_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::C_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('Y'): // year 4 digits case ('Y'): // year 4 digits
formatters_.push_back(details::make_unique<details::Y_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::Y_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('D'): case ('D'):
case ('x'): // datetime MM/DD/YY case ('x'): // datetime MM/DD/YY
formatters_.push_back(details::make_unique<details::D_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::D_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('m'): // month 1-12 case ('m'): // month 1-12
formatters_.push_back(details::make_unique<details::m_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::m_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('d'): // day of month 1-31 case ('d'): // day of month 1-31
formatters_.push_back(details::make_unique<details::d_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::d_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('H'): // hours 24 case ('H'): // hours 24
formatters_.push_back(details::make_unique<details::H_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::H_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('I'): // hours 12 case ('I'): // hours 12
formatters_.push_back(details::make_unique<details::I_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::I_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('M'): // minutes case ('M'): // minutes
formatters_.push_back(details::make_unique<details::M_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::M_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('S'): // seconds case ('S'): // seconds
formatters_.push_back(details::make_unique<details::S_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::S_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('e'): // milliseconds case ('e'): // milliseconds
@@ -1195,23 +1232,28 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
case ('p'): // am/pm case ('p'): // am/pm
formatters_.push_back(details::make_unique<details::p_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::p_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('r'): // 12 hour clock 02:55:02 pm case ('r'): // 12 hour clock 02:55:02 pm
formatters_.push_back(details::make_unique<details::r_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::r_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('R'): // 24-hour HH:MM time case ('R'): // 24-hour HH:MM time
formatters_.push_back(details::make_unique<details::R_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::R_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('T'): case ('T'):
case ('X'): // ISO 8601 time format (HH:MM:SS) case ('X'): // ISO 8601 time format (HH:MM:SS)
formatters_.push_back(details::make_unique<details::T_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::T_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('z'): // timezone case ('z'): // timezone
formatters_.push_back(details::make_unique<details::z_formatter<Padder>>(padding)); formatters_.push_back(details::make_unique<details::z_formatter<Padder>>(padding));
need_localtime_ = true;
break; break;
case ('P'): // pid case ('P'): // pid
@@ -1342,7 +1384,6 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri
{ {
truncate = false; truncate = false;
} }
return details::padding_info{std::min<size_t>(width, max_width), side, truncate}; return details::padding_info{std::min<size_t>(width, max_width), side, truncate};
} }

View File

@@ -68,7 +68,7 @@ class SPDLOG_API custom_flag_formatter : public details::flag_formatter
public: public:
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0; virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
void set_padding_info(details::padding_info padding) void set_padding_info(const details::padding_info &padding)
{ {
flag_formatter::padinfo_ = padding; flag_formatter::padinfo_ = padding;
} }
@@ -92,17 +92,19 @@ public:
void format(const details::log_msg &msg, memory_buf_t &dest) override; void format(const details::log_msg &msg, memory_buf_t &dest) override;
template<typename T, typename... Args> template<typename T, typename... Args>
pattern_formatter &add_flag(char flag, Args &&...args) pattern_formatter &add_flag(char flag, Args &&... args)
{ {
custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...); custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
return *this; return *this;
} }
void set_pattern(std::string pattern); void set_pattern(std::string pattern);
void need_localtime(bool need = true);
private: private:
std::string pattern_; std::string pattern_;
std::string eol_; std::string eol_;
pattern_time_type pattern_time_type_; pattern_time_type pattern_time_type_;
bool need_localtime_;
std::tm cached_tm_; std::tm cached_tm_;
std::chrono::seconds last_log_secs_; std::chrono::seconds last_log_secs_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_; std::vector<std::unique_ptr<details::flag_formatter>> formatters_;

View File

@@ -16,6 +16,7 @@
# include <mutex> # include <mutex>
# include <string> # include <string>
# include <thread> # include <thread>
# include <type_traits>
# if !defined(SPDLOG_ANDROID_RETRIES) # if !defined(SPDLOG_ANDROID_RETRIES)
# define SPDLOG_ANDROID_RETRIES 2 # define SPDLOG_ANDROID_RETRIES 2
@@ -25,9 +26,10 @@ namespace spdlog {
namespace sinks { namespace sinks {
/* /*
* Android sink (logging using __android_log_write) * Android sink
* (logging using __android_log_write or __android_log_buf_write depending on the specified BufferID)
*/ */
template<typename Mutex> template<typename Mutex, int BufferID = log_id::LOG_ID_MAIN>
class android_sink final : public base_sink<Mutex> class android_sink final : public base_sink<Mutex>
{ {
public: public:
@@ -53,24 +55,39 @@ protected:
const char *msg_output = formatted.data(); const char *msg_output = formatted.data();
// See system/core/liblog/logger_write.c for explanation of return value // See system/core/liblog/logger_write.c for explanation of return value
int ret = __android_log_write(priority, tag_.c_str(), msg_output); int ret = android_log(priority, tag_.c_str(), msg_output);
int retry_count = 0; int retry_count = 0;
while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
{ {
details::os::sleep_for_millis(5); details::os::sleep_for_millis(5);
ret = __android_log_write(priority, tag_.c_str(), msg_output); ret = android_log(priority, tag_.c_str(), msg_output);
retry_count++; retry_count++;
} }
if (ret < 0) if (ret < 0)
{ {
throw_spdlog_ex("__android_log_write() failed", ret); throw_spdlog_ex("logging to Android failed", ret);
} }
} }
void flush_() override {} void flush_() override {}
private: private:
// There might be liblog versions used, that do not support __android_log_buf_write. So we only compile and link against
// __android_log_buf_write, if user explicitely provides a non-default log buffer. Otherwise, when using the default log buffer, always
// log via __android_log_write.
template<int ID = BufferID>
typename std::enable_if<ID == static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(int prio, const char *tag, const char *text)
{
return __android_log_write(prio, tag, text);
}
template<int ID = BufferID>
typename std::enable_if<ID != static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(int prio, const char *tag, const char *text)
{
return __android_log_buf_write(ID, prio, tag, text);
}
static android_LogPriority convert_to_android_(spdlog::level::level_enum level) static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
{ {
switch (level) switch (level)
@@ -98,6 +115,12 @@ private:
using android_sink_mt = android_sink<std::mutex>; using android_sink_mt = android_sink<std::mutex>;
using android_sink_st = android_sink<details::null_mutex>; using android_sink_st = android_sink<details::null_mutex>;
template<int BufferId = log_id::LOG_ID_MAIN>
using android_sink_buf_mt = android_sink<std::mutex, BufferId>;
template<int BufferId = log_id::LOG_ID_MAIN>
using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
} // namespace sinks } // namespace sinks
// Create and register android syslog logger // Create and register android syslog logger

View File

@@ -34,7 +34,7 @@ template<typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color) SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color)
{ {
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<mutex_t> lock(mutex_);
colors_[color_level] = to_string_(color); colors_[static_cast<size_t>(color_level)] = to_string_(color);
} }
template<typename ConsoleMutex> template<typename ConsoleMutex>
@@ -52,7 +52,7 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
// before color range // before color range
print_range_(formatted, 0, msg.color_range_start); print_range_(formatted, 0, msg.color_range_start);
// in color range // in color range
print_ccode_(colors_[msg.level]); print_ccode_(colors_[static_cast<size_t>(msg.level)]);
print_range_(formatted, msg.color_range_start, msg.color_range_end); print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset); print_ccode_(reset);
// after color range // after color range

View File

@@ -16,7 +16,7 @@
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<typename Mutex> template<typename Mutex>
class base_sink : public sink class SPDLOG_API base_sink : public sink
{ {
public: public:
base_sink(); base_sink();
@@ -37,7 +37,7 @@ public:
protected: protected:
// sink formatter // sink formatter
std::unique_ptr<spdlog::formatter> formatter_; std::unique_ptr<spdlog::formatter> formatter_;
mutable Mutex mutex_; Mutex mutex_;
virtual void sink_it_(const details::log_msg &msg) = 0; virtual void sink_it_(const details::log_msg &msg) = 0;
virtual void flush_() = 0; virtual void flush_() = 0;

View File

@@ -14,7 +14,8 @@ namespace spdlog {
namespace sinks { namespace sinks {
template<typename Mutex> template<typename Mutex>
SPDLOG_INLINE basic_file_sink<Mutex>::basic_file_sink(const filename_t &filename, bool truncate) SPDLOG_INLINE basic_file_sink<Mutex>::basic_file_sink(const filename_t &filename, bool truncate, const file_event_handlers &event_handlers)
: file_helper_{event_handlers}
{ {
file_helper_.open(filename, truncate); file_helper_.open(filename, truncate);
} }

View File

@@ -20,7 +20,7 @@ template<typename Mutex>
class basic_file_sink final : public base_sink<Mutex> class basic_file_sink final : public base_sink<Mutex>
{ {
public: public:
explicit basic_file_sink(const filename_t &filename, bool truncate = false); explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {});
const filename_t &filename() const; const filename_t &filename() const;
protected: protected:
@@ -40,15 +40,17 @@ using basic_file_sink_st = basic_file_sink<details::null_mutex>;
// factory functions // factory functions
// //
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) inline std::shared_ptr<logger> basic_logger_mt(
const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate); return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate, event_handlers);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) inline std::shared_ptr<logger> basic_logger_st(
const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate); return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate, event_handlers);
} }
} // namespace spdlog } // namespace spdlog

View File

@@ -32,8 +32,8 @@ struct daily_filename_calculator
{ {
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extension(filename); std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
return fmt::format( return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")), basename, now_tm.tm_year + 1900,
SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext); now_tm.tm_mon + 1, now_tm.tm_mday, ext);
} }
}; };
@@ -48,14 +48,65 @@ struct daily_filename_format_calculator
{ {
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
{ {
// generate fmt datetime format string, e.g. {:%Y-%m-%d}. #ifdef SPDLOG_USE_STD_FORMAT
filename_t fmt_filename = fmt::format(SPDLOG_FILENAME_T("{{:{}}}"), filename); // adapted from fmtlib: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/chrono.h#L522-L546
#if defined(_MSC_VER) && defined(SPDLOG_WCHAR_FILENAMES) // for some reason msvc doesnt allow fmt::runtime(..) with wchar here
return fmt::format(fmt_filename, now_tm); filename_t tm_format;
tm_format.append(filename);
// By appending an extra space we can distinguish an empty result that
// indicates insufficient buffer size from a guaranteed non-empty result
// https://github.com/fmtlib/fmt/issues/2238
tm_format.push_back(' ');
const size_t MIN_SIZE = 10;
filename_t buf;
buf.resize(MIN_SIZE);
for (;;)
{
size_t count = strftime(buf.data(), buf.size(), tm_format.c_str(), &now_tm);
if (count != 0)
{
// Remove the extra space.
buf.resize(count - 1);
break;
}
buf.resize(buf.size() * 2);
}
return buf;
#else #else
// generate fmt datetime format string, e.g. {:%Y-%m-%d}.
filename_t fmt_filename = fmt::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{{:{}}}")), filename);
// MSVC doesn't allow fmt::runtime(..) with wchar, with fmtlib versions < 9.1.x
# if defined(_MSC_VER) && defined(SPDLOG_WCHAR_FILENAMES) && FMT_VERSION < 90101
return fmt::format(fmt_filename, now_tm);
# else
return fmt::format(SPDLOG_FMT_RUNTIME(fmt_filename), now_tm); return fmt::format(SPDLOG_FMT_RUNTIME(fmt_filename), now_tm);
# endif
#endif #endif
} }
private:
#if defined __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
static size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
{
return std::strftime(str, count, format, time);
}
static size_t strftime(wchar_t *str, size_t count, const wchar_t *format, const std::tm *time)
{
return std::wcsftime(str, count, format, time);
}
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
}; };
/* /*
@@ -68,10 +119,12 @@ class daily_file_sink final : public base_sink<Mutex>
{ {
public: public:
// create daily file sink which rotates on given time // create daily file sink which rotates on given time
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0) daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0,
const file_event_handlers &event_handlers = {})
: base_filename_(std::move(base_filename)) : base_filename_(std::move(base_filename))
, rotation_h_(rotation_hour) , rotation_h_(rotation_hour)
, rotation_m_(rotation_minute) , rotation_m_(rotation_minute)
, file_helper_{event_handlers}
, truncate_(truncate) , truncate_(truncate)
, max_files_(max_files) , max_files_(max_files)
, filenames_q_() , filenames_q_()
@@ -213,30 +266,32 @@ using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_fil
// factory functions // factory functions
// //
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_mt( inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0,
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files); return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_format_mt( inline std::shared_ptr<logger> daily_logger_format_mt(const std::string &logger_name, const filename_t &filename, int hour = 0,
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files); return Factory::template create<sinks::daily_file_format_sink_mt>(
logger_name, filename, hour, minute, truncate, max_files, event_handlers);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_st( inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0,
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files); return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_format_st( inline std::shared_ptr<logger> daily_logger_format_st(const std::string &logger_name, const filename_t &filename, int hour = 0,
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files); return Factory::template create<sinks::daily_file_format_sink_st>(
logger_name, filename, hour, minute, truncate, max_files, event_handlers);
} }
} // namespace spdlog } // namespace spdlog

View File

@@ -57,20 +57,20 @@ public:
protected: protected:
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
for (auto &sink : sinks_) for (auto &sub_sink : sinks_)
{ {
if (sink->should_log(msg.level)) if (sub_sink->should_log(msg.level))
{ {
sink->log(msg); sub_sink->log(msg);
} }
} }
} }
void flush_() override void flush_() override
{ {
for (auto &sink : sinks_) for (auto &sub_sink : sinks_)
{ {
sink->flush(); sub_sink->flush();
} }
} }
@@ -82,9 +82,9 @@ protected:
void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override
{ {
base_sink<Mutex>::formatter_ = std::move(sink_formatter); base_sink<Mutex>::formatter_ = std::move(sink_formatter);
for (auto &sink : sinks_) for (auto &sub_sink : sinks_)
{ {
sink->set_formatter(base_sink<Mutex>::formatter_->clone()); sub_sink->set_formatter(base_sink<Mutex>::formatter_->clone());
} }
} }
std::vector<std::shared_ptr<sink>> sinks_; std::vector<std::shared_ptr<sink>> sinks_;

View File

@@ -31,7 +31,7 @@ struct hourly_filename_calculator
{ {
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extension(filename); std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
return fmt::format(SPDLOG_FILENAME_T("{}_{:04d}{:02d}{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1,
now_tm.tm_mday, now_tm.tm_hour, ext); now_tm.tm_mday, now_tm.tm_hour, ext);
} }
}; };
@@ -46,8 +46,10 @@ class hourly_file_sink final : public base_sink<Mutex>
{ {
public: public:
// create hourly file sink which rotates on given time // create hourly file sink which rotates on given time
hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0) hourly_file_sink(
filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
: base_filename_(std::move(base_filename)) : base_filename_(std::move(base_filename))
, file_helper_{event_handlers}
, truncate_(truncate) , truncate_(truncate)
, max_files_(max_files) , max_files_(max_files)
, filenames_q_() , filenames_q_()
@@ -55,6 +57,7 @@ public:
auto now = log_clock::now(); auto now = log_clock::now();
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
file_helper_.open(filename, truncate_); file_helper_.open(filename, truncate_);
remove_init_file_ = file_helper_.size() == 0;
rotation_tp_ = next_rotation_tp_(); rotation_tp_ = next_rotation_tp_();
if (max_files_ > 0) if (max_files_ > 0)
@@ -76,10 +79,16 @@ protected:
bool should_rotate = time >= rotation_tp_; bool should_rotate = time >= rotation_tp_;
if (should_rotate) if (should_rotate)
{ {
if (remove_init_file_)
{
file_helper_.close();
details::os::remove(file_helper_.filename());
}
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
file_helper_.open(filename, truncate_); file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_(); rotation_tp_ = next_rotation_tp_();
} }
remove_init_file_ = false;
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
file_helper_.write(formatted); file_helper_.write(formatted);
@@ -168,6 +177,7 @@ private:
bool truncate_; bool truncate_;
uint16_t max_files_; uint16_t max_files_;
details::circular_q<filename_t> filenames_q_; details::circular_q<filename_t> filenames_q_;
bool remove_init_file_;
}; };
using hourly_file_sink_mt = hourly_file_sink<std::mutex>; using hourly_file_sink_mt = hourly_file_sink<std::mutex>;
@@ -179,16 +189,16 @@ using hourly_file_sink_st = hourly_file_sink<details::null_mutex>;
// factory functions // factory functions
// //
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> hourly_logger_mt( inline std::shared_ptr<logger> hourly_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false,
const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0) uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files); return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files, event_handlers);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> hourly_logger_st( inline std::shared_ptr<logger> hourly_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false,
const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0) uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files); return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files, event_handlers);
} }
} // namespace spdlog } // namespace spdlog

View File

@@ -30,16 +30,26 @@ class mongo_sink : public base_sink<Mutex>
{ {
public: public:
mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017") mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
try : mongo_sink(std::make_shared<mongocxx::instance>(), db_name, collection_name, uri)
{}
catch (const std::exception &e)
{
throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
}
mongo_sink(std::shared_ptr<mongocxx::instance> instance, const std::string &db_name, const std::string &collection_name,
const std::string &uri = "mongodb://localhost:27017")
: instance_(std::move(instance))
, db_name_(db_name)
, coll_name_(collection_name)
{ {
try try
{ {
client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri}); client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
db_name_ = db_name;
coll_name_ = collection_name;
} }
catch (const std::exception) catch (const std::exception &e)
{ {
throw spdlog_ex("Error opening database"); throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
} }
} }
@@ -57,8 +67,8 @@ protected:
if (client_ != nullptr) if (client_ != nullptr)
{ {
auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.level).data() auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.level).data()
<< "message" << std::string(msg.payload.begin(), msg.payload.end()) << "logger_name" << "level_num" << msg.level << "message" << std::string(msg.payload.begin(), msg.payload.end())
<< std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id" << "logger_name" << std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
<< static_cast<int>(msg.thread_id) << finalize; << static_cast<int>(msg.thread_id) << finalize;
client_->database(db_name_).collection(coll_name_).insert_one(doc.view()); client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
} }
@@ -67,12 +77,11 @@ protected:
void flush_() override {} void flush_() override {}
private: private:
static mongocxx::instance instance_; std::shared_ptr<mongocxx::instance> instance_;
std::string db_name_; std::string db_name_;
std::string coll_name_; std::string coll_name_;
std::unique_ptr<mongocxx::client> client_ = nullptr; std::unique_ptr<mongocxx::client> client_ = nullptr;
}; };
mongocxx::instance mongo_sink<std::mutex>::instance_{};
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include <mutex> #include <mutex>

View File

@@ -1,8 +1,9 @@
// Copyright(c) 2016 Alexander Dalshov. // Copyright(c) 2016 Alexander Dalshov & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once #pragma once
#if defined(_WIN32) #if defined(_WIN32)
# include <spdlog/details/null_mutex.h> # include <spdlog/details/null_mutex.h>
@@ -13,6 +14,7 @@
// Avoid including windows.h (https://stackoverflow.com/a/30741042) // Avoid including windows.h (https://stackoverflow.com/a/30741042)
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString);
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@@ -24,16 +26,25 @@ class msvc_sink : public base_sink<Mutex>
{ {
public: public:
msvc_sink() = default; msvc_sink() = default;
msvc_sink(bool check_ebugger_present)
: check_debbugger_present_{check_ebugger_present} {};
protected: protected:
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
if (check_debbugger_present_ && !IsDebuggerPresent())
{
return;
}
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
OutputDebugStringA(fmt::to_string(formatted).c_str()); formatted.push_back('\0'); // add a null terminator for OutputDebugStringA
OutputDebugStringA(formatted.data());
} }
void flush_() override {} void flush_() override {}
bool check_debbugger_present_ = true;
}; };
using msvc_sink_mt = msvc_sink<std::mutex>; using msvc_sink_mt = msvc_sink<std::mutex>;

View File

@@ -21,29 +21,36 @@
// //
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template <typename Mutex> class qt_sink : public base_sink<Mutex> { template<typename Mutex>
class qt_sink : public base_sink<Mutex>
{
public: public:
qt_sink(QObject *qt_object = nullptr, const std::string &meta_method = "") { qt_sink(QObject *qt_object, const std::string &meta_method)
qt_object_ = qt_object; {
meta_method_ = meta_method; qt_object_ = qt_object;
} meta_method_ = meta_method;
}
~qt_sink() { flush_(); } ~qt_sink()
{
flush_();
}
protected: protected:
void sink_it_(const details::log_msg &msg) override { void sink_it_(const details::log_msg &msg) override
memory_buf_t formatted; {
base_sink<Mutex>::formatter_->format(msg, formatted); memory_buf_t formatted;
string_view_t str = string_view_t(formatted.data(), formatted.size()); base_sink<Mutex>::formatter_->format(msg, formatted);
QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection, string_view_t str = string_view_t(formatted.data(), formatted.size());
Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed())); QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
} Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
}
void flush_() override {} void flush_() override {}
private: private:
QObject *qt_object_ = nullptr; QObject *qt_object_ = nullptr;
std::string meta_method_; std::string meta_method_;
}; };
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
@@ -55,39 +62,41 @@ using qt_sink_st = qt_sink<spdlog::details::null_mutex>;
// //
// Factory functions // Factory functions
// //
template <typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append")
qt_logger_mt(const std::string &logger_name, QTextEdit* qt_object, const std::string &meta_method = "append") { {
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger>
qt_logger_st(const std::string &logger_name, QTextEdit* qt_object, const std::string &meta_method = "append") {
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger>
qt_logger_mt(const std::string &logger_name, QPlainTextEdit* qt_object , const std::string &meta_method = "appendPlainText") {
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method); return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
} }
template <typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append")
qt_logger_st(const std::string &logger_name, QPlainTextEdit* qt_object, const std::string &meta_method = "appendPlainText") { {
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method); return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
} }
template <typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> inline std::shared_ptr<logger> qt_logger_mt(
qt_logger_mt(const std::string &logger_name, QObject* qt_object, const std::string &meta_method) { const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
{
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method); return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
} }
template <typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> inline std::shared_ptr<logger> qt_logger_st(
qt_logger_st(const std::string &logger_name, QObject* qt_object, const std::string &meta_method) { const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
{
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QObject *qt_object, const std::string &meta_method)
{
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::string &meta_method)
{
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method); return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
} }
} // namespace spdlog } // namespace spdlog

View File

@@ -50,7 +50,7 @@ public:
{ {
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(q_.at(i), formatted); base_sink<Mutex>::formatter_->format(q_.at(i), formatted);
ret.push_back(fmt::to_string(formatted)); ret.push_back(std::move(SPDLOG_BUF_TO_STRING(formatted)));
} }
return ret; return ret;
} }

View File

@@ -25,16 +25,27 @@ namespace sinks {
template<typename Mutex> template<typename Mutex>
SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink( SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(
filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open) filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open, const file_event_handlers &event_handlers)
: base_filename_(std::move(base_filename)) : base_filename_(std::move(base_filename))
, max_size_(max_size) , max_size_(max_size)
, max_files_(max_files) , max_files_(max_files)
, file_helper_{event_handlers}
{ {
if (max_size == 0)
{
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
}
if (max_files > 200000)
{
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
}
file_helper_.open(calc_filename(base_filename_, 0)); file_helper_.open(calc_filename(base_filename_, 0));
current_size_ = file_helper_.size(); // expensive. called only once current_size_ = file_helper_.size(); // expensive. called only once
if (rotate_on_open && current_size_ > 0) if (rotate_on_open && current_size_ > 0)
{ {
rotate_(); rotate_();
current_size_ = 0;
} }
} }
@@ -50,7 +61,7 @@ SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extension(filename); std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
return fmt::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext); return fmt_lib::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
} }
template<typename Mutex> template<typename Mutex>
@@ -65,13 +76,22 @@ SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &m
{ {
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
current_size_ += formatted.size(); auto new_size = current_size_ + formatted.size();
if (current_size_ > max_size_)
// rotate if the new estimated file size exceeds max size.
// rotate only if the real size > 0 to better deal with full disk (see issue #2261).
// we only check the real size when new_size > max_size_ because it is relatively expensive.
if (new_size > max_size_)
{ {
rotate_(); file_helper_.flush();
current_size_ = formatted.size(); if (file_helper_.size() > 0)
{
rotate_();
new_size = formatted.size();
}
} }
file_helper_.write(formatted); file_helper_.write(formatted);
current_size_ = new_size;
} }
template<typename Mutex> template<typename Mutex>
@@ -90,6 +110,7 @@ SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_()
{ {
using details::os::filename_to_str; using details::os::filename_to_str;
using details::os::path_exists; using details::os::path_exists;
file_helper_.close(); file_helper_.close();
for (auto i = max_files_; i > 0; --i) for (auto i = max_files_; i > 0; --i)
{ {

View File

@@ -22,7 +22,8 @@ template<typename Mutex>
class rotating_file_sink final : public base_sink<Mutex> class rotating_file_sink final : public base_sink<Mutex>
{ {
public: public:
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false); rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false,
const file_event_handlers &event_handlers = {});
static filename_t calc_filename(const filename_t &filename, std::size_t index); static filename_t calc_filename(const filename_t &filename, std::size_t index);
filename_t filename(); filename_t filename();
@@ -59,17 +60,19 @@ using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
// //
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> rotating_logger_mt( inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name, const filename_t &filename, size_t max_file_size,
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) size_t max_files, bool rotate_on_open = false, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open); return Factory::template create<sinks::rotating_file_sink_mt>(
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> rotating_logger_st( inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name, const filename_t &filename, size_t max_file_size,
const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) size_t max_files, bool rotate_on_open = false, const file_event_handlers &event_handlers = {})
{ {
return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files, rotate_on_open); return Factory::template create<sinks::rotating_file_sink_st>(
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
} }
} // namespace spdlog } // namespace spdlog

View File

@@ -16,7 +16,7 @@
// so instead we use ::FileWrite // so instead we use ::FileWrite
# include <spdlog/details/windows_include.h> # include <spdlog/details/windows_include.h>
# ifndef _USING_V110_SDK71_ // fileapi.h doesnt exist in winxp # ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
# include <fileapi.h> // WriteFile (..) # include <fileapi.h> // WriteFile (..)
# endif # endif
@@ -37,7 +37,7 @@ SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
#ifdef _WIN32 #ifdef _WIN32
// get windows handle from the FILE* object // get windows handle from the FILE* object
handle_ = (HANDLE)::_get_osfhandle(::_fileno(file_)); handle_ = reinterpret_cast<HANDLE>(::_get_osfhandle(::_fileno(file_)));
// don't throw to support cases where no console is attached, // don't throw to support cases where no console is attached,
// and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE). // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
@@ -60,7 +60,7 @@ SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &m
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<mutex_t> lock(mutex_);
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
::fflush(file_); // flush in case there is somthing in this file_ already ::fflush(file_); // flush in case there is something in this file_ already
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(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;

View File

@@ -18,16 +18,15 @@ namespace sinks {
/** /**
* Sink that write to systemd journal using the `sd_journal_send()` library call. * Sink that write to systemd journal using the `sd_journal_send()` library call.
*
* Locking is not needed, as `sd_journal_send()` itself is thread-safe.
*/ */
template<typename Mutex> template<typename Mutex>
class systemd_sink : public base_sink<Mutex> class systemd_sink : public base_sink<Mutex>
{ {
public: public:
// systemd_sink(std::string ident = "", bool enable_formatting = false)
systemd_sink() : ident_{std::move(ident)}
: syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, , enable_formatting_{enable_formatting}
, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
/* spdlog::level::debug */ LOG_DEBUG, /* spdlog::level::debug */ LOG_DEBUG,
/* spdlog::level::info */ LOG_INFO, /* spdlog::level::info */ LOG_INFO,
/* spdlog::level::warn */ LOG_WARNING, /* spdlog::level::warn */ LOG_WARNING,
@@ -42,31 +41,46 @@ public:
systemd_sink &operator=(const systemd_sink &) = delete; systemd_sink &operator=(const systemd_sink &) = delete;
protected: protected:
const std::string ident_;
bool enable_formatting_ = false;
using levels_array = std::array<int, 7>; using levels_array = std::array<int, 7>;
levels_array syslog_levels_; levels_array syslog_levels_;
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
int err; int err;
string_view_t payload;
memory_buf_t formatted;
if (enable_formatting_)
{
base_sink<Mutex>::formatter_->format(msg, formatted);
payload = string_view_t(formatted.data(), formatted.size());
}
else
{
payload = msg.payload;
}
size_t length = msg.payload.size(); size_t length = payload.size();
// limit to max int // limit to max int
if (length > static_cast<size_t>(std::numeric_limits<int>::max())) if (length > static_cast<size_t>(std::numeric_limits<int>::max()))
{ {
length = static_cast<size_t>(std::numeric_limits<int>::max()); length = static_cast<size_t>(std::numeric_limits<int>::max());
} }
const string_view_t syslog_identifier = ident_.empty() ? msg.logger_name : ident_;
// Do not send source location if not available // Do not send source location if not available
if (msg.source.empty()) if (msg.source.empty())
{ {
// Note: function call inside '()' to avoid macro expansion // Note: function call inside '()' to avoid macro expansion
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), nullptr); "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), nullptr);
} }
else else
{ {
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s", "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s",
msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
} }
@@ -90,14 +104,16 @@ using systemd_sink_st = systemd_sink<details::null_mutex>;
// Create and register a syslog logger // Create and register a syslog logger
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name) inline std::shared_ptr<logger> systemd_logger_mt(
const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false)
{ {
return Factory::template create<sinks::systemd_sink_mt>(logger_name); return Factory::template create<sinks::systemd_sink_mt>(logger_name, ident, enable_formatting);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name) inline std::shared_ptr<logger> systemd_logger_st(
const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false)
{ {
return Factory::template create<sinks::systemd_sink_st>(logger_name); return Factory::template create<sinks::systemd_sink_st>(logger_name, ident, enable_formatting);
} }
} // namespace spdlog } // namespace spdlog

74
thirdparty/spdlog/sinks/udp_sink.h vendored Normal file
View File

@@ -0,0 +1,74 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/common.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/null_mutex.h>
#ifdef _WIN32
# include <spdlog/details/udp_client-windows.h>
#else
# include <spdlog/details/udp_client.h>
#endif
#include <mutex>
#include <string>
#include <chrono>
#include <functional>
// Simple udp client sink
// Sends formatted log via udp
namespace spdlog {
namespace sinks {
struct udp_sink_config
{
std::string server_host;
uint16_t server_port;
udp_sink_config(std::string host, uint16_t port)
: server_host{std::move(host)}
, server_port{port}
{}
};
template<typename Mutex>
class udp_sink : public spdlog::sinks::base_sink<Mutex>
{
public:
// host can be hostname or ip address
explicit udp_sink(udp_sink_config sink_config)
: client_{sink_config.server_host, sink_config.server_port}
{}
~udp_sink() override = default;
protected:
void sink_it_(const spdlog::details::log_msg &msg) override
{
spdlog::memory_buf_t formatted;
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
client_.send(formatted.data(), formatted.size());
}
void flush_() override {}
details::udp_client client_;
};
using udp_sink_mt = udp_sink<std::mutex>;
using udp_sink_st = udp_sink<spdlog::details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config)
{
return Factory::template create<sinks::udp_sink_mt>(logger_name, skin_config);
}
} // namespace spdlog

View File

@@ -47,6 +47,24 @@ namespace win_eventlog {
namespace internal { namespace internal {
struct local_alloc_t
{
HLOCAL hlocal_;
SPDLOG_CONSTEXPR local_alloc_t() SPDLOG_NOEXCEPT : hlocal_(nullptr) {}
local_alloc_t(local_alloc_t const &) = delete;
local_alloc_t &operator=(local_alloc_t const &) = delete;
~local_alloc_t() SPDLOG_NOEXCEPT
{
if (hlocal_)
{
LocalFree(hlocal_);
}
}
};
/** Windows error */ /** Windows error */
struct win32_error : public spdlog_ex struct win32_error : public spdlog_ex
{ {
@@ -55,22 +73,17 @@ struct win32_error : public spdlog_ex
{ {
std::string system_message; std::string system_message;
LPSTR format_message_result{}; local_alloc_t format_message_result{};
auto format_message_succeeded = auto format_message_succeeded =
::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result, 0, nullptr); error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result.hlocal_, 0, nullptr);
if (format_message_succeeded && format_message_result) if (format_message_succeeded && format_message_result.hlocal_)
{ {
system_message = fmt::format(" ({})", format_message_result); system_message = fmt_lib::format(" ({})", (LPSTR)format_message_result.hlocal_);
} }
if (format_message_result) return fmt_lib::format("{}: {}{}", user_message, error_code, system_message);
{
LocalFree((HLOCAL)format_message_result);
}
return fmt::format("{}: {}{}", user_message, error_code, system_message);
} }
explicit win32_error(std::string const &func_name, DWORD error = GetLastError()) explicit win32_error(std::string const &func_name, DWORD error = GetLastError())
@@ -228,12 +241,12 @@ protected:
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf); details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
LPCWSTR lp_wstr = buf.data(); LPCWSTR lp_wstr = buf.data();
succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_, succeeded = static_cast<bool>(::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr); current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr));
#else #else
LPCSTR lp_str = formatted.data(); LPCSTR lp_str = formatted.data();
succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_, succeeded = static_cast<bool>(::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr); current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
#endif #endif
if (!succeeded) if (!succeeded)

View File

@@ -45,7 +45,7 @@ template<typename ConsoleMutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, std::uint16_t 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_[static_cast<size_t>(level)] = color;
} }
template<typename ConsoleMutex> template<typename ConsoleMutex>
@@ -66,7 +66,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
// before color range // before color range
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 = static_cast<WORD>(set_foreground_color_(colors_[msg.level])); auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(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(static_cast<HANDLE>(out_handle_), orig_attribs); ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);

View File

@@ -67,11 +67,6 @@ SPDLOG_INLINE void flush_on(level::level_enum log_level)
details::registry::instance().flush_on(log_level); details::registry::instance().flush_on(log_level);
} }
SPDLOG_INLINE void flush_every(std::chrono::seconds interval)
{
details::registry::instance().flush_every(interval);
}
SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg)) SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg))
{ {
details::registry::instance().set_error_handler(handler); details::registry::instance().set_error_handler(handler);

View File

@@ -31,7 +31,7 @@ using default_factory = synchronous_factory;
// Example: // Example:
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59); // spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...sink_args) inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... sink_args)
{ {
return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...); return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
} }
@@ -81,7 +81,11 @@ SPDLOG_API void flush_on(level::level_enum log_level);
// Start/Restart a periodic flusher thread // Start/Restart a periodic flusher thread
// Warning: Use only if all your loggers are thread safe! // Warning: Use only if all your loggers are thread safe!
SPDLOG_API void flush_every(std::chrono::seconds interval); template<typename Rep, typename Period>
inline void flush_every(std::chrono::duration<Rep, Period> interval)
{
details::registry::instance().flush_every(interval);
}
// Set global error handler // Set global error handler
SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg)); SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
@@ -128,49 +132,49 @@ SPDLOG_API spdlog::logger *default_logger_raw();
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger); SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
template<typename... Args> template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args) inline void log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...); default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args) inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...); default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void trace(fmt::format_string<Args...> fmt, Args &&...args) inline void trace(format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->trace(fmt, std::forward<Args>(args)...); default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void debug(fmt::format_string<Args...> fmt, Args &&...args) inline void debug(format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->debug(fmt, std::forward<Args>(args)...); default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void info(fmt::format_string<Args...> fmt, Args &&...args) inline void info(format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->info(fmt, std::forward<Args>(args)...); default_logger_raw()->info(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void warn(fmt::format_string<Args...> fmt, Args &&...args) inline void warn(format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->warn(fmt, std::forward<Args>(args)...); default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void error(fmt::format_string<Args...> fmt, Args &&...args) inline void error(format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->error(fmt, std::forward<Args>(args)...); default_logger_raw()->error(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void critical(fmt::format_string<Args...> fmt, Args &&...args) inline void critical(format_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->critical(fmt, std::forward<Args>(args)...); default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
} }
@@ -189,49 +193,49 @@ inline void log(level::level_enum lvl, const T &msg)
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args> template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args) inline void log(source_loc source, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...); default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args) inline void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...); default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void trace(fmt::wformat_string<Args...> fmt, Args &&...args) inline void trace(wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->trace(fmt, std::forward<Args>(args)...); default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void debug(fmt::wformat_string<Args...> fmt, Args &&...args) inline void debug(wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->debug(fmt, std::forward<Args>(args)...); default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void info(fmt::wformat_string<Args...> fmt, Args &&...args) inline void info(wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->info(fmt, std::forward<Args>(args)...); default_logger_raw()->info(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void warn(fmt::wformat_string<Args...> fmt, Args &&...args) inline void warn(wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->warn(fmt, std::forward<Args>(args)...); default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void error(fmt::wformat_string<Args...> fmt, Args &&...args) inline void error(wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->error(fmt, std::forward<Args>(args)...); default_logger_raw()->error(fmt, std::forward<Args>(args)...);
} }
template<typename... Args> template<typename... Args>
inline void critical(fmt::wformat_string<Args...> fmt, Args &&...args) inline void critical(wformat_string_t<Args...> fmt, Args &&... args)
{ {
default_logger_raw()->critical(fmt, std::forward<Args>(args)...); default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
} }
@@ -288,7 +292,12 @@ inline void critical(const T &msg)
// SPDLOG_LEVEL_OFF // SPDLOG_LEVEL_OFF
// //
#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) #ifndef SPDLOG_NO_SOURCE_LOC
# define SPDLOG_LOGGER_CALL(logger, level, ...) \
(logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
#else
# define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
#endif
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
# define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) # define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)

View File

@@ -4,6 +4,7 @@
#pragma once #pragma once
#include <spdlog/fmt/fmt.h> #include <spdlog/fmt/fmt.h>
#include <chrono>
// Stopwatch support for spdlog (using std::chrono::steady_clock). // Stopwatch support for spdlog (using std::chrono::steady_clock).
// Displays elapsed seconds since construction as double. // Displays elapsed seconds since construction as double.
@@ -42,13 +43,20 @@ public:
void reset() void reset()
{ {
start_tp_ = clock ::now(); start_tp_ = clock::now();
} }
}; };
} // 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 fmt { namespace
#ifdef SPDLOG_USE_STD_FORMAT
std
#else
fmt
#endif
{
template<> template<>
struct formatter<spdlog::stopwatch> : formatter<double> struct formatter<spdlog::stopwatch> : formatter<double>
{ {
@@ -58,4 +66,4 @@ struct formatter<spdlog::stopwatch> : formatter<double>
return formatter<double>::format(sw.elapsed().count(), ctx); return formatter<double>::format(sw.elapsed().count(), ctx);
} }
}; };
} // namespace fmt } // namespace std

View File

@@ -19,6 +19,13 @@
// #define SPDLOG_CLOCK_COARSE // #define SPDLOG_CLOCK_COARSE
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment if source location logging is not needed.
// This will prevent spdlog from using __FILE__, __LINE__ and SPDLOG_FUNCTION
//
// #define SPDLOG_NO_SOURCE_LOC
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Uncomment if thread id logging is not needed (i.e. no %t in the log pattern). // Uncomment if thread id logging is not needed (i.e. no %t in the log pattern).
// This will prevent spdlog from querying the thread id on each log call. // This will prevent spdlog from querying the thread id on each log call.
@@ -74,6 +81,13 @@
// #define SPDLOG_FMT_EXTERNAL // #define SPDLOG_FMT_EXTERNAL
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to use C++20 std::format instead of fmt. This removes compile
// time checking of format strings, but doesn't depend on the fmt library.
//
// #define SPDLOG_USE_STD_FORMAT
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable wchar_t support (convert to utf8) // Uncomment to enable wchar_t support (convert to utf8)
// //
@@ -89,8 +103,7 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Uncomment to customize level names (e.g. "MY TRACE") // Uncomment to customize level names (e.g. "MY TRACE")
// //
// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", // #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY CRITICAL", "OFF" }
// "MY ERROR", "MY CRITICAL", "OFF" }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -120,5 +133,9 @@
// __PRETTY_FUNCTION__ might be nicer in clang/gcc, and __FUNCTION__ in msvc. // __PRETTY_FUNCTION__ might be nicer in clang/gcc, and __FUNCTION__ in msvc.
// Defaults to __FUNCTION__ (should work on all compilers) if not defined. // Defaults to __FUNCTION__ (should work on all compilers) if not defined.
// //
// #define SPDLOG_FUNCTION __PRETTY_FUNCTION__ // #ifdef __PRETTY_FUNCTION__
// # define SPDLOG_FUNCTION __PRETTY_FUNCTION__
// #else
// # define SPDLOG_FUNCTION __FUNCTION__
// #endif
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@@ -4,7 +4,7 @@
#pragma once #pragma once
#define SPDLOG_VER_MAJOR 1 #define SPDLOG_VER_MAJOR 1
#define SPDLOG_VER_MINOR 9 #define SPDLOG_VER_MINOR 11
#define SPDLOG_VER_PATCH 2 #define SPDLOG_VER_PATCH 0
#define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH) #define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH)