mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
Merge branch 'v1.x' of https://github.com/gabime/spdlog into v1.x
This commit is contained in:
@@ -60,7 +60,7 @@ inline void spdlog::async_logger::flush_()
|
||||
}
|
||||
else
|
||||
{
|
||||
throw spdlog_ex("async flush: thread pool doens't exist anymore");
|
||||
throw spdlog_ex("async flush: thread pool doesn't exist anymore");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "stdio.h"
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
struct console_stdout_trait
|
||||
struct console_stdout_stream
|
||||
{
|
||||
static FILE *stream()
|
||||
{
|
||||
@@ -21,7 +21,7 @@ struct console_stdout_trait
|
||||
#endif
|
||||
};
|
||||
|
||||
struct console_stderr_trait
|
||||
struct console_stderr_stream
|
||||
{
|
||||
static FILE *stream()
|
||||
{
|
||||
@@ -35,7 +35,7 @@ struct console_stderr_trait
|
||||
#endif
|
||||
};
|
||||
|
||||
struct console_mutex_trait
|
||||
struct console_global_mutex
|
||||
{
|
||||
using mutex_t = std::mutex;
|
||||
static mutex_t &console_mutex()
|
||||
@@ -45,7 +45,7 @@ struct console_mutex_trait
|
||||
}
|
||||
};
|
||||
|
||||
struct console_null_mutex_trait
|
||||
struct console_global_nullmutex
|
||||
{
|
||||
using mutex_t = null_mutex;
|
||||
static mutex_t &console_mutex()
|
||||
@@ -8,13 +8,16 @@
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
namespace fmt_helper {
|
||||
inline void append_str(const std::string &str, fmt::memory_buffer &dest)
|
||||
|
||||
template <size_t Buffer_Size>
|
||||
inline void append_str(const std::string &str, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||
{
|
||||
auto *str_ptr = str.data();
|
||||
dest.append(str_ptr, str_ptr + str.size());
|
||||
}
|
||||
|
||||
inline void append_c_str(const char *c_str, fmt::memory_buffer &dest)
|
||||
template <size_t Buffer_Size>
|
||||
inline void append_c_str(const char *c_str, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||
{
|
||||
char ch;
|
||||
while ((ch = *c_str) != '\0')
|
||||
@@ -24,21 +27,22 @@ inline void append_c_str(const char *c_str, fmt::memory_buffer &dest)
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t N1, size_t N2>
|
||||
inline void append_buf(const fmt::basic_memory_buffer<char, N1> &buf, fmt::basic_memory_buffer<char, N2> &dest)
|
||||
template<size_t Buffer_Size1, size_t Buffer_Size2>
|
||||
inline void append_buf(const fmt::basic_memory_buffer<char, Buffer_Size1> &buf, fmt::basic_memory_buffer<char, Buffer_Size2> &dest)
|
||||
{
|
||||
auto *buf_ptr = buf.data();
|
||||
dest.append(buf_ptr, buf_ptr + buf.size());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void append_int(T n, fmt::memory_buffer &dest)
|
||||
template<typename T, size_t Buffer_Size>
|
||||
inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||
{
|
||||
fmt::format_int i(n);
|
||||
dest.append(i.data(), i.data() + i.size());
|
||||
}
|
||||
|
||||
inline void pad2(int n, fmt::memory_buffer &dest)
|
||||
template <size_t Buffer_Size>
|
||||
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||
{
|
||||
if (n > 99)
|
||||
{
|
||||
@@ -61,7 +65,8 @@ inline void pad2(int n, fmt::memory_buffer &dest)
|
||||
fmt::format_to(dest, "{:02}", n);
|
||||
}
|
||||
|
||||
inline void pad3(int n, fmt::memory_buffer &dest)
|
||||
template <size_t Buffer_Size>
|
||||
inline void pad3(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||
{
|
||||
if (n > 99)
|
||||
{
|
||||
@@ -86,8 +91,13 @@ inline void pad3(int n, fmt::memory_buffer &dest)
|
||||
fmt::format_to(dest, "{:03}", n);
|
||||
}
|
||||
|
||||
inline void pad6(size_t n, fmt::memory_buffer &dest)
|
||||
template <size_t Buffer_Size>
|
||||
inline void pad6(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||
{
|
||||
// todo: maybe replace this implementation with
|
||||
// pad3(n / 1000, dest);
|
||||
// pad3(n % 1000, dest);
|
||||
|
||||
if (n > 99999)
|
||||
{
|
||||
append_int(n, dest);
|
||||
@@ -122,7 +132,7 @@ inline void pad6(size_t n, fmt::memory_buffer &dest)
|
||||
dest.push_back('0');
|
||||
dest.push_back('0');
|
||||
dest.push_back('0');
|
||||
}
|
||||
}
|
||||
append_int(n, dest);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,9 +46,9 @@ inline void spdlog::logger::set_formatter(const Args &... args)
|
||||
}
|
||||
}
|
||||
|
||||
inline void spdlog::logger::set_pattern(const std::string &pattern, pattern_time_type pattern_time)
|
||||
inline void spdlog::logger::set_pattern(const std::string &pattern, pattern_time_type time_type)
|
||||
{
|
||||
set_formatter<spdlog::pattern_formatter>(pattern, pattern_time);
|
||||
set_formatter<spdlog::pattern_formatter>(pattern, time_type);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
@@ -100,40 +100,40 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
inline void spdlog::logger::trace(const char *fmt, const Arg1 &arg1, const Args &... args)
|
||||
template<typename... Args>
|
||||
inline void spdlog::logger::trace(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(level::trace, fmt, arg1, args...);
|
||||
log(level::trace, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
inline void spdlog::logger::debug(const char *fmt, const Arg1 &arg1, const Args &... args)
|
||||
template<typename... Args>
|
||||
inline void spdlog::logger::debug(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(level::debug, fmt, arg1, args...);
|
||||
log(level::debug, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
inline void spdlog::logger::info(const char *fmt, const Arg1 &arg1, const Args &... args)
|
||||
template<typename... Args>
|
||||
inline void spdlog::logger::info(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(level::info, fmt, arg1, args...);
|
||||
log(level::info, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
inline void spdlog::logger::warn(const char *fmt, const Arg1 &arg1, const Args &... args)
|
||||
template<typename... Args>
|
||||
inline void spdlog::logger::warn(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(level::warn, fmt, arg1, args...);
|
||||
log(level::warn, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
inline void spdlog::logger::error(const char *fmt, const Arg1 &arg1, const Args &... args)
|
||||
template<typename... Args>
|
||||
inline void spdlog::logger::error(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(level::err, fmt, arg1, args...);
|
||||
log(level::err, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
inline void spdlog::logger::critical(const char *fmt, const Arg1 &arg1, const Args &... args)
|
||||
template<typename... Args>
|
||||
inline void spdlog::logger::critical(const char *fmt, const Args &... args)
|
||||
{
|
||||
log(level::critical, fmt, arg1, args...);
|
||||
log(level::critical, fmt, args...);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -120,7 +120,16 @@ class c_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
|
||||
{
|
||||
fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday); //
|
||||
//fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday);
|
||||
// date
|
||||
fmt_helper::append_str(days[tm_time.tm_wday], dest);
|
||||
dest.push_back(' ');
|
||||
fmt_helper::append_str(months[tm_time.tm_mon], dest);
|
||||
dest.push_back(' ');
|
||||
fmt_helper::append_int(tm_time.tm_mday, dest);
|
||||
dest.push_back(' ');
|
||||
// time
|
||||
|
||||
fmt_helper::pad2(tm_time.tm_hour, dest);
|
||||
dest.push_back(':');
|
||||
fmt_helper::pad2(tm_time.tm_min, dest);
|
||||
@@ -176,7 +185,6 @@ class d_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
|
||||
{
|
||||
// fmt::format_to(dest, "{:02}", tm_time.tm_mday);
|
||||
fmt_helper::pad2(tm_time.tm_mday, dest);
|
||||
}
|
||||
};
|
||||
@@ -265,7 +273,7 @@ class p_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
|
||||
{
|
||||
fmt::format_to(dest, "{}", ampm(tm_time));
|
||||
fmt_helper::append_c_str(ampm(tm_time), dest);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -274,13 +282,13 @@ class r_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
|
||||
{
|
||||
// fmt::format_to(dest, "{:02}:{:02}:{:02} {}", to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ampm(tm_time));
|
||||
fmt_helper::pad2(to12h(tm_time), dest);
|
||||
dest.push_back(':');
|
||||
fmt_helper::pad2(tm_time.tm_min, dest);
|
||||
dest.push_back(':');
|
||||
fmt_helper::pad2(tm_time.tm_sec, dest);
|
||||
fmt::format_to(dest, " {}", ampm(tm_time));
|
||||
dest.push_back(' ');
|
||||
fmt_helper::append_c_str(ampm(tm_time), dest);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -330,23 +338,19 @@ public:
|
||||
int total_minutes = os::utc_minutes_offset(tm_time);
|
||||
#endif
|
||||
bool is_negative = total_minutes < 0;
|
||||
char sign;
|
||||
if (is_negative)
|
||||
{
|
||||
total_minutes = -total_minutes;
|
||||
sign = '-';
|
||||
dest.push_back('-');
|
||||
}
|
||||
else
|
||||
{
|
||||
sign = '+';
|
||||
dest.push_back('+');
|
||||
}
|
||||
|
||||
int h = total_minutes / 60;
|
||||
int m = total_minutes % 60;
|
||||
dest.push_back(sign);
|
||||
fmt_helper::pad2(h, dest);
|
||||
fmt_helper::pad2(total_minutes / 60, dest); // hours
|
||||
dest.push_back(':');
|
||||
fmt_helper::pad2(m, dest);
|
||||
fmt_helper::pad2(total_minutes % 60, dest); // minutes
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -371,7 +375,6 @@ class t_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
|
||||
{
|
||||
|
||||
fmt_helper::pad6(msg.thread_id, dest);
|
||||
}
|
||||
};
|
||||
@@ -390,7 +393,6 @@ class i_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
|
||||
{
|
||||
|
||||
fmt_helper::pad6(msg.msg_id, dest);
|
||||
}
|
||||
};
|
||||
@@ -462,39 +464,48 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
|
||||
{
|
||||
#ifndef SPDLOG_NO_DATETIME
|
||||
|
||||
// each second cache the header
|
||||
// cache the date/time part for the next second.
|
||||
auto duration = msg.time.time_since_epoch();
|
||||
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
|
||||
if (cached_header_.size() == 0 || cached_seconds_ts_ != seconds)
|
||||
std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
|
||||
|
||||
if (cache_timestamp_ != seconds || cached_datetime_.size() == 0)
|
||||
{
|
||||
cached_header_ = fmt::memory_buffer();
|
||||
cached_header_.push_back('[');
|
||||
fmt_helper::append_int(tm_time.tm_year + 1900, cached_header_);
|
||||
cached_header_.push_back('-');
|
||||
cached_datetime_.resize(0);
|
||||
cached_datetime_.push_back('[');
|
||||
fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_);
|
||||
cached_datetime_.push_back('-');
|
||||
|
||||
fmt_helper::pad2(tm_time.tm_mon + 1, cached_header_);
|
||||
cached_header_.push_back('-');
|
||||
fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_);
|
||||
cached_datetime_.push_back('-');
|
||||
|
||||
fmt_helper::pad2(tm_time.tm_mday, cached_header_);
|
||||
cached_header_.push_back(' ');
|
||||
fmt_helper::pad2(tm_time.tm_mday, cached_datetime_);
|
||||
cached_datetime_.push_back(' ');
|
||||
|
||||
fmt_helper::pad2(tm_time.tm_hour, cached_header_);
|
||||
cached_header_.push_back(':');
|
||||
fmt_helper::pad2(tm_time.tm_hour, cached_datetime_);
|
||||
cached_datetime_.push_back(':');
|
||||
|
||||
fmt_helper::pad2(tm_time.tm_min, cached_header_);
|
||||
cached_header_.push_back(':');
|
||||
fmt_helper::pad2(tm_time.tm_min, cached_datetime_);
|
||||
cached_datetime_.push_back(':');
|
||||
|
||||
fmt_helper::pad2(tm_time.tm_sec, cached_header_);
|
||||
cached_header_.push_back('.');
|
||||
fmt_helper::pad2(tm_time.tm_sec, cached_datetime_);
|
||||
cached_datetime_.push_back('.');
|
||||
|
||||
cached_seconds_ts_ = seconds;
|
||||
cache_timestamp_ = seconds;
|
||||
}
|
||||
fmt_helper::append_buf(cached_header_, dest);
|
||||
fmt_helper::append_buf(cached_datetime_, dest);
|
||||
|
||||
// cache the millis part for the next milli.
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
||||
fmt_helper::pad3(static_cast<int>(millis), dest);
|
||||
dest.push_back(']');
|
||||
dest.push_back(' ');
|
||||
if(millis != millis_cache_timestamp_ || cached_millis_.size() == 0)
|
||||
{
|
||||
cached_millis_.resize(0);
|
||||
fmt_helper::pad3(millis, cached_millis_);
|
||||
cached_millis_.push_back(']');
|
||||
cached_millis_.push_back(' ');
|
||||
millis_cache_timestamp_ = millis;
|
||||
}
|
||||
|
||||
fmt_helper::append_buf(cached_millis_, dest);
|
||||
#else // no datetime needed
|
||||
(void)tm_time;
|
||||
#endif
|
||||
@@ -517,8 +528,10 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::seconds::rep cached_seconds_ts_{0};
|
||||
fmt::memory_buffer cached_header_;
|
||||
std::chrono::seconds cache_timestamp_ {0};
|
||||
std::chrono::milliseconds::rep millis_cache_timestamp_ {0};
|
||||
fmt::basic_memory_buffer<char, 128> cached_datetime_;
|
||||
fmt::basic_memory_buffer<char, 8> cached_millis_;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
@@ -526,14 +539,14 @@ private:
|
||||
class pattern_formatter SPDLOG_FINAL : public formatter
|
||||
{
|
||||
public:
|
||||
explicit pattern_formatter(const std::string &pattern, pattern_time_type pattern_time = pattern_time_type::local,
|
||||
explicit pattern_formatter(const std::string &pattern, pattern_time_type time_type = pattern_time_type::local,
|
||||
std::string eol = spdlog::details::os::default_eol)
|
||||
: eol_(std::move(eol))
|
||||
, pattern_time_(pattern_time)
|
||||
, pattern_time_type_(time_type)
|
||||
, last_log_secs_(0)
|
||||
{
|
||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||
compile_pattern(pattern);
|
||||
compile_pattern_(pattern);
|
||||
}
|
||||
|
||||
pattern_formatter(const pattern_formatter &) = default;
|
||||
@@ -544,7 +557,7 @@ public:
|
||||
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||
if (secs != last_log_secs_)
|
||||
{
|
||||
cached_tm_ = get_time(msg);
|
||||
cached_tm_ = get_time_(msg);
|
||||
last_log_secs_ = secs;
|
||||
}
|
||||
#endif
|
||||
@@ -558,21 +571,22 @@ public:
|
||||
|
||||
private:
|
||||
const std::string eol_;
|
||||
const pattern_time_type pattern_time_;
|
||||
pattern_time_type pattern_time_type_;
|
||||
std::tm cached_tm_;
|
||||
std::chrono::seconds last_log_secs_;
|
||||
|
||||
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
||||
std::tm get_time(const details::log_msg &msg)
|
||||
|
||||
std::tm get_time_(const details::log_msg &msg)
|
||||
{
|
||||
if (pattern_time_ == pattern_time_type::local)
|
||||
if (pattern_time_type_ == pattern_time_type::local)
|
||||
{
|
||||
return details::os::localtime(log_clock::to_time_t(msg.time));
|
||||
}
|
||||
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
||||
}
|
||||
|
||||
void handle_flag(char flag)
|
||||
void handle_flag_(char flag)
|
||||
{
|
||||
switch (flag)
|
||||
{
|
||||
@@ -717,7 +731,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void compile_pattern(const std::string &pattern)
|
||||
void compile_pattern_(const std::string &pattern)
|
||||
{
|
||||
auto end = pattern.end();
|
||||
std::unique_ptr<details::aggregate_formatter> user_chars;
|
||||
@@ -732,7 +746,7 @@ private:
|
||||
// if(
|
||||
if (++it != end)
|
||||
{
|
||||
handle_flag(*it);
|
||||
handle_flag_(*it);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
auto logger_name = new_logger->name();
|
||||
throw_if_exists(logger_name);
|
||||
throw_if_exists_(logger_name);
|
||||
loggers_[logger_name] = new_logger;
|
||||
}
|
||||
|
||||
@@ -44,11 +44,11 @@ public:
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
auto logger_name = new_logger->name();
|
||||
throw_if_exists(logger_name);
|
||||
throw_if_exists_(logger_name);
|
||||
|
||||
// create default formatter if not exists
|
||||
|
||||
new_logger->set_pattern(formatter_pattern_);
|
||||
new_logger->set_formatter<pattern_formatter>(formatter_pattern_, pattern_time_type_);
|
||||
|
||||
if (err_handler_)
|
||||
{
|
||||
@@ -81,13 +81,14 @@ public:
|
||||
return tp_;
|
||||
}
|
||||
|
||||
void set_pattern(const std::string &pattern)
|
||||
void set_pattern(const std::string &pattern, pattern_time_type time_type)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
formatter_pattern_ = pattern;
|
||||
pattern_time_type_ = time_type;
|
||||
for (auto &l : loggers_)
|
||||
{
|
||||
l.second->set_pattern(pattern);
|
||||
l.second->set_pattern(pattern, time_type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +163,7 @@ public:
|
||||
private:
|
||||
registry_t<Mutex>() = default;
|
||||
|
||||
void throw_if_exists(const std::string &logger_name)
|
||||
void throw_if_exists_(const std::string &logger_name)
|
||||
{
|
||||
if (loggers_.find(logger_name) != loggers_.end())
|
||||
{
|
||||
@@ -174,6 +175,7 @@ private:
|
||||
Mutex tp_mutex_;
|
||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||
std::string formatter_pattern_ = "%+";
|
||||
pattern_time_type pattern_time_type_ = pattern_time_type::local;
|
||||
level::level_enum level_ = level::info;
|
||||
level::level_enum flush_level_ = level::off;
|
||||
log_err_handler err_handler_;
|
||||
|
||||
@@ -29,7 +29,7 @@ struct async_msg
|
||||
level::level_enum level;
|
||||
log_clock::time_point time;
|
||||
size_t thread_id;
|
||||
fmt::basic_memory_buffer<char, 128> raw;
|
||||
fmt::basic_memory_buffer<char, 176> raw;
|
||||
|
||||
size_t msg_id;
|
||||
async_logger_ptr worker_ptr;
|
||||
@@ -95,7 +95,7 @@ public:
|
||||
}
|
||||
for (size_t i = 0; i < threads_n; i++)
|
||||
{
|
||||
threads_.emplace_back(std::bind(&thread_pool::worker_loop, this));
|
||||
threads_.emplace_back(std::bind(&thread_pool::worker_loop_, this));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,14 +106,13 @@ public:
|
||||
{
|
||||
for (size_t i = 0; i < threads_.size(); i++)
|
||||
{
|
||||
post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
||||
post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
|
||||
}
|
||||
|
||||
for (auto &t : threads_)
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
// std::cout << "~thread_pool() msg_counter_: " << msg_counter_ << std::endl;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@@ -123,12 +122,12 @@ public:
|
||||
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy)
|
||||
{
|
||||
async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg));
|
||||
post_async_msg(std::move(async_m), overflow_policy);
|
||||
post_async_msg_(std::move(async_m), overflow_policy);
|
||||
}
|
||||
|
||||
void 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);
|
||||
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -136,9 +135,9 @@ private:
|
||||
|
||||
std::vector<std::thread> threads_;
|
||||
|
||||
void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
||||
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
||||
{
|
||||
if (overflow_policy == async_overflow_policy::block_retry)
|
||||
if (overflow_policy == async_overflow_policy::block)
|
||||
{
|
||||
q_.enqueue(std::move(new_msg));
|
||||
}
|
||||
@@ -148,14 +147,14 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void worker_loop()
|
||||
void worker_loop_()
|
||||
{
|
||||
while (process_next_msg()) {};
|
||||
while (process_next_msg_()) {};
|
||||
}
|
||||
|
||||
// process next message in the queue
|
||||
// return true if this thread should still be active (while no terminate msg was received)
|
||||
bool process_next_msg()
|
||||
bool process_next_msg_()
|
||||
{
|
||||
async_msg incoming_async_msg;
|
||||
bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
|
||||
|
||||
Reference in New Issue
Block a user