mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-24 09:31:34 +08:00
Refactored sink interface and base_sink
This commit is contained in:
parent
2bc05b6b17
commit
c2a49080aa
@ -110,6 +110,7 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
|
||||
auto delta = high_resolution_clock::now() - start;
|
||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||
|
||||
cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl;
|
||||
spdlog::drop(log->name());
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
CXX = g++
|
||||
CXX ?= g++
|
||||
CXX_FLAGS = -Wall -Wextra -pedantic -std=c++11 -pthread -I../include -fmax-errors=1
|
||||
CXX_RELEASE_FLAGS = -O3 -march=native
|
||||
CXX_DEBUG_FLAGS= -g
|
||||
|
@ -59,7 +59,8 @@ int main(int, char *[])
|
||||
}
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/stdout_color_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed
|
||||
void stdout_example()
|
||||
{
|
||||
// create color multi threaded logger
|
||||
@ -181,18 +182,18 @@ void err_handler_example()
|
||||
void syslog_example()
|
||||
{
|
||||
std::string ident = "spdlog-example";
|
||||
auto syslog_logger = spdlog::syslog_logger("syslog", ident, LOG_PID);
|
||||
auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID);
|
||||
syslog_logger->warn("This is warning that will end up in syslog.");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Android example
|
||||
#if defined(__ANDROID__)
|
||||
#incude "spdlog/sinks/android_sink.h"
|
||||
#include "spdlog/sinks/android_sink.h"
|
||||
void android_example()
|
||||
{
|
||||
std::string tag = "spdlog-android";
|
||||
auto android_logger = spdlog::android_logger("android", tag);
|
||||
auto android_logger = spdlog::android_logger_mt("android", tag);
|
||||
android_logger->critical("Use \"adb shell logcat\" to view this message.");
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ class async_logger SPDLOG_FINAL : public std::enable_shared_from_this<async_logg
|
||||
friend class details::thread_pool;
|
||||
|
||||
public:
|
||||
template<class It>
|
||||
template<typename It>
|
||||
async_logger(const std::string &logger_name, const It &begin, const It &end, std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy = async_overflow_policy::block);
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
template<class It>
|
||||
template<typename It>
|
||||
inline spdlog::async_logger::async_logger(const std::string &logger_name, const It &begin, const It &end,
|
||||
std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
|
||||
: logger(logger_name, begin, end)
|
||||
|
@ -4,10 +4,14 @@
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "stdio.h"
|
||||
#include <mutex>
|
||||
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
struct console_stdout_stream
|
||||
struct console_stdout
|
||||
{
|
||||
static FILE *stream()
|
||||
{
|
||||
@ -21,7 +25,7 @@ struct console_stdout_stream
|
||||
#endif
|
||||
};
|
||||
|
||||
struct console_stderr_stream
|
||||
struct console_stderr
|
||||
{
|
||||
static FILE *stream()
|
||||
{
|
||||
@ -35,23 +39,23 @@ struct console_stderr_stream
|
||||
#endif
|
||||
};
|
||||
|
||||
struct console_global_mutex
|
||||
struct console_mutex
|
||||
{
|
||||
using mutex_t = std::mutex;
|
||||
static mutex_t &console_mutex()
|
||||
static mutex_t &mutex()
|
||||
{
|
||||
static mutex_t mutex;
|
||||
return mutex;
|
||||
static mutex_t s_mutex;
|
||||
return s_mutex;
|
||||
}
|
||||
};
|
||||
|
||||
struct console_global_nullmutex
|
||||
struct console_nullmutex
|
||||
{
|
||||
using mutex_t = null_mutex;
|
||||
static mutex_t &console_mutex()
|
||||
static mutex_t &mutex()
|
||||
{
|
||||
static mutex_t mutex;
|
||||
return mutex;
|
||||
static mutex_t s_mutex;
|
||||
return s_mutex;
|
||||
}
|
||||
};
|
||||
} // namespace details
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
// create logger with given name, sinks and the default pattern formatter
|
||||
// all other ctors will call this one
|
||||
template<class It>
|
||||
template<typename It>
|
||||
inline spdlog::logger::logger(std::string logger_name, const It &begin, const It &end)
|
||||
: name_(std::move(logger_name))
|
||||
, sinks_(begin, end)
|
||||
@ -36,7 +36,7 @@ inline spdlog::logger::logger(const std::string &logger_name, spdlog::sink_ptr s
|
||||
|
||||
inline spdlog::logger::~logger() = default;
|
||||
|
||||
template<class FormatterT, typename... Args>
|
||||
template<typename FormatterT, typename... Args>
|
||||
inline void spdlog::logger::set_formatter(const Args &... args)
|
||||
{
|
||||
for (auto &sink : sinks_)
|
||||
|
@ -23,7 +23,7 @@ namespace spdlog {
|
||||
namespace details {
|
||||
class thread_pool;
|
||||
|
||||
template<class Mutex>
|
||||
template<typename Mutex>
|
||||
class registry_t
|
||||
{
|
||||
public:
|
||||
@ -46,8 +46,7 @@ public:
|
||||
auto logger_name = new_logger->name();
|
||||
throw_if_exists_(logger_name);
|
||||
|
||||
// create default formatter if not exists
|
||||
|
||||
// set the global formatter pattern
|
||||
new_logger->set_formatter<pattern_formatter>(formatter_pattern_, pattern_time_type_);
|
||||
|
||||
if (err_handler_)
|
||||
|
@ -31,7 +31,7 @@ public:
|
||||
logger(const std::string &name, sink_ptr single_sink);
|
||||
logger(const std::string &name, sinks_init_list sinks);
|
||||
|
||||
template<class It>
|
||||
template<typename It>
|
||||
logger(std::string name, const It &begin, const It &end);
|
||||
|
||||
virtual ~logger();
|
||||
@ -118,7 +118,7 @@ public:
|
||||
|
||||
// create a FormatterT formatter for each sink in this logger.
|
||||
// each sink gets its own private copy of a formatter object.
|
||||
template<class FormatterT, typename... Args>
|
||||
template<typename FormatterT, typename... Args>
|
||||
void set_formatter(const Args &... args);
|
||||
|
||||
void flush();
|
||||
|
@ -5,10 +5,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
|
||||
#include "spdlog/details/os.h"
|
||||
#include "spdlog/sinks/sink.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/fmt_helper.h"
|
||||
|
||||
#include <android/log.h>
|
||||
#include <chrono>
|
||||
@ -25,9 +25,9 @@ namespace sinks {
|
||||
|
||||
/*
|
||||
* Android sink (logging using __android_log_write)
|
||||
* __android_log_write is thread-safe. No lock is needed.
|
||||
*/
|
||||
class android_sink : public sink
|
||||
template<typename Mutex>
|
||||
class android_sink SPDLOG_FINAL: public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false)
|
||||
@ -36,13 +36,14 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
void log(const details::log_msg &msg) override
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
const android_LogPriority priority = convert_to_android_(msg.level);
|
||||
fmt::memory_buffer formatted;
|
||||
if (use_raw_msg_)
|
||||
{
|
||||
formatted.append(msg.raw.data(), msg.raw.data() + msg.raw.size());
|
||||
fmt_helper::append_buf(msg.raw, formatted);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -67,7 +68,9 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void flush() override {}
|
||||
void flush_() override
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
|
||||
@ -95,16 +98,24 @@ private:
|
||||
bool use_raw_msg_;
|
||||
};
|
||||
|
||||
using android_sink_mt = android_sink<std::mutex>;
|
||||
using android_sink_st = android_sink<details::null_mutex>;
|
||||
} // namespace sinks
|
||||
|
||||
// Create and register android syslog logger
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
inline std::shared_ptr<logger> android_logger(const std::string &logger_name, const std::string &tag = "spdlog")
|
||||
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>(logger_name, tag);
|
||||
return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
|
||||
}
|
||||
|
||||
template<typename Factory = default_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
|
||||
|
||||
|
@ -22,14 +22,14 @@ namespace sinks {
|
||||
* of the message.
|
||||
* If no color terminal detected, omit the escape codes.
|
||||
*/
|
||||
template<class TargetStream, class ConsoleMutex>
|
||||
template<typename TargetStream, class ConsoleMutex>
|
||||
class ansicolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
ansicolor_sink()
|
||||
: target_file_(TargetStream::stream())
|
||||
, mutex_(ConsoleMutex::console_mutex())
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
@ -115,6 +115,18 @@ public:
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
void set_pattern(const std::string &pattern) override SPDLOG_FINAL
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override SPDLOG_FINAL
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
private:
|
||||
void print_ccode_(const std::string &color_code)
|
||||
{
|
||||
@ -132,11 +144,11 @@ private:
|
||||
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
||||
};
|
||||
|
||||
using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout_stream, details::console_global_mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout_stream, details::console_global_nullmutex>;
|
||||
using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout, details::console_mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout, details::console_nullmutex>;
|
||||
|
||||
using ansicolor_stderr_sink_mt = ansicolor_sink<details::console_stderr_stream, details::console_global_mutex>;
|
||||
using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr_stream, details::console_global_nullmutex>;
|
||||
using ansicolor_stderr_sink_mt = ansicolor_sink<details::console_stderr, details::console_mutex>;
|
||||
using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr, details::console_nullmutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template<class Mutex>
|
||||
template<typename Mutex>
|
||||
class base_sink : public sink
|
||||
{
|
||||
public:
|
||||
@ -32,9 +32,7 @@ public:
|
||||
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
fmt::memory_buffer formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
sink_it_(msg, formatted);
|
||||
sink_it_(msg);
|
||||
}
|
||||
|
||||
void flush() SPDLOG_FINAL override
|
||||
@ -43,8 +41,20 @@ public:
|
||||
flush_();
|
||||
}
|
||||
|
||||
void set_pattern(const std::string &pattern) SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) = 0;
|
||||
virtual void sink_it_(const details::log_msg &msg) = 0;
|
||||
virtual void flush_() = 0;
|
||||
Mutex mutex_;
|
||||
};
|
||||
|
@ -17,7 +17,7 @@ namespace sinks {
|
||||
/*
|
||||
* Trivial file sink with single file as target
|
||||
*/
|
||||
template<class Mutex>
|
||||
template<typename Mutex>
|
||||
class basic_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
@ -27,8 +27,10 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
fmt::memory_buffer formatted;
|
||||
sink::formatter_->format(msg, formatted);
|
||||
file_helper_.write(formatted);
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ struct daily_filename_calculator
|
||||
/*
|
||||
* Rotating file sink based on date. rotates at midnight
|
||||
*/
|
||||
template<class Mutex, class FileNameCalc = daily_filename_calculator>
|
||||
template<typename Mutex, typename FileNameCalc = daily_filename_calculator>
|
||||
class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
@ -59,13 +59,16 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
|
||||
if (msg.time >= rotation_tp_)
|
||||
{
|
||||
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time)));
|
||||
rotation_tp_ = next_rotation_tp_();
|
||||
}
|
||||
fmt::memory_buffer formatted;
|
||||
sink::formatter_->format(msg, formatted);
|
||||
file_helper_.write(formatted);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base_sink.h"
|
||||
#include "spdlog/details/log_msg.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
|
||||
@ -19,16 +20,29 @@ namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<typename Mutex>
|
||||
class dist_sink : public sink
|
||||
class dist_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
dist_sink() = default;
|
||||
dist_sink(const dist_sink &) = delete;
|
||||
dist_sink &operator=(const dist_sink &) = delete;
|
||||
|
||||
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
||||
void add_sink(std::shared_ptr<sink> sink)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
sinks_.push_back(sink);
|
||||
}
|
||||
|
||||
void remove_sink(std::shared_ptr<sink> sink)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
|
||||
for (auto &sink : sinks_)
|
||||
{
|
||||
if (sink->should_log(msg.level))
|
||||
@ -38,27 +52,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void flush() SPDLOG_FINAL override
|
||||
void flush_() override
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
for (auto &sink : sinks_)
|
||||
sink->flush();
|
||||
}
|
||||
|
||||
void add_sink(std::shared_ptr<sink> sink)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
sinks_.push_back(sink);
|
||||
}
|
||||
|
||||
void remove_sink(std::shared_ptr<sink> sink)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
|
||||
}
|
||||
|
||||
private:
|
||||
Mutex mutex_;
|
||||
std::vector<std::shared_ptr<sink>> sinks_;
|
||||
};
|
||||
|
||||
|
@ -20,7 +20,7 @@ namespace sinks {
|
||||
/*
|
||||
* MSVC sink (logging using OutputDebugStringA)
|
||||
*/
|
||||
template<class Mutex>
|
||||
template<typename Mutex>
|
||||
class msvc_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
|
@ -13,12 +13,11 @@
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<class Mutex>
|
||||
template<typename Mutex>
|
||||
class null_sink : public base_sink<Mutex>
|
||||
{
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &) override {}
|
||||
|
||||
void sink_it_(const details::log_msg &) override {}
|
||||
void flush_() override {}
|
||||
};
|
||||
|
||||
|
@ -13,8 +13,8 @@
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template<class Mutex>
|
||||
class ostream_sink : public base_sink<Mutex>
|
||||
template<typename Mutex>
|
||||
class ostream_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit ostream_sink(std::ostream &os, bool force_flush = false)
|
||||
@ -26,8 +26,10 @@ public:
|
||||
ostream_sink &operator=(const ostream_sink &) = delete;
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
fmt::memory_buffer formatted;
|
||||
sink::formatter_->format(msg, formatted);
|
||||
ostream_.write(formatted.data(), formatted.size());
|
||||
if (force_flush_)
|
||||
ostream_.flush();
|
||||
|
@ -23,7 +23,7 @@ namespace sinks {
|
||||
//
|
||||
// Rotating file sink based on size
|
||||
//
|
||||
template<class Mutex>
|
||||
template<typename Mutex>
|
||||
class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
@ -55,8 +55,10 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
fmt::memory_buffer formatted;
|
||||
sink::formatter_->format(msg, formatted);
|
||||
current_size_ += formatted.size();
|
||||
if (current_size_ > max_size_)
|
||||
{
|
||||
|
@ -14,16 +14,21 @@ namespace sinks {
|
||||
class sink
|
||||
{
|
||||
public:
|
||||
// default sink ctor with default pattern formatter
|
||||
sink()
|
||||
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter("%+")))
|
||||
: level_(level::trace)
|
||||
, formatter_(new pattern_formatter("%+"))
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~sink() = default;
|
||||
sink(std::unique_ptr<spdlog::pattern_formatter> formatter)
|
||||
: level_(level::trace)
|
||||
, formatter_(std::move(formatter)){};
|
||||
|
||||
virtual ~sink() = default;
|
||||
virtual void log(const details::log_msg &msg) = 0;
|
||||
virtual void flush() = 0;
|
||||
virtual void set_pattern(const std::string &pattern) = 0;
|
||||
virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0;
|
||||
|
||||
bool should_log(level::level_enum msg_level) const
|
||||
{
|
||||
@ -40,18 +45,11 @@ public:
|
||||
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
void set_pattern(const std::string &pattern)
|
||||
{
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
protected:
|
||||
level_t level_{level::trace};
|
||||
// sink log level - default is all
|
||||
level_t level_;
|
||||
|
||||
// sink formatter - default is full format
|
||||
std::unique_ptr<spdlog::formatter> formatter_;
|
||||
};
|
||||
|
||||
|
@ -18,13 +18,13 @@ namespace spdlog {
|
||||
|
||||
namespace sinks {
|
||||
|
||||
template<class TargetStream, class ConsoleMutex>
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
class stdout_sink : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
stdout_sink()
|
||||
: mutex_(ConsoleMutex::console_mutex())
|
||||
: mutex_(ConsoleMutex::mutex())
|
||||
, file_(TargetStream::stream())
|
||||
{
|
||||
}
|
||||
@ -45,7 +45,19 @@ public:
|
||||
void flush() override
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
fflush(TargetStream::stream());
|
||||
fflush(file_);
|
||||
}
|
||||
|
||||
void set_pattern(const std::string &pattern) override SPDLOG_FINAL
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override SPDLOG_FINAL
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -53,11 +65,11 @@ private:
|
||||
FILE *file_;
|
||||
};
|
||||
|
||||
using stdout_sink_mt = stdout_sink<details::console_stdout_stream, details::console_global_mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::console_stdout_stream, details::console_global_nullmutex>;
|
||||
using stdout_sink_mt = stdout_sink<details::console_stdout, details::console_mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::console_stdout, details::console_nullmutex>;
|
||||
|
||||
using stderr_sink_mt = stdout_sink<details::console_stderr_stream, details::console_global_mutex>;
|
||||
using stderr_sink_st = stdout_sink<details::console_stderr_stream, details::console_global_nullmutex>;
|
||||
using stderr_sink_mt = stdout_sink<details::console_stderr, details::console_mutex>;
|
||||
using stderr_sink_st = stdout_sink<details::console_stderr, details::console_nullmutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
|
@ -5,10 +5,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/common.h"
|
||||
|
||||
#include "spdlog/details/log_msg.h"
|
||||
#include "spdlog/sinks/sink.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
@ -21,7 +19,8 @@ namespace sinks {
|
||||
*
|
||||
* Locking is not needed, as `syslog()` itself is thread-safe.
|
||||
*/
|
||||
class syslog_sink : public sink
|
||||
template<typename Mutex>
|
||||
class syslog_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
//
|
||||
@ -48,12 +47,13 @@ public:
|
||||
syslog_sink(const syslog_sink &) = delete;
|
||||
syslog_sink &operator=(const syslog_sink &) = delete;
|
||||
|
||||
void log(const details::log_msg &msg) override
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
::syslog(syslog_prio_from_level(msg), "%s", fmt::to_string(msg.raw).c_str());
|
||||
}
|
||||
|
||||
void flush() override {}
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
std::array<int, 7> priorities_;
|
||||
@ -68,13 +68,23 @@ private:
|
||||
return priorities_[static_cast<size_t>(msg.level)];
|
||||
}
|
||||
};
|
||||
|
||||
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 = default_factory>
|
||||
inline std::shared_ptr<logger> syslog_logger(
|
||||
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 = (1 << 3))
|
||||
{
|
||||
return Factory::template create<sinks::syslog_sink>(logger_name, syslog_ident, syslog_option, syslog_facility);
|
||||
return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility);
|
||||
}
|
||||
|
||||
template<typename Factory = default_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 = (1 << 3))
|
||||
{
|
||||
return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -5,7 +5,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../fmt/fmt.h"
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/console_globals.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
@ -22,7 +21,7 @@ namespace sinks {
|
||||
/*
|
||||
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
||||
*/
|
||||
template<class OutHandle, class ConsoleMutex>
|
||||
template<typename OutHandle, typename ConsoleMutex>
|
||||
class wincolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
@ -35,7 +34,7 @@ public:
|
||||
|
||||
wincolor_sink()
|
||||
: out_handle_(OutHandle::handle())
|
||||
, mutex_(ConsoleMutex::console_mutex())
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
{
|
||||
colors_[level::trace] = WHITE;
|
||||
colors_[level::debug] = CYAN;
|
||||
@ -89,6 +88,18 @@ public:
|
||||
// windows console always flushed?
|
||||
}
|
||||
|
||||
void set_pattern(const std::string &pattern) override SPDLOG_FINAL
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override SPDLOG_FINAL
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
private:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
// set color and return the orig console attributes (for resetting later)
|
||||
@ -116,11 +127,11 @@ private:
|
||||
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
|
||||
};
|
||||
|
||||
using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout_stream, details::console_global_mutex>;
|
||||
using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout_stream, details::console_global_nullmutex>;
|
||||
using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout, details::console_mutex>;
|
||||
using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout, details::console_nullmutex>;
|
||||
|
||||
using wincolor_stderr_sink_mt = wincolor_sink<details::console_stderr_stream, details::console_global_mutex>;
|
||||
using wincolor_stderr_sink_st = wincolor_sink<details::console_stderr_stream, details::console_global_nullmutex>;
|
||||
using wincolor_stderr_sink_mt = wincolor_sink<details::console_stderr, details::console_mutex>;
|
||||
using wincolor_stderr_sink_st = wincolor_sink<details::console_stderr, details::console_nullmutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
@ -5,14 +5,19 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class failing_sink : public spdlog::sinks::sink
|
||||
class failing_sink : public spdlog::sinks::base_sink<std::mutex>
|
||||
{
|
||||
void log(const spdlog::details::log_msg &) override
|
||||
public:
|
||||
failing_sink() = default;
|
||||
~failing_sink() = default;
|
||||
|
||||
protected:
|
||||
void sink_it_(const spdlog::details::log_msg &) override
|
||||
{
|
||||
throw std::runtime_error("some error happened during log");
|
||||
}
|
||||
|
||||
void flush() override
|
||||
void flush_() override
|
||||
{
|
||||
throw std::runtime_error("some error happened during flush");
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &) override
|
||||
void sink_it_(const details::log_msg &) override
|
||||
{
|
||||
msg_counter_++;
|
||||
std::this_thread::sleep_for(delay_);
|
||||
|
Loading…
Reference in New Issue
Block a user