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

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

View File

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

View File

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

View File

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