From 8d4943789c15d3d685442f521e34bc673a16acdd Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 7 Dec 2024 00:52:27 +0200 Subject: [PATCH] wip1 --- CMakeLists.txt | 17 +- README.md | 24 +- example/example.cpp | 50 +-- include/spdlog/async.h | 85 ----- include/spdlog/details/synchronous_factory.h | 21 -- include/spdlog/details/thread_pool.h | 112 ------ include/spdlog/sinks/android_sink.h | 14 - include/spdlog/sinks/base_sink.h | 1 + include/spdlog/sinks/basic_file_sink.h | 21 -- include/spdlog/sinks/callback_sink.h | 15 - include/spdlog/sinks/daily_file_sink.h | 52 --- include/spdlog/sinks/hourly_file_sink.h | 22 -- include/spdlog/sinks/kafka_sink.h | 23 -- include/spdlog/sinks/mongo_sink.h | 25 +- include/spdlog/sinks/msvc_sink.h | 1 - include/spdlog/sinks/null_sink.h | 16 - include/spdlog/sinks/qt_sinks.h | 68 +--- include/spdlog/sinks/rotating_file_sink.h | 32 +- include/spdlog/sinks/sink.h | 6 +- include/spdlog/sinks/stdout_color_sinks.h | 18 +- include/spdlog/sinks/stdout_sinks.h | 16 +- include/spdlog/sinks/syslog_sink.h | 25 +- include/spdlog/sinks/systemd_sink.h | 17 +- include/spdlog/sinks/tcp_sink.h | 2 +- include/spdlog/sinks/udp_sink.h | 13 +- include/spdlog/sinks/wincolor_sink.h | 1 + include/spdlog/spdlog.h | 5 +- src/async_logger.cpp | 79 ---- src/details/thread_pool.cpp | 117 ------ src/sinks/ansicolor_sink.cpp | 13 +- src/sinks/base_sink.cpp | 29 +- src/sinks/basic_file_sink.cpp | 11 +- src/sinks/rotating_file_sink.cpp | 9 +- src/sinks/sink.cpp | 14 - src/sinks/stdout_color_sinks.cpp | 57 --- src/sinks/stdout_sinks.cpp | 57 +-- src/sinks/wincolor_sink.cpp | 25 +- tests/includes.h | 1 - tests/test_async.cpp | 360 +++++++++---------- tests/test_custom_callbacks.cpp | 1 - tests/test_errors.cpp | 49 +-- tests/test_file_logging.cpp | 21 +- tests/test_macros.cpp | 4 +- tests/test_misc.cpp | 41 +-- tests/test_mpmc_q.cpp | 1 + tests/test_stdout_api.cpp | 12 +- tests/test_time_point.cpp | 1 - 47 files changed, 318 insertions(+), 1286 deletions(-) delete mode 100644 include/spdlog/async.h delete mode 100644 include/spdlog/details/synchronous_factory.h delete mode 100644 include/spdlog/details/thread_pool.h delete mode 100644 src/async_logger.cpp delete mode 100644 src/details/thread_pool.cpp delete mode 100644 src/sinks/sink.cpp delete mode 100644 src/sinks/stdout_color_sinks.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 64c81f70..fdb951d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,8 +139,7 @@ find_package(Threads REQUIRED) # Library sources # --------------------------------------------------------------------------------------- set(SPDLOG_HEADERS - "include/spdlog/async.h" - "include/spdlog/async_logger.h" + "include/spdlog/async_logger.h" "include/spdlog/common.h" "include/spdlog/formatter.h" "include/spdlog/fwd.h" @@ -160,9 +159,7 @@ set(SPDLOG_HEADERS "include/spdlog/details/os.h" "include/spdlog/details/periodic_worker.h" "include/spdlog/details/context.h" - "include/spdlog/details/synchronous_factory.h" - "include/spdlog/details/thread_pool.h" - "include/spdlog/fmt/bin_to_hex.h" + "include/spdlog/fmt/bin_to_hex.h" "include/spdlog/fmt/fmt.h" "include/spdlog/sinks/android_sink.h" "include/spdlog/sinks/base_sink.h" @@ -189,8 +186,7 @@ set(SPDLOG_HEADERS "include/spdlog/sinks/udp_sink.h") set(SPDLOG_SRCS - "src/async_logger.cpp" - "src/common.cpp" + "src/common.cpp" "src/logger.cpp" "src/pattern_formatter.cpp" "src/spdlog.cpp" @@ -199,13 +195,10 @@ set(SPDLOG_SRCS "src/details/log_msg.cpp" "src/details/log_msg_buffer.cpp" "src/details/context.cpp" - "src/details/thread_pool.cpp" - "src/sinks/base_sink.cpp" + "src/sinks/base_sink.cpp" "src/sinks/basic_file_sink.cpp" "src/sinks/rotating_file_sink.cpp" - "src/sinks/sink.cpp" - "src/sinks/stdout_color_sinks.cpp" - "src/sinks/stdout_sinks.cpp") + "src/sinks/stdout_sinks.cpp") if(WIN32) list(APPEND SPDLOG_SRCS diff --git a/README.md b/README.md index 31c845bb..89c83611 100644 --- a/README.md +++ b/README.md @@ -245,33 +245,11 @@ void callback_example() #include "spdlog/sinks/basic_file_sink.h" void async_example() { - // default thread pool settings can be modified *before* creating the async logger: - // spdlog::init_thread_pool(8192, 1); // queue with 8k items and 1 backing thread. - auto async_file = spdlog::basic_logger_mt("async_file_logger", "logs/async_log.txt"); - // alternatively: - // auto async_file = spdlog::create_async("async_file_logger", "logs/async_log.txt"); + // TODO } ``` ---- -#### Asynchronous logger with multi sinks -```c++ -#include "spdlog/async.h" -#include "spdlog/sinks/stdout_color_sinks.h" -#include "spdlog/sinks/rotating_file_sink.h" - -void multi_sink_example2() -{ - spdlog::init_thread_pool(8192, 1); - auto stdout_sink = std::make_shared(); - auto rotating_sink = std::make_shared("mylog.txt", 1024*1024*10, 3); - std::vector sinks {stdout_sink, rotating_sink}; - auto logger = std::make_shared("loggername", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); - spdlog::register_logger(logger); -} -``` - --- #### User-defined types ```c++ diff --git a/example/example.cpp b/example/example.cpp index 032e62c5..a86a049a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -29,6 +29,8 @@ void replace_global_logger_example(); #include "spdlog/spdlog.h" #include "spdlog/version.h" +using namespace spdlog::sinks; + int main(int, char *[]) { spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); spdlog::warn("Easy padding in numbers like {:08d}", 12); @@ -84,50 +86,50 @@ int main(int, char *[]) { // or #include "spdlog/sinks/stdout_sinks.h" if no colors needed. void stdout_logger_example() { // Create color multithreading logger. - auto console = spdlog::stdout_color_mt("console"); + auto console = spdlog::create("console"); // or for stderr: - // auto console = spdlog::stderr_color_mt("error-logger"); + //auto console = spdlog::create("console"); } #include "spdlog/sinks/basic_file_sink.h" void basic_example() { // Create basic file logger (not rotated). - auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt", true); + auto my_logger = spdlog::create("file_logger", "logs/basic-log.txt", true); } #include "spdlog/sinks/rotating_file_sink.h" void rotating_example() { // Create a file rotating logger with 5mb size max and 3 rotated files. - auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); + auto rotating_logger = spdlog::create("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); } #include "spdlog/sinks/daily_file_sink.h" void daily_example() { // Create a daily logger - a new file is created every day on 2:30am. - auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); + auto daily_logger = spdlog::create("daily_logger", "logs/daily.txt", 2, 30); } #include "spdlog/sinks/callback_sink.h" void callback_example() { // Create the logger - auto logger = spdlog::callback_logger_mt("custom_callback_logger", [](const spdlog::details::log_msg & /*msg*/) { + auto logger = spdlog::create("custom_callback_logger", [](const spdlog::details::log_msg & /*msg*/) { // do what you need to do with msg }); } -#include "spdlog/async.h" void async_example() { - // Default thread pool settings can be modified *before* creating the async logger: - // spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread. - auto async_file = spdlog::basic_logger_mt("async_file_logger", "logs/async_log.txt"); - // alternatively: - // auto async_file = - // spdlog::create_async("async_file_logger", - // "logs/async_log.txt"); - - for (int i = 1; i < 101; ++i) { - async_file->info("Async message #{}", i); - } + // TODO + // // Default thread pool settings can be modified *before* creating the async logger: + // // spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread. + // auto async_file = spdlog::basic_logger_mt("async_file_logger", "logs/async_log.txt"); + // // alternatively: + // // auto async_file = + // // spdlog::create_async("async_file_logger", + // // "logs/async_log.txt"); + // + // for (int i = 1; i < 101; ++i) { + // async_file->info("Async message #{}", i); + // } } // Log binary data as hex. @@ -183,19 +185,19 @@ void stopwatch_example() { #include "spdlog/sinks/udp_sink.h" void udp_example() { - spdlog::sinks::udp_sink_config cfg("127.0.0.1", 11091); - auto my_logger = spdlog::udp_logger_mt("udplog", cfg); + udp_sink_config cfg("127.0.0.1", 11091); + auto my_logger = spdlog::create("udplog", cfg); my_logger->set_level(spdlog::level::debug); my_logger->info("hello world"); } // A logger with multiple sinks (stdout and file) - each with a different format and log level. void multi_sink_example() { - auto console_sink = std::make_shared(); + auto console_sink = std::make_shared(); console_sink->set_level(spdlog::level::warn); console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v"); - auto file_sink = std::make_shared("logs/multisink.txt", true); + auto file_sink = std::make_shared("logs/multisink.txt", true); file_sink->set_level(spdlog::level::trace); spdlog::logger logger("multi_sink", {console_sink, file_sink}); @@ -231,7 +233,7 @@ void err_handler_example() { #include "spdlog/sinks/syslog_sink.h" void syslog_example() { std::string ident = "spdlog-example"; - auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID); + auto syslog_logger = spdlog::create("syslog", ident, 0); syslog_logger->warn("This is warning that will end up in syslog."); } #endif @@ -291,7 +293,7 @@ void replace_global_logger_example() { // store the old logger so we don't break other examples. auto old_logger = spdlog::global_logger(); - auto new_logger = spdlog::basic_logger_mt("new_global_logger", "logs/new-default-log.txt", true); + auto new_logger = spdlog::create("new_global_logger", "logs/new-default-log.txt", true); spdlog::set_global_logger(new_logger); spdlog::set_level(spdlog::level::info); spdlog::debug("This message should not be displayed!"); diff --git a/include/spdlog/async.h b/include/spdlog/async.h deleted file mode 100644 index 8dcb5462..00000000 --- a/include/spdlog/async.h +++ /dev/null @@ -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 -#include -#include - -#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 -struct async_factory_impl { - template - static std::shared_ptr 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 tp_lock(mutex); - auto tp = context->get_tp(); - if (tp == nullptr) { - tp = std::make_shared(details::default_async_q_size, 1U); - context->set_tp(tp); - } - auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy); - return new_logger; - } -}; - -using async_factory = async_factory_impl; -using async_factory_nonblock = async_factory_impl; - -template -std::shared_ptr create_async(std::string logger_name, SinkArgs &&...sink_args) { - return async_factory::create(std::move(logger_name), std::forward(sink_args)...); -} - -template -std::shared_ptr create_async_nb(std::string logger_name, SinkArgs &&...sink_args) { - return async_factory_nonblock::create(std::move(logger_name), std::forward(sink_args)...); -} - -// set global thread pool. -inline void init_thread_pool(size_t q_size, - size_t thread_count, - std::function on_thread_start, - std::function on_thread_stop) { - auto tp = std::make_shared(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 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 thread_pool() { return spdlog::context()->get_tp(); } -} // namespace spdlog diff --git a/include/spdlog/details/synchronous_factory.h b/include/spdlog/details/synchronous_factory.h deleted file mode 100644 index 3aee14c6..00000000 --- a/include/spdlog/details/synchronous_factory.h +++ /dev/null @@ -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 - static std::shared_ptr create(std::string logger_name, SinkArgs &&...args) { - auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(std::move(logger_name), std::move(sink)); - return new_logger; - } -}; -} // namespace spdlog diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h deleted file mode 100644 index 76f9bdd3..00000000 --- a/include/spdlog/details/thread_pool.h +++ /dev/null @@ -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 -#include -#include -#include -#include - -#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; - -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(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; - - thread_pool(size_t q_max_items, - size_t threads_n, - std::function on_thread_start, - std::function on_thread_stop); - thread_pool(size_t q_max_items, size_t threads_n, std::function 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 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 diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index e2d1607e..72e716cb 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -16,7 +16,6 @@ #include "../details/fmt_helper.h" #include "../details/null_mutex.h" #include "../details/os.h" - #include "../details/synchronous_factory.h" #include "./base_sink.h" #if !defined(SPDLOG_ANDROID_RETRIES) @@ -120,19 +119,6 @@ template using android_sink_buf_st = android_sink; } // namespace sinks - -// Create and register android syslog logger - -template -inline std::shared_ptr android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") { - return Factory::template create(logger_name, tag); -} - -template -inline std::shared_ptr android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") { - return Factory::template create(logger_name, tag); -} - } // namespace spdlog #endif // __ANDROID__ diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index 9dedbfc0..04cc6883 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -43,5 +43,6 @@ protected: virtual void set_pattern_(const std::string &pattern); virtual void set_formatter_(std::unique_ptr sink_formatter); }; + } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index 1ffb0604..3377061a 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -8,7 +8,6 @@ #include "../details/file_helper.h" #include "../details/null_mutex.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" namespace spdlog { @@ -34,24 +33,4 @@ using basic_file_sink_mt = basic_file_sink; using basic_file_sink_st = basic_file_sink; } // namespace sinks - -// -// factory functions -// -template -std::shared_ptr 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(logger_name, filename, truncate, event_handlers); -} - -template -std::shared_ptr 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(logger_name, filename, truncate, event_handlers); -} - } // namespace spdlog diff --git a/include/spdlog/sinks/callback_sink.h b/include/spdlog/sinks/callback_sink.h index ad0ec8ee..856c80b0 100644 --- a/include/spdlog/sinks/callback_sink.h +++ b/include/spdlog/sinks/callback_sink.h @@ -7,7 +7,6 @@ #include #include "../details/null_mutex.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" namespace spdlog { @@ -37,18 +36,4 @@ using callback_sink_mt = callback_sink; using callback_sink_st = callback_sink; } // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback) { - return Factory::template create(logger_name, callback); -} - -template -inline std::shared_ptr callback_logger_st(const std::string &logger_name, const custom_log_callback &callback) { - return Factory::template create(logger_name, callback); -} - } // namespace spdlog diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 80145dbb..481321e3 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -15,7 +15,6 @@ #include "../details/file_helper.h" #include "../details/null_mutex.h" #include "../details/os.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" namespace spdlog { @@ -190,55 +189,4 @@ using daily_file_format_sink_mt = daily_file_sink; } // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr daily_logger_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(logger_name, filename, hour, minute, truncate, max_files, - event_handlers); -} - -template -inline std::shared_ptr 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(logger_name, filename, hour, minute, truncate, max_files, - event_handlers); -} - -template -inline std::shared_ptr daily_logger_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(logger_name, filename, hour, minute, truncate, max_files, - event_handlers); -} - -template -inline std::shared_ptr 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(logger_name, filename, hour, minute, truncate, max_files, - event_handlers); -} } // namespace spdlog diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index cff218f0..7588b3ea 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -14,7 +14,6 @@ #include "../details/file_helper.h" #include "../details/null_mutex.h" #include "../details/os.h" -#include "../details/synchronous_factory.h" #include "../fmt/fmt.h" #include "./base_sink.h" @@ -167,25 +166,4 @@ using hourly_file_sink_mt = hourly_file_sink; using hourly_file_sink_st = hourly_file_sink; } // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr hourly_logger_mt(const std::string &logger_name, - const filename_t &filename, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, truncate, max_files, event_handlers); -} - -template -inline std::shared_ptr hourly_logger_st(const std::string &logger_name, - const filename_t &filename, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, truncate, max_files, event_handlers); -} } // namespace spdlog diff --git a/include/spdlog/sinks/kafka_sink.h b/include/spdlog/sinks/kafka_sink.h index 06f0d7fd..7cd52fd1 100644 --- a/include/spdlog/sinks/kafka_sink.h +++ b/include/spdlog/sinks/kafka_sink.h @@ -12,11 +12,9 @@ #include -#include "../async.h" #include "../common.h" #include "../details/log_msg.h" #include "../details/null_mutex.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" // kafka header @@ -89,25 +87,4 @@ using kafka_sink_mt = kafka_sink; using kafka_sink_st = kafka_sink; } // namespace sinks - -template -inline std::shared_ptr kafka_logger_mt(const std::string &logger_name, spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -template -inline std::shared_ptr kafka_logger_st(const std::string &logger_name, spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -template -inline std::shared_ptr kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -template -inline std::shared_ptr kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - } // namespace spdlog diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h index 56e21343..36d8945c 100644 --- a/include/spdlog/sinks/mongo_sink.h +++ b/include/spdlog/sinks/mongo_sink.h @@ -17,9 +17,10 @@ #include #include +#include +#include "../details/null_mutex.h" #include "../common.h" #include "../details/log_msg.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" namespace spdlog { @@ -75,28 +76,8 @@ private: std::unique_ptr client_ = nullptr; }; -#include - -#include "../details/null_mutex.h" using mongo_sink_mt = mongo_sink; -using mongo_sink_st = mongo_sink; +using mongo_sink_st = mongo_sink; } // namespace sinks - -template -inline std::shared_ptr 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(logger_name, db_name, collection_name, uri); -} - -template -inline std::shared_ptr 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(logger_name, db_name, collection_name, uri); -} - } // namespace spdlog diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index e2e0dd13..6aa7d036 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -4,7 +4,6 @@ #pragma once #include - #include "../details/null_mutex.h" #include "./base_sink.h" diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index e8ea21f1..3e45cc66 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -4,7 +4,6 @@ #pragma once #include "../details/null_mutex.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" namespace spdlog { @@ -21,19 +20,4 @@ using null_sink_mt = null_sink; using null_sink_st = null_sink; } // namespace sinks - -template -std::shared_ptr null_logger_mt(const std::string &logger_name) { - auto null_logger = Factory::template create(logger_name); - null_logger->set_level(level::off); - return null_logger; -} - -template -std::shared_ptr null_logger_st(const std::string &logger_name) { - auto null_logger = Factory::template create(logger_name); - null_logger->set_level(level::off); - return null_logger; -} - } // namespace spdlog diff --git a/include/spdlog/sinks/qt_sinks.h b/include/spdlog/sinks/qt_sinks.h index ceeeb59d..303e9509 100644 --- a/include/spdlog/sinks/qt_sinks.h +++ b/include/spdlog/sinks/qt_sinks.h @@ -17,7 +17,6 @@ #include "../common.h" #include "../details/log_msg.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" // @@ -218,75 +217,10 @@ protected: std::array colors_; }; -#include - -#include "../details/null_mutex.h" - using qt_sink_mt = qt_sink; using qt_sink_st = qt_sink; using qt_color_sink_mt = qt_color_sink; using qt_color_sink_st = qt_color_sink; + } // namespace sinks - -// -// Factory functions -// - -// log to QTextEdit -template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, - QTextEdit *qt_object, - const std::string &meta_method = "append") { - return Factory::template create(logger_name, qt_object, meta_method); -} - -template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, - QTextEdit *qt_object, - const std::string &meta_method = "append") { - return Factory::template create(logger_name, qt_object, meta_method); -} - -// log to QPlainTextEdit -template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, - QPlainTextEdit *qt_object, - const std::string &meta_method = "appendPlainText") { - return Factory::template create(logger_name, qt_object, meta_method); -} - -template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, - QPlainTextEdit *qt_object, - const std::string &meta_method = "appendPlainText") { - return Factory::template create(logger_name, qt_object, meta_method); -} -// log to QObject -template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, QObject *qt_object, const std::string &meta_method) { - return Factory::template create(logger_name, qt_object, meta_method); -} - -template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::string &meta_method) { - return Factory::template create(logger_name, qt_object, meta_method); -} - -// log to QTextEdit with colorized output -template -inline std::shared_ptr qt_color_logger_mt(const std::string &logger_name, - QTextEdit *qt_text_edit, - int max_lines, - bool is_utf8 = false) { - return Factory::template create(logger_name, qt_text_edit, max_lines, false, is_utf8); -} - -template -inline std::shared_ptr qt_color_logger_st(const std::string &logger_name, - QTextEdit *qt_text_edit, - int max_lines, - bool is_utf8 = false) { - return Factory::template create(logger_name, qt_text_edit, max_lines, false, is_utf8); -} - } // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index ec441168..ca6f52cc 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -8,14 +8,12 @@ #include "../details/file_helper.h" #include "../details/null_mutex.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" +// Rotating file sink based on size + namespace spdlog { namespace sinks { -// -// Rotating file sink based on size -// template class rotating_file_sink final : public base_sink { public: @@ -56,30 +54,4 @@ using rotating_file_sink_mt = rotating_file_sink; using rotating_file_sink_st = rotating_file_sink; } // namespace sinks - -// -// factory functions -// - -template -inline std::shared_ptr rotating_logger_mt(const std::string &logger_name, - const filename_t &filename, - size_t max_file_size, - size_t max_files, - bool rotate_on_open = false, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open, - event_handlers); -} - -template -inline std::shared_ptr rotating_logger_st(const std::string &logger_name, - const filename_t &filename, - size_t max_file_size, - size_t max_files, - bool rotate_on_open = false, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open, - event_handlers); -} } // namespace spdlog diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index b6a4c5c1..cce83299 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -16,9 +16,9 @@ public: virtual void set_pattern(const std::string &pattern) = 0; virtual void set_formatter(std::unique_ptr sink_formatter) = 0; - void set_level(level level); - level log_level() const; - bool should_log(level msg_level) const; + void set_level(level level) { level_.store(level, std::memory_order_relaxed); } + level log_level() const { return level_.load(std::memory_order_relaxed);} + bool should_log(level msg_level) const {return msg_level >= level_.load(std::memory_order_relaxed);} protected: // sink log level - default is all diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h index 73d5a470..7025c4cc 100644 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -9,9 +9,6 @@ #include "./ansicolor_sink.h" #endif -#include "../async.h" -#include "../details/synchronous_factory.h" - namespace spdlog { namespace sinks { #ifdef _WIN32 @@ -25,19 +22,6 @@ using stdout_color_sink_st = ansicolor_stdout_sink_st; using stderr_color_sink_mt = ansicolor_stderr_sink_mt; using stderr_color_sink_st = ansicolor_stderr_sink_st; #endif + } // namespace sinks - -// logger factory functions -template -std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); - -template -std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); - -template -std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); - -template -std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); - } // namespace spdlog diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index aeb0b0ab..638473e0 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -4,9 +4,9 @@ #pragma once #include +#include #include "../details/null_mutex.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" #include "./sink.h" @@ -58,18 +58,4 @@ using stderr_sink_mt = stderr_sink; using stderr_sink_st = stderr_sink; } // namespace sinks - -// factory methods -template -std::shared_ptr stdout_logger_mt(const std::string &logger_name); - -template -std::shared_ptr stdout_logger_st(const std::string &logger_name); - -template -std::shared_ptr stderr_logger_mt(const std::string &logger_name); - -template -std::shared_ptr stderr_logger_st(const std::string &logger_name); - } // namespace spdlog diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 65f74bfa..e7812510 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -4,7 +4,6 @@ #pragma once #include -#include #include #include @@ -19,7 +18,7 @@ namespace sinks { template class syslog_sink final : public base_sink { public: - syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting) + syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting=false) : enable_formatting_{enable_formatting}, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, /* spdlog::level::debug */ LOG_DEBUG, @@ -79,26 +78,6 @@ private: using syslog_sink_mt = syslog_sink; using syslog_sink_st = syslog_sink; + } // namespace sinks - -// Create and register a syslog logger -template -inline std::shared_ptr syslog_logger_mt(const std::string &logger_name, - const std::string &syslog_ident = "", - int syslog_option = 0, - int syslog_facility = LOG_USER, - bool enable_formatting = false) { - return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility, - enable_formatting); -} - -template -inline std::shared_ptr syslog_logger_st(const std::string &logger_name, - const std::string &syslog_ident = "", - int syslog_option = 0, - int syslog_facility = LOG_USER, - bool enable_formatting = false) { - return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility, - enable_formatting); -} } // namespace spdlog diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index 9390d0de..0ec81e28 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -7,7 +7,6 @@ #include "../details/null_mutex.h" #include "../details/os.h" -#include "../details/synchronous_factory.h" #include "./base_sink.h" #ifndef SD_JOURNAL_SUPPRESS_LOCATION #define SD_JOURNAL_SUPPRESS_LOCATION @@ -90,20 +89,6 @@ protected: using systemd_sink_mt = systemd_sink; using systemd_sink_st = systemd_sink; + } // namespace sinks - -// Create and register a syslog logger -template -inline std::shared_ptr systemd_logger_mt(const std::string &logger_name, - const std::string &ident = "", - bool enable_formatting = false) { - return Factory::template create(logger_name, ident, enable_formatting); -} - -template -inline std::shared_ptr systemd_logger_st(const std::string &logger_name, - const std::string &ident = "", - bool enable_formatting = false) { - return Factory::template create(logger_name, ident, enable_formatting); -} } // namespace spdlog diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index b2fb251a..be7bcc3b 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -69,7 +69,7 @@ protected: }; using tcp_sink_mt = tcp_sink; -using tcp_sink_st = tcp_sink; +using tcp_sink_st = tcp_sink; } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/sinks/udp_sink.h b/include/spdlog/sinks/udp_sink.h index 282f682f..0f0951da 100644 --- a/include/spdlog/sinks/udp_sink.h +++ b/include/spdlog/sinks/udp_sink.h @@ -33,7 +33,7 @@ struct udp_sink_config { }; template -class udp_sink final : public spdlog::sinks::base_sink { +class udp_sink final : public base_sink { public: // host can be hostname or ip address explicit udp_sink(udp_sink_config sink_config) @@ -53,16 +53,7 @@ protected: }; using udp_sink_mt = udp_sink; -using udp_sink_st = udp_sink; +using udp_sink_st = udp_sink; } // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config) { - return Factory::template create(logger_name, skin_config); -} - } // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 1f6c0974..ffb34f30 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -69,5 +69,6 @@ using wincolor_stdout_sink_st = wincolor_stdout_sink; using wincolor_stderr_sink_mt = wincolor_stderr_sink; using wincolor_stderr_sink_st = wincolor_stderr_sink; + } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index eb6ba4b2..15eb10d4 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -15,13 +15,10 @@ #include "./common.h" #include "./details/context.h" -#include "./details/synchronous_factory.h" #include "./logger.h" namespace spdlog { -using default_factory = synchronous_factory; - SPDLOG_API void set_context(std::shared_ptr context); SPDLOG_API std::shared_ptr context(); SPDLOG_API const std::shared_ptr &context_ref(); @@ -31,7 +28,7 @@ SPDLOG_API const std::shared_ptr &context_ref(); // spdlog::create("logger_name", "dailylog_filename", 11, 59); template std::shared_ptr create(std::string logger_name, SinkArgs &&...sink_args) { - return default_factory::create(std::move(logger_name), std::forward(sink_args)...); + return std::make_shared(std::move(logger_name), std::make_shared(std::forward(sink_args)...)); } // Set formatter of the global logger. Each sink in each logger will get a clone of this object diff --git a/src/async_logger.cpp b/src/async_logger.cpp deleted file mode 100644 index d891bb77..00000000 --- a/src/async_logger.cpp +++ /dev/null @@ -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 -#include - -#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 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 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::async_logger::clone(std::string new_name) { - auto cloned = std::make_shared(*this); - cloned->name_ = std::move(new_name); - return cloned; -} diff --git a/src/details/thread_pool.cpp b/src/details/thread_pool.cpp deleted file mode 100644 index 584241f4..00000000 --- a/src/details/thread_pool.cpp +++ /dev/null @@ -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 - -#include "spdlog/common.h" - -namespace spdlog { -namespace details { - -thread_pool::thread_pool(size_t q_max_items, - size_t threads_n, - std::function on_thread_start, - std::function 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 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 diff --git a/src/sinks/ansicolor_sink.cpp b/src/sinks/ansicolor_sink.cpp index d0bce481..3e1fe3cc 100644 --- a/src/sinks/ansicolor_sink.cpp +++ b/src/sinks/ansicolor_sink.cpp @@ -110,12 +110,13 @@ template ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) : ansicolor_sink(stderr, mode) {} -} // namespace sinks -} // namespace spdlog // template instantiations -template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink; -template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink; +template class SPDLOG_API ansicolor_stdout_sink; +template class SPDLOG_API ansicolor_stdout_sink; -template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink; -template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink; +template class SPDLOG_API ansicolor_stderr_sink; +template class SPDLOG_API ansicolor_stderr_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/src/sinks/base_sink.cpp b/src/sinks/base_sink.cpp index c5efae8f..ab393ba4 100644 --- a/src/sinks/base_sink.cpp +++ b/src/sinks/base_sink.cpp @@ -5,53 +5,58 @@ #include #include - #include "spdlog/common.h" #include "spdlog/details/null_mutex.h" #include "spdlog/pattern_formatter.h" +namespace spdlog { +namespace sinks { + template -spdlog::sinks::base_sink::base_sink() +base_sink::base_sink() : formatter_{std::make_unique()} {} template -spdlog::sinks::base_sink::base_sink(std::unique_ptr formatter) +base_sink::base_sink(std::unique_ptr formatter) : formatter_{std::move(formatter)} {} template -void spdlog::sinks::base_sink::log(const details::log_msg &msg) { +void base_sink::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); sink_it_(msg); } template -void spdlog::sinks::base_sink::flush() { +void base_sink::flush() { std::lock_guard lock(mutex_); flush_(); } template -void spdlog::sinks::base_sink::set_pattern(const std::string &pattern) { +void base_sink::set_pattern(const std::string &pattern) { std::lock_guard lock(mutex_); set_pattern_(pattern); } template -void spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) { +void base_sink::set_formatter(std::unique_ptr sink_formatter) { std::lock_guard lock(mutex_); set_formatter_(std::move(sink_formatter)); } template -void spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) { - set_formatter_(std::make_unique(pattern)); +void base_sink::set_pattern_(const std::string &pattern) { + set_formatter_(std::make_unique(pattern)); } template -void spdlog::sinks::base_sink::set_formatter_(std::unique_ptr sink_formatter) { +void base_sink::set_formatter_(std::unique_ptr sink_formatter) { formatter_ = std::move(sink_formatter); } // template instantiations -template class SPDLOG_API spdlog::sinks::base_sink; -template class SPDLOG_API spdlog::sinks::base_sink; +template class SPDLOG_API base_sink; +template class SPDLOG_API base_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/src/sinks/basic_file_sink.cpp b/src/sinks/basic_file_sink.cpp index b05dae31..f0d0203b 100644 --- a/src/sinks/basic_file_sink.cpp +++ b/src/sinks/basic_file_sink.cpp @@ -2,8 +2,9 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) #include "spdlog/sinks/basic_file_sink.h" - #include "spdlog/common.h" +#include "spdlog/details/null_mutex.h" +#include namespace spdlog { namespace sinks { @@ -31,9 +32,9 @@ void basic_file_sink::flush_() { file_helper_.flush(); } +// template instantiations +template class SPDLOG_API basic_file_sink; +template class SPDLOG_API basic_file_sink; + } // namespace sinks } // namespace spdlog - -// template instantiations -template class SPDLOG_API spdlog::sinks::basic_file_sink; -template class SPDLOG_API spdlog::sinks::basic_file_sink; \ No newline at end of file diff --git a/src/sinks/rotating_file_sink.cpp b/src/sinks/rotating_file_sink.cpp index 776dbf0f..0d7c1b67 100644 --- a/src/sinks/rotating_file_sink.cpp +++ b/src/sinks/rotating_file_sink.cpp @@ -12,6 +12,7 @@ #include "spdlog/common.h" #include "spdlog/details/file_helper.h" #include "spdlog/details/os.h" +#include "spdlog/details/null_mutex.h" namespace spdlog { namespace sinks { @@ -136,9 +137,9 @@ bool rotating_file_sink::rename_file_(const filename_t &src_filename, con return details::os::rename(src_filename, target_filename); } +// template instantiations +template class SPDLOG_API rotating_file_sink; +template class SPDLOG_API rotating_file_sink; + } // namespace sinks } // namespace spdlog - -// template instantiations -template class SPDLOG_API spdlog::sinks::rotating_file_sink; -template class SPDLOG_API spdlog::sinks::rotating_file_sink; diff --git a/src/sinks/sink.cpp b/src/sinks/sink.cpp deleted file mode 100644 index c0d8509e..00000000 --- a/src/sinks/sink.cpp +++ /dev/null @@ -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); } diff --git a/src/sinks/stdout_color_sinks.cpp b/src/sinks/stdout_color_sinks.cpp deleted file mode 100644 index c01b326c..00000000 --- a/src/sinks/stdout_color_sinks.cpp +++ /dev/null @@ -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 -std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); -} - -template -std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); -} - -template -std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); -} - -template -std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); -} -} // namespace spdlog - -// template instantiations -template SPDLOG_API std::shared_ptr spdlog::stdout_color_mt( - const std::string &logger_name, color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stdout_color_st( - const std::string &logger_name, color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stderr_color_mt( - const std::string &logger_name, color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stderr_color_st( - const std::string &logger_name, color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stdout_color_mt(const std::string &logger_name, - color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stdout_color_st(const std::string &logger_name, - color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stderr_color_mt(const std::string &logger_name, - color_mode mode); - -template SPDLOG_API std::shared_ptr spdlog::stderr_color_st(const std::string &logger_name, - color_mode mode); diff --git a/src/sinks/stdout_sinks.cpp b/src/sinks/stdout_sinks.cpp index ad081d8f..4eb8d083 100644 --- a/src/sinks/stdout_sinks.cpp +++ b/src/sinks/stdout_sinks.cpp @@ -81,56 +81,11 @@ template stderr_sink::stderr_sink() : stdout_sink_base(stderr) {} +// template installations +template class SPDLOG_API stdout_sink; +template class SPDLOG_API stdout_sink; +template class SPDLOG_API stderr_sink; +template class SPDLOG_API stderr_sink; + } // namespace sinks - -// factory methods -template -std::shared_ptr stdout_logger_mt(const std::string &logger_name) { - return Factory::template create(logger_name); -} - -template -std::shared_ptr stdout_logger_st(const std::string &logger_name) { - return Factory::template create(logger_name); -} - -template -std::shared_ptr stderr_logger_mt(const std::string &logger_name) { - return Factory::template create(logger_name); -} - -template -std::shared_ptr stderr_logger_st(const std::string &logger_name) { - return Factory::template create(logger_name); -} } // namespace spdlog - -// template instantiations for stdout/stderr loggers -template class SPDLOG_API spdlog::sinks::stdout_sink_base; -template class SPDLOG_API spdlog::sinks::stdout_sink_base; -template class SPDLOG_API spdlog::sinks::stdout_sink; -template class SPDLOG_API spdlog::sinks::stdout_sink; -template class SPDLOG_API spdlog::sinks::stderr_sink; -template class SPDLOG_API spdlog::sinks::stderr_sink; - -// template instantiations for stdout/stderr factory functions -#include "spdlog/async.h" -#include "spdlog/details/synchronous_factory.h" - -template SPDLOG_API std::shared_ptr spdlog::stdout_logger_mt( - const std::string &logger_name); -template SPDLOG_API std::shared_ptr spdlog::stdout_logger_st( - const std::string &logger_name); -template SPDLOG_API std::shared_ptr spdlog::stderr_logger_mt( - const std::string &logger_name); -template SPDLOG_API std::shared_ptr spdlog::stderr_logger_st( - const std::string &logger_name); - -template SPDLOG_API std::shared_ptr spdlog::stdout_logger_mt( - const std::string &logger_name); -template SPDLOG_API std::shared_ptr spdlog::stdout_logger_st( - const std::string &logger_name); -template SPDLOG_API std::shared_ptr spdlog::stderr_logger_mt( - const std::string &logger_name); -template SPDLOG_API std::shared_ptr spdlog::stderr_logger_st( - const std::string &logger_name); diff --git a/src/sinks/wincolor_sink.cpp b/src/sinks/wincolor_sink.cpp index f71dd130..a255909e 100644 --- a/src/sinks/wincolor_sink.cpp +++ b/src/sinks/wincolor_sink.cpp @@ -4,11 +4,9 @@ // clang-format off #include "spdlog/details/windows_include.h" #include -#include // clang-format on - +#include #include "spdlog/sinks/wincolor_sink.h" - #include "spdlog/common.h" #include "spdlog/details/null_mutex.h" @@ -123,24 +121,9 @@ void wincolor_sink::write_to_file_(const memory_buf_t &formatted) { (void)(ignored); } -// wincolor_stdout_sink -template -wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} +// template instantiations +template class SPDLOG_API wincolor_sink; +template class SPDLOG_API wincolor_sink; -// wincolor_stderr_sink -template -wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} } // namespace sinks } // namespace spdlog - -// template instantiations -template class SPDLOG_API spdlog::sinks::wincolor_sink; -template class SPDLOG_API spdlog::sinks::wincolor_sink; - -template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink; -template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink; - -template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink; -template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink; diff --git a/tests/includes.h b/tests/includes.h index f45174b5..e2fb6aad 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -25,7 +25,6 @@ #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG -#include "spdlog/async.h" #include "spdlog/details/fmt_helper.h" #include "spdlog/pattern_formatter.h" #include "spdlog/sinks/null_sink.h" diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 8c8a7e66..067eee0d 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -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(); - size_t overrun_counter = 0; - 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, 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(); - test_sink->set_delay(std::chrono::milliseconds(1)); - size_t queue_size = 4; - size_t messages = 1024; - - auto tp = std::make_shared(queue_size, 1); - auto logger = std::make_shared("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(); - test_sink->set_delay(std::chrono::milliseconds(1)); - size_t queue_size = 4; - size_t messages = 1024; - - auto tp = std::make_shared(queue_size, 1); - auto logger = std::make_shared("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("as2"); - auto test_sink = std::static_pointer_cast(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(); - 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, 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(); - test_sink->set_delay(std::chrono::milliseconds(5)); - size_t messages = 100; - - auto tp = std::make_shared(messages, 2); - auto logger = std::make_shared("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(); - 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, spdlog::async_overflow_policy::block); - - 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); - } - }); - 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(filename, true); - auto tp = std::make_shared(messages, tp_threads); - auto logger = std::make_shared("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(filename, true); - auto tp = std::make_shared(messages, tp_threads); - auto logger = std::make_shared("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(); - std::shared_ptr const empty_tp; - auto logger = std::make_shared("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(); +// size_t overrun_counter = 0; +// 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, 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(); +// test_sink->set_delay(std::chrono::milliseconds(1)); +// size_t queue_size = 4; +// size_t messages = 1024; +// +// auto tp = std::make_shared(queue_size, 1); +// auto logger = std::make_shared("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(); +// test_sink->set_delay(std::chrono::milliseconds(1)); +// size_t queue_size = 4; +// size_t messages = 1024; +// +// auto tp = std::make_shared(queue_size, 1); +// auto logger = std::make_shared("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("as2"); +// auto test_sink = std::static_pointer_cast(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(); +// 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, 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(); +// test_sink->set_delay(std::chrono::milliseconds(5)); +// size_t messages = 100; +// +// auto tp = std::make_shared(messages, 2); +// auto logger = std::make_shared("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(); +// 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, spdlog::async_overflow_policy::block); +// +// 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); +// } +// }); +// 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(filename, true); +// auto tp = std::make_shared(messages, tp_threads); +// auto logger = std::make_shared("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(filename, true); +// auto tp = std::make_shared(messages, tp_threads); +// auto logger = std::make_shared("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(); +// std::shared_ptr const empty_tp; +// auto logger = std::make_shared("as", test_sink, empty_tp); +// logger->info("Please throw an exception"); +// REQUIRE(test_sink->msg_counter() == 0); +//} diff --git a/tests/test_custom_callbacks.cpp b/tests/test_custom_callbacks.cpp index 5f5a9d07..cfae2119 100644 --- a/tests/test_custom_callbacks.cpp +++ b/tests/test_custom_callbacks.cpp @@ -3,7 +3,6 @@ * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE */ #include "includes.h" -#include "spdlog/async.h" #include "spdlog/common.h" #include "spdlog/sinks/callback_sink.h" #include "test_sink.h" diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index e3914e5d..9a04d4a3 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -18,10 +18,11 @@ protected: }; struct custom_ex {}; +using namespace spdlog::sinks; TEST_CASE("default_error_handler", "[errors]") { prepare_logdir(); spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); - auto logger = spdlog::basic_logger_mt("test-error", filename); + auto logger = spdlog::create("test-error", filename); logger->set_pattern("%v"); logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1); logger->info("Test message {}", 2); @@ -34,7 +35,7 @@ TEST_CASE("default_error_handler", "[errors]") { TEST_CASE("custom_error_handler", "[errors]") { prepare_logdir(); spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); - auto logger = spdlog::basic_logger_mt("test-error", filename); + auto logger = spdlog::create("test-error", filename); logger->flush_on(spdlog::level::info); logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); logger->info("Good message #1"); @@ -54,47 +55,3 @@ TEST_CASE("flush_error_handler", "[errors]") { logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); REQUIRE_THROWS_AS(logger->flush(), custom_ex); } - -TEST_CASE("async_error_handler", "[errors]") { - prepare_logdir(); - std::string err_msg("log failed with some msg"); - - spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_ASYNC_LOG); - { - spdlog::init_thread_pool(128, 1); - auto logger = spdlog::create_async("logger", filename, true); - logger->set_error_handler([=](const std::string &) { - std::ofstream ofs("test_logs/custom_err.txt"); - if (!ofs) { - throw std::runtime_error("Failed open test_logs/custom_err.txt"); - } - ofs << err_msg; - }); - logger->info("Good message #1"); - logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"); - logger->info("Good message #2"); - } - spdlog::init_thread_pool(128, 1); - require_message_count(SIMPLE_ASYNC_LOG, 2); - REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg); -} - -// Make sure async error handler is executed -TEST_CASE("async_error_handler2", "[errors]") { - prepare_logdir(); - std::string err_msg("This is async handler error message"); - { - spdlog::details::os::create_dir(SPDLOG_FILENAME_T("test_logs")); - spdlog::init_thread_pool(128, 1); - auto logger = spdlog::create_async("failed_logger"); - logger->set_error_handler([=](const std::string &) { - std::ofstream ofs("test_logs/custom_err2.txt"); - if (!ofs) throw std::runtime_error("Failed open test_logs/custom_err2.txt"); - ofs << err_msg; - }); - logger->info("Hello failure"); - } - - spdlog::init_thread_pool(128, 1); - REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg); -} diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 3ef4a3b4..56c7650e 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -9,10 +9,12 @@ #define SIMPLE_LOG "test_logs/simple_log" #define ROTATING_LOG "test_logs/rotating_log" +using namespace spdlog::sinks; + TEST_CASE("simple_file_logger", "[simple_logger]") { prepare_logdir(); spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); - auto logger = spdlog::basic_logger_mt("logger", filename); + auto logger = spdlog::create("test-error", filename); logger->set_pattern("%v"); logger->info("Test message {}", 1); logger->info("Test message {}", 2); @@ -25,7 +27,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]") { TEST_CASE("flush_on", "[flush_on]") { prepare_logdir(); spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); - auto logger = spdlog::basic_logger_mt("test-error", filename); + auto logger = spdlog::create("test-error", filename); logger->set_pattern("%v"); logger->set_level(spdlog::level::trace); logger->flush_on(spdlog::level::info); @@ -43,8 +45,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]") { prepare_logdir(); size_t max_size = 1024 * 10; spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); - auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0); - + auto logger = spdlog::create("logger", basename, max_size, 0); for (int i = 0; i < 10; ++i) { logger->info("Test message {}", i); } @@ -57,16 +58,14 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]") { prepare_logdir(); size_t max_size = 1024 * 10; spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); - { // make an initial logger to create the first output file - auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true); + auto logger = spdlog::create("logger", basename, max_size, 2, true); for (int i = 0; i < 10; ++i) { logger->info("Test message {}", i); } } - - auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true); + auto logger = spdlog::create("logger", basename, max_size, 2, true); for (int i = 0; i < 10; ++i) { logger->info("Test message {}", i); } @@ -89,7 +88,7 @@ TEST_CASE("rotating_file_logger3", "[rotating_logger]") { prepare_logdir(); size_t max_size = 0; spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); - REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0), spdlog::spdlog_ex); + REQUIRE_THROWS_AS(spdlog::create("logger", basename, max_size, 0), spdlog::spdlog_ex); } // test on-demand rotation of logs @@ -99,15 +98,11 @@ TEST_CASE("rotating_file_logger4", "[rotating_logger]") { spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); auto sink = std::make_shared(basename, max_size, 2); auto logger = std::make_shared("rotating_sink_logger", sink); - logger->info("Test message - pre-rotation"); logger->flush(); - sink->rotate_now(); - logger->info("Test message - post-rotation"); logger->flush(); - REQUIRE(get_filesize(ROTATING_LOG) > 0); REQUIRE(get_filesize(ROTATING_LOG ".1") > 0); } diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index cffa998a..47a0d41a 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -15,7 +15,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]") { prepare_logdir(); spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME); - auto logger = spdlog::basic_logger_mt("logger", filename); + auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); logger->set_level(spdlog::level::trace); @@ -44,7 +44,7 @@ TEST_CASE("disable param evaluation", "[macros]") { } TEST_CASE("pass logger pointer", "[macros]") { - auto logger = spdlog::null_logger_mt("refmacro"); + auto logger = spdlog::create("refmacro"); auto &ref = *logger; SPDLOG_LOGGER_TRACE(&ref, "Test message 1"); SPDLOG_LOGGER_DEBUG(&ref, "Test message 2"); diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index f172536b..fc729f76 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -93,26 +93,27 @@ TEST_CASE("clone-logger", "[clone]") { } TEST_CASE("clone async", "[clone]") { - using spdlog::sinks::test_sink_mt; - spdlog::init_thread_pool(4, 1); - auto test_sink = std::make_shared(); - auto logger = std::make_shared("orig", test_sink, spdlog::thread_pool()); - logger->set_pattern("%v"); - auto cloned = logger->clone("clone"); - - REQUIRE(cloned->name() == "clone"); - REQUIRE(logger->sinks() == cloned->sinks()); - REQUIRE(logger->log_level() == cloned->log_level()); - REQUIRE(logger->flush_level() == cloned->flush_level()); - - logger->info("Some message 1"); - cloned->info("Some message 2"); - - spdlog::details::os::sleep_for_millis(100); - - REQUIRE(test_sink->lines().size() == 2); - REQUIRE(test_sink->lines()[0] == "Some message 1"); - REQUIRE(test_sink->lines()[1] == "Some message 2"); + // TODO: fix this test + // using spdlog::sinks::test_sink_mt; + // spdlog::init_thread_pool(4, 1); + // auto test_sink = std::make_shared(); + // auto logger = std::make_shared("orig", test_sink, spdlog::thread_pool()); + // logger->set_pattern("%v"); + // auto cloned = logger->clone("clone"); + // + // REQUIRE(cloned->name() == "clone"); + // REQUIRE(logger->sinks() == cloned->sinks()); + // REQUIRE(logger->log_level() == cloned->log_level()); + // REQUIRE(logger->flush_level() == cloned->flush_level()); + // + // logger->info("Some message 1"); + // cloned->info("Some message 2"); + // + // spdlog::details::os::sleep_for_millis(100); + // + // REQUIRE(test_sink->lines().size() == 2); + // REQUIRE(test_sink->lines()[0] == "Some message 1"); + // REQUIRE(test_sink->lines()[1] == "Some message 2"); } TEST_CASE("global logger API", "[global logger]") { diff --git a/tests/test_mpmc_q.cpp b/tests/test_mpmc_q.cpp index bc7a37d9..022c1233 100644 --- a/tests/test_mpmc_q.cpp +++ b/tests/test_mpmc_q.cpp @@ -1,4 +1,5 @@ #include "includes.h" +#include "spdlog/details/mpmc_blocking_q.h" using std::chrono::milliseconds; using test_clock = std::chrono::high_resolution_clock; diff --git a/tests/test_stdout_api.cpp b/tests/test_stdout_api.cpp index 4db6f218..958352ed 100644 --- a/tests/test_stdout_api.cpp +++ b/tests/test_stdout_api.cpp @@ -8,7 +8,7 @@ TEST_CASE("stdout_st", "[stdout]") { spdlog::set_pattern("%+"); - auto l = spdlog::stdout_logger_st("test"); + auto l = spdlog::create("test"); l->set_level(spdlog::level::trace); l->trace("Test stdout_st"); l->debug("Test stdout_st"); @@ -19,7 +19,7 @@ TEST_CASE("stdout_st", "[stdout]") { } TEST_CASE("stderr_st", "[stderr]") { - auto l = spdlog::stderr_logger_st("test"); + auto l = spdlog::create("test"); l->set_level(spdlog::level::trace); l->trace("Test stderr_st"); l->debug("Test stderr_st"); @@ -43,7 +43,7 @@ TEST_CASE("stderr_mt", "[stderr]") { // color loggers TEST_CASE("stdout_color_st", "[stdout]") { - auto l = spdlog::stdout_color_st("test"); + auto l = spdlog::create("test"); l->set_pattern("%+"); l->set_level(spdlog::level::trace); l->trace("Test stdout_color_st"); @@ -55,7 +55,7 @@ TEST_CASE("stdout_color_st", "[stdout]") { } TEST_CASE("stdout_color_mt", "[stdout]") { - auto l = spdlog::stdout_color_mt("test"); + auto l = spdlog::create("test"); l->set_pattern("%+"); l->set_level(spdlog::level::trace); l->trace("Test stdout_color_mt"); @@ -67,14 +67,14 @@ TEST_CASE("stdout_color_mt", "[stdout]") { } TEST_CASE("stderr_color_st", "[stderr]") { - auto l = spdlog::stderr_color_st("test"); + auto l = spdlog::create("test"); l->set_pattern("%+"); l->set_level(spdlog::level::debug); l->debug("Test stderr_color_st"); } TEST_CASE("stderr_color_mt", "[stderr]") { - auto l = spdlog::stderr_color_mt("test"); + auto l = spdlog::create("test"); l->set_pattern("%+"); l->info("Test stderr_color_mt"); l->warn("Test stderr_color_mt"); diff --git a/tests/test_time_point.cpp b/tests/test_time_point.cpp index b5e15a4c..8b5791af 100644 --- a/tests/test_time_point.cpp +++ b/tests/test_time_point.cpp @@ -1,5 +1,4 @@ #include "includes.h" -#include "spdlog/async.h" #include "test_sink.h" TEST_CASE("time_point1", "[time_point log_msg]") {