Refactred spdlog.h and console sinks. Added global lock for all console sinks (traits)

This commit is contained in:
gabime
2018-04-18 02:04:10 +03:00
parent 9bffa921ae
commit 924ef84241
7 changed files with 206 additions and 212 deletions

View File

@@ -37,9 +37,7 @@ inline spdlog::async_logger::async_logger(
// send the log message to the thread pool
inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
{
try
{
{
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
_incr_msg_counter(msg);
#endif
@@ -50,16 +48,7 @@ inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
else
{
throw spdlog_ex("async log: thread pool doens't exist anymore");
}
}
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...)
{
_err_handler("Unknown exception in async logger " + _name);
}
}
}
// send flush request to the thread pool
@@ -99,6 +88,11 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms
{
_err_handler("Unknown exception in async logger " + _name);
}
if (_should_flush(incoming_log_msg))
{
_backend_flush();
}
}
inline void spdlog::async_logger::_backend_flush()

View File

@@ -148,8 +148,16 @@ public:
void drop_all()
{
std::lock_guard<Mutex> lock(_mutex);
_loggers.clear();
{
std::lock_guard<Mutex> lock(_mutex);
_loggers.clear();
}
{
std::lock_guard<Mutex> lock(_tp_mutex);
_tp.reset();
}
}
Mutex &tp_mutex()

View File

@@ -0,0 +1,45 @@
#pragma once
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
namespace spdlog {
namespace details {
struct console_stdout_trait
{
static FILE* stream() {return stdout;}
#ifdef _WIN32
static HANDLE handle() { return ::GetStdHandle(STD_OUTPUT_HANDLE); }
#endif
};
struct console_stderr_trait
{
static FILE* stream() { return stdout; }
#ifdef _WIN32
static HANDLE handle() { return ::GetStdHandle(STD_ERROR_HANDLE); }
#endif
};
struct console_mutex_trait
{
using mutex_t = std::mutex;
static mutex_t& console_mutex()
{
static auto mutex = mutex_t{};
return mutex;
}
};
struct console_null_mutex_trait
{
using mutex_t = null_mutex;
static mutex_t& console_mutex()
{
static auto mutex = mutex_t{};
return mutex;
}
};
}
}