Renamed simple_file_sink -> basic_file_sink

This commit is contained in:
gabime 2018-07-07 12:12:45 +03:00
parent 69c11ea7d2
commit cd4dcbab36
14 changed files with 54 additions and 61 deletions

View File

@ -10,7 +10,7 @@
#include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/rotating_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/spdlog.h" #include "spdlog/spdlog.h"
#include "utils.h" #include "utils.h"
#include <atomic> #include <atomic>
@ -84,7 +84,7 @@ int main(int argc, char *argv[])
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
{ {
spdlog::init_thread_pool(queue_size, 1); spdlog::init_thread_pool(queue_size, 1);
auto as = spdlog::basic_logger_mt<spdlog::create_async>("as", "logs/basic_async.log", true); auto as = spdlog::basic_logger_mt<spdlog::async_factory>("as", "logs/basic_async.log", true);
bench_mt(howmany, as, threads); bench_mt(howmany, as, threads);
spdlog::drop("as"); spdlog::drop("as");
} }

View File

@ -11,7 +11,7 @@
#include <vector> #include <vector>
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/sinks/simple_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
using namespace std; using namespace std;
@ -27,7 +27,7 @@ int main(int argc, char *argv[])
int howmany = 1000000; int howmany = 1000000;
spdlog::init_thread_pool(howmany, 1); spdlog::init_thread_pool(howmany, 1);
auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("file_logger", "logs/spdlog-bench-async.log", false); auto logger = spdlog::create_async_logger<spdlog::sinks::basic_file_sink_mt>("file_logger", "logs/spdlog-bench-async.log", false);
logger->set_pattern("[%Y-%m-%d %T.%F]: %L %t %v"); logger->set_pattern("[%Y-%m-%d %T.%F]: %L %t %v");
std::cout << "To stop, press <Enter>" << std::endl; std::cout << "To stop, press <Enter>" << std::endl;

View File

@ -12,7 +12,7 @@
#include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_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 "spdlog/sinks/stdout_color_sinks.h"
#include <iostream> #include <iostream>
@ -29,6 +29,7 @@ int main(int, char *[])
try try
{ {
spd::set_pattern("TEST PATTERN %v");
auto console = spdlog::stdout_color_st("console"); auto console = spdlog::stdout_color_st("console");
console->info("Welcome to spdlog!"); console->info("Welcome to spdlog!");
@ -107,10 +108,15 @@ int main(int, char *[])
#include "spdlog/async.h" #include "spdlog/async.h"
void async_example() void async_example()
{ {
auto async_file = spd::basic_logger_mt<spdlog::create_async>("async_file_logger", "logs/async_log.txt"); // 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.
// thread pool settings can be modified *before* creating the async logger:
// spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads. // spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads.
auto async_file = spd::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
// alternatively:
// auto async_file = spd::create_async<spd::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");
for (int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
{ {
async_file->info("Async message #{}", i + 1); async_file->info("Async message #{}", i + 1);

View File

@ -15,8 +15,8 @@ int main(int, char *[])
// Each sink can have it's own log level and a message will be logged. // Each sink can have it's own log level and a message will be logged.
std::vector<spdlog::sink_ptr> sinks; std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>()); sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::simple_file_sink_mt>("./log_regular_file.txt")); sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./log_regular_file.txt"));
sinks.push_back(std::make_shared<spdlog::sinks::simple_file_sink_mt>("./log_debug_file.txt")); sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./log_debug_file.txt"));
spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end()); spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end());
console_multisink.set_level(spdlog::level::warn); console_multisink.set_level(spdlog::level::warn);

View File

@ -23,7 +23,7 @@ namespace spdlog {
// async logger factory - creates async loggers backed with thread pool. // 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. // 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<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
static std::shared_ptr<async_logger> create(const std::string &logger_name, SinkArgs &&... args) static std::shared_ptr<async_logger> create(const std::string &logger_name, SinkArgs &&... args)
@ -46,9 +46,9 @@ struct create_async
}; };
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async_logger(const std::string &logger_name, SinkArgs &&... sink_args) inline std::shared_ptr<spdlog::logger> create_async(const std::string &logger_name, SinkArgs &&... sink_args)
{ {
return create_async::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...); return async_factory::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
} }
// set global thread pool. // set global thread pool.

View File

@ -6,8 +6,8 @@
#pragma once #pragma once
// //
// base sink templated over a mutex (either dummy or real) // base sink templated over a mutex (either dummy or real)
// concrete implementation should only override the sink_it_ method. // concrete implementation should override the sink_it_() and flush_() methods.
// all locking is taken care of here so no locking needed by the implementers.. // locking is taken care of in this class - no locking needed by the implementers..
// //
#include "spdlog/common.h" #include "spdlog/common.h"
@ -26,16 +26,6 @@ public:
{ {
} }
base_sink(const std::string &formatter_pattern)
: sink(formatter_pattern)
{
}
base_sink(std::unique_ptr<spdlog::formatter> sink_formatter)
: sink(std::move(sink_formatter))
{
}
base_sink(const base_sink &) = delete; base_sink(const base_sink &) = delete;
base_sink &operator=(const base_sink &) = delete; base_sink &operator=(const base_sink &) = delete;

View File

@ -18,10 +18,10 @@ namespace sinks {
* Trivial file sink with single file as target * Trivial file sink with single file as target
*/ */
template<class Mutex> template<class Mutex>
class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex> class basic_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{ {
public: 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); file_helper_.open(filename, truncate);
} }
@ -43,8 +43,8 @@ private:
details::file_helper file_helper_; details::file_helper file_helper_;
}; };
using simple_file_sink_mt = simple_file_sink<std::mutex>; using basic_file_sink_mt = basic_file_sink<std::mutex>;
using simple_file_sink_st = simple_file_sink<details::null_mutex>; using basic_file_sink_st = basic_file_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks
@ -54,13 +54,13 @@ using simple_file_sink_st = simple_file_sink<details::null_mutex>;
template<typename Factory = default_factory> template<typename Factory = default_factory>
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false)
{ {
return Factory::template create<sinks::simple_file_sink_mt>(logger_name, filename, truncate); return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate);
} }
template<typename Factory = default_factory> template<typename Factory = default_factory>
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false)
{ {
return Factory::template create<sinks::simple_file_sink_st>(logger_name, filename, truncate); return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate);
} }
} // namespace spdlog } // namespace spdlog

View File

@ -18,17 +18,17 @@ public:
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter("%+"))) : formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter("%+")))
{ {
} }
//
explicit sink(const std::string &formatter_pattern) // explicit sink(const std::string &formatter_pattern)
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter(formatter_pattern))) // : formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter(formatter_pattern)))
{ // {
} // }
//
// sink with custom formatter // // sink with custom formatter
explicit sink(std::unique_ptr<spdlog::formatter> sink_formatter) // explicit sink(std::unique_ptr<spdlog::formatter> sink_formatter)
: formatter_(std::move(sink_formatter)) // : formatter_(std::move(sink_formatter))
{ // {
} // }
virtual ~sink() = default; virtual ~sink() = default;
@ -39,10 +39,12 @@ public:
{ {
return msg_level >= level_.load(std::memory_order_relaxed); return msg_level >= level_.load(std::memory_order_relaxed);
} }
void set_level(level::level_enum log_level) void set_level(level::level_enum log_level)
{ {
level_.store(log_level); level_.store(log_level);
} }
level::level_enum level() const level::level_enum level() const
{ {
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed)); return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
@ -58,10 +60,6 @@ public:
formatter_ = std::move(sink_formatter); formatter_ = std::move(sink_formatter);
} }
spdlog::formatter *formatter()
{
return formatter_.get();
}
protected: protected:
level_t level_{level::trace}; level_t level_{level::trace};

View File

@ -19,7 +19,7 @@
namespace spdlog { namespace spdlog {
// Default logger factory- creates synchronous loggers // Default logger factory- creates synchronous loggers
struct create_synchronous struct synchronous_factory
{ {
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
@ -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 // 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. // The logger's level, formatter and flush level will be set according the global settings.

View File

@ -24,7 +24,7 @@ TEST_CASE("default_error_handler", "[errors]]")
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log.txt"; std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("test-error", filename, true); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->info("Test message {} {}", 1); logger->info("Test message {} {}", 1);
logger->info("Test message {}", 2); logger->info("Test message {}", 2);
@ -41,7 +41,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log.txt"; std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->info("Good message #1"); logger->info("Good message #1");
@ -75,7 +75,7 @@ TEST_CASE("async_error_handler", "[errors]]")
std::string filename = "logs/simple_async_log.txt"; std::string filename = "logs/simple_async_log.txt";
{ {
spdlog::init_thread_pool(128, 1); spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string &) { logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("logs/custom_err.txt"); std::ofstream ofs("logs/custom_err.txt");
if (!ofs) if (!ofs)
@ -99,7 +99,7 @@ TEST_CASE("async_error_handler2", "[errors]]")
std::string err_msg("This is async handler error message"); std::string err_msg("This is async handler error message");
{ {
spdlog::init_thread_pool(128, 1); spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async_logger<failing_sink>("failed_logger"); auto logger = spdlog::create_async<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("logs/custom_err2.txt"); std::ofstream ofs("logs/custom_err2.txt");
if (!ofs) if (!ofs)

View File

@ -8,7 +8,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log"; std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->info("Test message {}", 1); logger->info("Test message {}", 1);
@ -24,7 +24,7 @@ TEST_CASE("flush_on", "[flush_on]]")
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log"; std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);
logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);

View File

@ -17,6 +17,6 @@
#include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/ostream_sink.h" #include "spdlog/sinks/ostream_sink.h"
#include "spdlog/sinks/rotating_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 "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"

View File

@ -1,6 +1,6 @@
#include "includes.h" #include "includes.h"
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/sinks/simple_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "test_sink.h" #include "test_sink.h"
TEST_CASE("basic async test ", "[async]") TEST_CASE("basic async test ", "[async]")
@ -121,7 +121,7 @@ TEST_CASE("to_file", "[async]")
size_t tp_threads = 1; size_t tp_threads = 1;
std::string filename = "logs/async_test.log"; std::string filename = "logs/async_test.log";
{ {
auto file_sink = std::make_shared<spdlog::sinks::simple_file_sink_mt>(filename, true); auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads); auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp)); auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
@ -143,7 +143,7 @@ TEST_CASE("to_file multi-workers", "[async]")
size_t tp_threads = 10; size_t tp_threads = 10;
std::string filename = "logs/async_test.log"; std::string filename = "logs/async_test.log";
{ {
auto file_sink = std::make_shared<spdlog::sinks::simple_file_sink_mt>(filename, true); auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads); auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp)); auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));

View File

@ -9,12 +9,11 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log"; std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);
SPDLOG_TRACE(logger, "Test message 1"); SPDLOG_TRACE(logger, "Test message 1");
// SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_DEBUG(logger, "Test message 2"); SPDLOG_DEBUG(logger, "Test message 2");
logger->flush(); logger->flush();
@ -27,7 +26,7 @@ TEST_CASE("debug and trace with format strings", "[macros]]")
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log"; std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);