Remove global console mutex (wip)

This commit is contained in:
gabime 2023-10-01 12:20:48 +03:00
parent 2fb5e75950
commit a8efa85b86
20 changed files with 198 additions and 227 deletions

View File

@ -27,6 +27,8 @@ class thread_pool;
class SPDLOG_API registry { class SPDLOG_API registry {
public: public:
using log_levels = std::unordered_map<std::string, level>; using log_levels = std::unordered_map<std::string, level>;
static registry &instance();
registry(const registry &) = delete; registry(const registry &) = delete;
registry &operator=(const registry &) = delete; registry &operator=(const registry &) = delete;
@ -84,8 +86,6 @@ public:
// set levels for all existing/future loggers. global_level can be null if should not set. // set levels for all existing/future loggers. global_level can be null if should not set.
void set_levels(log_levels levels, level *global_level); void set_levels(log_levels levels, level *global_level);
static registry &instance();
void apply_logger_env_levels(std::shared_ptr<logger> new_logger); void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
private: private:
@ -94,7 +94,6 @@ private:
void throw_if_exists_(const std::string &logger_name); void throw_if_exists_(const std::string &logger_name);
void register_logger_(std::shared_ptr<logger> new_logger); void register_logger_(std::shared_ptr<logger> new_logger);
bool set_level_from_cfg_(logger *logger);
std::mutex logger_map_mutex_, flusher_mutex_; std::mutex logger_map_mutex_, flusher_mutex_;
std::recursive_mutex tp_mutex_; std::recursive_mutex tp_mutex_;
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_; std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;

View File

@ -8,8 +8,8 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include "../details/console_globals.h"
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "base_sink.h"
#include "sink.h" #include "sink.h"
namespace spdlog { namespace spdlog {
@ -22,91 +22,48 @@ namespace sinks {
* If no color terminal detected, omit the escape codes. * If no color terminal detected, omit the escape codes.
*/ */
template <typename ConsoleMutex> template <typename Mutex>
class ansicolor_sink : public sink { class ansicolor_sink : public base_sink<Mutex> {
public: public:
using mutex_t = typename ConsoleMutex::mutex_t;
ansicolor_sink(FILE *target_file, color_mode mode); ansicolor_sink(FILE *target_file, color_mode mode);
~ansicolor_sink() override = default;
ansicolor_sink(const ansicolor_sink &other) = delete; ansicolor_sink(const ansicolor_sink &other) = delete;
ansicolor_sink(ansicolor_sink &&other) = delete; ansicolor_sink(ansicolor_sink &&other) = delete;
ansicolor_sink &operator=(const ansicolor_sink &other) = delete; ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
ansicolor_sink &operator=(ansicolor_sink &&other) = delete; ansicolor_sink &operator=(ansicolor_sink &&other) = delete;
~ansicolor_sink() override = default;
void set_color(level color_level, string_view_t color); void set_color(level color_level, string_view_t color);
void set_color_mode(color_mode mode); void set_color_mode(color_mode mode);
bool should_color(); bool should_color();
void log(const details::log_msg &msg) override;
void flush() override;
void set_pattern(const std::string &pattern) final;
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
// Formatting codes
static constexpr string_view_t reset = "\033[m";
static constexpr string_view_t bold = "\033[1m";
static constexpr string_view_t dark = "\033[2m";
static constexpr string_view_t underline = "\033[4m";
static constexpr string_view_t blink = "\033[5m";
static constexpr string_view_t reverse = "\033[7m";
static constexpr string_view_t concealed = "\033[8m";
static constexpr string_view_t clear_line = "\033[K";
// Foreground colors
static constexpr string_view_t black = "\033[30m";
static constexpr string_view_t red = "\033[31m";
static constexpr string_view_t green = "\033[32m";
static constexpr string_view_t yellow = "\033[33m";
static constexpr string_view_t blue = "\033[34m";
static constexpr string_view_t magenta = "\033[35m";
static constexpr string_view_t cyan = "\033[36m";
static constexpr string_view_t white = "\033[37m";
/// Background colors
static constexpr string_view_t on_black = "\033[40m";
static constexpr string_view_t on_red = "\033[41m";
static constexpr string_view_t on_green = "\033[42m";
static constexpr string_view_t on_yellow = "\033[43m";
static constexpr string_view_t on_blue = "\033[44m";
static constexpr string_view_t on_magenta = "\033[45m";
static constexpr string_view_t on_cyan = "\033[46m";
static constexpr string_view_t on_white = "\033[47m";
/// Bold colors
static constexpr string_view_t yellow_bold = "\033[33m\033[1m";
static constexpr string_view_t red_bold = "\033[31m\033[1m";
static constexpr string_view_t bold_on_red = "\033[1m\033[41m";
private: private:
void sink_it_(const details::log_msg &msg) override;
void flush_() override;
FILE *target_file_; FILE *target_file_;
mutex_t &mutex_;
bool should_do_colors_; bool should_do_colors_;
std::unique_ptr<spdlog::formatter> formatter_;
std::array<std::string, levels_count> colors_; std::array<std::string, levels_count> colors_;
void print_ccode_(const string_view_t &color_code); void print_ccode_(const string_view_t &color_code);
void print_range_(const memory_buf_t &formatted, size_t start, size_t end); void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
static std::string to_string_(const string_view_t &sv); static std::string to_string_(const string_view_t &sv);
}; };
template <typename ConsoleMutex> template <typename Mutex>
class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex> { class ansicolor_stdout_sink : public ansicolor_sink<Mutex> {
public: public:
explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic);
}; };
template <typename ConsoleMutex> template <typename Mutex>
class ansicolor_stderr_sink : public ansicolor_sink<ConsoleMutex> { class ansicolor_stderr_sink : public ansicolor_sink<Mutex> {
public: public:
explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic);
}; };
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<details::console_mutex>; using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<std::mutex>;
using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::console_nullmutex>; using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::null_mutex>;
using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<details::console_mutex>; using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<std::mutex>;
using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::console_nullmutex>; using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

View File

@ -27,7 +27,7 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st;
#endif #endif
} // namespace sinks } // namespace sinks
// template instantiations // logger factory functions
template <typename Factory = spdlog::synchronous_factory> template <typename Factory = spdlog::synchronous_factory>
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name,
color_mode mode = color_mode::automatic); color_mode mode = color_mode::automatic);

View File

@ -5,8 +5,8 @@
#include <cstdio> #include <cstdio>
#include "../details/console_globals.h"
#include "../details/synchronous_factory.h" #include "../details/synchronous_factory.h"
#include "base_sink.h"
#include "sink.h" #include "sink.h"
#ifdef _WIN32 #ifdef _WIN32
@ -17,10 +17,9 @@ namespace spdlog {
namespace sinks { namespace sinks {
template <typename ConsoleMutex> template <typename Mutex>
class stdout_sink_base : public sink { class stdout_sink_base : public base_sink<Mutex> {
public: public:
using mutex_t = typename ConsoleMutex::mutex_t;
explicit stdout_sink_base(FILE *file); explicit stdout_sink_base(FILE *file);
~stdout_sink_base() override = default; ~stdout_sink_base() override = default;
@ -30,38 +29,32 @@ public:
stdout_sink_base &operator=(const stdout_sink_base &other) = delete; stdout_sink_base &operator=(const stdout_sink_base &other) = delete;
stdout_sink_base &operator=(stdout_sink_base &&other) = delete; stdout_sink_base &operator=(stdout_sink_base &&other) = delete;
void log(const details::log_msg &msg) override; private:
void flush() override;
void set_pattern(const std::string &pattern) override;
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
protected:
mutex_t &mutex_;
FILE *file_; FILE *file_;
std::unique_ptr<spdlog::formatter> formatter_; void sink_it_(const details::log_msg &msg) override;
void flush_() override;
#ifdef _WIN32 #ifdef _WIN32
HANDLE handle_; HANDLE handle_;
#endif // WIN32 #endif // WIN32
}; };
template <typename ConsoleMutex> template <typename Mutex>
class stdout_sink : public stdout_sink_base<ConsoleMutex> { class stdout_sink : public stdout_sink_base<Mutex> {
public: public:
stdout_sink(); stdout_sink();
}; };
template <typename ConsoleMutex> template <typename Mutex>
class stderr_sink : public stdout_sink_base<ConsoleMutex> { class stderr_sink : public stdout_sink_base<Mutex> {
public: public:
stderr_sink(); stderr_sink();
}; };
using stdout_sink_mt = stdout_sink<details::console_mutex>; using stdout_sink_mt = stdout_sink<std::mutex>;
using stdout_sink_st = stdout_sink<details::console_nullmutex>; using stdout_sink_st = stdout_sink<details::null_mutex>;
using stderr_sink_mt = stderr_sink<details::console_mutex>; using stderr_sink_mt = stderr_sink<std::mutex>;
using stderr_sink_st = stderr_sink<details::console_nullmutex>; using stderr_sink_st = stderr_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

View File

@ -10,7 +10,6 @@
#include <string> #include <string>
#include "../common.h" #include "../common.h"
#include "../details/console_globals.h"
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "sink.h" #include "sink.h"

View File

@ -12,6 +12,7 @@
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex>
#include <string> #include <string>
#include "common.h" #include "common.h"

View File

@ -3,17 +3,55 @@
#include "spdlog/sinks/ansicolor_sink.h" #include "spdlog/sinks/ansicolor_sink.h"
#include <mutex>
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include "spdlog/pattern_formatter.h" #include "spdlog/pattern_formatter.h"
namespace {
// Formatting codes
constexpr spdlog::string_view_t reset = "\033[m";
constexpr spdlog::string_view_t bold = "\033[1m";
constexpr spdlog::string_view_t dark = "\033[2m";
constexpr spdlog::string_view_t underline = "\033[4m";
constexpr spdlog::string_view_t blink = "\033[5m";
constexpr spdlog::string_view_t reverse = "\033[7m";
constexpr spdlog::string_view_t concealed = "\033[8m";
constexpr spdlog::string_view_t clear_line = "\033[K";
// Foreground colors
constexpr spdlog::string_view_t black = "\033[30m";
constexpr spdlog::string_view_t red = "\033[31m";
constexpr spdlog::string_view_t green = "\033[32m";
constexpr spdlog::string_view_t yellow = "\033[33m";
constexpr spdlog::string_view_t blue = "\033[34m";
constexpr spdlog::string_view_t magenta = "\033[35m";
constexpr spdlog::string_view_t cyan = "\033[36m";
constexpr spdlog::string_view_t white = "\033[37m";
// Background colors
static constexpr spdlog::string_view_t on_black = "\033[40m";
constexpr spdlog::string_view_t on_red = "\033[41m";
constexpr spdlog::string_view_t on_green = "\033[42m";
constexpr spdlog::string_view_t on_yellow = "\033[43m";
constexpr spdlog::string_view_t on_blue = "\033[44m";
constexpr spdlog::string_view_t on_magenta = "\033[45m";
constexpr spdlog::string_view_t on_cyan = "\033[46m";
constexpr spdlog::string_view_t on_white = "\033[47m";
// Bold colors
constexpr spdlog::string_view_t yellow_bold = "\033[33m\033[1m";
constexpr spdlog::string_view_t red_bold = "\033[31m\033[1m";
constexpr spdlog::string_view_t bold_on_red = "\033[1m\033[41m";
} // namespace
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template <typename ConsoleMutex> template <typename Mutex>
ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode) ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode)
: target_file_(target_file), : target_file_(target_file)
mutex_(ConsoleMutex::mutex()),
formatter_(std::make_unique<spdlog::pattern_formatter>())
{ {
set_color_mode(mode); set_color_mode(mode);
@ -26,63 +64,19 @@ ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
colors_.at(level_to_number(level::off)) = to_string_(reset); colors_.at(level_to_number(level::off)) = to_string_(reset);
} }
template <typename ConsoleMutex> template <typename Mutex>
void ansicolor_sink<ConsoleMutex>::set_color(level color_level, string_view_t color) { void ansicolor_sink<Mutex>::set_color(level color_level, string_view_t color) {
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
colors_.at(level_to_number(color_level)) = to_string_(color); colors_.at(level_to_number(color_level)) = to_string_(color);
} }
template <typename ConsoleMutex> template <typename Mutex>
void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg) { bool ansicolor_sink<Mutex>::should_color() {
// Wrap the originally formatted message in color codes.
// If color is not supported in the terminal, log as is instead.
std::lock_guard<mutex_t> lock(mutex_);
msg.color_range_start = 0;
msg.color_range_end = 0;
memory_buf_t formatted;
formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
print_ccode_(colors_.at(level_to_number(msg.log_level)));
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset);
// after color range
print_range_(formatted, msg.color_range_end, formatted.size());
} else // no color
{
print_range_(formatted, 0, formatted.size());
}
fflush(target_file_);
}
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::flush() {
std::lock_guard<mutex_t> lock(mutex_);
fflush(target_file_);
}
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_formatter(
std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
template <typename ConsoleMutex>
bool ansicolor_sink<ConsoleMutex>::should_color() {
return should_do_colors_; return should_do_colors_;
} }
template <typename ConsoleMutex> template <typename Mutex>
void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) { void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
switch (mode) { switch (mode) {
case color_mode::always: case color_mode::always:
should_do_colors_ = true; should_do_colors_ = true;
@ -99,39 +93,67 @@ void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
} }
} }
template <typename ConsoleMutex> template <typename Mutex>
void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) { void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
// Wrap the originally formatted message in color codes.
// If color is not supported in the terminal, log as is instead.
msg.color_range_start = 0;
msg.color_range_end = 0;
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
print_ccode_(colors_.at(level_to_number(msg.log_level)));
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset);
// after color range
print_range_(formatted, msg.color_range_end, formatted.size());
} else // no color
{
print_range_(formatted, 0, formatted.size());
}
fflush(target_file_);
}
template <typename Mutex>
void ansicolor_sink<Mutex>::flush_() {
fflush(target_file_);
}
template <typename Mutex>
void ansicolor_sink<Mutex>::print_ccode_(const string_view_t &color_code) {
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
} }
template <typename ConsoleMutex> template <typename Mutex>
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, void ansicolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
size_t start,
size_t end) {
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
} }
template <typename ConsoleMutex> template <typename Mutex>
std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv) { std::string ansicolor_sink<Mutex>::to_string_(const string_view_t &sv) {
return {sv.data(), sv.size()}; return {sv.data(), sv.size()};
} }
// ansicolor_stdout_sink // ansicolor_stdout_sink
template <typename ConsoleMutex> template <typename Mutex>
ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode) ansicolor_stdout_sink<Mutex>::ansicolor_stdout_sink(color_mode mode)
: ansicolor_sink<ConsoleMutex>(stdout, mode) {} : ansicolor_sink<Mutex>(stdout, mode) {}
// ansicolor_stderr_sink // ansicolor_stderr_sink
template <typename ConsoleMutex> template <typename Mutex>
ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode) ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
: ansicolor_sink<ConsoleMutex>(stderr, mode) {} : ansicolor_sink<Mutex>(stderr, mode) {}
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
// template instantiations // template instantiations
template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::console_mutex>; template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<std::mutex>;
template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::console_nullmutex>; template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::null_mutex>;
template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_mutex>; template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<std::mutex>;
template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_nullmutex>; template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::null_mutex>;

View File

@ -4,7 +4,6 @@
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/os.h"
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {

View File

@ -26,11 +26,9 @@ namespace spdlog {
namespace sinks { namespace sinks {
template <typename ConsoleMutex> template <typename Mutex>
stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file) stdout_sink_base<Mutex>::stdout_sink_base(FILE *file)
: mutex_(ConsoleMutex::mutex()), : file_(file) {
file_(file),
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
#ifdef _WIN32 #ifdef _WIN32
// get windows handle from the FILE* object // get windows handle from the FILE* object
@ -45,15 +43,15 @@ stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
#endif // WIN32 #endif // WIN32
} }
template <typename ConsoleMutex> template <typename Mutex>
void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) { void stdout_sink_base<Mutex>::sink_it_(const details::log_msg &msg) {
#ifdef _WIN32 #ifdef _WIN32
if (handle_ == INVALID_HANDLE_VALUE) { if (handle_ == INVALID_HANDLE_VALUE) {
return; return;
} }
std::lock_guard<mutex_t> lock(mutex_);
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->formatter_->format(msg, formatted);
auto size = static_cast<DWORD>(formatted.size()); auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0; DWORD bytes_written = 0;
bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
@ -62,42 +60,27 @@ void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
std::to_string(::GetLastError())); std::to_string(::GetLastError()));
} }
#else #else
std::lock_guard<mutex_t> lock(mutex_);
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_); ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
#endif // WIN32 #endif // WIN32
::fflush(file_); // flush every line to terminal ::fflush(file_); // flush every line to terminal
} }
template <typename ConsoleMutex> template <typename Mutex>
void stdout_sink_base<ConsoleMutex>::flush() { void stdout_sink_base<Mutex>::flush_() {
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_); fflush(file_);
} }
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_formatter(
std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
// stdout sink // stdout sink
template <typename ConsoleMutex> template <typename Mutex>
stdout_sink<ConsoleMutex>::stdout_sink() stdout_sink<Mutex>::stdout_sink()
: stdout_sink_base<ConsoleMutex>(stdout) {} : stdout_sink_base<Mutex>(stdout) {}
// stderr sink // stderr sink
template <typename ConsoleMutex> template <typename Mutex>
stderr_sink<ConsoleMutex>::stderr_sink() stderr_sink<Mutex>::stderr_sink()
: stdout_sink_base<ConsoleMutex>(stderr) {} : stdout_sink_base<Mutex>(stderr) {}
} // namespace sinks } // namespace sinks
@ -124,13 +107,12 @@ std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
} // namespace spdlog } // namespace spdlog
// template instantiations for stdout/stderr loggers // template instantiations for stdout/stderr loggers
#include "spdlog/details/console_globals.h" template class SPDLOG_API spdlog::sinks::stdout_sink_base<std::mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::console_mutex>; template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::console_nullmutex>; template class SPDLOG_API spdlog::sinks::stdout_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::console_mutex>; template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::console_nullmutex>; template class SPDLOG_API spdlog::sinks::stderr_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_mutex>; template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_nullmutex>;
// template instantiations for stdout/stderr factory functions // template instantiations for stdout/stderr factory functions
#include "spdlog/async.h" #include "spdlog/async.h"

View File

@ -2,7 +2,7 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#ifdef _WIN32 #ifdef _WIN32
// clang-format off // clang-format off
#include "spdlog/details/windows_include.h" #include "spdlog/details/windows_include.h"
#include "spdlog/sinks/wincolor_sink.h" #include "spdlog/sinks/wincolor_sink.h"
#include "spdlog/common.h" #include "spdlog/common.h"

View File

@ -3,8 +3,12 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <memory>
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/logger.h"
#include "spdlog/pattern_formatter.h" #include "spdlog/pattern_formatter.h"
#include "spdlog/sinks/stdout_color_sinks.h"
namespace spdlog { namespace spdlog {
@ -22,7 +26,7 @@ void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
void set_pattern(std::string pattern, pattern_time_type time_type) { void set_pattern(std::string pattern, pattern_time_type time_type) {
set_formatter( set_formatter(
std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type))); std::make_unique<spdlog::pattern_formatter>(std::move(pattern), time_type));
} }
level get_level() { return default_logger_raw()->log_level(); } level get_level() { return default_logger_raw()->log_level(); }
@ -68,5 +72,4 @@ void set_default_logger(std::shared_ptr<spdlog::logger> default_logger) {
void apply_logger_env_levels(std::shared_ptr<logger> logger) { void apply_logger_env_levels(std::shared_ptr<logger> logger) {
details::registry::instance().apply_logger_env_levels(std::move(logger)); details::registry::instance().apply_logger_env_levels(std::move(logger));
} }
} // namespace spdlog } // namespace spdlog

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"
#include "spdlog/async.h" #include "spdlog/async.h"

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"
#include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/daily_file_sink.h"

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include <iostream> #include <iostream>

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"
#include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h"

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"

View File

@ -14,8 +14,8 @@
#include "spdlog/sinks/udp_sink.h" #include "spdlog/sinks/udp_sink.h"
#ifdef _WIN32 #ifdef _WIN32
#include "spdlog/sinks/win_eventlog_sink.h"
#include "spdlog/sinks/msvc_sink.h" #include "spdlog/sinks/msvc_sink.h"
#include "spdlog/sinks/win_eventlog_sink.h"
#else #else
#include "spdlog/sinks/syslog_sink.h" #include "spdlog/sinks/syslog_sink.h"
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"

View File

@ -1,48 +1,59 @@
/* /*
* This content is released under the MIT License as specified in * This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE * https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
*/ */
#include "includes.h" #include "includes.h"
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/stdout_sinks.h" #include "spdlog/sinks/stdout_sinks.h"
TEST_CASE("stdout_st", "[stdout]") { TEST_CASE("stdout_st", "[stdout]") {
spdlog::set_pattern("%+");
auto l = spdlog::stdout_logger_st("test"); auto l = spdlog::stdout_logger_st("test");
l->set_pattern("%+");
l->set_level(spdlog::level::trace); l->set_level(spdlog::level::trace);
l->trace("Test stdout_st"); l->trace("Test stdout_st");
spdlog::drop_all(); l->debug("Test stdout_st");
} l->info("Test stdout_st");
l->warn("Test stdout_st");
TEST_CASE("stdout_mt", "[stdout]") { l->error("Test stdout_st");
auto l = spdlog::stdout_logger_mt("test"); l->critical("Test stdout_st");
l->set_pattern("%+");
l->set_level(spdlog::level::debug);
l->debug("Test stdout_mt");
spdlog::drop_all(); spdlog::drop_all();
} }
TEST_CASE("stderr_st", "[stderr]") { TEST_CASE("stderr_st", "[stderr]") {
auto l = spdlog::stderr_logger_st("test"); auto l = spdlog::stderr_logger_st("test");
l->set_pattern("%+"); l->set_level(spdlog::level::trace);
l->trace("Test stderr_st");
l->debug("Test stderr_st");
l->info("Test stderr_st"); l->info("Test stderr_st");
l->warn("Test stderr_st");
l->error("Test stderr_st");
l->critical("Test stderr_st");
spdlog::drop_all(); spdlog::drop_all();
} }
TEST_CASE("stdout_mt", "[stdout]") {
auto sink = std::make_shared<spdlog::sinks::stdout_sink_mt>();
spdlog::logger logger("logger", sink);
logger.debug("Test stdout_mt should not be displayed !!!!");
}
TEST_CASE("stderr_mt", "[stderr]") { TEST_CASE("stderr_mt", "[stderr]") {
auto l = spdlog::stderr_logger_mt("test"); auto sink = std::make_shared<spdlog::sinks::stderr_sink_mt>();
l->set_pattern("%+"); spdlog::logger logger("logger", sink);
l->info("Test stderr_mt"); logger.debug("Test stderr_mt should not be displayed !!!!");
l->warn("Test stderr_mt");
l->error("Test stderr_mt");
l->critical("Test stderr_mt");
spdlog::drop_all();
} }
// color loggers // color loggers
TEST_CASE("stdout_color_st", "[stdout]") { TEST_CASE("stdout_color_st", "[stdout]") {
auto l = spdlog::stdout_color_st("test"); auto l = spdlog::stdout_color_st("test");
l->set_pattern("%+"); l->set_pattern("%+");
l->set_level(spdlog::level::trace);
l->trace("Test stdout_color_st");
l->debug("Test stdout_color_st");
l->info("Test stdout_color_st"); l->info("Test stdout_color_st");
l->warn("Test stdout_color_st");
l->error("Test stdout_color_st");
l->critical("Test stdout_color_st");
spdlog::drop_all(); spdlog::drop_all();
} }
@ -51,6 +62,11 @@ TEST_CASE("stdout_color_mt", "[stdout]") {
l->set_pattern("%+"); l->set_pattern("%+");
l->set_level(spdlog::level::trace); l->set_level(spdlog::level::trace);
l->trace("Test stdout_color_mt"); l->trace("Test stdout_color_mt");
l->debug("Test stdout_color_mt");
l->info("Test stdout_color_mt");
l->warn("Test stdout_color_mt");
l->error("Test stdout_color_mt");
l->critical("Test stdout_color_mt");
spdlog::drop_all(); spdlog::drop_all();
} }