mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 18:11:33 +08:00
Remove global console mutex (wip)
This commit is contained in:
parent
2fb5e75950
commit
a8efa85b86
@ -27,6 +27,8 @@ class thread_pool;
|
||||
class SPDLOG_API registry {
|
||||
public:
|
||||
using log_levels = std::unordered_map<std::string, level>;
|
||||
|
||||
static registry &instance();
|
||||
registry(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.
|
||||
void set_levels(log_levels levels, level *global_level);
|
||||
|
||||
static registry &instance();
|
||||
|
||||
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
|
||||
|
||||
private:
|
||||
@ -94,7 +94,6 @@ private:
|
||||
|
||||
void throw_if_exists_(const std::string &logger_name);
|
||||
void register_logger_(std::shared_ptr<logger> new_logger);
|
||||
bool set_level_from_cfg_(logger *logger);
|
||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||
std::recursive_mutex tp_mutex_;
|
||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||
|
@ -8,8 +8,8 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "../details/console_globals.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
#include "sink.h"
|
||||
|
||||
namespace spdlog {
|
||||
@ -22,91 +22,48 @@ namespace sinks {
|
||||
* If no color terminal detected, omit the escape codes.
|
||||
*/
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
class ansicolor_sink : public sink {
|
||||
template <typename Mutex>
|
||||
class ansicolor_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
ansicolor_sink(FILE *target_file, color_mode mode);
|
||||
~ansicolor_sink() override = default;
|
||||
|
||||
ansicolor_sink(const ansicolor_sink &other) = delete;
|
||||
ansicolor_sink(ansicolor_sink &&other) = delete;
|
||||
|
||||
ansicolor_sink &operator=(const 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_mode(color_mode mode);
|
||||
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:
|
||||
void sink_it_(const details::log_msg &msg) override;
|
||||
void flush_() override;
|
||||
FILE *target_file_;
|
||||
mutex_t &mutex_;
|
||||
bool should_do_colors_;
|
||||
std::unique_ptr<spdlog::formatter> formatter_;
|
||||
std::array<std::string, levels_count> colors_;
|
||||
void print_ccode_(const string_view_t &color_code);
|
||||
void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
|
||||
static std::string to_string_(const string_view_t &sv);
|
||||
};
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex> {
|
||||
template <typename Mutex>
|
||||
class ansicolor_stdout_sink : public ansicolor_sink<Mutex> {
|
||||
public:
|
||||
explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic);
|
||||
};
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
class ansicolor_stderr_sink : public ansicolor_sink<ConsoleMutex> {
|
||||
template <typename Mutex>
|
||||
class ansicolor_stderr_sink : public ansicolor_sink<Mutex> {
|
||||
public:
|
||||
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_st = ansicolor_stdout_sink<details::console_nullmutex>;
|
||||
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<std::mutex>;
|
||||
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_st = ansicolor_stderr_sink<details::console_nullmutex>;
|
||||
using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<std::mutex>;
|
||||
using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
@ -27,7 +27,7 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st;
|
||||
#endif
|
||||
} // namespace sinks
|
||||
|
||||
// template instantiations
|
||||
// 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);
|
||||
|
@ -5,8 +5,8 @@
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "../details/console_globals.h"
|
||||
#include "../details/synchronous_factory.h"
|
||||
#include "base_sink.h"
|
||||
#include "sink.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
@ -17,10 +17,9 @@ namespace spdlog {
|
||||
|
||||
namespace sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
class stdout_sink_base : public sink {
|
||||
template <typename Mutex>
|
||||
class stdout_sink_base : public base_sink<Mutex> {
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
explicit stdout_sink_base(FILE *file);
|
||||
~stdout_sink_base() override = default;
|
||||
|
||||
@ -30,38 +29,32 @@ public:
|
||||
stdout_sink_base &operator=(const stdout_sink_base &other) = delete;
|
||||
stdout_sink_base &operator=(stdout_sink_base &&other) = delete;
|
||||
|
||||
void log(const details::log_msg &msg) override;
|
||||
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_;
|
||||
private:
|
||||
FILE *file_;
|
||||
std::unique_ptr<spdlog::formatter> formatter_;
|
||||
void sink_it_(const details::log_msg &msg) override;
|
||||
void flush_() override;
|
||||
#ifdef _WIN32
|
||||
HANDLE handle_;
|
||||
#endif // WIN32
|
||||
};
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
class stdout_sink : public stdout_sink_base<ConsoleMutex> {
|
||||
template <typename Mutex>
|
||||
class stdout_sink : public stdout_sink_base<Mutex> {
|
||||
public:
|
||||
stdout_sink();
|
||||
};
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
class stderr_sink : public stdout_sink_base<ConsoleMutex> {
|
||||
template <typename Mutex>
|
||||
class stderr_sink : public stdout_sink_base<Mutex> {
|
||||
public:
|
||||
stderr_sink();
|
||||
};
|
||||
|
||||
using stdout_sink_mt = stdout_sink<details::console_mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::console_nullmutex>;
|
||||
using stdout_sink_mt = stdout_sink<std::mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::null_mutex>;
|
||||
|
||||
using stderr_sink_mt = stderr_sink<details::console_mutex>;
|
||||
using stderr_sink_st = stderr_sink<details::console_nullmutex>;
|
||||
using stderr_sink_mt = stderr_sink<std::mutex>;
|
||||
using stderr_sink_st = stderr_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
|
@ -10,7 +10,6 @@
|
||||
#include <string>
|
||||
|
||||
#include "../common.h"
|
||||
#include "../details/console_globals.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "sink.h"
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "common.h"
|
||||
|
@ -3,17 +3,55 @@
|
||||
|
||||
#include "spdlog/sinks/ansicolor_sink.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/os.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 sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
||||
: target_file_(target_file),
|
||||
mutex_(ConsoleMutex::mutex()),
|
||||
formatter_(std::make_unique<spdlog::pattern_formatter>())
|
||||
template <typename Mutex>
|
||||
ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
||||
: target_file_(target_file)
|
||||
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::set_color(level color_level, string_view_t color) {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
template <typename Mutex>
|
||||
void ansicolor_sink<Mutex>::set_color(level color_level, string_view_t color) {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
colors_.at(level_to_number(color_level)) = to_string_(color);
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::log(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.
|
||||
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() {
|
||||
template <typename Mutex>
|
||||
bool ansicolor_sink<Mutex>::should_color() {
|
||||
return should_do_colors_;
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
|
||||
template <typename Mutex>
|
||||
void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
||||
switch (mode) {
|
||||
case color_mode::always:
|
||||
should_do_colors_ = true;
|
||||
@ -99,39 +93,67 @@ void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) {
|
||||
template <typename Mutex>
|
||||
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_);
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
|
||||
size_t start,
|
||||
size_t end) {
|
||||
template <typename Mutex>
|
||||
void ansicolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv) {
|
||||
template <typename Mutex>
|
||||
std::string ansicolor_sink<Mutex>::to_string_(const string_view_t &sv) {
|
||||
return {sv.data(), sv.size()};
|
||||
}
|
||||
|
||||
// ansicolor_stdout_sink
|
||||
template <typename ConsoleMutex>
|
||||
ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
|
||||
: ansicolor_sink<ConsoleMutex>(stdout, mode) {}
|
||||
template <typename Mutex>
|
||||
ansicolor_stdout_sink<Mutex>::ansicolor_stdout_sink(color_mode mode)
|
||||
: ansicolor_sink<Mutex>(stdout, mode) {}
|
||||
|
||||
// ansicolor_stderr_sink
|
||||
template <typename ConsoleMutex>
|
||||
ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode)
|
||||
: ansicolor_sink<ConsoleMutex>(stderr, mode) {}
|
||||
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 SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::console_mutex>;
|
||||
template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::console_nullmutex>;
|
||||
template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<std::mutex>;
|
||||
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<spdlog::details::console_nullmutex>;
|
||||
template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<std::mutex>;
|
||||
template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::null_mutex>;
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/os.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
@ -26,11 +26,9 @@ namespace spdlog {
|
||||
|
||||
namespace sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
|
||||
: mutex_(ConsoleMutex::mutex()),
|
||||
file_(file),
|
||||
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
|
||||
template <typename Mutex>
|
||||
stdout_sink_base<Mutex>::stdout_sink_base(FILE *file)
|
||||
: file_(file) {
|
||||
#ifdef _WIN32
|
||||
// get windows handle from the FILE* object
|
||||
|
||||
@ -45,15 +43,15 @@ stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
|
||||
#endif // WIN32
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
|
||||
template <typename Mutex>
|
||||
void stdout_sink_base<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||
#ifdef _WIN32
|
||||
if (handle_ == INVALID_HANDLE_VALUE) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
|
||||
memory_buf_t formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
base_sink<Mutex>::formatter_->formatter_->format(msg, formatted);
|
||||
auto size = static_cast<DWORD>(formatted.size());
|
||||
DWORD bytes_written = 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()));
|
||||
}
|
||||
#else
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
memory_buf_t formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
|
||||
#endif // WIN32
|
||||
::fflush(file_); // flush every line to terminal
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void stdout_sink_base<ConsoleMutex>::flush() {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
template <typename Mutex>
|
||||
void stdout_sink_base<Mutex>::flush_() {
|
||||
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
|
||||
template <typename ConsoleMutex>
|
||||
stdout_sink<ConsoleMutex>::stdout_sink()
|
||||
: stdout_sink_base<ConsoleMutex>(stdout) {}
|
||||
template <typename Mutex>
|
||||
stdout_sink<Mutex>::stdout_sink()
|
||||
: stdout_sink_base<Mutex>(stdout) {}
|
||||
|
||||
// stderr sink
|
||||
template <typename ConsoleMutex>
|
||||
stderr_sink<ConsoleMutex>::stderr_sink()
|
||||
: stdout_sink_base<ConsoleMutex>(stderr) {}
|
||||
template <typename Mutex>
|
||||
stderr_sink<Mutex>::stderr_sink()
|
||||
: stdout_sink_base<Mutex>(stderr) {}
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
@ -124,13 +107,12 @@ std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
|
||||
} // namespace spdlog
|
||||
|
||||
// template instantiations for stdout/stderr loggers
|
||||
#include "spdlog/details/console_globals.h"
|
||||
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::console_mutex>;
|
||||
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::console_nullmutex>;
|
||||
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::console_mutex>;
|
||||
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::console_nullmutex>;
|
||||
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_mutex>;
|
||||
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_nullmutex>;
|
||||
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"
|
||||
|
@ -2,7 +2,7 @@
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
#ifdef _WIN32
|
||||
|
||||
// clang-format off
|
||||
// clang-format off
|
||||
#include "spdlog/details/windows_include.h"
|
||||
#include "spdlog/sinks/wincolor_sink.h"
|
||||
#include "spdlog/common.h"
|
||||
|
@ -3,8 +3,12 @@
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/logger.h"
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
|
||||
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) {
|
||||
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(); }
|
||||
@ -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) {
|
||||
details::registry::instance().apply_logger_env_levels(std::move(logger));
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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 "spdlog/async.h"
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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 "spdlog/sinks/daily_file_sink.h"
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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 "spdlog/sinks/rotating_file_sink.h"
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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 "spdlog/sinks/basic_file_sink.h"
|
||||
|
@ -14,8 +14,8 @@
|
||||
#include "spdlog/sinks/udp_sink.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "spdlog/sinks/win_eventlog_sink.h"
|
||||
#include "spdlog/sinks/msvc_sink.h"
|
||||
#include "spdlog/sinks/win_eventlog_sink.h"
|
||||
#else
|
||||
#include "spdlog/sinks/syslog_sink.h"
|
||||
#endif
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* 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"
|
||||
|
@ -1,48 +1,59 @@
|
||||
/*
|
||||
* 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 "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/sinks/stdout_sinks.h"
|
||||
|
||||
TEST_CASE("stdout_st", "[stdout]") {
|
||||
spdlog::set_pattern("%+");
|
||||
auto l = spdlog::stdout_logger_st("test");
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::trace);
|
||||
l->trace("Test stdout_st");
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stdout_mt", "[stdout]") {
|
||||
auto l = spdlog::stdout_logger_mt("test");
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::debug);
|
||||
l->debug("Test stdout_mt");
|
||||
l->debug("Test stdout_st");
|
||||
l->info("Test stdout_st");
|
||||
l->warn("Test stdout_st");
|
||||
l->error("Test stdout_st");
|
||||
l->critical("Test stdout_st");
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stderr_st", "[stderr]") {
|
||||
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->warn("Test stderr_st");
|
||||
l->error("Test stderr_st");
|
||||
l->critical("Test stderr_st");
|
||||
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]") {
|
||||
auto l = spdlog::stderr_logger_mt("test");
|
||||
l->set_pattern("%+");
|
||||
l->info("Test stderr_mt");
|
||||
l->warn("Test stderr_mt");
|
||||
l->error("Test stderr_mt");
|
||||
l->critical("Test stderr_mt");
|
||||
spdlog::drop_all();
|
||||
auto sink = std::make_shared<spdlog::sinks::stderr_sink_mt>();
|
||||
spdlog::logger logger("logger", sink);
|
||||
logger.debug("Test stderr_mt should not be displayed !!!!");
|
||||
}
|
||||
|
||||
// color loggers
|
||||
TEST_CASE("stdout_color_st", "[stdout]") {
|
||||
auto l = spdlog::stdout_color_st("test");
|
||||
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->warn("Test stdout_color_st");
|
||||
l->error("Test stdout_color_st");
|
||||
l->critical("Test stdout_color_st");
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
@ -51,6 +62,11 @@ TEST_CASE("stdout_color_mt", "[stdout]") {
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::trace);
|
||||
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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user