Replace direct usage of std::mutex,std::lock,std::thread,std::chrono by 'imported' namespaces. By importing this in common.h, it is now possible to use boost::thread and boost::chrono as replacement of std::thread/mutex/chrono.

Current MinGW compiler comes with mutex headers, but without and implementation. and boost::thread only works with boost::chrono.

Now there is a central place to decided which implementation is used, kind of abstraction for lazy.

Additional this commit removes all double includes, whoich is already included in common.h and os.h -> which is currenlty included in all paths. This pretend including useless mutex/chrono headers and speedup compile time a little bit.

This is a relly fast change - and can be improved.
The changes are only made to the header only libs.  Change to the testsuite and examples are seperated.
pull/793/head
gajanak 7 years ago committed by Gabor Janak
parent 56b3a17e56
commit e32c0596ad

@ -21,8 +21,6 @@
#include "spdlog/details/registry.h" #include "spdlog/details/registry.h"
#include "spdlog/details/thread_pool.h" #include "spdlog/details/thread_pool.h"
#include <memory>
#include <mutex>
namespace spdlog { namespace spdlog {
@ -42,7 +40,7 @@ struct async_factory_impl
auto &registry_inst = details::registry::instance(); auto &registry_inst = details::registry::instance();
// create global thread pool if not already exists.. // create global thread pool if not already exists..
std::lock_guard<std::recursive_mutex> tp_lock(registry_inst.tp_mutex()); lock_guard<std::recursive_mutex> tp_lock(registry_inst.tp_mutex());
auto tp = registry_inst.get_tp(); auto tp = registry_inst.get_tp();
if (tp == nullptr) if (tp == nullptr)
{ {

@ -22,10 +22,6 @@
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/logger.h" #include "spdlog/logger.h"
#include <chrono>
#include <memory>
#include <string>
namespace spdlog { namespace spdlog {
// Async overflow policy - block by default. // Async overflow policy - block by default.

@ -8,7 +8,26 @@
#include "spdlog/tweakme.h" #include "spdlog/tweakme.h"
#include <atomic> #include <atomic>
#include <chrono>
#if defined(__MINGW32__)
# define SPDLOG_USE_BOOST_THREAD
# define SPDLOG_USE_BOOST_CHRONO
#endif
#if defined(SPDLOG_USE_BOOST_THREAD)
# define SPDLOG_USE_BOOST_CHRONO
# include <boost/thread.hpp>
#else
# include <mutex>
# include <thread>
# include <condition_variable>
#endif
#if defined(SPDLOG_USE_BOOST_CHRONO)
# include <boost/chrono.hpp>
#else
# include <chrono>
#endif
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
#include <memory> #include <memory>
@ -57,7 +76,36 @@ namespace sinks {
class sink; class sink;
} }
using log_clock = std::chrono::system_clock; // choose thread/mutex 'implementation'
#if defined(SPDLOG_USE_BOOST_THREAD)
// import into namespace spdlog
namespace details {
using boost::condition_variable;
using boost::recursive_mutex;
using boost::thread;
using boost::unique_lock;
} // namespace details
using boost::lock_guard;
using boost::mutex;
#else
namespace details {
using std::condition_variable;
using std::recursive_mutex;
using std::thread;
using std::unique_lock;
} // namespace details
using std::lock_guard;
using std::mutex;
#endif
// choose chrono implementation
#if defined(SPDLOG_USE_BOOST_CHRONO)
namespace chrono = boost::chrono;
#else
namespace chrono = std::chrono;
#endif
using log_clock = chrono::system_clock;
using sink_ptr = std::shared_ptr<sinks::sink>; using sink_ptr = std::shared_ptr<sinks::sink>;
using sinks_init_list = std::initializer_list<sink_ptr>; using sinks_init_list = std::initializer_list<sink_ptr>;
using log_err_handler = std::function<void(const std::string &err_msg)>; using log_err_handler = std::function<void(const std::string &err_msg)>;

@ -10,9 +10,6 @@
#include "spdlog/details/thread_pool.h" #include "spdlog/details/thread_pool.h"
#include <chrono>
#include <memory>
#include <string>
template<typename It> template<typename It>
inline spdlog::async_logger::async_logger( inline spdlog::async_logger::async_logger(

@ -6,7 +6,6 @@
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include <cstdio> #include <cstdio>
#include <mutex>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -40,7 +39,7 @@ struct console_stderr
struct console_mutex struct console_mutex
{ {
using mutex_t = std::mutex; using mutex_t = mutex;
static mutex_t &mutex() static mutex_t &mutex()
{ {
static mutex_t s_mutex; static mutex_t s_mutex;

@ -13,12 +13,6 @@
#include "../details/log_msg.h" #include "../details/log_msg.h"
#include "../details/os.h" #include "../details/os.h"
#include <cerrno>
#include <chrono>
#include <cstdio>
#include <string>
#include <thread>
#include <tuple>
namespace spdlog { namespace spdlog {
namespace details { namespace details {

@ -4,7 +4,6 @@
#pragma once #pragma once
#include "chrono"
#include "spdlog/fmt/fmt.h" #include "spdlog/fmt/fmt.h"
// Some fmt helpers to efficiently format and pad ints and strings // Some fmt helpers to efficiently format and pad ints and strings
@ -119,7 +118,7 @@ inline void pad6(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
template<typename ToDuration> template<typename ToDuration>
inline ToDuration time_fraction(const log_clock::time_point &tp) inline ToDuration time_fraction(const log_clock::time_point &tp)
{ {
using namespace std::chrono; using namespace chrono;
auto duration = tp.time_since_epoch(); auto duration = tp.time_since_epoch();
auto secs = duration_cast<seconds>(duration); auto secs = duration_cast<seconds>(duration);
return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs); return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);

@ -8,8 +8,6 @@
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include <string>
#include <utility>
namespace spdlog { namespace spdlog {
namespace details { namespace details {

@ -5,8 +5,6 @@
#pragma once #pragma once
#include <memory>
#include <string>
// create logger with given name, sinks and the default pattern formatter // create logger with given name, sinks and the default pattern formatter
// all other ctors will call this one // all other ctors will call this one
@ -185,7 +183,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const
try try
{ {
{ {
std::lock_guard<std::mutex> lock(wstring_converter_mutex_); lock_guard<mutex> lock(wstring_converter_mutex_);
utf8_string = wstring_converter_.to_bytes(fmt); utf8_string = wstring_converter_.to_bytes(fmt);
} }
log(lvl, utf8_string.c_str(), args...); log(lvl, utf8_string.c_str(), args...);

@ -14,8 +14,6 @@
#include "spdlog/details/circular_q.h" #include "spdlog/details/circular_q.h"
#include <condition_variable>
#include <mutex>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -35,7 +33,7 @@ public:
void enqueue(T &&item) void enqueue(T &&item)
{ {
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<mutex> lock(queue_mutex_);
pop_cv_.wait(lock, [this] { return !this->q_.full(); }); pop_cv_.wait(lock, [this] { return !this->q_.full(); });
q_.push_back(std::move(item)); q_.push_back(std::move(item));
} }
@ -46,7 +44,7 @@ public:
void enqueue_nowait(T &&item) void enqueue_nowait(T &&item)
{ {
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<mutex> lock(queue_mutex_);
q_.push_back(std::move(item)); q_.push_back(std::move(item));
} }
push_cv_.notify_one(); push_cv_.notify_one();
@ -54,10 +52,10 @@ public:
// try to dequeue item. if no item found. wait upto timeout and try again // try to dequeue item. if no item found. wait upto timeout and try again
// Return true, if succeeded dequeue item, false otherwise // Return true, if succeeded dequeue item, false otherwise
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) bool dequeue_for(T &popped_item, chrono::milliseconds wait_duration)
{ {
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<mutex> lock(queue_mutex_);
if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); }))
{ {
return false; return false;
@ -75,7 +73,7 @@ public:
// try to enqueue and block if no room left // try to enqueue and block if no room left
void enqueue(T &&item) void enqueue(T &&item)
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<mutex> lock(queue_mutex_);
pop_cv_.wait(lock, [this] { return !this->q_.full(); }); pop_cv_.wait(lock, [this] { return !this->q_.full(); });
q_.push_back(std::move(item)); q_.push_back(std::move(item));
push_cv_.notify_one(); push_cv_.notify_one();
@ -84,16 +82,16 @@ public:
// enqueue immediately. overrun oldest message in the queue if no room left. // enqueue immediately. overrun oldest message in the queue if no room left.
void enqueue_nowait(T &&item) void enqueue_nowait(T &&item)
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<mutex> lock(queue_mutex_);
q_.push_back(std::move(item)); q_.push_back(std::move(item));
push_cv_.notify_one(); push_cv_.notify_one();
} }
// try to dequeue item. if no item found. wait upto timeout and try again // try to dequeue item. if no item found. wait upto timeout and try again
// Return true, if succeeded dequeue item, false otherwise // Return true, if succeeded dequeue item, false otherwise
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) bool dequeue_for(T &popped_item, chrono::milliseconds wait_duration)
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<mutex> lock(queue_mutex_);
if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); }))
{ {
return false; return false;
@ -106,9 +104,9 @@ public:
#endif #endif
private: private:
std::mutex queue_mutex_; mutex queue_mutex_;
std::condition_variable push_cv_; condition_variable push_cv_;
std::condition_variable pop_cv_; condition_variable pop_cv_;
spdlog::details::circular_q<T> q_; spdlog::details::circular_q<T> q_;
}; };
} // namespace details } // namespace details

@ -5,7 +5,6 @@
#pragma once #pragma once
#include <atomic>
// null, no cost dummy "mutex" and dummy "atomic" int // null, no cost dummy "mutex" and dummy "atomic" int
namespace spdlog { namespace spdlog {

@ -7,7 +7,6 @@
#include "../common.h" #include "../common.h"
#include <algorithm> #include <algorithm>
#include <chrono>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@ -16,7 +15,6 @@
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <thread>
#ifdef _WIN32 #ifdef _WIN32
@ -63,8 +61,8 @@ inline spdlog::log_clock::time_point now()
#if defined __linux__ && defined SPDLOG_CLOCK_COARSE #if defined __linux__ && defined SPDLOG_CLOCK_COARSE
timespec ts; timespec ts;
::clock_gettime(CLOCK_REALTIME_COARSE, &ts); ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
return std::chrono::time_point<log_clock, typename log_clock::duration>( return chrono::time_point<log_clock, typename log_clock::duration>(
std::chrono::duration_cast<typename log_clock::duration>(std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec))); chrono::duration_cast<typename log_clock::duration>(chrono::seconds(ts.tv_sec) + chrono::nanoseconds(ts.tv_nsec)));
#else #else
return log_clock::now(); return log_clock::now();
@ -341,7 +339,7 @@ inline size_t _thread_id()
pthread_threadid_np(nullptr, &tid); pthread_threadid_np(nullptr, &tid);
return static_cast<size_t>(tid); return static_cast<size_t>(tid);
#else // Default to standard C++11 (other Unix) #else // Default to standard C++11 (other Unix)
return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id())); return static_cast<size_t>(std::hash<thread::id>()(std::this_thread::get_id()));
#endif #endif
} }
@ -364,7 +362,7 @@ inline void sleep_for_millis(int milliseconds)
#if defined(_WIN32) #if defined(_WIN32)
::Sleep(milliseconds); ::Sleep(milliseconds);
#else #else
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); std::this_thread::sleep_for(chrono::milliseconds(milliseconds));
#endif #endif
} }

@ -11,15 +11,6 @@
#include "spdlog/fmt/fmt.h" #include "spdlog/fmt/fmt.h"
#include "spdlog/formatter.h" #include "spdlog/formatter.h"
#include <array>
#include <chrono>
#include <ctime>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <utility>
#include <vector>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -231,7 +222,7 @@ class e_formatter SPDLOG_FINAL : public flag_formatter
{ {
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{ {
auto millis = fmt_helper::time_fraction<std::chrono::milliseconds>(msg.time); auto millis = fmt_helper::time_fraction<chrono::milliseconds>(msg.time);
fmt_helper::pad3(static_cast<int>(millis.count()), dest); fmt_helper::pad3(static_cast<int>(millis.count()), dest);
} }
}; };
@ -241,7 +232,7 @@ class f_formatter SPDLOG_FINAL : public flag_formatter
{ {
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{ {
auto micros = fmt_helper::time_fraction<std::chrono::microseconds>(msg.time); auto micros = fmt_helper::time_fraction<chrono::microseconds>(msg.time);
fmt_helper::pad6(static_cast<size_t>(micros.count()), dest); fmt_helper::pad6(static_cast<size_t>(micros.count()), dest);
} }
}; };
@ -251,7 +242,7 @@ class F_formatter SPDLOG_FINAL : public flag_formatter
{ {
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{ {
auto ns = fmt_helper::time_fraction<std::chrono::nanoseconds>(msg.time); auto ns = fmt_helper::time_fraction<chrono::nanoseconds>(msg.time);
fmt::format_to(dest, "{:09}", ns.count()); fmt::format_to(dest, "{:09}", ns.count());
} }
}; };
@ -262,7 +253,7 @@ class E_formatter SPDLOG_FINAL : public flag_formatter
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count(); auto seconds = chrono::duration_cast<chrono::seconds>(duration).count();
fmt_helper::append_int(seconds, dest); fmt_helper::append_int(seconds, dest);
} }
}; };
@ -321,7 +312,7 @@ class T_formatter SPDLOG_FINAL : public flag_formatter
class z_formatter SPDLOG_FINAL : public flag_formatter class z_formatter SPDLOG_FINAL : public flag_formatter
{ {
public: public:
const std::chrono::seconds cache_refresh = std::chrono::seconds(5); const chrono::seconds cache_refresh = chrono::seconds(5);
z_formatter() = default; z_formatter() = default;
z_formatter(const z_formatter &) = delete; z_formatter(const z_formatter &) = delete;
@ -354,7 +345,7 @@ public:
} }
private: private:
log_clock::time_point last_update_{std::chrono::seconds(0)}; log_clock::time_point last_update_{chrono::seconds(0)};
#ifdef _WIN32 #ifdef _WIN32
int offset_minutes_{0}; int offset_minutes_{0};
@ -462,7 +453,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
{ {
void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override
{ {
using namespace std::chrono; using namespace chrono;
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
// cache the date/time part for the next second. // cache the date/time part for the next second.
@ -522,7 +513,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
} }
private: private:
std::chrono::seconds cache_timestamp_{0}; chrono::seconds cache_timestamp_{0};
fmt::basic_memory_buffer<char, 128> cached_datetime_; fmt::basic_memory_buffer<char, 128> cached_datetime_;
}; };
@ -553,7 +544,7 @@ public:
void format(const details::log_msg &msg, fmt::memory_buffer &dest) override void format(const details::log_msg &msg, fmt::memory_buffer &dest) override
{ {
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch()); auto secs = chrono::duration_cast<chrono::seconds>(msg.time.time_since_epoch());
if (secs != last_log_secs_) if (secs != last_log_secs_)
{ {
cached_tm_ = get_time_(msg); cached_tm_ = get_time_(msg);
@ -573,7 +564,7 @@ private:
std::string eol_; std::string eol_;
pattern_time_type pattern_time_type_; pattern_time_type pattern_time_type_;
std::tm cached_tm_; std::tm cached_tm_;
std::chrono::seconds last_log_secs_; chrono::seconds last_log_secs_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_; std::vector<std::unique_ptr<details::flag_formatter>> formatters_;

@ -12,29 +12,25 @@
// creates the thread on construction. // creates the thread on construction.
// stops and joins the thread on destruction. // stops and joins the thread on destruction.
#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
class periodic_worker class periodic_worker
{ {
public: public:
periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval) periodic_worker(const std::function<void()> &callback_fun, chrono::seconds interval)
{ {
active_ = (interval > std::chrono::seconds::zero()); active_ = (interval > chrono::seconds::zero());
if (!active_) if (!active_)
{ {
return; return;
} }
worker_thread_ = std::thread([this, callback_fun, interval]() { worker_thread_ = thread([this, callback_fun, interval]() {
for (;;) for (;;)
{ {
std::unique_lock<std::mutex> lock(this->mutex_); unique_lock<mutex> lock(this->mutex_);
if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; })) if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
{ {
return; // active_ == false, so exit this thread return; // active_ == false, so exit this thread
@ -53,7 +49,7 @@ public:
if (worker_thread_.joinable()) if (worker_thread_.joinable())
{ {
{ {
std::lock_guard<std::mutex> lock(mutex_); lock_guard<mutex> lock(mutex_);
active_ = false; active_ = false;
} }
cv_.notify_one(); cv_.notify_one();
@ -63,9 +59,9 @@ public:
private: private:
bool active_; bool active_;
std::thread worker_thread_; thread worker_thread_;
std::mutex mutex_; mutex mutex_;
std::condition_variable cv_; condition_variable cv_;
}; };
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

@ -14,11 +14,6 @@
#include "spdlog/details/periodic_worker.h" #include "spdlog/details/periodic_worker.h"
#include "spdlog/logger.h" #include "spdlog/logger.h"
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -32,7 +27,7 @@ public:
void register_logger(std::shared_ptr<logger> new_logger) void register_logger(std::shared_ptr<logger> new_logger)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
auto logger_name = new_logger->name(); auto logger_name = new_logger->name();
throw_if_exists_(logger_name); throw_if_exists_(logger_name);
loggers_[logger_name] = std::move(new_logger); loggers_[logger_name] = std::move(new_logger);
@ -40,7 +35,7 @@ public:
void register_and_init(std::shared_ptr<logger> new_logger) void register_and_init(std::shared_ptr<logger> new_logger)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
auto logger_name = new_logger->name(); auto logger_name = new_logger->name();
throw_if_exists_(logger_name); throw_if_exists_(logger_name);
@ -61,27 +56,27 @@ public:
std::shared_ptr<logger> get(const std::string &logger_name) std::shared_ptr<logger> get(const std::string &logger_name)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
auto found = loggers_.find(logger_name); auto found = loggers_.find(logger_name);
return found == loggers_.end() ? nullptr : found->second; return found == loggers_.end() ? nullptr : found->second;
} }
void set_tp(std::shared_ptr<thread_pool> tp) void set_tp(std::shared_ptr<thread_pool> tp)
{ {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_); lock_guard<recursive_mutex> lock(tp_mutex_);
tp_ = std::move(tp); tp_ = std::move(tp);
} }
std::shared_ptr<thread_pool> get_tp() std::shared_ptr<thread_pool> get_tp()
{ {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_); lock_guard<recursive_mutex> lock(tp_mutex_);
return tp_; return tp_;
} }
// Set global formatter. Each sink in each logger will get a clone of this object // Set global formatter. Each sink in each logger will get a clone of this object
void set_formatter(std::unique_ptr<formatter> formatter) void set_formatter(std::unique_ptr<formatter> formatter)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
formatter_ = std::move(formatter); formatter_ = std::move(formatter);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
@ -91,7 +86,7 @@ public:
void set_level(level::level_enum log_level) void set_level(level::level_enum log_level)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
l.second->set_level(log_level); l.second->set_level(log_level);
@ -101,7 +96,7 @@ public:
void flush_on(level::level_enum log_level) void flush_on(level::level_enum log_level)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
l.second->flush_on(log_level); l.second->flush_on(log_level);
@ -109,16 +104,16 @@ public:
flush_level_ = log_level; flush_level_ = log_level;
} }
void flush_every(std::chrono::seconds interval) void flush_every(chrono::seconds interval)
{ {
std::lock_guard<std::mutex> lock(flusher_mutex_); lock_guard<mutex> lock(flusher_mutex_);
std::function<void()> clbk = std::bind(&registry::flush_all, this); std::function<void()> clbk = std::bind(&registry::flush_all, this);
periodic_flusher_.reset(new periodic_worker(clbk, interval)); periodic_flusher_.reset(new periodic_worker(clbk, interval));
} }
void set_error_handler(log_err_handler handler) void set_error_handler(log_err_handler handler)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
l.second->set_error_handler(handler); l.second->set_error_handler(handler);
@ -128,7 +123,7 @@ public:
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
fun(l.second); fun(l.second);
@ -137,7 +132,7 @@ public:
void flush_all() void flush_all()
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
for (auto &l : loggers_) for (auto &l : loggers_)
{ {
l.second->flush(); l.second->flush();
@ -146,13 +141,13 @@ public:
void drop(const std::string &logger_name) void drop(const std::string &logger_name)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
loggers_.erase(logger_name); loggers_.erase(logger_name);
} }
void drop_all() void drop_all()
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); lock_guard<mutex> lock(logger_map_mutex_);
loggers_.clear(); loggers_.clear();
} }
@ -160,19 +155,19 @@ public:
void shutdown() void shutdown()
{ {
{ {
std::lock_guard<std::mutex> lock(flusher_mutex_); lock_guard<mutex> lock(flusher_mutex_);
periodic_flusher_.reset(); periodic_flusher_.reset();
} }
drop_all(); drop_all();
{ {
std::lock_guard<std::recursive_mutex> lock(tp_mutex_); lock_guard<recursive_mutex> lock(tp_mutex_);
tp_.reset(); tp_.reset();
} }
} }
std::recursive_mutex &tp_mutex() recursive_mutex &tp_mutex()
{ {
return tp_mutex_; return tp_mutex_;
} }
@ -199,8 +194,8 @@ private:
} }
} }
std::mutex logger_map_mutex_, flusher_mutex_; mutex logger_map_mutex_, flusher_mutex_;
std::recursive_mutex tp_mutex_; recursive_mutex tp_mutex_;
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_; std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
std::unique_ptr<formatter> formatter_; std::unique_ptr<formatter> formatter_;
level::level_enum level_ = level::info; level::level_enum level_ = level::info;

@ -4,10 +4,8 @@
#include "spdlog/details/mpmc_blocking_q.h" #include "spdlog/details/mpmc_blocking_q.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include <chrono> // #include <memory>
#include <memory> // #include <vector>
#include <thread>
#include <vector>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -160,7 +158,7 @@ public:
private: private:
q_type q_; q_type q_;
std::vector<std::thread> threads_; std::vector<thread> threads_;
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
{ {
@ -185,7 +183,7 @@ private:
bool process_next_msg_() bool process_next_msg_()
{ {
async_msg incoming_async_msg; async_msg incoming_async_msg;
bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); bool dequeued = q_.dequeue_for(incoming_async_msg, chrono::seconds(10));
if (!dequeued) if (!dequeued)
{ {
return true; return true;

@ -22,9 +22,6 @@
#include "spdlog/formatter.h" #include "spdlog/formatter.h"
#include "spdlog/sinks/sink.h" #include "spdlog/sinks/sink.h"
#include <memory>
#include <string>
#include <vector>
namespace spdlog { namespace spdlog {
@ -154,7 +151,7 @@ protected:
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
std::wstring_convert<std::codecvt_utf8<wchar_t>> wstring_converter_; std::wstring_convert<std::codecvt_utf8<wchar_t>> wstring_converter_;
std::mutex wstring_converter_mutex_; mutex wstring_converter_mutex_;
#endif #endif
}; };
} // namespace spdlog } // namespace spdlog

@ -9,10 +9,6 @@
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -50,7 +46,7 @@ public:
void set_color(level::level_enum color_level, const std::string &color) void set_color(level::level_enum color_level, const std::string &color)
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
colors_[color_level] = color; colors_[color_level] = color;
} }
@ -84,11 +80,11 @@ public:
const std::string on_cyan = "\033[46m"; const std::string on_cyan = "\033[46m";
const std::string on_white = "\033[47m"; const std::string on_white = "\033[47m";
void log(const details::log_msg &msg) override void log(const details::log_msg &msg) SPDLOG_FINAL override
{ {
// Wrap the originally formatted message in color codes. // Wrap the originally formatted message in color codes.
// If color is not supported in the terminal, log as is instead. // If color is not supported in the terminal, log as is instead.
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
@ -112,19 +108,19 @@ public:
void flush() override void flush() override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
fflush(target_file_); fflush(target_file_);
} }
void set_pattern(const std::string &pattern) SPDLOG_FINAL void set_pattern(const std::string &pattern) SPDLOG_FINAL
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
} }
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter); formatter_ = std::move(sink_formatter);
} }

@ -32,25 +32,25 @@ public:
void log(const details::log_msg &msg) SPDLOG_FINAL void log(const details::log_msg &msg) SPDLOG_FINAL
{ {
std::lock_guard<Mutex> lock(mutex_); lock_guard<Mutex> lock(mutex_);
sink_it_(msg); sink_it_(msg);
} }
void flush() SPDLOG_FINAL override void flush() SPDLOG_FINAL override
{ {
std::lock_guard<Mutex> lock(mutex_); lock_guard<Mutex> lock(mutex_);
flush_(); flush_();
} }
void set_pattern(const std::string &pattern) SPDLOG_FINAL override void set_pattern(const std::string &pattern) SPDLOG_FINAL override
{ {
std::lock_guard<Mutex> lock(mutex_); lock_guard<Mutex> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
} }
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) SPDLOG_FINAL override void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) SPDLOG_FINAL override
{ {
std::lock_guard<Mutex> lock(mutex_); lock_guard<Mutex> lock(mutex_);
formatter_ = std::move(sink_formatter); formatter_ = std::move(sink_formatter);
} }

@ -9,8 +9,6 @@
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <mutex>
#include <string>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -43,7 +41,7 @@ private:
details::file_helper file_helper_; details::file_helper file_helper_;
}; };
using basic_file_sink_mt = basic_file_sink<std::mutex>; using basic_file_sink_mt = basic_file_sink<mutex>;
using basic_file_sink_st = basic_file_sink<details::null_mutex>; using basic_file_sink_st = basic_file_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -10,11 +10,6 @@
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <chrono>
#include <cstdio>
#include <ctime>
#include <mutex>
#include <string>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -97,7 +92,7 @@ private:
{ {
return rotation_time; return rotation_time;
} }
return {rotation_time + std::chrono::hours(24)}; return {rotation_time + chrono::hours(24)};
} }
filename_t base_filename_; filename_t base_filename_;
@ -108,7 +103,7 @@ private:
bool truncate_; bool truncate_;
}; };
using daily_file_sink_mt = daily_file_sink<std::mutex>; using daily_file_sink_mt = daily_file_sink<mutex>;
using daily_file_sink_st = daily_file_sink<details::null_mutex>; using daily_file_sink_st = daily_file_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -9,10 +9,6 @@
#include "spdlog/details/log_msg.h" #include "spdlog/details/log_msg.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include <algorithm>
#include <memory>
#include <mutex>
#include <vector>
// Distribution sink (mux). Stores a vector of sinks which get called when log // Distribution sink (mux). Stores a vector of sinks which get called when log
// is called // is called
@ -30,13 +26,13 @@ public:
void add_sink(std::shared_ptr<sink> sink) void add_sink(std::shared_ptr<sink> sink)
{ {
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.push_back(sink); sinks_.push_back(sink);
} }
void remove_sink(std::shared_ptr<sink> sink) void remove_sink(std::shared_ptr<sink> sink)
{ {
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
} }
@ -61,7 +57,7 @@ protected:
std::vector<std::shared_ptr<sink>> sinks_; std::vector<std::shared_ptr<sink>> sinks_;
}; };
using dist_sink_mt = dist_sink<std::mutex>; using dist_sink_mt = dist_sink<mutex>;
using dist_sink_st = dist_sink<details::null_mutex>; using dist_sink_st = dist_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -8,7 +8,6 @@
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include <mutex>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -21,7 +20,7 @@ protected:
void flush_() override {} void flush_() override {}
}; };
using null_sink_mt = null_sink<std::mutex>; using null_sink_mt = null_sink<mutex>;
using null_sink_st = null_sink<details::null_mutex>; using null_sink_st = null_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -8,8 +8,6 @@
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include <mutex>
#include <ostream>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -44,7 +42,7 @@ protected:
bool force_flush_; bool force_flush_;
}; };
using ostream_sink_mt = ostream_sink<std::mutex>; using ostream_sink_mt = ostream_sink<mutex>;
using ostream_sink_st = ostream_sink<details::null_mutex>; using ostream_sink_st = ostream_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -4,18 +4,12 @@
// //
#pragma once #pragma once
#include "spdlog/spdlog.h"
#include "spdlog/details/file_helper.h" #include "spdlog/details/file_helper.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/fmt/fmt.h" #include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"
#include <cerrno>
#include <chrono>
#include <ctime>
#include <mutex>
#include <string>
#include <tuple>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -119,7 +113,7 @@ private:
details::file_helper file_helper_; details::file_helper file_helper_;
}; };
using rotating_file_sink_mt = rotating_file_sink<std::mutex>; using rotating_file_sink_mt = rotating_file_sink<mutex>;
using rotating_file_sink_st = rotating_file_sink<details::null_mutex>; using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -5,14 +5,11 @@
#pragma once #pragma once
#include "spdlog/spdlog.h"
#include "spdlog/details/console_globals.h" #include "spdlog/details/console_globals.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/spdlog.h"
#include <cstdio> #include "spdlog/details/console_globals.h"
#include <memory>
#include <mutex>
#include <spdlog/details/console_globals.h>
namespace spdlog { namespace spdlog {
@ -35,7 +32,7 @@ public:
void log(const details::log_msg &msg) override void log(const details::log_msg &msg) override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
fwrite(formatted.data(), sizeof(char), formatted.size(), file_); fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
@ -44,19 +41,19 @@ public:
void flush() override void flush() override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
fflush(file_); fflush(file_);
} }
void set_pattern(const std::string &pattern) override void set_pattern(const std::string &pattern) override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
} }
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter); formatter_ = std::move(sink_formatter);
} }

@ -8,8 +8,6 @@
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <array>
#include <string>
#include <syslog.h> #include <syslog.h>
namespace spdlog { namespace spdlog {
@ -70,7 +68,7 @@ private:
} }
}; };
using syslog_sink_mt = syslog_sink<std::mutex>; using syslog_sink_mt = syslog_sink<mutex>;
using syslog_sink_st = syslog_sink<details::null_mutex>; using syslog_sink_st = syslog_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

@ -10,10 +10,6 @@
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/sink.h" #include "spdlog/sinks/sink.h"
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <wincon.h> #include <wincon.h>
namespace spdlog { namespace spdlog {
@ -57,13 +53,13 @@ public:
// change the color for the given level // change the color for the given level
void set_color(level::level_enum level, WORD color) void set_color(level::level_enum level, WORD color)
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
colors_[level] = color; colors_[level] = color;
} }
void log(const details::log_msg &msg) SPDLOG_FINAL override void log(const details::log_msg &msg) SPDLOG_FINAL override
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
if (msg.color_range_end > msg.color_range_start) if (msg.color_range_end > msg.color_range_start)
@ -92,13 +88,13 @@ public:
void set_pattern(const std::string &pattern) override SPDLOG_FINAL void set_pattern(const std::string &pattern) override SPDLOG_FINAL
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
} }
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override SPDLOG_FINAL void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override SPDLOG_FINAL
{ {
std::lock_guard<mutex_t> lock(mutex_); lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter); formatter_ = std::move(sink_formatter);
} }

@ -12,10 +12,6 @@
#include "spdlog/logger.h" #include "spdlog/logger.h"
#include "spdlog/version.h" #include "spdlog/version.h"
#include <chrono>
#include <functional>
#include <memory>
#include <string>
namespace spdlog { namespace spdlog {
@ -81,7 +77,7 @@ inline void flush_on(level::level_enum log_level)
// Start/Restart a periodic flusher thread // Start/Restart a periodic flusher thread
// Warning: Use only if all your loggers are thread safe! // Warning: Use only if all your loggers are thread safe!
inline void flush_every(std::chrono::seconds interval) inline void flush_every(chrono::seconds interval)
{ {
details::registry::instance().flush_every(interval); details::registry::instance().flush_every(interval);
} }

Loading…
Cancel
Save