mirror of https://github.com/gabime/spdlog.git
wip1
parent
166843ff3a
commit
8d4943789c
@ -1,85 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
//
|
||||
// Async logging using global thread pool
|
||||
// All loggers created here share same global thread pool.
|
||||
// Each log message is pushed to a queue along with a shared pointer to the
|
||||
// logger.
|
||||
// If a logger deleted while having pending messages in the queue, it's actual
|
||||
// destruction will defer
|
||||
// until all its messages are processed by the thread pool.
|
||||
// This is because each message in the queue holds a shared_ptr to the
|
||||
// originating logger.
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include "./async_logger.h"
|
||||
#include "./details/context.h"
|
||||
#include "./details/thread_pool.h"
|
||||
#include "spdlog.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
static constexpr size_t default_async_q_size = 8192;
|
||||
}
|
||||
|
||||
// async logger factory - creates async loggers backed with thread pool.
|
||||
// if a global thread pool doesn't already exist, create it with default queue
|
||||
// size of 8192 items and single thread.
|
||||
template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
|
||||
struct async_factory_impl {
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
|
||||
auto context = spdlog::context();
|
||||
// create global thread pool if not already exists
|
||||
auto &mutex = context->tp_mutex();
|
||||
std::lock_guard<std::recursive_mutex> tp_lock(mutex);
|
||||
auto tp = context->get_tp();
|
||||
if (tp == nullptr) {
|
||||
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
|
||||
context->set_tp(tp);
|
||||
}
|
||||
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);
|
||||
return new_logger;
|
||||
}
|
||||
};
|
||||
|
||||
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>
|
||||
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>
|
||||
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.
|
||||
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);
|
||||
spdlog::context()->set_tp(std::move(tp));
|
||||
}
|
||||
|
||||
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) {
|
||||
init_thread_pool(q_size, thread_count, on_thread_start, [] {});
|
||||
}
|
||||
|
||||
inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
||||
init_thread_pool(q_size, thread_count, [] {}, [] {});
|
||||
}
|
||||
|
||||
// get the global thread pool.
|
||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() { return spdlog::context()->get_tp(); }
|
||||
} // namespace spdlog
|
@ -1,21 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "./context.h"
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
// Default logger factory- creates synchronous loggers
|
||||
class logger;
|
||||
|
||||
struct synchronous_factory {
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args) {
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
||||
return new_logger;
|
||||
}
|
||||
};
|
||||
} // namespace spdlog
|
@ -1,112 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "../async.h"
|
||||
#include "./log_msg_buffer.h"
|
||||
#include "./mpmc_blocking_q.h"
|
||||
#include "./os.h"
|
||||
|
||||
namespace spdlog {
|
||||
class async_logger;
|
||||
|
||||
namespace details {
|
||||
|
||||
using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
|
||||
|
||||
enum class async_msg_type { log, flush, terminate };
|
||||
|
||||
// Async msg to move to/from the queue
|
||||
// Movable only. should never be copied
|
||||
struct async_msg : log_msg_buffer {
|
||||
async_msg_type msg_type{async_msg_type::log};
|
||||
async_logger_ptr worker_ptr;
|
||||
|
||||
async_msg() = default;
|
||||
~async_msg() = default;
|
||||
|
||||
// should only be moved in or out of the queue..
|
||||
async_msg(const async_msg &) = delete;
|
||||
|
||||
// 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)) {}
|
||||
|
||||
async_msg &operator=(async_msg &&other) {
|
||||
*static_cast<log_msg_buffer *>(this) = std::move(other);
|
||||
msg_type = other.msg_type;
|
||||
worker_ptr = std::move(other.worker_ptr);
|
||||
return *this;
|
||||
}
|
||||
#else // (_MSC_VER) && _MSC_VER <= 1800
|
||||
async_msg(async_msg &&) = default;
|
||||
async_msg &operator=(async_msg &&) = default;
|
||||
#endif
|
||||
|
||||
// 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)} {}
|
||||
|
||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
|
||||
: 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} {}
|
||||
};
|
||||
|
||||
class SPDLOG_API thread_pool {
|
||||
public:
|
||||
using item_type = async_msg;
|
||||
using q_type = details::mpmc_blocking_queue<item_type>;
|
||||
|
||||
thread_pool(size_t q_max_items,
|
||||
size_t threads_n,
|
||||
std::function<void()> on_thread_start,
|
||||
std::function<void()> on_thread_stop);
|
||||
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
|
||||
thread_pool(size_t q_max_items, size_t threads_n);
|
||||
|
||||
// message all threads to terminate gracefully and join them
|
||||
~thread_pool();
|
||||
|
||||
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_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
||||
size_t overrun_counter();
|
||||
void reset_overrun_counter();
|
||||
size_t discard_counter();
|
||||
void reset_discard_counter();
|
||||
size_t queue_size();
|
||||
|
||||
private:
|
||||
q_type q_;
|
||||
|
||||
std::vector<std::thread> threads_;
|
||||
|
||||
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
|
||||
void worker_loop_();
|
||||
|
||||
// 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_();
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@ -1,79 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/async_logger.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "spdlog/details/thread_pool.h"
|
||||
#include "spdlog/sinks/sink.h"
|
||||
|
||||
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) {}
|
||||
|
||||
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) {}
|
||||
|
||||
// send the log message to the thread pool
|
||||
void spdlog::async_logger::sink_it_(const details::log_msg &msg) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH(msg.source)
|
||||
}
|
||||
|
||||
// send flush request to the thread pool
|
||||
void spdlog::async_logger::flush_() {
|
||||
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");
|
||||
}
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH(source_loc())
|
||||
}
|
||||
|
||||
//
|
||||
// backend functions - called from the thread pool to do the actual job
|
||||
//
|
||||
void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) {
|
||||
for (auto &sink : sinks_) {
|
||||
if (sink->should_log(msg.log_level)) {
|
||||
try {
|
||||
sink->log(msg);
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH(msg.source)
|
||||
}
|
||||
}
|
||||
|
||||
if (should_flush_(msg)) {
|
||||
backend_flush_();
|
||||
}
|
||||
}
|
||||
|
||||
void spdlog::async_logger::backend_flush_() {
|
||||
for (auto &sink : sinks_) {
|
||||
try {
|
||||
sink->flush();
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH(source_loc())
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) {
|
||||
auto cloned = std::make_shared<spdlog::async_logger>(*this);
|
||||
cloned->name_ = std::move(new_name);
|
||||
return cloned;
|
||||
}
|
@ -1,117 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/details/thread_pool.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "spdlog/common.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
thread_pool::thread_pool(size_t q_max_items,
|
||||
size_t threads_n,
|
||||
std::function<void()> on_thread_start,
|
||||
std::function<void()> on_thread_stop)
|
||||
: q_(q_max_items) {
|
||||
if (threads_n == 0 || threads_n > 1000) {
|
||||
throw_spdlog_ex(
|
||||
"spdlog::thread_pool(): invalid threads_n param (valid "
|
||||
"range is 1-1000)");
|
||||
}
|
||||
for (size_t i = 0; i < threads_n; i++) {
|
||||
threads_.emplace_back([this, on_thread_start, on_thread_stop] {
|
||||
on_thread_start();
|
||||
this->thread_pool::worker_loop_();
|
||||
on_thread_stop();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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, [] {}) {}
|
||||
|
||||
thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
|
||||
: thread_pool(q_max_items, threads_n, [] {}, [] {}) {}
|
||||
|
||||
// message all threads to terminate gracefully join them
|
||||
thread_pool::~thread_pool() {
|
||||
try {
|
||||
for (size_t i = 0; i < threads_.size(); i++) {
|
||||
post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
|
||||
}
|
||||
|
||||
for (auto &t : threads_) {
|
||||
t.join();
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
void thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy) {
|
||||
async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
|
||||
post_async_msg_(std::move(async_m), overflow_policy);
|
||||
}
|
||||
|
||||
void 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);
|
||||
}
|
||||
|
||||
size_t thread_pool::overrun_counter() { return q_.overrun_counter(); }
|
||||
|
||||
void thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); }
|
||||
|
||||
size_t thread_pool::discard_counter() { return q_.discard_counter(); }
|
||||
|
||||
void thread_pool::reset_discard_counter() { q_.reset_discard_counter(); }
|
||||
|
||||
size_t thread_pool::queue_size() { return q_.size(); }
|
||||
|
||||
void 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) {
|
||||
q_.enqueue_nowait(std::move(new_msg));
|
||||
} else {
|
||||
assert(overflow_policy == async_overflow_policy::discard_new);
|
||||
q_.enqueue_if_have_room(std::move(new_msg));
|
||||
}
|
||||
}
|
||||
|
||||
void thread_pool::worker_loop_() {
|
||||
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 thread_pool::process_next_msg_() {
|
||||
async_msg incoming_async_msg;
|
||||
q_.dequeue(incoming_async_msg);
|
||||
|
||||
switch (incoming_async_msg.msg_type) {
|
||||
case async_msg_type::log: {
|
||||
incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
|
||||
return true;
|
||||
}
|
||||
case async_msg_type::flush: {
|
||||
incoming_async_msg.worker_ptr->backend_flush_();
|
||||
return true;
|
||||
}
|
||||
|
||||
case async_msg_type::terminate: {
|
||||
return false;
|
||||
}
|
||||
|
||||
default: {
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@ -1,14 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/sinks/sink.h"
|
||||
|
||||
#include "spdlog/common.h"
|
||||
|
||||
bool spdlog::sinks::sink::should_log(spdlog::level msg_level) const {
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void spdlog::sinks::sink::set_level(level level) { level_.store(level, std::memory_order_relaxed); }
|
||||
|
||||
spdlog::level spdlog::sinks::sink::log_level() const { return level_.load(std::memory_order_relaxed); }
|
@ -1,57 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
|
||||
#include "spdlog/async.h"
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
#include "spdlog/logger.h"
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
template <typename Factory>
|
||||
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>
|
||||
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>
|
||||
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>
|
||||
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
|
||||
|
||||
// template instantiations
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(
|
||||
const std::string &logger_name, color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::synchronous_factory>(
|
||||
const std::string &logger_name, color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::synchronous_factory>(
|
||||
const std::string &logger_name, color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::synchronous_factory>(
|
||||
const std::string &logger_name, color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::async_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::async_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
@ -1,180 +1,180 @@
|
||||
#include "includes.h"
|
||||
#include "spdlog/async.h"
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
#define TEST_FILENAME "test_logs/async_test.log"
|
||||
|
||||
TEST_CASE("basic async test ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t overrun_counter = 0;
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
overrun_counter = tp->overrun_counter();
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() == messages);
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
REQUIRE(overrun_counter == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::overrun_oldest);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->overrun_counter() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy discard_new ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::discard_new);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->discard_counter() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy using factory ", "[async]") {
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
spdlog::init_thread_pool(queue_size, 1);
|
||||
|
||||
auto logger = spdlog::create_async_nb<spdlog::sinks::test_sink_mt>("as2");
|
||||
auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
test_sink->set_delay(std::chrono::milliseconds(3));
|
||||
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
}
|
||||
|
||||
TEST_CASE("flush", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 256;
|
||||
size_t messages = 256;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
}
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
REQUIRE(test_sink->msg_counter() == messages);
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("tp->wait_empty() ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(5));
|
||||
size_t messages = 100;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, 2);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
tp.reset();
|
||||
|
||||
REQUIRE(test_sink->msg_counter() == messages);
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("multi threads", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
size_t n_threads = 10;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (size_t i = 0; i < n_threads; i++) {
|
||||
threads.emplace_back([logger, messages] {
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
});
|
||||
logger->flush();
|
||||
}
|
||||
|
||||
for (auto &t : threads) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(test_sink->msg_counter() == messages * n_threads);
|
||||
REQUIRE(test_sink->flush_counter() == n_threads);
|
||||
}
|
||||
|
||||
TEST_CASE("to_file", "[async]") {
|
||||
prepare_logdir();
|
||||
size_t messages = 1024;
|
||||
size_t tp_threads = 1;
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
}
|
||||
|
||||
require_message_count(TEST_FILENAME, messages);
|
||||
auto contents = file_contents(TEST_FILENAME);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_file multi-workers", "[async]") {
|
||||
prepare_logdir();
|
||||
size_t messages = 1024 * 10;
|
||||
size_t tp_threads = 10;
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
}
|
||||
require_message_count(TEST_FILENAME, messages);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_tp", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
std::shared_ptr<spdlog::details::thread_pool> const empty_tp;
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, empty_tp);
|
||||
logger->info("Please throw an exception");
|
||||
REQUIRE(test_sink->msg_counter() == 0);
|
||||
}
|
||||
//#include "includes.h"
|
||||
//#include "spdlog/async.h"
|
||||
//#include "spdlog/sinks/basic_file_sink.h"
|
||||
//#include "test_sink.h"
|
||||
//
|
||||
//#define TEST_FILENAME "test_logs/async_test.log"
|
||||
//
|
||||
//TEST_CASE("basic async test ", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// size_t overrun_counter = 0;
|
||||
// size_t queue_size = 128;
|
||||
// size_t messages = 256;
|
||||
// {
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
// for (size_t i = 0; i < messages; i++) {
|
||||
// logger->info("Hello message #{}", i);
|
||||
// }
|
||||
// logger->flush();
|
||||
// overrun_counter = tp->overrun_counter();
|
||||
// }
|
||||
// REQUIRE(test_sink->msg_counter() == messages);
|
||||
// REQUIRE(test_sink->flush_counter() == 1);
|
||||
// REQUIRE(overrun_counter == 0);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("discard policy ", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
// size_t queue_size = 4;
|
||||
// size_t messages = 1024;
|
||||
//
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::overrun_oldest);
|
||||
// for (size_t i = 0; i < messages; i++) {
|
||||
// logger->info("Hello message");
|
||||
// }
|
||||
// REQUIRE(test_sink->msg_counter() < messages);
|
||||
// REQUIRE(tp->overrun_counter() > 0);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("discard policy discard_new ", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
// size_t queue_size = 4;
|
||||
// size_t messages = 1024;
|
||||
//
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::discard_new);
|
||||
// for (size_t i = 0; i < messages; i++) {
|
||||
// logger->info("Hello message");
|
||||
// }
|
||||
// REQUIRE(test_sink->msg_counter() < messages);
|
||||
// REQUIRE(tp->discard_counter() > 0);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("discard policy using factory ", "[async]") {
|
||||
// size_t queue_size = 4;
|
||||
// size_t messages = 1024;
|
||||
// spdlog::init_thread_pool(queue_size, 1);
|
||||
//
|
||||
// auto logger = spdlog::create_async_nb<spdlog::sinks::test_sink_mt>("as2");
|
||||
// auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
// test_sink->set_delay(std::chrono::milliseconds(3));
|
||||
//
|
||||
// for (size_t i = 0; i < messages; i++) {
|
||||
// logger->info("Hello message");
|
||||
// }
|
||||
//
|
||||
// REQUIRE(test_sink->msg_counter() < messages);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("flush", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// size_t queue_size = 256;
|
||||
// size_t messages = 256;
|
||||
// {
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
// for (size_t i = 0; i < messages; i++) {
|
||||
// logger->info("Hello message #{}", i);
|
||||
// }
|
||||
//
|
||||
// logger->flush();
|
||||
// }
|
||||
// // std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
// REQUIRE(test_sink->msg_counter() == messages);
|
||||
// REQUIRE(test_sink->flush_counter() == 1);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("tp->wait_empty() ", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// test_sink->set_delay(std::chrono::milliseconds(5));
|
||||
// size_t messages = 100;
|
||||
//
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(messages, 2);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
// for (size_t i = 0; i < messages; i++) {
|
||||
// logger->info("Hello message #{}", i);
|
||||
// }
|
||||
// logger->flush();
|
||||
// tp.reset();
|
||||
//
|
||||
// REQUIRE(test_sink->msg_counter() == messages);
|
||||
// REQUIRE(test_sink->flush_counter() == 1);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("multi threads", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// size_t queue_size = 128;
|
||||
// size_t messages = 256;
|
||||
// size_t n_threads = 10;
|
||||
// {
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
//
|
||||
// std::vector<std::thread> threads;
|
||||
// for (size_t i = 0; i < n_threads; i++) {
|
||||
// threads.emplace_back([logger, messages] {
|
||||
// for (size_t j = 0; j < messages; j++) {
|
||||
// logger->info("Hello message #{}", j);
|
||||
// }
|
||||
// });
|
||||
// logger->flush();
|
||||
// }
|
||||
//
|
||||
// for (auto &t : threads) {
|
||||
// t.join();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// REQUIRE(test_sink->msg_counter() == messages * n_threads);
|
||||
// REQUIRE(test_sink->flush_counter() == n_threads);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("to_file", "[async]") {
|
||||
// prepare_logdir();
|
||||
// size_t messages = 1024;
|
||||
// size_t tp_threads = 1;
|
||||
// spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
// {
|
||||
// auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
//
|
||||
// for (size_t j = 0; j < messages; j++) {
|
||||
// logger->info("Hello message #{}", j);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// require_message_count(TEST_FILENAME, messages);
|
||||
// auto contents = file_contents(TEST_FILENAME);
|
||||
// using spdlog::details::os::default_eol;
|
||||
// REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("to_file multi-workers", "[async]") {
|
||||
// prepare_logdir();
|
||||
// size_t messages = 1024 * 10;
|
||||
// size_t tp_threads = 10;
|
||||
// spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
// {
|
||||
// auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
// auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
//
|
||||
// for (size_t j = 0; j < messages; j++) {
|
||||
// logger->info("Hello message #{}", j);
|
||||
// }
|
||||
// }
|
||||
// require_message_count(TEST_FILENAME, messages);
|
||||
//}
|
||||
//
|
||||
//TEST_CASE("bad_tp", "[async]") {
|
||||
// auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
// std::shared_ptr<spdlog::details::thread_pool> const empty_tp;
|
||||
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, empty_tp);
|
||||
// logger->info("Please throw an exception");
|
||||
// REQUIRE(test_sink->msg_counter() == 0);
|
||||
//}
|
||||
|
Loading…
Reference in New Issue