From 0d7a1d1ef93f141cac24c8d2aca4af75ac5cf6d1 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 26 May 2018 18:48:39 +0300 Subject: [PATCH] format --- bench/spdlog-async.cpp | 4 +- bench/spdlog-bench-mt.cpp | 2 +- bench/spdlog-bench.cpp | 2 +- example/example.cpp | 31 +- include/spdlog/common.h | 12 +- include/spdlog/details/async_logger_impl.h | 4 +- include/spdlog/details/logger_impl.h | 16 +- include/spdlog/details/thread_pool.h | 332 ++++++++++----------- tests/errors.cpp | 24 +- tests/file_log.cpp | 4 +- tests/test_async.cpp | 204 +++++++------ 11 files changed, 318 insertions(+), 317 deletions(-) diff --git a/bench/spdlog-async.cpp b/bench/spdlog-async.cpp index 32c9541c..9f2f75b9 100644 --- a/bench/spdlog-async.cpp +++ b/bench/spdlog-async.cpp @@ -25,8 +25,8 @@ int main(int argc, char *argv[]) thread_count = std::atoi(argv[1]); int howmany = 1000000; - spdlog::init_thread_pool (howmany, 1); - + spdlog::init_thread_pool(howmany, 1); + auto logger = spdlog::create_async_logger("file_logger", "logs/spdlog-bench-async.log", false); logger->set_pattern("[%Y-%m-%d %T.%F]: %L %t %v"); diff --git a/bench/spdlog-bench-mt.cpp b/bench/spdlog-bench-mt.cpp index 02ce98f5..c271519d 100644 --- a/bench/spdlog-bench-mt.cpp +++ b/bench/spdlog-bench-mt.cpp @@ -10,8 +10,8 @@ #include #include -#include "spdlog/spdlog.h" #include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/spdlog.h" using namespace std; diff --git a/bench/spdlog-bench.cpp b/bench/spdlog-bench.cpp index 395dc271..570581ca 100644 --- a/bench/spdlog-bench.cpp +++ b/bench/spdlog-bench.cpp @@ -6,8 +6,8 @@ #include #include -#include "spdlog/spdlog.h" #include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/spdlog.h" int main(int, char *[]) { diff --git a/example/example.cpp b/example/example.cpp index 71713d3f..ccc0bae3 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -82,7 +82,7 @@ int main(int, char *[]) // Asynchronous logging is very fast.. // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. - //async_example(); + // async_example(); // Log user-defined types example user_defined_example(); @@ -107,23 +107,22 @@ int main(int, char *[]) #include "spdlog/async.h" void async_example() { - //auto async_file = spd::basic_logger_mt("async_file_logger", "logs/async_log.txt"); - - for (int j = 0; j < 1; j++) - { - spdlog::init_thread_pool(1024, 10); - auto async_file = spd::stderr_color_mt("console"); - for (int i = 0; i < 1024; ++i) - { - async_file->info("{} Async message #{}", j, i); - } - spdlog::drop_all(); - } - //std::this_thread::sleep_for(std::chrono::seconds(1)); - + // auto async_file = spd::basic_logger_mt("async_file_logger", "logs/async_log.txt"); + + for (int j = 0; j < 1; j++) + { + spdlog::init_thread_pool(1024, 10); + auto async_file = spd::stderr_color_mt("console"); + for (int i = 0; i < 1024; ++i) + { + async_file->info("{} Async message #{}", j, i); + } + spdlog::drop_all(); + } + // std::this_thread::sleep_for(std::chrono::seconds(1)); // you can also modify thread pool settings *before* creating the logger: - // spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads. + // spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads. } // syslog example (linux/osx/freebsd) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index fc5cc8be..a2cd226e 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -179,7 +179,13 @@ using filename_t = std::wstring; using filename_t = std::string; #endif - -#define SPDLOG_CATCH_AND_HANDLE catch (const std::exception &ex) {_err_handler(ex.what());}\ - catch (...) {_err_handler("Unknown exeption in logger");} +#define SPDLOG_CATCH_AND_HANDLE \ + catch (const std::exception &ex) \ + { \ + _err_handler(ex.what()); \ + } \ + catch (...) \ + { \ + _err_handler("Unknown exeption in logger"); \ + } } // namespace spdlog diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index e49cee2b..66e089dc 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -80,7 +80,7 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms } } } - SPDLOG_CATCH_AND_HANDLE + SPDLOG_CATCH_AND_HANDLE if (_should_flush(incoming_log_msg)) { @@ -97,5 +97,5 @@ inline void spdlog::async_logger::_backend_flush() sink->flush(); } } - SPDLOG_CATCH_AND_HANDLE + SPDLOG_CATCH_AND_HANDLE } diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index c8ccac17..6cfbcf2c 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -66,7 +66,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar #endif _sink_it(log_msg); } - SPDLOG_CATCH_AND_HANDLE + SPDLOG_CATCH_AND_HANDLE } template @@ -82,7 +82,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg) log_msg.raw << msg; _sink_it(log_msg); } - SPDLOG_CATCH_AND_HANDLE + SPDLOG_CATCH_AND_HANDLE } template @@ -98,7 +98,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg) log_msg.raw << msg; _sink_it(log_msg); } - SPDLOG_CATCH_AND_HANDLE + SPDLOG_CATCH_AND_HANDLE } template @@ -257,11 +257,11 @@ inline spdlog::log_err_handler spdlog::logger::error_handler() inline void spdlog::logger::flush() { - try - { - _flush(); - } - SPDLOG_CATCH_AND_HANDLE + try + { + _flush(); + } + SPDLOG_CATCH_AND_HANDLE } inline void spdlog::logger::flush_on(level::level_enum log_level) diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 9e8a8f4b..4a476dd6 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -10,197 +10,195 @@ #include namespace spdlog { - namespace details { +namespace details { - using async_logger_ptr = std::shared_ptr; +using async_logger_ptr = std::shared_ptr; - enum class async_msg_type - { - log, - flush, - terminate - }; +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 - { - async_msg_type msg_type; - level::level_enum level; - log_clock::time_point time; - size_t thread_id; - std::string txt; +// Async msg to move to/from the queue +// Movable only. should never be copied +struct async_msg +{ + async_msg_type msg_type; + level::level_enum level; + log_clock::time_point time; + size_t thread_id; + std::string txt; - size_t msg_id; - async_logger_ptr worker_ptr; + size_t msg_id; + async_logger_ptr worker_ptr; - async_msg() = default; - ~async_msg() = default; + async_msg() = default; + ~async_msg() = default; - // should only be moved in or out of the queue.. - async_msg(const async_msg &) = delete; - async_msg(async_msg &&other) = default; - async_msg &operator=(async_msg &&other) = default; + // should only be moved in or out of the queue.. + async_msg(const async_msg &) = delete; + async_msg(async_msg &&other) = default; + async_msg &operator=(async_msg &&other) = default; - // construct from log_msg with given type - async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &&m) - : msg_type(the_type) - , level(m.level) - , time(m.time) - , thread_id(m.thread_id) - , txt(m.raw.data(), m.raw.size()) - , msg_id(m.msg_id) - , worker_ptr(std::forward(worker)) - { - } + // construct from log_msg with given type + async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &&m) + : msg_type(the_type) + , level(m.level) + , time(m.time) + , thread_id(m.thread_id) + , txt(m.raw.data(), m.raw.size()) + , msg_id(m.msg_id) + , worker_ptr(std::forward(worker)) + { + } - async_msg(async_logger_ptr &&worker, async_msg_type the_type) - : async_msg(std::forward(worker), the_type, details::log_msg()) - { - } + async_msg(async_logger_ptr &&worker, async_msg_type the_type) + : async_msg(std::forward(worker), the_type, details::log_msg()) + { + } - async_msg(async_msg_type the_type) - : async_msg(nullptr, the_type, details::log_msg()) - { - } + async_msg(async_msg_type the_type) + : async_msg(nullptr, the_type, details::log_msg()) + { + } - // copy into log_msg - void to_log_msg(log_msg &&msg) - { - msg.logger_name = &worker_ptr->name(); - msg.level = level; - msg.time = time; - msg.thread_id = thread_id; - msg.raw.clear(); - msg.raw << txt; - msg.formatted.clear(); - msg.msg_id = msg_id; - msg.color_range_start = 0; - msg.color_range_end = 0; - } - }; + // copy into log_msg + void to_log_msg(log_msg &&msg) + { + msg.logger_name = &worker_ptr->name(); + msg.level = level; + msg.time = time; + msg.thread_id = thread_id; + msg.raw.clear(); + msg.raw << txt; + msg.formatted.clear(); + msg.msg_id = msg_id; + msg.color_range_start = 0; + msg.color_range_end = 0; + } +}; - class thread_pool - { - public: - using item_type = async_msg; - using q_type = details::mpmc_blocking_queue; - using clock_type = std::chrono::steady_clock; +class thread_pool +{ +public: + using item_type = async_msg; + using q_type = details::mpmc_blocking_queue; + using clock_type = std::chrono::steady_clock; - thread_pool(size_t q_size_bytes, size_t threads_n) - : msg_counter_(0) - , _q(q_size_bytes) - { - // std::cout << "thread_pool() q_size_bytes: " << q_size_bytes << "\tthreads_n: " << threads_n << std::endl; - 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(std::bind(&thread_pool::worker_loop, this)); - } - } + thread_pool(size_t q_size_bytes, size_t threads_n) + : msg_counter_(0) + , _q(q_size_bytes) + { + // std::cout << "thread_pool() q_size_bytes: " << q_size_bytes << "\tthreads_n: " << threads_n << std::endl; + 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(std::bind(&thread_pool::worker_loop, this)); + } + } - // message all threads to terminate gracefully join them - ~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_retry); - } + // message all threads to terminate gracefully join them + ~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_retry); + } - for (auto &t : _threads) - { - t.join(); - } - // std::cout << "~thread_pool() msg_counter_: " << msg_counter_ << std::endl; - } - catch (...) - { - } - } + for (auto &t : _threads) + { + t.join(); + } + // std::cout << "~thread_pool() msg_counter_: " << msg_counter_ << std::endl; + } + catch (...) + { + } + } - void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy) - { - async_msg async_m(std::forward(worker_ptr), async_msg_type::log, std::forward(msg)); - post_async_msg(std::move(async_m), overflow_policy); - } + void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy) + { + async_msg async_m(std::forward(worker_ptr), async_msg_type::log, std::forward(msg)); + 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); - } + 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); + } - size_t msg_counter() - { - return msg_counter_.load(std::memory_order_relaxed); - } + size_t msg_counter() + { + return msg_counter_.load(std::memory_order_relaxed); + } - private: - std::atomic msg_counter_; // total # of messages processed in this pool - q_type _q; +private: + std::atomic msg_counter_; // total # of messages processed in this pool + q_type _q; - std::vector _threads; + std::vector _threads; - void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy) - { - if (overflow_policy == async_overflow_policy::block_retry) - { - _q.enqueue(std::move(new_msg)); - } - else - { - _q.enqueue_nowait(std::move(new_msg)); - } - } + void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy) + { + if (overflow_policy == async_overflow_policy::block_retry) + { + _q.enqueue(std::move(new_msg)); + } + else + { + _q.enqueue_nowait(std::move(new_msg)); + } + } - void worker_loop() - { - while (process_next_msg()) - { - }; - } + void 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 process_next_msg() - { - async_msg incoming_async_msg; - bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); - if (!dequeued) - { - return true; - } + // 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() + { + async_msg incoming_async_msg; + bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); + if (!dequeued) + { + return true; + } - switch (incoming_async_msg.msg_type) - { - case async_msg_type::flush: - { - incoming_async_msg.worker_ptr->_backend_flush(); - return true; - } + switch (incoming_async_msg.msg_type) + { + case async_msg_type::flush: + { + incoming_async_msg.worker_ptr->_backend_flush(); + return true; + } - case async_msg_type::terminate: - { - return false; - } + case async_msg_type::terminate: + { + return false; + } - default: - { - log_msg msg; - incoming_async_msg.to_log_msg(std::move(msg)); - incoming_async_msg.worker_ptr->_backend_log(msg); - msg_counter_.fetch_add(1, std::memory_order_relaxed); - return true; - } - } - assert(false); - return true; // should not be reached - } - }; + default: + { + log_msg msg; + incoming_async_msg.to_log_msg(std::move(msg)); + incoming_async_msg.worker_ptr->_backend_log(msg); + msg_counter_.fetch_add(1, std::memory_order_relaxed); + return true; + } + } + assert(false); + return true; // should not be reached + } +}; - } // namespace details +} // namespace details } // namespace spdlog diff --git a/tests/errors.cpp b/tests/errors.cpp index 0e5475b4..f0d7125d 100644 --- a/tests/errors.cpp +++ b/tests/errors.cpp @@ -13,14 +13,14 @@ class failing_sink : public spdlog::sinks::sink } void flush() override - { - throw std::runtime_error("some error happened during flush"); - } + { + throw std::runtime_error("some error happened during flush"); + } }; TEST_CASE("default_error_handler", "[errors]]") { - spdlog::drop_all(); + spdlog::drop_all(); prepare_logdir(); std::string filename = "logs/simple_log.txt"; @@ -44,7 +44,7 @@ struct custom_ex }; TEST_CASE("custom_error_handler", "[errors]]") { - spdlog::drop_all(); + spdlog::drop_all(); prepare_logdir(); std::string filename = "logs/simple_log.txt"; auto logger = spdlog::create("logger", filename, true); @@ -62,7 +62,7 @@ TEST_CASE("custom_error_handler", "[errors]]") TEST_CASE("default_error_handler2", "[errors]]") { - spdlog::drop_all(); + spdlog::drop_all(); auto logger = spdlog::create("failed_logger"); logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex); @@ -70,15 +70,15 @@ TEST_CASE("default_error_handler2", "[errors]]") TEST_CASE("flush_error_handler", "[errors]]") { - spdlog::drop_all(); - auto logger = spdlog::create("failed_logger"); - logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); - REQUIRE_THROWS_AS(logger->flush(), custom_ex); + spdlog::drop_all(); + auto logger = spdlog::create("failed_logger"); + logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); + REQUIRE_THROWS_AS(logger->flush(), custom_ex); } TEST_CASE("async_error_handler", "[errors]]") { - spdlog::drop_all(); + spdlog::drop_all(); prepare_logdir(); std::string err_msg("log failed with some msg"); @@ -109,7 +109,7 @@ TEST_CASE("async_error_handler", "[errors]]") // Make sure async error handler is executed TEST_CASE("async_error_handler2", "[errors]]") { - spdlog::drop_all(); + spdlog::drop_all(); prepare_logdir(); std::string err_msg("This is async handler error message"); { diff --git a/tests/file_log.cpp b/tests/file_log.cpp index f5ad0cea..dbd75273 100644 --- a/tests/file_log.cpp +++ b/tests/file_log.cpp @@ -50,7 +50,7 @@ TEST_CASE("flush_on", "[flush_on]]") TEST_CASE("rotating_file_logger1", "[rotating_logger]]") { prepare_logdir(); - size_t max_size = 1024 * 10; + size_t max_size = 1024 * 10; std::string basename = "logs/rotating_log"; auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0); @@ -71,7 +71,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") TEST_CASE("rotating_file_logger2", "[rotating_logger]]") { prepare_logdir(); - size_t max_size = 1024 * 10; + size_t max_size = 1024 * 10; std::string basename = "logs/rotating_log"; auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1); for (int i = 0; i < 10; ++i) diff --git a/tests/test_async.cpp b/tests/test_async.cpp index eb5840be..2d86dafd 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -1,9 +1,9 @@ #include "includes.h" -#include "test_sink.h" #include "spdlog/async.h" #include "spdlog/sinks/simple_file_sink.h" +#include "test_sink.h" -//std::unique_ptr create_logger(size_t tp_queue_size, size_t tp_threads) +// std::unique_ptr create_logger(size_t tp_queue_size, size_t tp_threads) //{ // auto tp = std::make_shared(8192, 1); // auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); @@ -11,124 +11,122 @@ TEST_CASE("basic async test ", "[async]") { - using namespace spdlog; - auto test_sink = std::make_shared(); - size_t queue_size = 128; - size_t messages = 256; - { - auto tp = std::make_shared(queue_size, 1); - auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); - for (size_t i = 0; i < messages; i++) - { - logger->info("Hello message #{}", i); - } - logger->flush(); - } - REQUIRE(test_sink->msg_counter() == messages); - REQUIRE(test_sink->flushed_msg_counter() == messages); + using namespace spdlog; + auto test_sink = std::make_shared(); + size_t queue_size = 128; + size_t messages = 256; + { + auto tp = std::make_shared(queue_size, 1); + auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); + for (size_t i = 0; i < messages; i++) + { + logger->info("Hello message #{}", i); + } + logger->flush(); + } + REQUIRE(test_sink->msg_counter() == messages); + REQUIRE(test_sink->flushed_msg_counter() == messages); } TEST_CASE("discard policy ", "[async]") { - using namespace spdlog; - auto test_sink = std::make_shared(); - size_t queue_size = 2; - size_t messages = 1024; - { - auto tp = std::make_shared(queue_size, 1); - auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::discard_log_msg); - for (size_t i = 0; i < messages; i++) - { - logger->info("Hello message #{}", i); - } - } - - REQUIRE(test_sink->msg_counter() < messages); - REQUIRE(test_sink->flushed_msg_counter() < messages); + using namespace spdlog; + auto test_sink = std::make_shared(); + size_t queue_size = 2; + size_t messages = 1024; + { + auto tp = std::make_shared(queue_size, 1); + auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::discard_log_msg); + for (size_t i = 0; i < messages; i++) + { + logger->info("Hello message #{}", i); + } + } + + REQUIRE(test_sink->msg_counter() < messages); + REQUIRE(test_sink->flushed_msg_counter() < messages); } TEST_CASE("flush", "[async]") { - using namespace spdlog; - auto test_sink = std::make_shared(); - size_t queue_size = 256; - size_t messages = 256; - { - auto tp = std::make_shared(queue_size, 1); - auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); - 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->flushed_msg_counter() == messages); + using namespace spdlog; + auto test_sink = std::make_shared(); + size_t queue_size = 256; + size_t messages = 256; + { + auto tp = std::make_shared(queue_size, 1); + auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); + 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->flushed_msg_counter() == messages); } TEST_CASE("multi threads", "[async]") { - using namespace spdlog; - auto test_sink = std::make_shared(); - size_t queue_size = 128; - size_t messages = 256; - size_t n_threads = 10; - { - auto tp = std::make_shared(queue_size, 1); - auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); + using namespace spdlog; + auto test_sink = std::make_shared(); + size_t queue_size = 128; + size_t messages = 256; + size_t n_threads = 10; + { + auto tp = std::make_shared(queue_size, 1); + auto logger = std::make_shared("as", test_sink, tp, async_overflow_policy::block_retry); - std::vector 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); - } - - }); - } - - for (auto &t : threads) - { - t.join(); - } - logger->flush(); + std::vector 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); + } + }); + } - } - - REQUIRE(test_sink->msg_counter() == messages * n_threads); - REQUIRE(test_sink->flushed_msg_counter() == messages * n_threads); + for (auto &t : threads) + { + t.join(); + } + logger->flush(); + } + + REQUIRE(test_sink->msg_counter() == messages * n_threads); + REQUIRE(test_sink->flushed_msg_counter() == messages * n_threads); } TEST_CASE("to_file", "[async]") { - prepare_logdir(); - size_t queue_size = 512; - size_t messages = 512; - size_t n_threads = 4; - spdlog::init_thread_pool(queue_size, n_threads); - auto logger= spdlog::basic_logger_mt("as", "logs/async_test.log", true); - - std::vector 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); - } - }); - } + prepare_logdir(); + size_t queue_size = 512; + size_t messages = 512; + size_t n_threads = 4; + spdlog::init_thread_pool(queue_size, n_threads); + auto logger = spdlog::basic_logger_mt("as", "logs/async_test.log", true); - for (auto &t : threads) - { - t.join(); - } - logger.reset(); - spdlog::drop("as"); - std::this_thread::sleep_for(std::chrono::seconds(1)); - REQUIRE(count_lines("logs/async_test.log") == messages * n_threads); + std::vector 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); + } + }); + } + + for (auto &t : threads) + { + t.join(); + } + logger.reset(); + spdlog::drop("as"); + std::this_thread::sleep_for(std::chrono::seconds(1)); + REQUIRE(count_lines("logs/async_test.log") == messages * n_threads); }