Merge remote-tracking branch 'origin/remove-statics' into remove-statics
Some checks failed
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[asan:ON build_type:Debug … (push) Has been cancelled
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Debug compiler… (push) Has been cancelled
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Release compil… (push) Has been cancelled
linux / OS X Clang (C++11, Release) (push) Has been cancelled
macos / macOS Clang (C++11, Release) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022 USE_STD_FORMAT:ON WCHAR:OFF WCHAR_FILES:OFF]) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022 USE_STD_FORMAT:ON WCHAR:ON WCHAR_FILES:ON]) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:11 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:14 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019 USE_STD_FORMAT:OFF WCHAR:OFF WCHAR_FILES:OFF]) (push) Has been cancelled

This commit is contained in:
gabime 2024-11-10 23:54:11 +02:00
commit fceb33fa86
4 changed files with 71 additions and 79 deletions

3
.gitignore vendored
View File

@ -96,3 +96,6 @@ cmake-build-*/
/.vs /.vs
/out/build /out/build
/CMakeSettings.json /CMakeSettings.json
/build-2019/x64-Debug-vs2019
/fmt
/src/fmt

View File

@ -15,11 +15,9 @@
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template <typename ConsoleMutex> template <typename Mutex>
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode) SPDLOG_INLINE wincolor_sink<Mutex>::wincolor_sink(void *out_handle, color_mode mode)
: out_handle_(out_handle), : out_handle_(out_handle), console_mutex_(nullptr) {
mutex_(ConsoleMutex::mutex()),
formatter_(details::make_unique<spdlog::pattern_formatter>()) {
set_color_mode_impl(mode); set_color_mode_impl(mode);
// set level colors // set level colors
colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
@ -34,30 +32,38 @@ SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color
colors_[level::off] = 0; colors_[level::off] = 0;
} }
template <typename ConsoleMutex> template <typename Mutex>
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink() { SPDLOG_INLINE wincolor_sink<Mutex>::~wincolor_sink() {
this->flush(); this->flush();
} }
// change the color for the given level // change the color for the given level
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, void SPDLOG_INLINE wincolor_sink<Mutex>::set_color(level::level_enum level,
std::uint16_t color) { std::uint16_t color) {
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<Mutex> lock(base_t::mutex_);
colors_[static_cast<size_t>(level)] = color; colors_[static_cast<size_t>(level)] = color;
} }
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) { void SPDLOG_INLINE wincolor_sink<Mutex>::set_console_mutex(std::mutex* mutex) {
std::lock_guard<Mutex> lock(base_t::mutex_);
console_mutex_ = mutex;
}
template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) { if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) {
return; return;
} }
std::lock_guard<mutex_t> lock(mutex_);
msg.color_range_start = 0; msg.color_range_start = 0;
msg.color_range_end = 0; msg.color_range_end = 0;
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); base_t::formatter_->format(msg, formatted);
auto console_lock = console_mutex_ != nullptr ? std::unique_lock<std::mutex>(*console_mutex_)
: std::unique_lock<std::mutex>();
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range // before color range
print_range_(formatted, 0, msg.color_range_start); print_range_(formatted, 0, msg.color_range_start);
@ -74,32 +80,19 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
} }
} }
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush() { void SPDLOG_INLINE wincolor_sink<Mutex>::flush_() {
// windows console always flushed? // windows console always flushed?
} }
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) { void SPDLOG_INLINE wincolor_sink<Mutex>::set_color_mode(color_mode mode) {
std::lock_guard<mutex_t> lock(mutex_); std::lock_guard<Mutex> lock(base_t::mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template <typename ConsoleMutex>
void SPDLOG_INLINE
wincolor_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>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
std::lock_guard<mutex_t> lock(mutex_);
set_color_mode_impl(mode); set_color_mode_impl(mode);
} }
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode) { void SPDLOG_INLINE wincolor_sink<Mutex>::set_color_mode_impl(color_mode mode) {
if (mode == color_mode::automatic) { if (mode == color_mode::automatic) {
// should do colors only if out_handle_ points to actual console. // should do colors only if out_handle_ points to actual console.
DWORD console_mode; DWORD console_mode;
@ -111,9 +104,9 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode m
} }
// set foreground color and return the orig console attributes (for resetting later) // set foreground color and return the orig console attributes (for resetting later)
template <typename ConsoleMutex> template <typename Mutex>
std::uint16_t SPDLOG_INLINE std::uint16_t SPDLOG_INLINE
wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs) { wincolor_sink<Mutex>::set_foreground_color_(std::uint16_t attribs) {
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info)) { if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info)) {
// just return white if failed getting console info // just return white if failed getting console info
@ -129,8 +122,8 @@ wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs) {
} }
// print a range of formatted message to console // print a range of formatted message to console
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, void SPDLOG_INLINE wincolor_sink<Mutex>::print_range_(const memory_buf_t &formatted,
size_t start, size_t start,
size_t end) { size_t end) {
if (end > start) { if (end > start) {
@ -150,8 +143,8 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t
} }
} }
template <typename ConsoleMutex> template <typename Mutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted) { void SPDLOG_INLINE wincolor_sink<Mutex>::write_to_file_(const memory_buf_t &formatted) {
auto size = static_cast<DWORD>(formatted.size()); auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0; DWORD bytes_written = 0;
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size,
@ -160,13 +153,13 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_
} }
// wincolor_stdout_sink // wincolor_stdout_sink
template <typename ConsoleMutex> template <typename Mutex>
SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode) SPDLOG_INLINE wincolor_stdout_sink<Mutex>::wincolor_stdout_sink(color_mode mode)
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} : wincolor_sink<Mutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {}
// wincolor_stderr_sink // wincolor_stderr_sink
template <typename ConsoleMutex> template <typename Mutex>
SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode) SPDLOG_INLINE wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode)
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {} : wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

View File

@ -4,15 +4,12 @@
#pragma once #pragma once
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/details/console_globals.h> #include <spdlog/sinks/base_sink.h>
#include <spdlog/details/null_mutex.h> #include <spdlog/details/null_mutex.h>
#include <spdlog/sinks/sink.h>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <memory>
#include <mutex> #include <mutex>
#include <string>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -20,9 +17,10 @@ namespace sinks {
* Windows color console sink. Uses WriteConsoleA to write to the console with * Windows color console sink. Uses WriteConsoleA to write to the console with
* colors * colors
*/ */
template <typename ConsoleMutex> template <typename Mutex>
class wincolor_sink : public sink { class wincolor_sink : public base_sink<Mutex> {
public: public:
using base_t = base_sink<Mutex>;
wincolor_sink(void *out_handle, color_mode mode); wincolor_sink(void *out_handle, color_mode mode);
~wincolor_sink() override; ~wincolor_sink() override;
@ -31,19 +29,17 @@ public:
// change the color for the given level // change the color for the given level
void set_color(level::level_enum level, std::uint16_t color); void set_color(level::level_enum level, std::uint16_t color);
void log(const details::log_msg &msg) final override;
void flush() final override;
void set_pattern(const std::string &pattern) override final;
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override final;
void set_color_mode(color_mode mode); void set_color_mode(color_mode mode);
void set_console_mutex(std::mutex *console_mutex);
protected: protected:
using mutex_t = typename ConsoleMutex::mutex_t;
void *out_handle_; void *out_handle_;
mutex_t &mutex_;
bool should_do_colors_; bool should_do_colors_;
std::unique_ptr<spdlog::formatter> formatter_;
std::array<std::uint16_t, level::n_levels> colors_; std::array<std::uint16_t, level::n_levels> colors_;
std::mutex *console_mutex_;
void sink_it_(const details::log_msg &msg) final override;
void flush_() final override;
// set foreground color and return the orig console attributes (for resetting later) // set foreground color and return the orig console attributes (for resetting later)
std::uint16_t set_foreground_color_(std::uint16_t attribs); std::uint16_t set_foreground_color_(std::uint16_t attribs);
@ -57,23 +53,23 @@ protected:
void set_color_mode_impl(color_mode mode); void set_color_mode_impl(color_mode mode);
}; };
template <typename ConsoleMutex> template <typename Mutex>
class wincolor_stdout_sink : public wincolor_sink<ConsoleMutex> { class wincolor_stdout_sink : public wincolor_sink<Mutex> {
public: public:
explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic);
}; };
template <typename ConsoleMutex> template <typename Mutex>
class wincolor_stderr_sink : public wincolor_sink<ConsoleMutex> { class wincolor_stderr_sink : public wincolor_sink<Mutex> {
public: public:
explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic);
}; };
using wincolor_stdout_sink_mt = wincolor_stdout_sink<details::console_mutex>; using wincolor_stdout_sink_mt = wincolor_stdout_sink<std::mutex>;
using wincolor_stdout_sink_st = wincolor_stdout_sink<details::console_nullmutex>; using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
using wincolor_stderr_sink_mt = wincolor_stderr_sink<details::console_mutex>; using wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>;
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::console_nullmutex>; using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

View File

@ -14,12 +14,12 @@
// //
#ifdef _WIN32 #ifdef _WIN32
#include <spdlog/sinks/wincolor_sink-inl.h> #include <spdlog/sinks/wincolor_sink-inl.h>
template class SPDLOG_API spdlog::sinks::wincolor_sink<spdlog::details::console_mutex>; template class SPDLOG_API spdlog::sinks::wincolor_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_sink<spdlog::details::console_nullmutex>; template class SPDLOG_API spdlog::sinks::wincolor_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink<spdlog::details::console_mutex>; template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink<spdlog::details::console_nullmutex>; template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink<spdlog::details::null_mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink<spdlog::details::console_mutex>; template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink<spdlog::details::console_nullmutex>; template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink<spdlog::details::null_mutex>;
#else #else
#include "spdlog/sinks/ansicolor_sink-inl.h" #include "spdlog/sinks/ansicolor_sink-inl.h"
template class SPDLOG_API spdlog::sinks::ansicolor_sink<std::mutex>; template class SPDLOG_API spdlog::sinks::ansicolor_sink<std::mutex>;