From cd4dcbab36ad47fcfe6a58424c127ae1ccf14458 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 7 Jul 2018 12:12:45 +0300 Subject: [PATCH] Renamed simple_file_sink -> basic_file_sink --- bench/bench.cpp | 4 +-- bench/spdlog-async.cpp | 4 +-- example/example.cpp | 14 +++++++--- example/multisink.cpp | 4 +-- include/spdlog/async.h | 6 ++-- include/spdlog/sinks/base_sink.h | 14 ++-------- .../{simple_file_sink.h => basic_file_sink.h} | 12 ++++---- include/spdlog/sinks/sink.h | 28 +++++++++---------- include/spdlog/spdlog.h | 4 +-- tests/errors.cpp | 8 +++--- tests/file_log.cpp | 4 +-- tests/includes.h | 2 +- tests/test_async.cpp | 6 ++-- tests/test_macros.cpp | 5 ++-- 14 files changed, 54 insertions(+), 61 deletions(-) rename include/spdlog/sinks/{simple_file_sink.h => basic_file_sink.h} (72%) diff --git a/bench/bench.cpp b/bench/bench.cpp index 52e2ffaf..b1275f20 100644 --- a/bench/bench.cpp +++ b/bench/bench.cpp @@ -10,7 +10,7 @@ #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/rotating_file_sink.h" -#include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/spdlog.h" #include "utils.h" #include @@ -84,7 +84,7 @@ int main(int argc, char *argv[]) for (int i = 0; i < 3; ++i) { spdlog::init_thread_pool(queue_size, 1); - auto as = spdlog::basic_logger_mt("as", "logs/basic_async.log", true); + auto as = spdlog::basic_logger_mt("as", "logs/basic_async.log", true); bench_mt(howmany, as, threads); spdlog::drop("as"); } diff --git a/bench/spdlog-async.cpp b/bench/spdlog-async.cpp index 9f2f75b9..fc4a61fe 100644 --- a/bench/spdlog-async.cpp +++ b/bench/spdlog-async.cpp @@ -11,7 +11,7 @@ #include #include "spdlog/async.h" -#include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/sinks/basic_file_sink.h" using namespace std; @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) int howmany = 1000000; spdlog::init_thread_pool(howmany, 1); - auto logger = spdlog::create_async_logger("file_logger", "logs/spdlog-bench-async.log", false); + 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"); std::cout << "To stop, press " << std::endl; diff --git a/example/example.cpp b/example/example.cpp index e491518a..765dc121 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -12,7 +12,7 @@ #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h" -#include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" #include @@ -29,6 +29,7 @@ int main(int, char *[]) try { + spd::set_pattern("TEST PATTERN %v"); auto console = spdlog::stdout_color_st("console"); console->info("Welcome to spdlog!"); @@ -107,10 +108,15 @@ 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"); - - // thread pool settings can be modified *before* creating the async logger: + // 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. // spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads. + + auto async_file = spd::basic_logger_mt("async_file_logger", "logs/async_log.txt"); + + // alternatively: + // auto async_file = spd::create_async("async_file_logger", "logs/async_log.txt"); + for (int i = 0; i < 100; ++i) { async_file->info("Async message #{}", i + 1); diff --git a/example/multisink.cpp b/example/multisink.cpp index 98b86ce2..4b3f2d9c 100644 --- a/example/multisink.cpp +++ b/example/multisink.cpp @@ -15,8 +15,8 @@ int main(int, char *[]) // Each sink can have it's own log level and a message will be logged. std::vector sinks; sinks.push_back(std::make_shared()); - sinks.push_back(std::make_shared("./log_regular_file.txt")); - sinks.push_back(std::make_shared("./log_debug_file.txt")); + sinks.push_back(std::make_shared("./log_regular_file.txt")); + sinks.push_back(std::make_shared("./log_debug_file.txt")); spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end()); console_multisink.set_level(spdlog::level::warn); diff --git a/include/spdlog/async.h b/include/spdlog/async.h index a0ede2ad..2e13f3b3 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -23,7 +23,7 @@ namespace spdlog { // 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. -struct create_async +struct async_factory { template static std::shared_ptr create(const std::string &logger_name, SinkArgs &&... args) @@ -46,9 +46,9 @@ struct create_async }; template -inline std::shared_ptr create_async_logger(const std::string &logger_name, SinkArgs &&... sink_args) +inline std::shared_ptr create_async(const std::string &logger_name, SinkArgs &&... sink_args) { - return create_async::create(logger_name, std::forward(sink_args)...); + return async_factory::create(logger_name, std::forward(sink_args)...); } // set global thread pool. diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index 871f2b81..292a3261 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -6,8 +6,8 @@ #pragma once // // base sink templated over a mutex (either dummy or real) -// concrete implementation should only override the sink_it_ method. -// all locking is taken care of here so no locking needed by the implementers.. +// concrete implementation should override the sink_it_() and flush_() methods. +// locking is taken care of in this class - no locking needed by the implementers.. // #include "spdlog/common.h" @@ -26,16 +26,6 @@ public: { } - base_sink(const std::string &formatter_pattern) - : sink(formatter_pattern) - { - } - - base_sink(std::unique_ptr sink_formatter) - : sink(std::move(sink_formatter)) - { - } - base_sink(const base_sink &) = delete; base_sink &operator=(const base_sink &) = delete; diff --git a/include/spdlog/sinks/simple_file_sink.h b/include/spdlog/sinks/basic_file_sink.h similarity index 72% rename from include/spdlog/sinks/simple_file_sink.h rename to include/spdlog/sinks/basic_file_sink.h index f9e68b95..59795401 100644 --- a/include/spdlog/sinks/simple_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -18,10 +18,10 @@ namespace sinks { * Trivial file sink with single file as target */ template -class simple_file_sink SPDLOG_FINAL : public base_sink +class basic_file_sink SPDLOG_FINAL : public base_sink { public: - explicit simple_file_sink(const filename_t &filename, bool truncate = false) + explicit basic_file_sink(const filename_t &filename, bool truncate = false) { file_helper_.open(filename, truncate); } @@ -43,8 +43,8 @@ private: details::file_helper file_helper_; }; -using simple_file_sink_mt = simple_file_sink; -using simple_file_sink_st = simple_file_sink; +using basic_file_sink_mt = basic_file_sink; +using basic_file_sink_st = basic_file_sink; } // namespace sinks @@ -54,13 +54,13 @@ using simple_file_sink_st = simple_file_sink; template inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) { - return Factory::template create(logger_name, filename, truncate); + return Factory::template create(logger_name, filename, truncate); } template inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) { - return Factory::template create(logger_name, filename, truncate); + return Factory::template create(logger_name, filename, truncate); } } // namespace spdlog diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 513e0661..9f661908 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -18,17 +18,17 @@ public: : formatter_(std::unique_ptr(new pattern_formatter("%+"))) { } - - explicit sink(const std::string &formatter_pattern) - : formatter_(std::unique_ptr(new pattern_formatter(formatter_pattern))) - { - } - - // sink with custom formatter - explicit sink(std::unique_ptr sink_formatter) - : formatter_(std::move(sink_formatter)) - { - } +// +// explicit sink(const std::string &formatter_pattern) +// : formatter_(std::unique_ptr(new pattern_formatter(formatter_pattern))) +// { +// } +// +// // sink with custom formatter +// explicit sink(std::unique_ptr sink_formatter) +// : formatter_(std::move(sink_formatter)) +// { +// } virtual ~sink() = default; @@ -39,10 +39,12 @@ public: { return msg_level >= level_.load(std::memory_order_relaxed); } + void set_level(level::level_enum log_level) { level_.store(log_level); } + level::level_enum level() const { return static_cast(level_.load(std::memory_order_relaxed)); @@ -58,10 +60,6 @@ public: formatter_ = std::move(sink_formatter); } - spdlog::formatter *formatter() - { - return formatter_.get(); - } protected: level_t level_{level::trace}; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index b51578fa..53c56a30 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -19,7 +19,7 @@ namespace spdlog { // Default logger factory- creates synchronous loggers -struct create_synchronous +struct synchronous_factory { template @@ -32,7 +32,7 @@ struct create_synchronous } }; -using default_factory = create_synchronous; +using default_factory = synchronous_factory; // Create and register a logger with a templated sink type // The logger's level, formatter and flush level will be set according the global settings. diff --git a/tests/errors.cpp b/tests/errors.cpp index c3ccbe2f..1320b748 100644 --- a/tests/errors.cpp +++ b/tests/errors.cpp @@ -24,7 +24,7 @@ TEST_CASE("default_error_handler", "[errors]]") prepare_logdir(); std::string filename = "logs/simple_log.txt"; - auto logger = spdlog::create("test-error", filename, true); + auto logger = spdlog::create("test-error", filename, true); logger->set_pattern("%v"); logger->info("Test message {} {}", 1); logger->info("Test message {}", 2); @@ -41,7 +41,7 @@ TEST_CASE("custom_error_handler", "[errors]]") { prepare_logdir(); std::string filename = "logs/simple_log.txt"; - auto logger = spdlog::create("logger", filename, true); + auto logger = spdlog::create("logger", filename, true); logger->flush_on(spdlog::level::info); logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); logger->info("Good message #1"); @@ -75,7 +75,7 @@ TEST_CASE("async_error_handler", "[errors]]") std::string filename = "logs/simple_async_log.txt"; { spdlog::init_thread_pool(128, 1); - auto logger = spdlog::create_async_logger("logger", filename, true); + auto logger = spdlog::create_async("logger", filename, true); logger->set_error_handler([=](const std::string &) { std::ofstream ofs("logs/custom_err.txt"); if (!ofs) @@ -99,7 +99,7 @@ TEST_CASE("async_error_handler2", "[errors]]") std::string err_msg("This is async handler error message"); { spdlog::init_thread_pool(128, 1); - auto logger = spdlog::create_async_logger("failed_logger"); + auto logger = spdlog::create_async("failed_logger"); logger->set_error_handler([=](const std::string &) { std::ofstream ofs("logs/custom_err2.txt"); if (!ofs) diff --git a/tests/file_log.cpp b/tests/file_log.cpp index 9cef256d..55240b78 100644 --- a/tests/file_log.cpp +++ b/tests/file_log.cpp @@ -8,7 +8,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") prepare_logdir(); std::string filename = "logs/simple_log"; - auto logger = spdlog::create("logger", filename); + auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); logger->info("Test message {}", 1); @@ -24,7 +24,7 @@ TEST_CASE("flush_on", "[flush_on]]") prepare_logdir(); std::string filename = "logs/simple_log"; - auto logger = spdlog::create("logger", filename); + auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); logger->set_level(spdlog::level::trace); logger->flush_on(spdlog::level::info); diff --git a/tests/includes.h b/tests/includes.h index 10bfb062..6a61bc06 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -17,6 +17,6 @@ #include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/ostream_sink.h" #include "spdlog/sinks/rotating_file_sink.h" -#include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/spdlog.h" diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 7ade2676..fd53504a 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -1,6 +1,6 @@ #include "includes.h" #include "spdlog/async.h" -#include "spdlog/sinks/simple_file_sink.h" +#include "spdlog/sinks/basic_file_sink.h" #include "test_sink.h" TEST_CASE("basic async test ", "[async]") @@ -121,7 +121,7 @@ TEST_CASE("to_file", "[async]") size_t tp_threads = 1; std::string filename = "logs/async_test.log"; { - auto file_sink = std::make_shared(filename, true); + 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)); @@ -143,7 +143,7 @@ TEST_CASE("to_file multi-workers", "[async]") size_t tp_threads = 10; std::string filename = "logs/async_test.log"; { - auto file_sink = std::make_shared(filename, true); + 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)); diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 2d165aae..0b3e0b7d 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -9,12 +9,11 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") prepare_logdir(); std::string filename = "logs/simple_log"; - auto logger = spdlog::create("logger", filename); + auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); logger->set_level(spdlog::level::trace); SPDLOG_TRACE(logger, "Test message 1"); - // SPDLOG_DEBUG(logger, "Test message 2"); SPDLOG_DEBUG(logger, "Test message 2"); logger->flush(); @@ -27,7 +26,7 @@ TEST_CASE("debug and trace with format strings", "[macros]]") prepare_logdir(); std::string filename = "logs/simple_log"; - auto logger = spdlog::create("logger", filename); + auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); logger->set_level(spdlog::level::trace);