mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-24 09:31:34 +08:00
Renamed simple_file_sink -> basic_file_sink
This commit is contained in:
parent
69c11ea7d2
commit
cd4dcbab36
@ -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 <atomic>
|
||||
@ -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<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);
|
||||
spdlog::drop("as");
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#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<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");
|
||||
|
||||
std::cout << "To stop, press <Enter>" << std::endl;
|
||||
|
@ -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 <iostream>
|
||||
@ -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<spdlog::create_async>("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<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)
|
||||
{
|
||||
async_file->info("Async message #{}", i + 1);
|
||||
|
@ -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<spdlog::sink_ptr> sinks;
|
||||
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::simple_file_sink_mt>("./log_debug_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::basic_file_sink_mt>("./log_debug_file.txt"));
|
||||
|
||||
spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end());
|
||||
console_multisink.set_level(spdlog::level::warn);
|
||||
|
@ -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<typename Sink, typename... SinkArgs>
|
||||
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>
|
||||
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.
|
||||
|
@ -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<spdlog::formatter> sink_formatter)
|
||||
: sink(std::move(sink_formatter))
|
||||
{
|
||||
}
|
||||
|
||||
base_sink(const base_sink &) = delete;
|
||||
base_sink &operator=(const base_sink &) = delete;
|
||||
|
||||
|
@ -18,10 +18,10 @@ namespace sinks {
|
||||
* Trivial file sink with single file as target
|
||||
*/
|
||||
template<class Mutex>
|
||||
class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
class basic_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
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<std::mutex>;
|
||||
using simple_file_sink_st = simple_file_sink<details::null_mutex>;
|
||||
using basic_file_sink_mt = basic_file_sink<std::mutex>;
|
||||
using basic_file_sink_st = basic_file_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
@ -54,13 +54,13 @@ using simple_file_sink_st = simple_file_sink<details::null_mutex>;
|
||||
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)
|
||||
{
|
||||
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>
|
||||
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
|
@ -18,17 +18,17 @@ public:
|
||||
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter("%+")))
|
||||
{
|
||||
}
|
||||
|
||||
explicit sink(const std::string &formatter_pattern)
|
||||
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter(formatter_pattern)))
|
||||
{
|
||||
}
|
||||
|
||||
// sink with custom formatter
|
||||
explicit sink(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
: formatter_(std::move(sink_formatter))
|
||||
{
|
||||
}
|
||||
//
|
||||
// explicit sink(const std::string &formatter_pattern)
|
||||
// : formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter(formatter_pattern)))
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// // sink with custom formatter
|
||||
// explicit sink(std::unique_ptr<spdlog::formatter> 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<spdlog::level::level_enum>(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};
|
||||
|
@ -19,7 +19,7 @@
|
||||
namespace spdlog {
|
||||
|
||||
// Default logger factory- creates synchronous loggers
|
||||
struct create_synchronous
|
||||
struct synchronous_factory
|
||||
{
|
||||
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
|
||||
// The logger's level, formatter and flush level will be set according the global settings.
|
||||
|
@ -24,7 +24,7 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
prepare_logdir();
|
||||
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->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<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->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<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 &) {
|
||||
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<failing_sink>("failed_logger");
|
||||
auto logger = spdlog::create_async<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err2.txt");
|
||||
if (!ofs)
|
||||
|
@ -8,7 +8,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
|
||||
prepare_logdir();
|
||||
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->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<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_level(spdlog::level::trace);
|
||||
logger->flush_on(spdlog::level::info);
|
||||
|
@ -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"
|
||||
|
@ -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<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 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;
|
||||
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 logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
|
||||
|
@ -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<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_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<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_level(spdlog::level::trace);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user