mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 10:29:02 +08:00
Remove global console mutex (wip)
This commit is contained in:
@@ -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"
|
||||
|
Reference in New Issue
Block a user