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