This commit is contained in:
gabime 2024-12-07 00:52:27 +02:00
parent 166843ff3a
commit 8d4943789c
47 changed files with 318 additions and 1286 deletions

View File

@ -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

View File

@ -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<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
// alternatively:
// auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("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<spdlog::sinks::stdout_color_sink_mt >();
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("mylog.txt", 1024*1024*10, 3);
std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
auto logger = std::make_shared<spdlog::async_logger>("loggername", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
spdlog::register_logger(logger);
}
```
---
#### User-defined types
```c++

View File

@ -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<stdout_color_sink_mt>("console");
// or for stderr:
// auto console = spdlog::stderr_color_mt("error-logger");
//auto console = spdlog::create<stderr_color_sink_mt>("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<basic_file_sink_mt>("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<rotating_file_sink_mt>("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_file_format_sink_mt>("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<callback_sink_mt>("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<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
// alternatively:
// auto async_file =
// spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("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<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
// // alternatively:
// // auto async_file =
// // spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("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<udp_sink_mt>("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<spdlog::sinks::stdout_color_sink_mt>();
auto console_sink = std::make_shared<stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::warn);
console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
auto file_sink = std::make_shared<basic_file_sink_mt>("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_sink_mt>("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<basic_file_sink_mt>("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!");

View File

@ -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 <functional>
#include <memory>
#include <mutex>
#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 <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
struct async_factory_impl {
template <typename Sink, typename... SinkArgs>
static std::shared_ptr<async_logger> 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<std::recursive_mutex> tp_lock(mutex);
auto tp = context->get_tp();
if (tp == nullptr) {
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
context->set_tp(tp);
}
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
return new_logger;
}
};
using async_factory = async_factory_impl<async_overflow_policy::block>;
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
template <typename Sink, typename... SinkArgs>
std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args) {
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}
template <typename Sink, typename... SinkArgs>
std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args) {
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}
// set global thread pool.
inline void init_thread_pool(size_t q_size,
size_t thread_count,
std::function<void()> on_thread_start,
std::function<void()> on_thread_stop) {
auto tp = std::make_shared<details::thread_pool>(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<void()> 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<spdlog::details::thread_pool> thread_pool() { return spdlog::context()->get_tp(); }
} // namespace spdlog

View File

@ -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 <typename Sink, typename... SinkArgs>
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args) {
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
return new_logger;
}
};
} // namespace spdlog

View File

@ -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 <chrono>
#include <functional>
#include <memory>
#include <thread>
#include <vector>
#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<spdlog::async_logger>;
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<log_msg_buffer *>(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<item_type>;
thread_pool(size_t q_max_items,
size_t threads_n,
std::function<void()> on_thread_start,
std::function<void()> on_thread_stop);
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> 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<std::thread> 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

View File

@ -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 <int BufferId = log_id::LOG_ID_MAIN>
using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
} // namespace sinks
// Create and register android syslog logger
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") {
return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") {
return Factory::template create<sinks::android_sink_st>(logger_name, tag);
}
} // namespace spdlog
#endif // __ANDROID__

View File

@ -43,5 +43,6 @@ protected:
virtual void set_pattern_(const std::string &pattern);
virtual void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter);
};
} // namespace sinks
} // namespace spdlog

View File

@ -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<std::mutex>;
using basic_file_sink_st = basic_file_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> 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<sinks::basic_file_sink_mt>(logger_name, filename, truncate, event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> 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<sinks::basic_file_sink_st>(logger_name, filename, truncate, event_handlers);
}
} // namespace spdlog

View File

@ -7,7 +7,6 @@
#include <string>
#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<std::mutex>;
using callback_sink_st = callback_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback) {
return Factory::template create<sinks::callback_sink_mt>(logger_name, callback);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callback &callback) {
return Factory::template create<sinks::callback_sink_st>(logger_name, callback);
}
} // namespace spdlog

View File

@ -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<std::mutex, daily_filename_for
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files,
event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files,
event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files,
event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files,
event_handlers);
}
} // namespace spdlog

View File

@ -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<std::mutex>;
using hourly_file_sink_st = hourly_file_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files, event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files, event_handlers);
}
} // namespace spdlog

View File

@ -12,11 +12,9 @@
#include <mutex>
#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<std::mutex>;
using kafka_sink_st = kafka_sink<spdlog::details::null_mutex>;
} // namespace sinks
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> kafka_logger_mt(const std::string &logger_name, spdlog::sinks::kafka_sink_config config) {
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name, spdlog::sinks::kafka_sink_config config) {
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
}
template <typename Factory = spdlog::async_factory>
inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config) {
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
}
template <typename Factory = spdlog::async_factory>
inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config) {
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
}
} // namespace spdlog

View File

@ -17,9 +17,10 @@
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mutex>
#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<mongocxx::client> client_ = nullptr;
};
#include <mutex>
#include "../details/null_mutex.h"
using mongo_sink_mt = mongo_sink<std::mutex>;
using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
using mongo_sink_st = mongo_sink<details::null_mutex>;
} // namespace sinks
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
}
} // namespace spdlog

View File

@ -4,7 +4,6 @@
#pragma once
#include <mutex>
#include "../details/null_mutex.h"
#include "./base_sink.h"

View File

@ -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<details::null_mutex>;
using null_sink_st = null_sink<details::null_mutex>;
} // namespace sinks
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> null_logger_mt(const std::string &logger_name) {
auto null_logger = Factory::template create<sinks::null_sink_mt>(logger_name);
null_logger->set_level(level::off);
return null_logger;
}
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> null_logger_st(const std::string &logger_name) {
auto null_logger = Factory::template create<sinks::null_sink_st>(logger_name);
null_logger->set_level(level::off);
return null_logger;
}
} // namespace spdlog

View File

@ -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<QTextCharFormat, level::n_levels> colors_;
};
#include <mutex>
#include "../details/null_mutex.h"
using qt_sink_mt = qt_sink<std::mutex>;
using qt_sink_st = qt_sink<details::null_mutex>;
using qt_color_sink_mt = qt_color_sink<std::mutex>;
using qt_color_sink_st = qt_color_sink<details::null_mutex>;
} // namespace sinks
//
// Factory functions
//
// log to QTextEdit
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name,
QTextEdit *qt_object,
const std::string &meta_method = "append") {
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name,
QTextEdit *qt_object,
const std::string &meta_method = "append") {
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
}
// log to QPlainTextEdit
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name,
QPlainTextEdit *qt_object,
const std::string &meta_method = "appendPlainText") {
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name,
QPlainTextEdit *qt_object,
const std::string &meta_method = "appendPlainText") {
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
}
// log to QObject
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QObject *qt_object, const std::string &meta_method) {
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::string &meta_method) {
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
}
// log to QTextEdit with colorized output
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_color_logger_mt(const std::string &logger_name,
QTextEdit *qt_text_edit,
int max_lines,
bool is_utf8 = false) {
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines, false, is_utf8);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_color_logger_st(const std::string &logger_name,
QTextEdit *qt_text_edit,
int max_lines,
bool is_utf8 = false) {
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines, false, is_utf8);
}
} // namespace spdlog

View File

@ -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 <typename Mutex>
class rotating_file_sink final : public base_sink<Mutex> {
public:
@ -56,30 +54,4 @@ using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open,
event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files, rotate_on_open,
event_handlers);
}
} // namespace spdlog

View File

@ -16,9 +16,9 @@ public:
virtual void set_pattern(const std::string &pattern) = 0;
virtual void set_formatter(std::unique_ptr<spdlog::formatter> 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

View File

@ -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 <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
} // namespace spdlog

View File

@ -4,9 +4,9 @@
#pragma once
#include <cstdio>
#include <mutex>
#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<std::mutex>;
using stderr_sink_st = stderr_sink<details::null_mutex>;
} // namespace sinks
// factory methods
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name);
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name);
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name);
template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name);
} // namespace spdlog

View File

@ -4,7 +4,6 @@
#pragma once
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/sinks/base_sink.h>
#include <syslog.h>
@ -19,7 +18,7 @@ namespace sinks {
template <typename Mutex>
class syslog_sink final : public base_sink<Mutex> {
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<std::mutex>;
using syslog_sink_st = syslog_sink<details::null_mutex>;
} // namespace sinks
// Create and register a syslog logger
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility,
enable_formatting);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> 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<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility,
enable_formatting);
}
} // namespace spdlog

View File

@ -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<std::mutex>;
using systemd_sink_st = systemd_sink<details::null_mutex>;
} // namespace sinks
// Create and register a syslog logger
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name,
const std::string &ident = "",
bool enable_formatting = false) {
return Factory::template create<sinks::systemd_sink_mt>(logger_name, ident, enable_formatting);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name,
const std::string &ident = "",
bool enable_formatting = false) {
return Factory::template create<sinks::systemd_sink_st>(logger_name, ident, enable_formatting);
}
} // namespace spdlog

View File

@ -69,7 +69,7 @@ protected:
};
using tcp_sink_mt = tcp_sink<std::mutex>;
using tcp_sink_st = tcp_sink<spdlog::details::null_mutex>;
using tcp_sink_st = tcp_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog

View File

@ -33,7 +33,7 @@ struct udp_sink_config {
};
template <typename Mutex>
class udp_sink final : public spdlog::sinks::base_sink<Mutex> {
class udp_sink final : public base_sink<Mutex> {
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<std::mutex>;
using udp_sink_st = udp_sink<spdlog::details::null_mutex>;
using udp_sink_st = udp_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config) {
return Factory::template create<sinks::udp_sink_mt>(logger_name, skin_config);
}
} // namespace spdlog

View File

@ -69,5 +69,6 @@ using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
using wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>;
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog

View File

@ -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<details::context> context);
SPDLOG_API std::shared_ptr<details::context> context();
SPDLOG_API const std::shared_ptr<details::context> &context_ref();
@ -31,7 +28,7 @@ SPDLOG_API const std::shared_ptr<details::context> &context_ref();
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
template <typename Sink, typename... SinkArgs>
std::shared_ptr<logger> create(std::string logger_name, SinkArgs &&...sink_args) {
return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
return std::make_shared<logger>(std::move(logger_name), std::make_shared<Sink>(std::forward<SinkArgs>(sink_args)...));
}
// Set formatter of the global logger. Each sink in each logger will get a clone of this object

View File

@ -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 <memory>
#include <string>
#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<details::thread_pool> 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<details::thread_pool> 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::logger> spdlog::async_logger::clone(std::string new_name) {
auto cloned = std::make_shared<spdlog::async_logger>(*this);
cloned->name_ = std::move(new_name);
return cloned;
}

View File

@ -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 <cassert>
#include "spdlog/common.h"
namespace spdlog {
namespace details {
thread_pool::thread_pool(size_t q_max_items,
size_t threads_n,
std::function<void()> on_thread_start,
std::function<void()> 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<void()> 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

View File

@ -110,12 +110,13 @@ template <typename Mutex>
ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
: ansicolor_sink<Mutex>(stderr, mode) {}
} // namespace sinks
} // namespace spdlog
// template instantiations
template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink<spdlog::details::null_mutex>;
template class SPDLOG_API ansicolor_stdout_sink<std::mutex>;
template class SPDLOG_API ansicolor_stdout_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink<spdlog::details::null_mutex>;
template class SPDLOG_API ansicolor_stderr_sink<std::mutex>;
template class SPDLOG_API ansicolor_stderr_sink<spdlog::details::null_mutex>;
} // namespace sinks
} // namespace spdlog

View File

@ -5,53 +5,58 @@
#include <memory>
#include <mutex>
#include "spdlog/common.h"
#include "spdlog/details/null_mutex.h"
#include "spdlog/pattern_formatter.h"
namespace spdlog {
namespace sinks {
template <typename Mutex>
spdlog::sinks::base_sink<Mutex>::base_sink()
base_sink<Mutex>::base_sink()
: formatter_{std::make_unique<spdlog::pattern_formatter>()} {}
template <typename Mutex>
spdlog::sinks::base_sink<Mutex>::base_sink(std::unique_ptr<spdlog::formatter> formatter)
base_sink<Mutex>::base_sink(std::unique_ptr<spdlog::formatter> formatter)
: formatter_{std::move(formatter)} {}
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg) {
void base_sink<Mutex>::log(const details::log_msg &msg) {
std::lock_guard<Mutex> lock(mutex_);
sink_it_(msg);
}
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::flush() {
void base_sink<Mutex>::flush() {
std::lock_guard<Mutex> lock(mutex_);
flush_();
}
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern) {
void base_sink<Mutex>::set_pattern(const std::string &pattern) {
std::lock_guard<Mutex> lock(mutex_);
set_pattern_(pattern);
}
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
void base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<Mutex> lock(mutex_);
set_formatter_(std::move(sink_formatter));
}
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern) {
set_formatter_(std::make_unique<spdlog::pattern_formatter>(pattern));
void base_sink<Mutex>::set_pattern_(const std::string &pattern) {
set_formatter_(std::make_unique<pattern_formatter>(pattern));
}
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) {
void base_sink<Mutex>::set_formatter_(std::unique_ptr<formatter> sink_formatter) {
formatter_ = std::move(sink_formatter);
}
// template instantiations
template class SPDLOG_API spdlog::sinks::base_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::base_sink<spdlog::details::null_mutex>;
template class SPDLOG_API base_sink<std::mutex>;
template class SPDLOG_API base_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog

View File

@ -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 <mutex>
namespace spdlog {
namespace sinks {
@ -31,9 +32,9 @@ void basic_file_sink<Mutex>::flush_() {
file_helper_.flush();
}
// template instantiations
template class SPDLOG_API basic_file_sink<std::mutex>;
template class SPDLOG_API basic_file_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog
// template instantiations
template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::basic_file_sink<spdlog::details::null_mutex>;

View File

@ -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<Mutex>::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<std::mutex>;
template class SPDLOG_API rotating_file_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog
// template instantiations
template class SPDLOG_API spdlog::sinks::rotating_file_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::rotating_file_sink<spdlog::details::null_mutex>;

View File

@ -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); }

View File

@ -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 <typename Factory>
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stdout_color_sink_mt>(logger_name, mode);
}
template <typename Factory>
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stdout_color_sink_st>(logger_name, mode);
}
template <typename Factory>
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stderr_color_sink_mt>(logger_name, mode);
}
template <typename Factory>
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stderr_color_sink_st>(logger_name, mode);
}
} // namespace spdlog
// template instantiations
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::async_factory>(const std::string &logger_name,
color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::async_factory>(const std::string &logger_name,
color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name,
color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name,
color_mode mode);

View File

@ -81,56 +81,11 @@ template <typename Mutex>
stderr_sink<Mutex>::stderr_sink()
: stdout_sink_base<Mutex>(stderr) {}
// template installations
template class SPDLOG_API stdout_sink<std::mutex>;
template class SPDLOG_API stdout_sink<details::null_mutex>;
template class SPDLOG_API stderr_sink<std::mutex>;
template class SPDLOG_API stderr_sink<details::null_mutex>;
} // namespace sinks
// factory methods
template <typename Factory>
std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) {
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
template <typename Factory>
std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) {
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
template <typename Factory>
std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) {
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
template <typename Factory>
std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
} // namespace spdlog
// template instantiations for stdout/stderr loggers
template class SPDLOG_API spdlog::sinks::stdout_sink_base<std::mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::stderr_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::null_mutex>;
// template instantiations for stdout/stderr factory functions
#include "spdlog/async.h"
#include "spdlog/details/synchronous_factory.h"
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::synchronous_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::synchronous_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::synchronous_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::synchronous_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::async_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::async_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::async_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::async_factory>(
const std::string &logger_name);

View File

@ -4,11 +4,9 @@
// clang-format off
#include "spdlog/details/windows_include.h"
#include <wincon.h>
#include <mutex>
// clang-format on
#include <mutex>
#include "spdlog/sinks/wincolor_sink.h"
#include "spdlog/common.h"
#include "spdlog/details/null_mutex.h"
@ -123,24 +121,9 @@ void wincolor_sink<Mutex>::write_to_file_(const memory_buf_t &formatted) {
(void)(ignored);
}
// wincolor_stdout_sink
template <typename Mutex>
wincolor_stdout_sink<Mutex>::wincolor_stdout_sink(color_mode mode)
: wincolor_sink<Mutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {}
// template instantiations
template class SPDLOG_API wincolor_sink<std::mutex>;
template class SPDLOG_API wincolor_sink<details::null_mutex>;
// wincolor_stderr_sink
template <typename Mutex>
wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode)
: wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}
} // namespace sinks
} // namespace spdlog
// template instantiations
template class SPDLOG_API spdlog::sinks::wincolor_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink<spdlog::details::null_mutex>;

View File

@ -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"

View File

@ -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<spdlog::sinks::test_sink_mt>();
size_t overrun_counter = 0;
size_t queue_size = 128;
size_t messages = 256;
{
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
test_sink->set_delay(std::chrono::milliseconds(1));
size_t queue_size = 4;
size_t messages = 1024;
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
test_sink->set_delay(std::chrono::milliseconds(1));
size_t queue_size = 4;
size_t messages = 1024;
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>("as2");
auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(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<spdlog::sinks::test_sink_mt>();
size_t queue_size = 256;
size_t messages = 256;
{
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
test_sink->set_delay(std::chrono::milliseconds(5));
size_t messages = 100;
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, 2);
auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
size_t queue_size = 128;
size_t messages = 256;
size_t n_threads = 10;
{
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
std::vector<std::thread> 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<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));
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<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));
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<spdlog::sinks::test_sink_mt>();
std::shared_ptr<spdlog::details::thread_pool> const empty_tp;
auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
// size_t overrun_counter = 0;
// size_t queue_size = 128;
// size_t messages = 256;
// {
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
// auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
// test_sink->set_delay(std::chrono::milliseconds(1));
// size_t queue_size = 4;
// size_t messages = 1024;
//
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
// auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
// test_sink->set_delay(std::chrono::milliseconds(1));
// size_t queue_size = 4;
// size_t messages = 1024;
//
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
// auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>("as2");
// auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(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<spdlog::sinks::test_sink_mt>();
// size_t queue_size = 256;
// size_t messages = 256;
// {
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
// auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
// test_sink->set_delay(std::chrono::milliseconds(5));
// size_t messages = 100;
//
// auto tp = std::make_shared<spdlog::details::thread_pool>(messages, 2);
// auto logger = std::make_shared<spdlog::async_logger>("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<spdlog::sinks::test_sink_mt>();
// size_t queue_size = 128;
// size_t messages = 256;
// size_t n_threads = 10;
// {
// auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
//
// std::vector<std::thread> 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<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));
//
// 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<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));
//
// 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<spdlog::sinks::test_sink_mt>();
// std::shared_ptr<spdlog::details::thread_pool> const empty_tp;
// auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, empty_tp);
// logger->info("Please throw an exception");
// REQUIRE(test_sink->msg_counter() == 0);
//}

View File

@ -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"

View File

@ -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<basic_file_sink_mt>("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<basic_file_sink_mt>("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<spdlog::sinks::basic_file_sink_mt>("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<failing_sink>("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);
}

View File

@ -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<basic_file_sink_mt>("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<basic_file_sink_mt>("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<rotating_file_sink_mt>("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<rotating_file_sink_mt>("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<rotating_file_sink_mt>("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<rotating_file_sink_mt>("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<spdlog::sinks::rotating_file_sink_st>(basename, max_size, 2);
auto logger = std::make_shared<spdlog::logger>("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);
}

View File

@ -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<spdlog::sinks::basic_file_sink_mt>("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<spdlog::sinks::null_sink_mt>("refmacro");
auto &ref = *logger;
SPDLOG_LOGGER_TRACE(&ref, "Test message 1");
SPDLOG_LOGGER_DEBUG(&ref, "Test message 2");

View File

@ -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<test_sink_mt>();
auto logger = std::make_shared<spdlog::async_logger>("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<test_sink_mt>();
// auto logger = std::make_shared<spdlog::async_logger>("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]") {

View File

@ -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;

View File

@ -8,7 +8,7 @@
TEST_CASE("stdout_st", "[stdout]") {
spdlog::set_pattern("%+");
auto l = spdlog::stdout_logger_st("test");
auto l = spdlog::create<spdlog::sinks::stdout_color_sink_st>("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<spdlog::sinks::stderr_color_sink_st>("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<spdlog::sinks::stdout_color_sink_st>("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<spdlog::sinks::stdout_color_sink_mt>("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<spdlog::sinks::stderr_color_sink_st>("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<spdlog::sinks::stderr_color_sink_mt>("test");
l->set_pattern("%+");
l->info("Test stderr_color_mt");
l->warn("Test stderr_color_mt");

View File

@ -1,5 +1,4 @@
#include "includes.h"
#include "spdlog/async.h"
#include "test_sink.h"
TEST_CASE("time_point1", "[time_point log_msg]") {