mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 02:19:35 +08:00
clang format
This commit is contained in:
@@ -48,8 +48,8 @@ struct async_factory_impl {
|
||||
}
|
||||
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger =
|
||||
std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
|
||||
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
|
||||
std::move(tp), OverflowPolicy);
|
||||
registry_inst.initialize_logger(new_logger);
|
||||
return new_logger;
|
||||
}
|
||||
@@ -59,13 +59,17 @@ using async_factory = async_factory_impl<async_overflow_policy::block>;
|
||||
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
|
||||
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
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)...);
|
||||
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)...);
|
||||
}
|
||||
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
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)...);
|
||||
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)...);
|
||||
}
|
||||
|
||||
// set global thread pool.
|
||||
@@ -73,11 +77,13 @@ 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, on_thread_stop);
|
||||
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));
|
||||
}
|
||||
|
||||
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) {
|
||||
init_thread_pool(q_size, thread_count, on_thread_start, [] {});
|
||||
}
|
||||
|
||||
@@ -87,5 +93,7 @@ inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
||||
}
|
||||
|
||||
// get the global thread pool.
|
||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() { return details::registry::instance().get_tp(); }
|
||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
|
||||
return details::registry::instance().get_tp();
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -17,17 +17,23 @@ SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
|
||||
sinks_init_list sinks_list,
|
||||
std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy)
|
||||
: async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy) {}
|
||||
: async_logger(std::move(logger_name),
|
||||
sinks_list.begin(),
|
||||
sinks_list.end(),
|
||||
std::move(tp),
|
||||
overflow_policy) {}
|
||||
|
||||
SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
|
||||
sink_ptr single_sink,
|
||||
std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy)
|
||||
: async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
|
||||
: async_logger(
|
||||
std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
|
||||
|
||||
// send the log message to the thread pool
|
||||
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
|
||||
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
|
||||
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
|
||||
pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
|
||||
}
|
||||
else {
|
||||
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
|
||||
@@ -38,7 +44,8 @@ SPDLOG_LOGGER_CATCH(msg.source)
|
||||
|
||||
// send flush request to the thread pool
|
||||
SPDLOG_INLINE void spdlog::async_logger::flush_(){
|
||||
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){pool_ptr->post_flush(shared_from_this(), overflow_policy_);
|
||||
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
|
||||
pool_ptr->post_flush(shared_from_this(), overflow_policy_);
|
||||
}
|
||||
else {
|
||||
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
|
||||
|
@@ -30,7 +30,8 @@ namespace details {
|
||||
class thread_pool;
|
||||
}
|
||||
|
||||
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>, public logger {
|
||||
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>,
|
||||
public logger {
|
||||
friend class details::thread_pool;
|
||||
|
||||
public:
|
||||
@@ -40,9 +41,9 @@ public:
|
||||
It end,
|
||||
std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy = async_overflow_policy::block)
|
||||
: logger(std::move(logger_name), begin, end),
|
||||
thread_pool_(std::move(tp)),
|
||||
overflow_policy_(overflow_policy) {}
|
||||
: logger(std::move(logger_name), begin, end)
|
||||
, thread_pool_(std::move(tp))
|
||||
, overflow_policy_(overflow_policy) {}
|
||||
|
||||
async_logger(std::string logger_name,
|
||||
sinks_init_list sinks_list,
|
||||
|
@@ -32,7 +32,9 @@ inline void load_argv_levels(int argc, const char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
inline void load_argv_levels(int argc, char **argv) { load_argv_levels(argc, const_cast<const char **>(argv)); }
|
||||
inline void load_argv_levels(int argc, char **argv) {
|
||||
load_argv_levels(argc, const_cast<const char **>(argv));
|
||||
}
|
||||
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
||||
|
@@ -22,8 +22,9 @@ namespace helpers {
|
||||
|
||||
// inplace convert to lowercase
|
||||
inline std::string &to_lower_(std::string &str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(),
|
||||
[](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](char ch) {
|
||||
return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch);
|
||||
});
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -97,7 +98,8 @@ SPDLOG_INLINE void load_levels(const std::string &input) {
|
||||
}
|
||||
}
|
||||
|
||||
details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
||||
details::registry::instance().set_levels(std::move(levels),
|
||||
global_level_found ? &global_level : nullptr);
|
||||
}
|
||||
|
||||
} // namespace helpers
|
||||
|
@@ -24,7 +24,9 @@ SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) S
|
||||
return level_string_views[l];
|
||||
}
|
||||
|
||||
SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { return short_level_names[l]; }
|
||||
SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT {
|
||||
return short_level_names[l];
|
||||
}
|
||||
|
||||
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT {
|
||||
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
|
||||
@@ -57,7 +59,9 @@ SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) {
|
||||
|
||||
SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); }
|
||||
|
||||
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) { SPDLOG_THROW(spdlog_ex(msg, last_errno)); }
|
||||
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) {
|
||||
SPDLOG_THROW(spdlog_ex(msg, last_errno));
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); }
|
||||
|
||||
|
@@ -49,7 +49,8 @@
|
||||
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000 // 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
|
||||
#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)
|
||||
@@ -180,14 +181,15 @@ using fmt_runtime_string = fmt::runtime_format_string<Char>;
|
||||
using fmt_runtime_string = fmt::basic_runtime<Char>;
|
||||
#endif
|
||||
|
||||
// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the condition from
|
||||
// basic_format_string here, in addition, fmt::basic_runtime<Char> is only convertible to basic_format_string<Char> but
|
||||
// not basic_string_view<Char>
|
||||
// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the
|
||||
// condition from basic_format_string here, in addition, fmt::basic_runtime<Char> is only
|
||||
// convertible to basic_format_string<Char> but not basic_string_view<Char>
|
||||
template <class T, class Char = char>
|
||||
struct is_convertible_to_basic_format_string
|
||||
: std::integral_constant<bool,
|
||||
std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
|
||||
std::is_same<remove_cvref_t<T>, fmt_runtime_string<Char>>::value> {};
|
||||
std::is_same<remove_cvref_t<T>, fmt_runtime_string<Char>>::value> {
|
||||
};
|
||||
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
using wstring_view_t = fmt::basic_string_view<wchar_t>;
|
||||
@@ -251,10 +253,11 @@ enum level_enum : int {
|
||||
#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
|
||||
|
||||
#if !defined(SPDLOG_LEVEL_NAMES)
|
||||
#define SPDLOG_LEVEL_NAMES \
|
||||
{ \
|
||||
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, SPDLOG_LEVEL_NAME_WARNING, \
|
||||
SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, SPDLOG_LEVEL_NAME_OFF \
|
||||
#define SPDLOG_LEVEL_NAMES \
|
||||
{ \
|
||||
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, \
|
||||
SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \
|
||||
SPDLOG_LEVEL_NAME_OFF \
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -303,9 +306,9 @@ private:
|
||||
struct source_loc {
|
||||
SPDLOG_CONSTEXPR source_loc() = default;
|
||||
SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
|
||||
: filename{filename_in},
|
||||
line{line_in},
|
||||
funcname{funcname_in} {}
|
||||
: filename{filename_in}
|
||||
, line{line_in}
|
||||
, funcname{funcname_in} {}
|
||||
|
||||
SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line == 0; }
|
||||
const char *filename{nullptr};
|
||||
@@ -315,10 +318,10 @@ struct source_loc {
|
||||
|
||||
struct file_event_handlers {
|
||||
file_event_handlers()
|
||||
: before_open(nullptr),
|
||||
after_open(nullptr),
|
||||
before_close(nullptr),
|
||||
after_close(nullptr) {}
|
||||
: 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;
|
||||
@@ -330,18 +333,26 @@ namespace details {
|
||||
|
||||
// to_string_view
|
||||
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT {
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t
|
||||
to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT {
|
||||
return spdlog::string_view_t{buf.data(), buf.size()};
|
||||
}
|
||||
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t str) SPDLOG_NOEXCEPT { return str; }
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t
|
||||
to_string_view(spdlog::string_view_t str) SPDLOG_NOEXCEPT {
|
||||
return str;
|
||||
}
|
||||
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) SPDLOG_NOEXCEPT {
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t
|
||||
to_string_view(const wmemory_buf_t &buf) SPDLOG_NOEXCEPT {
|
||||
return spdlog::wstring_view_t{buf.data(), buf.size()};
|
||||
}
|
||||
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) SPDLOG_NOEXCEPT { return str; }
|
||||
SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t
|
||||
to_string_view(spdlog::wstring_view_t str) SPDLOG_NOEXCEPT {
|
||||
return str;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef SPDLOG_USE_STD_FORMAT
|
||||
|
@@ -25,8 +25,7 @@ public:
|
||||
|
||||
explicit circular_q(size_t max_items)
|
||||
: max_items_(max_items + 1) // one item is reserved as marker for full q
|
||||
,
|
||||
v_(max_items_) {}
|
||||
, v_(max_items_) {}
|
||||
|
||||
circular_q(const circular_q &) = default;
|
||||
circular_q &operator=(const circular_q &) = default;
|
||||
|
@@ -59,7 +59,8 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) {
|
||||
details::os::sleep_for_millis(open_interval_);
|
||||
}
|
||||
|
||||
throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
|
||||
throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing",
|
||||
errno);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void file_helper::reopen(bool truncate) {
|
||||
@@ -126,7 +127,8 @@ SPDLOG_INLINE const filename_t &file_helper::filename() const { return filename_
|
||||
// ".mylog" => (".mylog". "")
|
||||
// "my_folder/.mylog" => ("my_folder/.mylog", "")
|
||||
// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
|
||||
SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension(const filename_t &fname) {
|
||||
SPDLOG_INLINE std::tuple<filename_t, filename_t>
|
||||
file_helper::split_by_extension(const filename_t &fname) {
|
||||
auto ext_index = fname.rfind('.');
|
||||
|
||||
// no valid extension found - return whole path and empty string as
|
||||
|
@@ -68,7 +68,8 @@ SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n) {
|
||||
|
||||
template <typename T>
|
||||
inline unsigned int count_digits(T n) {
|
||||
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
|
||||
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
|
||||
|
@@ -17,16 +17,14 @@ SPDLOG_INLINE log_msg::log_msg(spdlog::log_clock::time_point log_time,
|
||||
string_view_t a_logger_name,
|
||||
spdlog::level::level_enum lvl,
|
||||
spdlog::string_view_t msg)
|
||||
: logger_name(a_logger_name),
|
||||
level(lvl),
|
||||
time(log_time)
|
||||
: logger_name(a_logger_name)
|
||||
, level(lvl)
|
||||
, time(log_time)
|
||||
#ifndef SPDLOG_NO_THREAD_ID
|
||||
,
|
||||
thread_id(os::thread_id())
|
||||
, thread_id(os::thread_id())
|
||||
#endif
|
||||
,
|
||||
source(loc),
|
||||
payload(msg) {
|
||||
, source(loc)
|
||||
, payload(msg) {
|
||||
}
|
||||
|
||||
SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc,
|
||||
@@ -35,7 +33,9 @@ SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc,
|
||||
spdlog::string_view_t msg)
|
||||
: log_msg(os::now(), loc, a_logger_name, lvl, msg) {}
|
||||
|
||||
SPDLOG_INLINE log_msg::log_msg(string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
|
||||
SPDLOG_INLINE log_msg::log_msg(string_view_t a_logger_name,
|
||||
spdlog::level::level_enum lvl,
|
||||
spdlog::string_view_t msg)
|
||||
: log_msg(os::now(), source_loc{}, a_logger_name, lvl, msg) {}
|
||||
|
||||
} // namespace details
|
||||
|
@@ -24,8 +24,9 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
|
||||
update_string_views();
|
||||
}
|
||||
|
||||
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT : log_msg{other},
|
||||
buffer{std::move(other.buffer)} {
|
||||
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT
|
||||
: log_msg{other},
|
||||
buffer{std::move(other.buffer)} {
|
||||
update_string_views();
|
||||
}
|
||||
|
||||
|
@@ -79,8 +79,8 @@ SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT {
|
||||
timespec ts;
|
||||
::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
|
||||
return std::chrono::time_point<log_clock, typename log_clock::duration>(
|
||||
std::chrono::duration_cast<typename log_clock::duration>(std::chrono::seconds(ts.tv_sec) +
|
||||
std::chrono::nanoseconds(ts.tv_nsec)));
|
||||
std::chrono::duration_cast<typename log_clock::duration>(
|
||||
std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec)));
|
||||
|
||||
#else
|
||||
return log_clock::now();
|
||||
@@ -140,7 +140,8 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
|
||||
#else // unix
|
||||
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
||||
const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
|
||||
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) {
|
||||
return true;
|
||||
}
|
||||
@@ -269,7 +270,8 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
|
||||
return offset;
|
||||
#else
|
||||
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX) || \
|
||||
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
|
||||
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||
struct helper {
|
||||
@@ -407,17 +409,18 @@ SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT {
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr std::array<const char *, 16> terms = {{"ansi", "color", "console", "cygwin", "gnome",
|
||||
"konsole", "kterm", "linux", "msys", "putty", "rxvt",
|
||||
"screen", "vt100", "xterm", "alacritty", "vt102"}};
|
||||
static constexpr std::array<const char *, 16> terms = {
|
||||
{"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys",
|
||||
"putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}};
|
||||
|
||||
const char *env_term_p = std::getenv("TERM");
|
||||
if (env_term_p == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return std::any_of(terms.begin(), terms.end(),
|
||||
[&](const char *term) { return std::strstr(env_term_p, term) != nullptr; });
|
||||
return std::any_of(terms.begin(), terms.end(), [&](const char *term) {
|
||||
return std::strstr(env_term_p, term) != nullptr;
|
||||
});
|
||||
}();
|
||||
|
||||
return result;
|
||||
@@ -449,12 +452,14 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
|
||||
|
||||
int result_size = static_cast<int>(target.capacity());
|
||||
if ((wstr_size + 1) * 2 > result_size) {
|
||||
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
|
||||
result_size =
|
||||
::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
|
||||
}
|
||||
|
||||
if (result_size > 0) {
|
||||
target.resize(result_size);
|
||||
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL);
|
||||
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(),
|
||||
result_size, NULL, NULL);
|
||||
|
||||
if (result_size > 0) {
|
||||
target.resize(result_size);
|
||||
@@ -462,7 +467,8 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
|
||||
}
|
||||
}
|
||||
|
||||
throw_spdlog_ex(fmt_lib::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) {
|
||||
@@ -477,21 +483,24 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
||||
}
|
||||
|
||||
// find the size to allocate for the result buffer
|
||||
int result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
||||
int result_size =
|
||||
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
||||
|
||||
if (result_size > 0) {
|
||||
target.resize(result_size);
|
||||
result_size =
|
||||
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
|
||||
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size,
|
||||
target.data(), result_size);
|
||||
if (result_size > 0) {
|
||||
assert(result_size == target.size());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw_spdlog_ex(fmt_lib::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)
|
||||
|
||||
// return true on success
|
||||
static SPDLOG_INLINE bool mkdir_(const filename_t &path) {
|
||||
|
@@ -41,7 +41,8 @@ SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL;
|
||||
#endif
|
||||
|
||||
SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS;
|
||||
SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
|
||||
SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] =
|
||||
SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
|
||||
|
||||
// fopen_s on non windows for writing
|
||||
SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
|
||||
|
@@ -7,7 +7,8 @@
|
||||
//
|
||||
// RAII over the owned thread:
|
||||
// creates the thread on construction.
|
||||
// stops and joins the thread on destruction (if the thread is executing a callback, wait for it to finish first).
|
||||
// stops and joins the thread on destruction (if the thread is executing a callback, wait for it
|
||||
// to finish first).
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
@@ -20,7 +21,8 @@ namespace details {
|
||||
class SPDLOG_API periodic_worker {
|
||||
public:
|
||||
template <typename Rep, typename Period>
|
||||
periodic_worker(const std::function<void()> &callback_fun, std::chrono::duration<Rep, Period> interval) {
|
||||
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;
|
||||
|
@@ -170,7 +170,8 @@ SPDLOG_INLINE void registry::set_error_handler(err_handler handler) {
|
||||
err_handler_ = std::move(handler);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
|
||||
SPDLOG_INLINE void
|
||||
registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
for (auto &l : loggers_) {
|
||||
fun(l.second);
|
||||
|
@@ -38,7 +38,8 @@ public:
|
||||
// Return raw ptr to the default logger.
|
||||
// To be used directly by the spdlog default api (e.g. spdlog::info)
|
||||
// This make the default API faster, but cannot be used concurrently with set_default_logger().
|
||||
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
|
||||
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from
|
||||
// another.
|
||||
logger *get_default_raw();
|
||||
|
||||
// set default logger.
|
||||
|
@@ -34,8 +34,9 @@ class tcp_client {
|
||||
|
||||
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);
|
||||
::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("tcp_sink - {}: {}", msg, buf));
|
||||
}
|
||||
@@ -104,7 +105,8 @@ public:
|
||||
|
||||
// set TCP_NODELAY
|
||||
int enable_flag = 1;
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag),
|
||||
sizeof(enable_flag));
|
||||
}
|
||||
|
||||
// Send exactly n_bytes of the given data.
|
||||
@@ -113,7 +115,8 @@ public:
|
||||
size_t bytes_sent = 0;
|
||||
while (bytes_sent < n_bytes) {
|
||||
const int send_flags = 0;
|
||||
auto write_result = ::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags);
|
||||
auto write_result =
|
||||
::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags);
|
||||
if (write_result == SOCKET_ERROR) {
|
||||
int last_error = ::WSAGetLastError();
|
||||
close();
|
||||
|
@@ -84,11 +84,13 @@ public:
|
||||
|
||||
// set TCP_NODELAY
|
||||
int enable_flag = 1;
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag),
|
||||
sizeof(enable_flag));
|
||||
|
||||
// prevent sigpipe on systems where MSG_NOSIGNAL is not available
|
||||
#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<char *>(&enable_flag),
|
||||
sizeof(enable_flag));
|
||||
#endif
|
||||
|
||||
#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
||||
@@ -106,7 +108,8 @@ public:
|
||||
#else
|
||||
const int send_flags = 0;
|
||||
#endif
|
||||
auto write_result = ::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags);
|
||||
auto write_result =
|
||||
::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags);
|
||||
if (write_result < 0) {
|
||||
close();
|
||||
throw_spdlog_ex("write(2) failed", errno);
|
||||
|
@@ -31,7 +31,9 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
: 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)
|
||||
@@ -59,7 +61,8 @@ void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr,
|
||||
post_async_msg_(std::move(async_m), overflow_policy);
|
||||
}
|
||||
|
||||
void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) {
|
||||
void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr,
|
||||
async_overflow_policy overflow_policy) {
|
||||
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
||||
}
|
||||
|
||||
@@ -73,7 +76,8 @@ void SPDLOG_INLINE thread_pool::reset_discard_counter() { q_.reset_discard_count
|
||||
|
||||
size_t SPDLOG_INLINE thread_pool::queue_size() { return q_.size(); }
|
||||
|
||||
void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) {
|
||||
void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg,
|
||||
async_overflow_policy overflow_policy) {
|
||||
if (overflow_policy == async_overflow_policy::block) {
|
||||
q_.enqueue(std::move(new_msg));
|
||||
} else if (overflow_policy == async_overflow_policy::overrun_oldest) {
|
||||
|
@@ -37,9 +37,9 @@ struct async_msg : log_msg_buffer {
|
||||
// support for vs2013 move
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
||||
async_msg(async_msg &&other)
|
||||
: log_msg_buffer(std::move(other)),
|
||||
msg_type(other.msg_type),
|
||||
worker_ptr(std::move(other.worker_ptr)) {}
|
||||
: log_msg_buffer(std::move(other))
|
||||
, msg_type(other.msg_type)
|
||||
, worker_ptr(std::move(other.worker_ptr)) {}
|
||||
|
||||
async_msg &operator=(async_msg &&other) {
|
||||
*static_cast<log_msg_buffer *>(this) = std::move(other);
|
||||
@@ -54,14 +54,14 @@ struct async_msg : log_msg_buffer {
|
||||
|
||||
// construct from log_msg with given type
|
||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
|
||||
: log_msg_buffer{m},
|
||||
msg_type{the_type},
|
||||
worker_ptr{std::move(worker)} {}
|
||||
: log_msg_buffer{m}
|
||||
, msg_type{the_type}
|
||||
, worker_ptr{std::move(worker)} {}
|
||||
|
||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
|
||||
: log_msg_buffer{},
|
||||
msg_type{the_type},
|
||||
worker_ptr{std::move(worker)} {}
|
||||
: log_msg_buffer{}
|
||||
, msg_type{the_type}
|
||||
, worker_ptr{std::move(worker)} {}
|
||||
|
||||
explicit async_msg(async_msg_type the_type)
|
||||
: async_msg{nullptr, the_type} {}
|
||||
@@ -85,7 +85,9 @@ public:
|
||||
thread_pool(const thread_pool &) = delete;
|
||||
thread_pool &operator=(thread_pool &&) = delete;
|
||||
|
||||
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);
|
||||
size_t overrun_counter();
|
||||
void reset_overrun_counter();
|
||||
|
@@ -38,8 +38,9 @@ class udp_client {
|
||||
|
||||
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);
|
||||
::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));
|
||||
}
|
||||
@@ -73,8 +74,8 @@ public:
|
||||
}
|
||||
|
||||
int option_value = TX_BUFFER_SIZE;
|
||||
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value),
|
||||
sizeof(option_value)) < 0) {
|
||||
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);
|
||||
@@ -87,7 +88,8 @@ public:
|
||||
|
||||
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) {
|
||||
if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_,
|
||||
tolen) == -1) {
|
||||
throw_spdlog_ex("sendto(2) failed", errno);
|
||||
}
|
||||
}
|
||||
|
@@ -45,8 +45,8 @@ public:
|
||||
}
|
||||
|
||||
int option_value = TX_BUFFER_SIZE;
|
||||
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value),
|
||||
sizeof(option_value)) < 0) {
|
||||
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!");
|
||||
}
|
||||
@@ -71,7 +71,8 @@ public:
|
||||
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) {
|
||||
if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) ==
|
||||
-1) {
|
||||
throw_spdlog_ex("sendto(2) failed", errno);
|
||||
}
|
||||
}
|
||||
|
@@ -43,9 +43,9 @@ template <typename It>
|
||||
class dump_info {
|
||||
public:
|
||||
dump_info(It range_begin, It range_end, size_t size_per_line)
|
||||
: begin_(range_begin),
|
||||
end_(range_end),
|
||||
size_per_line_(size_per_line) {}
|
||||
: begin_(range_begin)
|
||||
, end_(range_end)
|
||||
, size_per_line_(size_per_line) {}
|
||||
|
||||
// do not use begin() and end() to avoid collision with fmt/ranges
|
||||
It get_begin() const { return begin_; }
|
||||
@@ -62,7 +62,8 @@ private:
|
||||
template <typename Container>
|
||||
inline details::dump_info<typename Container::const_iterator> to_hex(const Container &container,
|
||||
size_t size_per_line = 32) {
|
||||
static_assert(sizeof(typename Container::value_type) == 1, "sizeof(Container::value_type) != 1");
|
||||
static_assert(sizeof(typename Container::value_type) == 1,
|
||||
"sizeof(Container::value_type) != 1");
|
||||
using Iter = typename Container::const_iterator;
|
||||
return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
|
||||
}
|
||||
@@ -70,10 +71,11 @@ inline details::dump_info<typename Container::const_iterator> to_hex(const Conta
|
||||
#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) {
|
||||
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");
|
||||
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);
|
||||
}
|
||||
@@ -82,7 +84,8 @@ inline details::dump_info<typename std::span<Value, Extent>::iterator> to_hex(co
|
||||
|
||||
// create dump_info from ranges
|
||||
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) {
|
||||
return details::dump_info<It>(range_begin, range_end, size_per_line);
|
||||
}
|
||||
|
||||
@@ -155,7 +158,8 @@ struct formatter<spdlog::details::dump_info<T>, char> {
|
||||
for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) {
|
||||
auto ch = static_cast<unsigned char>(*i);
|
||||
|
||||
if (put_newlines && (i == the_range.get_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.get_begin()) {
|
||||
*inserter++ = delimiter;
|
||||
*inserter++ = delimiter;
|
||||
|
@@ -5,8 +5,8 @@
|
||||
|
||||
#pragma once
|
||||
//
|
||||
// include bundled or external copy of fmtlib's std support (for formatting e.g. std::filesystem::path, std::thread::id,
|
||||
// std::monostate, std::variant, ...)
|
||||
// include bundled or external copy of fmtlib's std support (for formatting e.g.
|
||||
// std::filesystem::path, std::thread::id, std::monostate, std::variant, ...)
|
||||
//
|
||||
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT)
|
||||
|
@@ -17,12 +17,12 @@ namespace spdlog {
|
||||
|
||||
// public methods
|
||||
SPDLOG_INLINE logger::logger(const logger &other)
|
||||
: name_(other.name_),
|
||||
sinks_(other.sinks_),
|
||||
level_(other.level_.load(std::memory_order_relaxed)),
|
||||
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
||||
custom_err_handler_(other.custom_err_handler_),
|
||||
tracer_(other.tracer_) {}
|
||||
: name_(other.name_)
|
||||
, sinks_(other.sinks_)
|
||||
, level_(other.level_.load(std::memory_order_relaxed))
|
||||
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
||||
, custom_err_handler_(other.custom_err_handler_)
|
||||
, tracer_(other.tracer_) {}
|
||||
|
||||
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT
|
||||
: name_(std::move(other.name_)),
|
||||
@@ -109,7 +109,9 @@ SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const { return sinks_
|
||||
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks() { return sinks_; }
|
||||
|
||||
// error handler
|
||||
SPDLOG_INLINE void logger::set_error_handler(err_handler handler) { custom_err_handler_ = std::move(handler); }
|
||||
SPDLOG_INLINE void logger::set_error_handler(err_handler handler) {
|
||||
custom_err_handler_ = std::move(handler);
|
||||
}
|
||||
|
||||
// create new logger with same sinks and configuration.
|
||||
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name) {
|
||||
@@ -119,7 +121,8 @@ SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name) {
|
||||
}
|
||||
|
||||
// protected methods
|
||||
SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) {
|
||||
SPDLOG_INLINE void
|
||||
logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) {
|
||||
if (log_enabled) {
|
||||
sink_it_(log_msg);
|
||||
}
|
||||
@@ -151,9 +154,11 @@ SPDLOG_INLINE void logger::flush_() {
|
||||
SPDLOG_INLINE void logger::dump_backtrace_() {
|
||||
using details::log_msg;
|
||||
if (tracer_.enabled() && !tracer_.empty()) {
|
||||
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
|
||||
sink_it_(
|
||||
log_msg{name(), level::info, "****************** Backtrace Start ******************"});
|
||||
tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
|
||||
sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
|
||||
sink_it_(
|
||||
log_msg{name(), level::info, "****************** Backtrace End ********************"});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,10 +186,11 @@ SPDLOG_INLINE void logger::err_handler_(const std::string &msg) {
|
||||
char date_buf[64];
|
||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||
#if defined(USING_R) && defined(R_R_H) // if in R environment
|
||||
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
||||
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(),
|
||||
msg.c_str());
|
||||
#else
|
||||
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(),
|
||||
msg.c_str());
|
||||
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf,
|
||||
name().c_str(), msg.c_str());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@@ -28,18 +28,18 @@
|
||||
#include <vector>
|
||||
|
||||
#ifndef SPDLOG_NO_EXCEPTIONS
|
||||
#define SPDLOG_LOGGER_CATCH(location) \
|
||||
catch (const std::exception &ex) { \
|
||||
if (location.filename) { \
|
||||
err_handler_( \
|
||||
fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), location.filename, location.line)); \
|
||||
} else { \
|
||||
err_handler_(ex.what()); \
|
||||
} \
|
||||
} \
|
||||
catch (...) { \
|
||||
err_handler_("Rethrowing unknown exception in logger"); \
|
||||
throw; \
|
||||
#define SPDLOG_LOGGER_CATCH(location) \
|
||||
catch (const std::exception &ex) { \
|
||||
if (location.filename) { \
|
||||
err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), \
|
||||
location.filename, location.line)); \
|
||||
} else { \
|
||||
err_handler_(ex.what()); \
|
||||
} \
|
||||
} \
|
||||
catch (...) { \
|
||||
err_handler_("Rethrowing unknown exception in logger"); \
|
||||
throw; \
|
||||
}
|
||||
#else
|
||||
#define SPDLOG_LOGGER_CATCH(location)
|
||||
@@ -51,14 +51,14 @@ class SPDLOG_API logger {
|
||||
public:
|
||||
// Empty logger
|
||||
explicit logger(std::string name)
|
||||
: name_(std::move(name)),
|
||||
sinks_() {}
|
||||
: name_(std::move(name))
|
||||
, sinks_() {}
|
||||
|
||||
// Logger with range on sinks
|
||||
template <typename It>
|
||||
logger(std::string name, It begin, It end)
|
||||
: name_(std::move(name)),
|
||||
sinks_(begin, end) {}
|
||||
: name_(std::move(name))
|
||||
, sinks_(begin, end) {}
|
||||
|
||||
// Logger with single sink
|
||||
logger(std::string name, sink_ptr single_sink)
|
||||
@@ -91,12 +91,15 @@ public:
|
||||
}
|
||||
|
||||
// T cannot be statically converted to format string (including string_view/wstring_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) {
|
||||
log(loc, lvl, "{}", msg);
|
||||
}
|
||||
|
||||
void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg) {
|
||||
void
|
||||
log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg) {
|
||||
bool log_enabled = should_log(lvl);
|
||||
bool traceback_enabled = tracer_.enabled();
|
||||
if (!log_enabled && !traceback_enabled) {
|
||||
@@ -161,7 +164,8 @@ public:
|
||||
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) {
|
||||
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) {
|
||||
@@ -251,7 +255,9 @@ public:
|
||||
}
|
||||
|
||||
// return true logging is enabled for the given level.
|
||||
bool should_log(level::level_enum msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); }
|
||||
bool should_log(level::level_enum msg_level) const {
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// return true if backtrace logging is enabled.
|
||||
bool should_backtrace() const { return tracer_.enabled(); }
|
||||
|
@@ -37,8 +37,8 @@ namespace details {
|
||||
class scoped_padder {
|
||||
public:
|
||||
scoped_padder(size_t wrapped_size, const padding_info &padinfo, memory_buf_t &dest)
|
||||
: padinfo_(padinfo),
|
||||
dest_(dest) {
|
||||
: padinfo_(padinfo)
|
||||
, dest_(dest) {
|
||||
remaining_pad_ = static_cast<long>(padinfo.width_) - static_cast<long>(wrapped_size);
|
||||
if (remaining_pad_ <= 0) {
|
||||
return;
|
||||
@@ -71,7 +71,8 @@ public:
|
||||
|
||||
private:
|
||||
void pad_it(long count) {
|
||||
fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast<size_t>(count)), dest_);
|
||||
fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast<size_t>(count)),
|
||||
dest_);
|
||||
}
|
||||
|
||||
const padding_info &padinfo_;
|
||||
@@ -81,7 +82,9 @@ private:
|
||||
};
|
||||
|
||||
struct null_scoped_padder {
|
||||
null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {}
|
||||
null_scoped_padder(size_t /*wrapped_size*/,
|
||||
const padding_info & /*padinfo*/,
|
||||
memory_buf_t & /*dest*/) {}
|
||||
|
||||
template <typename T>
|
||||
static unsigned int count_digits(T /* number */) {
|
||||
@@ -188,8 +191,9 @@ public:
|
||||
};
|
||||
|
||||
// Full month name
|
||||
static const std::array<const char *, 12> full_months{{"January", "February", "March", "April", "May", "June", "July",
|
||||
"August", "September", "October", "November", "December"}};
|
||||
static const std::array<const char *, 12> full_months{{"January", "February", "March", "April",
|
||||
"May", "June", "July", "August", "September",
|
||||
"October", "November", "December"}};
|
||||
|
||||
template <typename ScopedPadder>
|
||||
class B_formatter final : public flag_formatter {
|
||||
@@ -586,7 +590,9 @@ public:
|
||||
explicit ch_formatter(char ch)
|
||||
: ch_(ch) {}
|
||||
|
||||
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { dest.push_back(ch_); }
|
||||
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
|
||||
dest.push_back(ch_);
|
||||
}
|
||||
|
||||
private:
|
||||
char ch_;
|
||||
@@ -643,8 +649,8 @@ public:
|
||||
size_t text_size;
|
||||
if (padinfo_.enabled()) {
|
||||
// calc text size for padding based on "filename:line"
|
||||
text_size =
|
||||
std::char_traits<char>::length(msg.source.filename) + ScopedPadder::count_digits(msg.source.line) + 1;
|
||||
text_size = std::char_traits<char>::length(msg.source.filename) +
|
||||
ScopedPadder::count_digits(msg.source.line) + 1;
|
||||
} else {
|
||||
text_size = 0;
|
||||
}
|
||||
@@ -668,7 +674,8 @@ public:
|
||||
ScopedPadder p(0, padinfo_, dest);
|
||||
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;
|
||||
ScopedPadder p(text_size, padinfo_, dest);
|
||||
fmt_helper::append_string_view(msg.source.filename, dest);
|
||||
}
|
||||
@@ -694,7 +701,8 @@ public:
|
||||
const std::reverse_iterator<const char *> begin(filename + std::strlen(filename));
|
||||
const std::reverse_iterator<const char *> end(filename);
|
||||
|
||||
const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps), std::end(os::folder_seps) - 1);
|
||||
const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps),
|
||||
std::end(os::folder_seps) - 1);
|
||||
return it != end ? it.base() : filename;
|
||||
}
|
||||
}
|
||||
@@ -744,7 +752,8 @@ public:
|
||||
ScopedPadder p(0, padinfo_, dest);
|
||||
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;
|
||||
ScopedPadder p(text_size, padinfo_, dest);
|
||||
fmt_helper::append_string_view(msg.source.funcname, dest);
|
||||
}
|
||||
@@ -757,8 +766,8 @@ public:
|
||||
using DurationUnits = Units;
|
||||
|
||||
explicit elapsed_formatter(padding_info padinfo)
|
||||
: flag_formatter(padinfo),
|
||||
last_message_time_(log_clock::now()) {}
|
||||
: flag_formatter(padinfo)
|
||||
, last_message_time_(log_clock::now()) {}
|
||||
|
||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override {
|
||||
auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero());
|
||||
@@ -841,7 +850,8 @@ public:
|
||||
if (!msg.source.empty()) {
|
||||
dest.push_back('[');
|
||||
const char *filename =
|
||||
details::short_filename_formatter<details::null_scoped_padder>::basename(msg.source.filename);
|
||||
details::short_filename_formatter<details::null_scoped_padder>::basename(
|
||||
msg.source.filename);
|
||||
fmt_helper::append_string_view(filename, dest);
|
||||
dest.push_back(':');
|
||||
fmt_helper::append_int(msg.source.line, dest);
|
||||
@@ -863,23 +873,23 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(std::string pattern,
|
||||
pattern_time_type time_type,
|
||||
std::string eol,
|
||||
custom_flags custom_user_flags)
|
||||
: pattern_(std::move(pattern)),
|
||||
eol_(std::move(eol)),
|
||||
pattern_time_type_(time_type),
|
||||
need_localtime_(false),
|
||||
last_log_secs_(0),
|
||||
custom_handlers_(std::move(custom_user_flags)) {
|
||||
: pattern_(std::move(pattern))
|
||||
, eol_(std::move(eol))
|
||||
, pattern_time_type_(time_type)
|
||||
, need_localtime_(false)
|
||||
, last_log_secs_(0)
|
||||
, custom_handlers_(std::move(custom_user_flags)) {
|
||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||
compile_pattern_(pattern_);
|
||||
}
|
||||
|
||||
// use by default full formatter for if pattern is not given
|
||||
SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol)
|
||||
: pattern_("%+"),
|
||||
eol_(std::move(eol)),
|
||||
pattern_time_type_(time_type),
|
||||
need_localtime_(true),
|
||||
last_log_secs_(0) {
|
||||
: pattern_("%+")
|
||||
, eol_(std::move(eol))
|
||||
, pattern_time_type_(time_type)
|
||||
, need_localtime_(true)
|
||||
, last_log_secs_(0) {
|
||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||
formatters_.push_back(details::make_unique<details::full_formatter>(details::padding_info{}));
|
||||
}
|
||||
@@ -901,7 +911,8 @@ SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const {
|
||||
|
||||
SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) {
|
||||
if (need_localtime_) {
|
||||
const auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||
const auto secs =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||
if (secs != last_log_secs_) {
|
||||
cached_tm_ = get_time_(msg);
|
||||
last_log_secs_ = secs;
|
||||
@@ -957,7 +968,8 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
|
||||
break;
|
||||
|
||||
case 'L': // short level
|
||||
formatters_.push_back(details::make_unique<details::short_level_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::short_level_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('t'): // thread id
|
||||
@@ -1095,23 +1107,28 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
|
||||
break;
|
||||
|
||||
case ('@'): // source location (filename:filenumber)
|
||||
formatters_.push_back(details::make_unique<details::source_location_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::source_location_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('s'): // short source filename - without directory name
|
||||
formatters_.push_back(details::make_unique<details::short_filename_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::short_filename_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('g'): // full source filename
|
||||
formatters_.push_back(details::make_unique<details::source_filename_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::source_filename_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('#'): // source line number
|
||||
formatters_.push_back(details::make_unique<details::source_linenum_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::source_linenum_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('!'): // source funcname
|
||||
formatters_.push_back(details::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('%'): // % char
|
||||
@@ -1120,21 +1137,26 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
|
||||
|
||||
case ('u'): // elapsed time since last log message in nanos
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::nanoseconds>>(padding));
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::nanoseconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
case ('i'): // elapsed time since last log message in micros
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::microseconds>>(padding));
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::microseconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
case ('o'): // elapsed time since last log message in millis
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::milliseconds>>(padding));
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::milliseconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
case ('O'): // elapsed time since last log message in seconds
|
||||
formatters_.push_back(details::make_unique<details::elapsed_formatter<Padder, std::chrono::seconds>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::elapsed_formatter<Padder, std::chrono::seconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
default: // Unknown flag appears as is
|
||||
@@ -1145,12 +1167,13 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
|
||||
unknown_flag->add_ch(flag);
|
||||
formatters_.push_back((std::move(unknown_flag)));
|
||||
}
|
||||
// fix issue #1617 (prev char was '!' and should have been treated as funcname flag instead of truncating flag)
|
||||
// spdlog::set_pattern("[%10!] %v") => "[ main] some message"
|
||||
// fix issue #1617 (prev char was '!' and should have been treated as funcname flag instead
|
||||
// of truncating flag) spdlog::set_pattern("[%10!] %v") => "[ main] some message"
|
||||
// spdlog::set_pattern("[%3!!] %v") => "[mai] some message"
|
||||
else {
|
||||
padding.truncate_ = false;
|
||||
formatters_.push_back(details::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
details::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
unknown_flag->add_ch(flag);
|
||||
formatters_.push_back((std::move(unknown_flag)));
|
||||
}
|
||||
@@ -1162,8 +1185,9 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
|
||||
// Extract given pad spec (e.g. %8X, %=8X, %-8!X, %8!X, %=8!X, %-8!X, %+8!X)
|
||||
// Advance the given it pass the end of the padding spec found (if any)
|
||||
// Return padding.
|
||||
SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::string::const_iterator &it,
|
||||
std::string::const_iterator end) {
|
||||
SPDLOG_INLINE details::padding_info
|
||||
pattern_formatter::handle_padspec_(std::string::const_iterator &it,
|
||||
std::string::const_iterator end) {
|
||||
using details::padding_info;
|
||||
using details::scoped_padder;
|
||||
const size_t max_width = 64;
|
||||
|
@@ -25,10 +25,10 @@ struct padding_info {
|
||||
|
||||
padding_info() = default;
|
||||
padding_info(size_t width, padding_info::pad_side side, bool truncate)
|
||||
: width_(width),
|
||||
side_(side),
|
||||
truncate_(truncate),
|
||||
enabled_(true) {}
|
||||
: width_(width)
|
||||
, side_(side)
|
||||
, truncate_(truncate)
|
||||
, enabled_(true) {}
|
||||
|
||||
bool enabled() const { return enabled_; }
|
||||
size_t width_ = 0;
|
||||
@@ -43,7 +43,8 @@ public:
|
||||
: padinfo_(padinfo) {}
|
||||
flag_formatter() = default;
|
||||
virtual ~flag_formatter() = default;
|
||||
virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
|
||||
virtual void
|
||||
format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
|
||||
|
||||
protected:
|
||||
padding_info padinfo_;
|
||||
@@ -55,7 +56,9 @@ class SPDLOG_API custom_flag_formatter : public details::flag_formatter {
|
||||
public:
|
||||
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
|
||||
|
||||
void set_padding_info(const details::padding_info &padding) { flag_formatter::padinfo_ = padding; }
|
||||
void set_padding_info(const details::padding_info &padding) {
|
||||
flag_formatter::padinfo_ = padding;
|
||||
}
|
||||
};
|
||||
|
||||
class SPDLOG_API pattern_formatter final : public formatter {
|
||||
@@ -102,7 +105,8 @@ private:
|
||||
// Extract given pad spec (e.g. %8X)
|
||||
// Advance the given it pass the end of the padding spec found (if any)
|
||||
// Return padding.
|
||||
static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
|
||||
static details::padding_info handle_padspec_(std::string::const_iterator &it,
|
||||
std::string::const_iterator end);
|
||||
|
||||
void compile_pattern_(const std::string &pattern);
|
||||
};
|
||||
|
@@ -27,14 +27,15 @@ namespace sinks {
|
||||
|
||||
/*
|
||||
* Android sink
|
||||
* (logging using __android_log_write or __android_log_buf_write depending on the specified BufferID)
|
||||
* (logging using __android_log_write or __android_log_buf_write depending on the specified
|
||||
* BufferID)
|
||||
*/
|
||||
template <typename Mutex, int BufferID = log_id::LOG_ID_MAIN>
|
||||
class android_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit android_sink(std::string tag = "spdlog", bool use_raw_msg = false)
|
||||
: tag_(std::move(tag)),
|
||||
use_raw_msg_(use_raw_msg) {}
|
||||
: tag_(std::move(tag))
|
||||
, use_raw_msg_(use_raw_msg) {}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
@@ -68,10 +69,10 @@ protected:
|
||||
void flush_() override {}
|
||||
|
||||
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 explicitly provides a non-default log buffer. Otherwise, when using the default
|
||||
// log buffer, always log via __android_log_write.
|
||||
// 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 explicitly 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) {
|
||||
@@ -120,12 +121,14 @@ using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
|
||||
// Create and register android syslog logger
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") {
|
||||
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name,
|
||||
const std::string &tag = "spdlog") {
|
||||
return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") {
|
||||
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name,
|
||||
const std::string &tag = "spdlog") {
|
||||
return Factory::template create<sinks::android_sink_st>(logger_name, tag);
|
||||
}
|
||||
|
||||
|
@@ -15,9 +15,9 @@ namespace sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
||||
: target_file_(target_file),
|
||||
mutex_(ConsoleMutex::mutex()),
|
||||
formatter_(details::make_unique<spdlog::pattern_formatter>())
|
||||
: target_file_(target_file)
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
, formatter_(details::make_unique<spdlog::pattern_formatter>())
|
||||
|
||||
{
|
||||
set_color_mode(mode);
|
||||
@@ -31,7 +31,8 @@ SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, co
|
||||
}
|
||||
|
||||
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_);
|
||||
colors_.at(static_cast<size_t>(color_level)) = to_string_(color);
|
||||
}
|
||||
@@ -74,7 +75,8 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
SPDLOG_INLINE void
|
||||
ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
@@ -91,7 +93,8 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
|
||||
should_do_colors_ = true;
|
||||
return;
|
||||
case color_mode::automatic:
|
||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
should_do_colors_ =
|
||||
details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
return;
|
||||
case color_mode::never:
|
||||
should_do_colors_ = false;
|
||||
@@ -107,7 +110,9 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
|
||||
size_t start,
|
||||
size_t end) {
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
|
||||
|
@@ -18,7 +18,8 @@ SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink()
|
||||
: formatter_{details::make_unique<spdlog::pattern_formatter>()} {}
|
||||
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink(std::unique_ptr<spdlog::formatter> formatter)
|
||||
SPDLOG_INLINE
|
||||
spdlog::sinks::base_sink<Mutex>::base_sink(std::unique_ptr<spdlog::formatter> formatter)
|
||||
: formatter_{std::move(formatter)} {}
|
||||
|
||||
template <typename Mutex>
|
||||
@@ -40,7 +41,8 @@ void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern(const std::strin
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void SPDLOG_INLINE
|
||||
spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
set_formatter_(std::move(sink_formatter));
|
||||
}
|
||||
@@ -51,6 +53,7 @@ void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::stri
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void SPDLOG_INLINE
|
||||
spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
@@ -45,7 +45,8 @@ 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, event_handlers);
|
||||
return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate,
|
||||
event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@@ -53,7 +54,8 @@ 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, event_handlers);
|
||||
return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate,
|
||||
event_handlers);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@@ -42,12 +42,14 @@ using callback_sink_st = callback_sink<details::null_mutex>;
|
||||
// factory functions
|
||||
//
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback) {
|
||||
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name,
|
||||
const custom_log_callback &callback) {
|
||||
return Factory::template create<sinks::callback_sink_mt>(logger_name, callback);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callback &callback) {
|
||||
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name,
|
||||
const custom_log_callback &callback) {
|
||||
return Factory::template create<sinks::callback_sink_st>(logger_name, callback);
|
||||
}
|
||||
|
||||
|
@@ -31,16 +31,19 @@ struct daily_filename_calculator {
|
||||
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
|
||||
filename_t basename, ext;
|
||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||
return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")), basename,
|
||||
now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext);
|
||||
return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")),
|
||||
basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday,
|
||||
ext);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Generator of daily log file names with strftime format.
|
||||
* Usages:
|
||||
* auto sink = std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour,
|
||||
* minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)"
|
||||
* auto sink =
|
||||
* std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour,
|
||||
* minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log",
|
||||
* hour, minute)"
|
||||
*
|
||||
*/
|
||||
struct daily_filename_format_calculator {
|
||||
@@ -70,14 +73,15 @@ public:
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {})
|
||||
: base_filename_(std::move(base_filename)),
|
||||
rotation_h_(rotation_hour),
|
||||
rotation_m_(rotation_minute),
|
||||
file_helper_{event_handlers},
|
||||
truncate_(truncate),
|
||||
max_files_(max_files),
|
||||
filenames_q_() {
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) {
|
||||
: base_filename_(std::move(base_filename))
|
||||
, rotation_h_(rotation_hour)
|
||||
, rotation_m_(rotation_minute)
|
||||
, file_helper_{event_handlers}
|
||||
, truncate_(truncate)
|
||||
, max_files_(max_files)
|
||||
, filenames_q_() {
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 ||
|
||||
rotation_minute > 59) {
|
||||
throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
||||
}
|
||||
|
||||
@@ -168,7 +172,8 @@ private:
|
||||
bool ok = remove_if_exists(old_filename) == 0;
|
||||
if (!ok) {
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno);
|
||||
throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename),
|
||||
errno);
|
||||
}
|
||||
}
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
@@ -187,7 +192,8 @@ private:
|
||||
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
||||
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
||||
using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
|
||||
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
|
||||
using daily_file_format_sink_st =
|
||||
daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
@@ -202,20 +208,21 @@ inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name,
|
||||
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,
|
||||
event_handlers);
|
||||
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>
|
||||
inline std::shared_ptr<logger> daily_logger_format_mt(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
int hour = 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, event_handlers);
|
||||
inline std::shared_ptr<logger>
|
||||
daily_logger_format_mt(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
int hour = 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, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@@ -226,19 +233,20 @@ inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name,
|
||||
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,
|
||||
event_handlers);
|
||||
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>
|
||||
inline std::shared_ptr<logger> daily_logger_format_st(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
int hour = 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, event_handlers);
|
||||
inline std::shared_ptr<logger>
|
||||
daily_logger_format_st(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
int hour = 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, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -20,8 +20,8 @@
|
||||
// #include <spdlog/sinks/dup_filter_sink.h>
|
||||
//
|
||||
// int main() {
|
||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5), level::info);
|
||||
// dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5),
|
||||
// level::info); dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||
// spdlog::logger l("logger", dup_filter);
|
||||
// l.info("Hello");
|
||||
// l.info("Hello");
|
||||
@@ -42,8 +42,8 @@ public:
|
||||
template <class Rep, class Period>
|
||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration,
|
||||
level::level_enum notification_level = level::info)
|
||||
: max_skip_duration_{max_skip_duration},
|
||||
log_level_{notification_level} {}
|
||||
: max_skip_duration_{max_skip_duration}
|
||||
, log_level_{notification_level} {}
|
||||
|
||||
protected:
|
||||
std::chrono::microseconds max_skip_duration_;
|
||||
@@ -62,8 +62,8 @@ protected:
|
||||
// log the "skipped.." message
|
||||
if (skip_counter_ > 0) {
|
||||
char buf[64];
|
||||
auto msg_size =
|
||||
::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", static_cast<unsigned>(skip_counter_));
|
||||
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..",
|
||||
static_cast<unsigned>(skip_counter_));
|
||||
if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf)) {
|
||||
details::log_msg skipped_msg{msg.source, msg.logger_name, log_level_,
|
||||
string_view_t{buf, static_cast<size_t>(msg_size)}};
|
||||
|
@@ -29,8 +29,9 @@ struct hourly_filename_calculator {
|
||||
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
|
||||
filename_t basename, ext;
|
||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||
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);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,11 +48,11 @@ public:
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {})
|
||||
: base_filename_(std::move(base_filename)),
|
||||
file_helper_{event_handlers},
|
||||
truncate_(truncate),
|
||||
max_files_(max_files),
|
||||
filenames_q_() {
|
||||
: base_filename_(std::move(base_filename))
|
||||
, file_helper_{event_handlers}
|
||||
, truncate_(truncate)
|
||||
, max_files_(max_files)
|
||||
, filenames_q_() {
|
||||
auto now = log_clock::now();
|
||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||
file_helper_.open(filename, truncate_);
|
||||
@@ -144,7 +145,8 @@ private:
|
||||
bool ok = remove_if_exists(old_filename) == 0;
|
||||
if (!ok) {
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
SPDLOG_THROW(spdlog_ex("Failed removing hourly file " + filename_to_str(old_filename), errno));
|
||||
SPDLOG_THROW(spdlog_ex(
|
||||
"Failed removing hourly file " + filename_to_str(old_filename), errno));
|
||||
}
|
||||
}
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
@@ -173,8 +175,8 @@ inline std::shared_ptr<logger> hourly_logger_mt(const std::string &logger_name,
|
||||
bool truncate = false,
|
||||
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,
|
||||
event_handlers);
|
||||
return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate,
|
||||
max_files, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@@ -183,7 +185,7 @@ inline std::shared_ptr<logger> hourly_logger_st(const std::string &logger_name,
|
||||
bool truncate = false,
|
||||
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,
|
||||
event_handlers);
|
||||
return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate,
|
||||
max_files, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -30,9 +30,9 @@ struct kafka_sink_config {
|
||||
int32_t flush_timeout_ms = 1000;
|
||||
|
||||
kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
|
||||
: server_addr{std::move(addr)},
|
||||
produce_topic{std::move(topic)},
|
||||
flush_timeout_ms(flush_timeout_ms) {}
|
||||
: server_addr{std::move(addr)}
|
||||
, produce_topic{std::move(topic)}
|
||||
, flush_timeout_ms(flush_timeout_ms) {}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
@@ -43,9 +43,11 @@ public:
|
||||
try {
|
||||
std::string errstr;
|
||||
conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL));
|
||||
RdKafka::Conf::ConfResult confRes = conf_->set("bootstrap.servers", config_.server_addr, errstr);
|
||||
RdKafka::Conf::ConfResult confRes =
|
||||
conf_->set("bootstrap.servers", config_.server_addr, errstr);
|
||||
if (confRes != RdKafka::Conf::CONF_OK) {
|
||||
throw_spdlog_ex(fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
|
||||
throw_spdlog_ex(
|
||||
fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
|
||||
}
|
||||
|
||||
tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC));
|
||||
@@ -57,7 +59,8 @@ public:
|
||||
if (producer_ == nullptr) {
|
||||
throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr));
|
||||
}
|
||||
topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic, tconf_.get(), errstr));
|
||||
topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic,
|
||||
tconf_.get(), errstr));
|
||||
if (topic_ == nullptr) {
|
||||
throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr));
|
||||
}
|
||||
@@ -70,8 +73,8 @@ public:
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(),
|
||||
msg.payload.size(), NULL, NULL);
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY,
|
||||
(void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
|
||||
}
|
||||
|
||||
void flush_() override { producer_->flush(config_.flush_timeout_ms); }
|
||||
@@ -102,14 +105,14 @@ inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name,
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name,
|
||||
spdlog::sinks::kafka_sink_config config) {
|
||||
inline std::shared_ptr<spdlog::logger>
|
||||
kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config) {
|
||||
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name,
|
||||
spdlog::sinks::kafka_sink_config config) {
|
||||
inline std::shared_ptr<spdlog::logger>
|
||||
kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config) {
|
||||
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
|
||||
}
|
||||
|
||||
|
@@ -40,9 +40,9 @@ public:
|
||||
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) {
|
||||
: instance_(std::move(instance))
|
||||
, db_name_(db_name)
|
||||
, coll_name_(collection_name) {
|
||||
try {
|
||||
client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
|
||||
} catch (const std::exception &e) {
|
||||
@@ -59,10 +59,12 @@ protected:
|
||||
|
||||
if (client_ != nullptr) {
|
||||
auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level"
|
||||
<< level::to_string_view(msg.level).data() << "level_num" << msg.level << "message"
|
||||
<< std::string(msg.payload.begin(), msg.payload.end()) << "logger_name"
|
||||
<< std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
|
||||
<< static_cast<int>(msg.thread_id) << finalize;
|
||||
<< level::to_string_view(msg.level).data() << "level_num"
|
||||
<< msg.level << "message"
|
||||
<< std::string(msg.payload.begin(), msg.payload.end())
|
||||
<< "logger_name"
|
||||
<< std::string(msg.logger_name.begin(), msg.logger_name.end())
|
||||
<< "thread_id" << static_cast<int>(msg.thread_id) << finalize;
|
||||
client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
|
||||
}
|
||||
}
|
||||
@@ -84,19 +86,23 @@ using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
|
||||
} // namespace sinks
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> mongo_logger_mt(const std::string &logger_name,
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017") {
|
||||
return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
|
||||
inline std::shared_ptr<logger>
|
||||
mongo_logger_mt(const std::string &logger_name,
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017") {
|
||||
return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name,
|
||||
uri);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> mongo_logger_st(const std::string &logger_name,
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017") {
|
||||
return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
|
||||
inline std::shared_ptr<logger>
|
||||
mongo_logger_st(const std::string &logger_name,
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017") {
|
||||
return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name,
|
||||
uri);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@@ -15,8 +15,8 @@ template <typename Mutex>
|
||||
class ostream_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit ostream_sink(std::ostream &os, bool force_flush = false)
|
||||
: ostream_(os),
|
||||
force_flush_(force_flush) {}
|
||||
: ostream_(os)
|
||||
, force_flush_(force_flush) {}
|
||||
ostream_sink(const ostream_sink &) = delete;
|
||||
ostream_sink &operator=(const ostream_sink &) = delete;
|
||||
|
||||
|
@@ -8,8 +8,8 @@
|
||||
// etc) Building and using requires Qt library.
|
||||
//
|
||||
// Warning: the qt_sink won't be notified if the target widget is destroyed.
|
||||
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent QObject,
|
||||
// and then use a standard signal/slot.
|
||||
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent
|
||||
// QObject, and then use a standard signal/slot.
|
||||
//
|
||||
|
||||
#include "spdlog/common.h"
|
||||
@@ -30,8 +30,8 @@ template <typename Mutex>
|
||||
class qt_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
qt_sink(QObject *qt_object, std::string meta_method)
|
||||
: qt_object_(qt_object),
|
||||
meta_method_(std::move(meta_method)) {
|
||||
: qt_object_(qt_object)
|
||||
, meta_method_(std::move(meta_method)) {
|
||||
if (!qt_object_) {
|
||||
throw_spdlog_ex("qt_sink: qt_object is null");
|
||||
}
|
||||
@@ -59,15 +59,19 @@ private:
|
||||
// QT color sink to QTextEdit.
|
||||
// Color location is determined by the sink log pattern like in the rest of spdlog sinks.
|
||||
// Colors can be modified if needed using sink->set_color(level, qtTextCharFormat).
|
||||
// max_lines is the maximum number of lines that the sink will hold before removing the oldest lines.
|
||||
// By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8 support is needed.
|
||||
// max_lines is the maximum number of lines that the sink will hold before removing the oldest
|
||||
// lines. By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8
|
||||
// support is needed.
|
||||
template <typename Mutex>
|
||||
class qt_color_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
qt_color_sink(QTextEdit *qt_text_edit, int max_lines, bool dark_colors = false, bool is_utf8 = false)
|
||||
: qt_text_edit_(qt_text_edit),
|
||||
max_lines_(max_lines),
|
||||
is_utf8_(is_utf8) {
|
||||
qt_color_sink(QTextEdit *qt_text_edit,
|
||||
int max_lines,
|
||||
bool dark_colors = false,
|
||||
bool is_utf8 = false)
|
||||
: qt_text_edit_(qt_text_edit)
|
||||
, max_lines_(max_lines)
|
||||
, is_utf8_(is_utf8) {
|
||||
if (!qt_text_edit_) {
|
||||
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
|
||||
}
|
||||
@@ -127,13 +131,13 @@ protected:
|
||||
QTextCharFormat level_color,
|
||||
int color_range_start,
|
||||
int color_range_end)
|
||||
: max_lines(max_lines),
|
||||
q_text_edit(q_text_edit),
|
||||
payload(std::move(payload)),
|
||||
default_color(default_color),
|
||||
level_color(level_color),
|
||||
color_range_start(color_range_start),
|
||||
color_range_end(color_range_end) {}
|
||||
: max_lines(max_lines)
|
||||
, q_text_edit(q_text_edit)
|
||||
, payload(std::move(payload))
|
||||
, default_color(default_color)
|
||||
, level_color(level_color)
|
||||
, color_range_start(color_range_start)
|
||||
, color_range_end(color_range_end) {}
|
||||
int max_lines;
|
||||
QTextEdit *q_text_edit;
|
||||
QString payload;
|
||||
@@ -178,8 +182,8 @@ protected:
|
||||
void flush_() override {}
|
||||
|
||||
// Add colored text to the text edit widget. This method is invoked in the GUI thread.
|
||||
// It is a static method to ensure that it is handled correctly even if the sink is destroyed prematurely
|
||||
// before it is invoked.
|
||||
// It is a static method to ensure that it is handled correctly even if the sink is destroyed
|
||||
// prematurely before it is invoked.
|
||||
|
||||
static void invoke_method_(invoke_params params) {
|
||||
auto *document = params.q_text_edit->document();
|
||||
@@ -206,8 +210,8 @@ protected:
|
||||
|
||||
// insert the colorized text
|
||||
cursor.setCharFormat(params.level_color);
|
||||
cursor.insertText(
|
||||
params.payload.mid(params.color_range_start, params.color_range_end - params.color_range_start));
|
||||
cursor.insertText(params.payload.mid(params.color_range_start,
|
||||
params.color_range_end - params.color_range_start));
|
||||
|
||||
// insert the text after the color range with default format
|
||||
cursor.setCharFormat(params.default_color);
|
||||
@@ -236,14 +240,16 @@ using qt_color_sink_st = qt_color_sink<details::null_mutex>;
|
||||
|
||||
// log to QTextEdit
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append") {
|
||||
inline std::shared_ptr<logger> 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") {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -276,15 +282,21 @@ qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::stri
|
||||
|
||||
// log to QTextEdit with colorize output
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_color_logger_mt(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines, false, is_utf8);
|
||||
inline std::shared_ptr<logger> qt_color_logger_mt(const std::string &logger_name,
|
||||
QTextEdit *qt_text_edit,
|
||||
int max_lines,
|
||||
bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines,
|
||||
false, is_utf8);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_color_logger_st(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines, false, is_utf8);
|
||||
inline std::shared_ptr<logger> qt_color_logger_st(const std::string &logger_name,
|
||||
QTextEdit *qt_text_edit,
|
||||
int max_lines,
|
||||
bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines,
|
||||
false, is_utf8);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@@ -50,7 +50,9 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override { q_.push_back(details::log_msg_buffer{msg}); }
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
q_.push_back(details::log_msg_buffer{msg});
|
||||
}
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
|
@@ -24,15 +24,16 @@ namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template <typename Mutex>
|
||||
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,
|
||||
const file_event_handlers &event_handlers)
|
||||
: base_filename_(std::move(base_filename)),
|
||||
max_size_(max_size),
|
||||
max_files_(max_files),
|
||||
file_helper_{event_handlers} {
|
||||
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,
|
||||
const file_event_handlers &event_handlers)
|
||||
: base_filename_(std::move(base_filename))
|
||||
, max_size_(max_size)
|
||||
, max_files_(max_files)
|
||||
, file_helper_{event_handlers} {
|
||||
if (max_size == 0) {
|
||||
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
|
||||
}
|
||||
@@ -51,7 +52,8 @@ SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(filename_t base_file
|
||||
// calc filename according to index and file extension if exists.
|
||||
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename, std::size_t index) {
|
||||
SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename,
|
||||
std::size_t index) {
|
||||
if (index == 0u) {
|
||||
return filename;
|
||||
}
|
||||
@@ -116,10 +118,11 @@ SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_() {
|
||||
// rates can cause the rename to fail with permission denied (because of antivirus?).
|
||||
details::os::sleep_for_millis(100);
|
||||
if (!rename_file_(src, target)) {
|
||||
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
|
||||
file_helper_.reopen(
|
||||
true); // truncate the log file anyway to prevent it to grow beyond its limit!
|
||||
current_size_ = 0;
|
||||
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " +
|
||||
filename_to_str(target),
|
||||
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) +
|
||||
" to " + filename_to_str(target),
|
||||
errno);
|
||||
}
|
||||
}
|
||||
|
@@ -68,8 +68,8 @@ inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name
|
||||
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, event_handlers);
|
||||
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>
|
||||
@@ -79,8 +79,8 @@ inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name
|
||||
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, event_handlers);
|
||||
return Factory::template create<sinks::rotating_file_sink_st>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
||||
|
@@ -13,22 +13,26 @@
|
||||
namespace spdlog {
|
||||
|
||||
template <typename Factory>
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode) {
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name,
|
||||
color_mode mode) {
|
||||
return Factory::template create<sinks::stdout_color_sink_mt>(logger_name, mode);
|
||||
}
|
||||
|
||||
template <typename Factory>
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode) {
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_st(const std::string &logger_name,
|
||||
color_mode mode) {
|
||||
return Factory::template create<sinks::stdout_color_sink_st>(logger_name, mode);
|
||||
}
|
||||
|
||||
template <typename Factory>
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode) {
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name,
|
||||
color_mode mode) {
|
||||
return Factory::template create<sinks::stderr_color_sink_mt>(logger_name, mode);
|
||||
}
|
||||
|
||||
template <typename Factory>
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode) {
|
||||
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_st(const std::string &logger_name,
|
||||
color_mode mode) {
|
||||
return Factory::template create<sinks::stderr_color_sink_st>(logger_name, mode);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -27,16 +27,20 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st;
|
||||
} // namespace sinks
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
} // namespace spdlog
|
||||
|
||||
|
@@ -30,9 +30,9 @@ namespace sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
|
||||
: mutex_(ConsoleMutex::mutex()),
|
||||
file_(file),
|
||||
formatter_(details::make_unique<spdlog::pattern_formatter>()) {
|
||||
: mutex_(ConsoleMutex::mutex())
|
||||
, file_(file)
|
||||
, formatter_(details::make_unique<spdlog::pattern_formatter>()) {
|
||||
#ifdef _WIN32
|
||||
// get windows handle from the FILE* object
|
||||
|
||||
@@ -60,7 +60,8 @@ SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &m
|
||||
DWORD bytes_written = 0;
|
||||
bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
|
||||
if (!ok) {
|
||||
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
|
||||
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " +
|
||||
std::to_string(::GetLastError()));
|
||||
}
|
||||
#else
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
@@ -84,7 +85,8 @@ SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
SPDLOG_INLINE void
|
||||
stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
@@ -21,15 +21,15 @@ class syslog_sink : public base_sink<Mutex> {
|
||||
|
||||
public:
|
||||
syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
|
||||
: enable_formatting_{enable_formatting},
|
||||
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::info */ LOG_INFO,
|
||||
/* spdlog::level::warn */ LOG_WARNING,
|
||||
/* spdlog::level::err */ LOG_ERR,
|
||||
/* spdlog::level::critical */ LOG_CRIT,
|
||||
/* spdlog::level::off */ LOG_INFO}},
|
||||
ident_{std::move(ident)} {
|
||||
/* spdlog::level::off */ LOG_INFO}}
|
||||
, ident_{std::move(ident)} {
|
||||
// set ident to be program name if empty
|
||||
::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
|
||||
}
|
||||
@@ -88,8 +88,8 @@ inline std::shared_ptr<logger> syslog_logger_mt(const std::string &logger_name,
|
||||
int syslog_option = 0,
|
||||
int syslog_facility = LOG_USER,
|
||||
bool enable_formatting = false) {
|
||||
return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility,
|
||||
enable_formatting);
|
||||
return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option,
|
||||
syslog_facility, enable_formatting);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@@ -98,7 +98,7 @@ inline std::shared_ptr<logger> syslog_logger_st(const std::string &logger_name,
|
||||
int syslog_option = 0,
|
||||
int syslog_facility = LOG_USER,
|
||||
bool enable_formatting = false) {
|
||||
return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility,
|
||||
enable_formatting);
|
||||
return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option,
|
||||
syslog_facility, enable_formatting);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -24,9 +24,9 @@ template <typename Mutex>
|
||||
class systemd_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
systemd_sink(std::string ident = "", bool enable_formatting = false)
|
||||
: ident_{std::move(ident)},
|
||||
enable_formatting_{enable_formatting},
|
||||
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||
: ident_{std::move(ident)}
|
||||
, enable_formatting_{enable_formatting}
|
||||
, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||
/* spdlog::level::debug */ LOG_DEBUG,
|
||||
/* spdlog::level::info */ LOG_INFO,
|
||||
/* spdlog::level::warn */ LOG_WARNING,
|
||||
@@ -67,22 +67,25 @@ protected:
|
||||
// Do not send source location if not available
|
||||
if (msg.source.empty()) {
|
||||
// Note: function call inside '()' to avoid macro expansion
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), 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),
|
||||
#ifndef SPDLOG_NO_THREAD_ID
|
||||
"TID=%zu", details::os::thread_id(),
|
||||
#endif
|
||||
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()),
|
||||
"SYSLOG_IDENTIFIER=%.*s",
|
||||
static_cast<int>(syslog_identifier.size()),
|
||||
syslog_identifier.data(), nullptr);
|
||||
} else {
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), 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),
|
||||
#ifndef SPDLOG_NO_THREAD_ID
|
||||
"TID=%zu", details::os::thread_id(),
|
||||
#endif
|
||||
"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);
|
||||
"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);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
@@ -90,7 +93,9 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
int syslog_level(level::level_enum l) { return syslog_levels_.at(static_cast<levels_array::size_type>(l)); }
|
||||
int syslog_level(level::level_enum l) {
|
||||
return syslog_levels_.at(static_cast<levels_array::size_type>(l));
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
};
|
||||
@@ -101,14 +106,16 @@ using systemd_sink_st = systemd_sink<details::null_mutex>;
|
||||
|
||||
// Create and register a syslog logger
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
systemd_logger_mt(const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false) {
|
||||
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, ident, enable_formatting);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
systemd_logger_st(const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false) {
|
||||
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, ident, enable_formatting);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -22,7 +22,8 @@
|
||||
// Simple tcp client sink
|
||||
// Connects to remote address and send the formatted log.
|
||||
// Will attempt to reconnect if connection drops.
|
||||
// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the sink_it_ method.
|
||||
// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the
|
||||
// sink_it_ method.
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
@@ -33,8 +34,8 @@ struct tcp_sink_config {
|
||||
bool lazy_connect = false; // if true connect on first log call instead of on construction
|
||||
|
||||
tcp_sink_config(std::string host, int port)
|
||||
: server_host{std::move(host)},
|
||||
server_port{port} {}
|
||||
: server_host{std::move(host)}
|
||||
, server_port{port} {}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
|
@@ -28,8 +28,8 @@ struct udp_sink_config {
|
||||
uint16_t server_port;
|
||||
|
||||
udp_sink_config(std::string host, uint16_t port)
|
||||
: server_host{std::move(host)},
|
||||
server_port{port} {}
|
||||
: server_host{std::move(host)}
|
||||
, server_port{port} {}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
@@ -61,7 +61,8 @@ using udp_sink_st = udp_sink<spdlog::details::null_mutex>;
|
||||
// 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) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@@ -1,17 +1,20 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
// Writing to Windows Event Log requires the registry entries below to be present, with the following modifications:
|
||||
// Writing to Windows Event Log requires the registry entries below to be present, with the
|
||||
// following modifications:
|
||||
// 1. <log_name> should be replaced with your log name (e.g. your application name)
|
||||
// 2. <source_name> should be replaced with the specific source name and the key should be duplicated for
|
||||
// 2. <source_name> should be replaced with the specific source name and the key should be
|
||||
// duplicated for
|
||||
// each source used in the application
|
||||
//
|
||||
// Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure.
|
||||
// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and
|
||||
// happens to contain the needed resource.
|
||||
// Since typically modifications of this kind require elevation, it's better to do it as a part of
|
||||
// setup procedure. The snippet below uses mscoree.dll as the message file as it exists on most of
|
||||
// the Windows systems anyway and happens to contain the needed resource.
|
||||
//
|
||||
// You can also specify a custom message file if needed.
|
||||
// Please refer to Event Log functions descriptions in MSDN for more details on custom message files.
|
||||
// Please refer to Event Log functions descriptions in MSDN for more details on custom message
|
||||
// files.
|
||||
|
||||
/*---------------------------------------------------------------------------------------
|
||||
|
||||
@@ -69,9 +72,11 @@ struct win32_error : public spdlog_ex {
|
||||
std::string system_message;
|
||||
|
||||
local_alloc_t format_message_result{};
|
||||
auto format_message_succeeded = ::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.hlocal_, 0, nullptr);
|
||||
auto format_message_succeeded =
|
||||
::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.hlocal_, 0, nullptr);
|
||||
|
||||
if (format_message_succeeded && format_message_result.hlocal_) {
|
||||
system_message = fmt_lib::format(" ({})", (LPSTR)format_message_result.hlocal_);
|
||||
@@ -124,18 +129,21 @@ public:
|
||||
|
||||
~process_token_t() { ::CloseHandle(token_handle_); }
|
||||
|
||||
} current_process_token(::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here!
|
||||
} current_process_token(
|
||||
::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here!
|
||||
|
||||
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size
|
||||
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return
|
||||
// the token size
|
||||
DWORD tusize = 0;
|
||||
if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize)) {
|
||||
if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0,
|
||||
&tusize)) {
|
||||
SPDLOG_THROW(win32_error("GetTokenInformation should fail"));
|
||||
}
|
||||
|
||||
// get user token
|
||||
std::vector<unsigned char> buffer(static_cast<size_t>(tusize));
|
||||
if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize,
|
||||
&tusize)) {
|
||||
if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser,
|
||||
(LPVOID)buffer.data(), tusize, &tusize)) {
|
||||
SPDLOG_THROW(win32_error("GetTokenInformation"));
|
||||
}
|
||||
|
||||
@@ -208,14 +216,14 @@ protected:
|
||||
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
|
||||
|
||||
LPCWSTR lp_wstr = buf.data();
|
||||
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));
|
||||
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));
|
||||
#else
|
||||
LPCSTR lp_str = formatted.data();
|
||||
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));
|
||||
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));
|
||||
#endif
|
||||
|
||||
if (!succeeded) {
|
||||
@@ -226,14 +234,15 @@ protected:
|
||||
void flush_() override {}
|
||||
|
||||
public:
|
||||
win_eventlog_sink(std::string const &source, DWORD event_id = 1000 /* according to mscoree.dll */)
|
||||
: source_(source),
|
||||
event_id_(event_id) {
|
||||
win_eventlog_sink(std::string const &source,
|
||||
DWORD event_id = 1000 /* according to mscoree.dll */)
|
||||
: source_(source)
|
||||
, event_id_(event_id) {
|
||||
try {
|
||||
current_user_sid_ = internal::sid_t::get_current_user_sid();
|
||||
} catch (...) {
|
||||
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed without
|
||||
// current_user_sid but in the event log the record will have no user name
|
||||
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed
|
||||
// without current_user_sid but in the event log the record will have no user name
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -17,18 +17,20 @@ namespace spdlog {
|
||||
namespace sinks {
|
||||
template <typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode)
|
||||
: out_handle_(out_handle),
|
||||
mutex_(ConsoleMutex::mutex()),
|
||||
formatter_(details::make_unique<spdlog::pattern_formatter>()) {
|
||||
: out_handle_(out_handle)
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
, formatter_(details::make_unique<spdlog::pattern_formatter>()) {
|
||||
|
||||
set_color_mode_impl(mode);
|
||||
// set level colors
|
||||
colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
|
||||
colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
|
||||
colors_[level::info] = FOREGROUND_GREEN; // green
|
||||
colors_[level::warn] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
|
||||
colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
|
||||
colors_[level::critical] = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
|
||||
colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
|
||||
colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
|
||||
colors_[level::info] = FOREGROUND_GREEN; // green
|
||||
colors_[level::warn] =
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
|
||||
colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
|
||||
colors_[level::critical] = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN |
|
||||
FOREGROUND_BLUE |
|
||||
FOREGROUND_INTENSITY; // intense white on red background
|
||||
colors_[level::off] = 0;
|
||||
}
|
||||
@@ -40,7 +42,8 @@ SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink() {
|
||||
|
||||
// change the color for the given level
|
||||
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_);
|
||||
colors_[static_cast<size_t>(level)] = color;
|
||||
}
|
||||
@@ -60,7 +63,8 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
|
||||
// before color range
|
||||
print_range_(formatted, 0, msg.color_range_start);
|
||||
// in color range
|
||||
auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(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);
|
||||
// reset to orig colors
|
||||
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
|
||||
@@ -83,7 +87,8 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &p
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void SPDLOG_INLINE
|
||||
wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
@@ -108,7 +113,8 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode m
|
||||
|
||||
// set foreground color and return the orig console attributes (for resetting later)
|
||||
template <typename ConsoleMutex>
|
||||
std::uint16_t SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs) {
|
||||
std::uint16_t SPDLOG_INLINE
|
||||
wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs) {
|
||||
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
|
||||
if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info)) {
|
||||
// just return white if failed getting console info
|
||||
@@ -117,18 +123,21 @@ std::uint16_t SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_foreground_color_(s
|
||||
|
||||
// change only the foreground bits (lowest 4 bits)
|
||||
auto new_attribs = static_cast<WORD>(attribs) | (orig_buffer_info.wAttributes & 0xfff0);
|
||||
auto ignored = ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
|
||||
auto ignored =
|
||||
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
|
||||
(void)(ignored);
|
||||
return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs
|
||||
}
|
||||
|
||||
// print a range of formatted message to console
|
||||
template <typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
|
||||
size_t start,
|
||||
size_t end) {
|
||||
if (end > start) {
|
||||
auto size = static_cast<DWORD>(end - start);
|
||||
auto ignored =
|
||||
::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
|
||||
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start,
|
||||
size, nullptr, nullptr);
|
||||
(void)(ignored);
|
||||
}
|
||||
}
|
||||
@@ -137,7 +146,8 @@ template <typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted) {
|
||||
auto size = static_cast<DWORD>(formatted.size());
|
||||
DWORD bytes_written = 0;
|
||||
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr);
|
||||
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size,
|
||||
&bytes_written, nullptr);
|
||||
(void)(ignored);
|
||||
}
|
||||
|
||||
|
@@ -16,17 +16,22 @@ SPDLOG_INLINE void initialize_logger(std::shared_ptr<logger> logger) {
|
||||
details::registry::instance().initialize_logger(std::move(logger));
|
||||
}
|
||||
|
||||
SPDLOG_INLINE std::shared_ptr<logger> get(const std::string &name) { return details::registry::instance().get(name); }
|
||||
SPDLOG_INLINE std::shared_ptr<logger> get(const std::string &name) {
|
||||
return details::registry::instance().get(name);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
|
||||
details::registry::instance().set_formatter(std::move(formatter));
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type) {
|
||||
set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
||||
set_formatter(
|
||||
std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void enable_backtrace(size_t n_messages) { details::registry::instance().enable_backtrace(n_messages); }
|
||||
SPDLOG_INLINE void enable_backtrace(size_t n_messages) {
|
||||
details::registry::instance().enable_backtrace(n_messages);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void disable_backtrace() { details::registry::instance().disable_backtrace(); }
|
||||
|
||||
@@ -34,11 +39,17 @@ SPDLOG_INLINE void dump_backtrace() { default_logger_raw()->dump_backtrace(); }
|
||||
|
||||
SPDLOG_INLINE level::level_enum get_level() { return default_logger_raw()->level(); }
|
||||
|
||||
SPDLOG_INLINE bool should_log(level::level_enum log_level) { return default_logger_raw()->should_log(log_level); }
|
||||
SPDLOG_INLINE bool should_log(level::level_enum log_level) {
|
||||
return default_logger_raw()->should_log(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void set_level(level::level_enum log_level) { details::registry::instance().set_level(log_level); }
|
||||
SPDLOG_INLINE void set_level(level::level_enum log_level) {
|
||||
details::registry::instance().set_level(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void flush_on(level::level_enum log_level) { details::registry::instance().flush_on(log_level); }
|
||||
SPDLOG_INLINE void flush_on(level::level_enum log_level) {
|
||||
details::registry::instance().flush_on(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg)) {
|
||||
details::registry::instance().set_error_handler(handler);
|
||||
@@ -66,7 +77,9 @@ SPDLOG_INLINE std::shared_ptr<spdlog::logger> default_logger() {
|
||||
return details::registry::instance().default_logger();
|
||||
}
|
||||
|
||||
SPDLOG_INLINE spdlog::logger *default_logger_raw() { return details::registry::instance().get_default_raw(); }
|
||||
SPDLOG_INLINE spdlog::logger *default_logger_raw() {
|
||||
return details::registry::instance().get_default_raw();
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void set_default_logger(std::shared_ptr<spdlog::logger> default_logger) {
|
||||
details::registry::instance().set_default_logger(std::move(default_logger));
|
||||
|
@@ -32,7 +32,8 @@ using default_factory = synchronous_factory;
|
||||
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
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)...);
|
||||
}
|
||||
|
||||
// Initialize and register a logger,
|
||||
@@ -55,7 +56,8 @@ SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
|
||||
|
||||
// Set global format string.
|
||||
// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
|
||||
SPDLOG_API void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||
SPDLOG_API void set_pattern(std::string pattern,
|
||||
pattern_time_type time_type = pattern_time_type::local);
|
||||
|
||||
// enable global backtrace support
|
||||
SPDLOG_API void enable_backtrace(size_t n_messages);
|
||||
@@ -139,7 +141,8 @@ SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logge
|
||||
SPDLOG_API void apply_logger_env_levels(std::shared_ptr<logger> logger);
|
||||
|
||||
template <typename... Args>
|
||||
inline void log(source_loc source, level::level_enum lvl, format_string_t<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)...);
|
||||
}
|
||||
|
||||
@@ -190,7 +193,8 @@ inline void log(level::level_enum lvl, const T &msg) {
|
||||
|
||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
template <typename... Args>
|
||||
inline void log(source_loc source, level::level_enum lvl, wformat_string_t<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)...);
|
||||
}
|
||||
|
||||
@@ -279,11 +283,13 @@ inline void critical(const T &msg) {
|
||||
#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__)
|
||||
#define SPDLOG_LOGGER_CALL(logger, level, ...) \
|
||||
(logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#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__)
|
||||
#define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
|
||||
@@ -291,7 +297,8 @@ inline void critical(const T &msg) {
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
|
||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
|
||||
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
|
||||
@@ -299,7 +306,8 @@ inline void critical(const T &msg) {
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
|
||||
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_INFO(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
|
||||
#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
|
||||
@@ -307,7 +315,8 @@ inline void critical(const T &msg) {
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
|
||||
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_WARN(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
|
||||
#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
|
||||
@@ -315,7 +324,8 @@ inline void critical(const T &msg) {
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
|
||||
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_ERROR(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
|
||||
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
|
||||
@@ -323,7 +333,8 @@ inline void critical(const T &msg) {
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
|
||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
|
||||
#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
|
||||
|
@@ -35,7 +35,9 @@ public:
|
||||
stopwatch()
|
||||
: start_tp_{clock::now()} {}
|
||||
|
||||
std::chrono::duration<double> elapsed() const { return std::chrono::duration<double>(clock::now() - start_tp_); }
|
||||
std::chrono::duration<double> elapsed() const {
|
||||
return std::chrono::duration<double>(clock::now() - start_tp_);
|
||||
}
|
||||
|
||||
void reset() { start_tp_ = clock::now(); }
|
||||
};
|
||||
|
@@ -102,7 +102,8 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Uncomment to customize level names (e.g. "MY TRACE")
|
||||
//
|
||||
// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY CRITICAL", "OFF" }
|
||||
// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY
|
||||
// CRITICAL", "OFF" }
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
Reference in New Issue
Block a user