From e32c0596adfb782f37a6aee58f7cc75ef3ef5817 Mon Sep 17 00:00:00 2001 From: gajanak Date: Mon, 13 Aug 2018 13:00:22 +0200 Subject: [PATCH] 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. --- include/spdlog/async.h | 4 +- include/spdlog/async_logger.h | 4 -- include/spdlog/common.h | 52 +++++++++++++++++++++- include/spdlog/details/async_logger_impl.h | 3 -- include/spdlog/details/console_globals.h | 3 +- include/spdlog/details/file_helper.h | 6 --- include/spdlog/details/fmt_helper.h | 3 +- include/spdlog/details/log_msg.h | 2 - include/spdlog/details/logger_impl.h | 4 +- include/spdlog/details/mpmc_blocking_q.h | 24 +++++----- include/spdlog/details/null_mutex.h | 1 - include/spdlog/details/os.h | 10 ++--- include/spdlog/details/pattern_formatter.h | 29 +++++------- include/spdlog/details/periodic_worker.h | 22 ++++----- include/spdlog/details/registry.h | 45 +++++++++---------- include/spdlog/details/thread_pool.h | 10 ++--- include/spdlog/logger.h | 5 +-- include/spdlog/sinks/ansicolor_sink.h | 16 +++---- include/spdlog/sinks/base_sink.h | 8 ++-- include/spdlog/sinks/basic_file_sink.h | 4 +- include/spdlog/sinks/daily_file_sink.h | 9 +--- include/spdlog/sinks/dist_sink.h | 10 ++--- include/spdlog/sinks/null_sink.h | 3 +- include/spdlog/sinks/ostream_sink.h | 4 +- include/spdlog/sinks/rotating_file_sink.h | 10 +---- include/spdlog/sinks/stdout_sinks.h | 15 +++---- include/spdlog/sinks/syslog_sink.h | 4 +- include/spdlog/sinks/wincolor_sink.h | 12 ++--- include/spdlog/spdlog.h | 6 +-- 29 files changed, 145 insertions(+), 183 deletions(-) diff --git a/include/spdlog/async.h b/include/spdlog/async.h index 9c3e9551..e17ccd14 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -21,8 +21,6 @@ #include "spdlog/details/registry.h" #include "spdlog/details/thread_pool.h" -#include -#include namespace spdlog { @@ -42,7 +40,7 @@ struct async_factory_impl auto ®istry_inst = details::registry::instance(); // create global thread pool if not already exists.. - std::lock_guard tp_lock(registry_inst.tp_mutex()); + lock_guard tp_lock(registry_inst.tp_mutex()); auto tp = registry_inst.get_tp(); if (tp == nullptr) { diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 3250f4ad..3980494b 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -22,10 +22,6 @@ #include "spdlog/common.h" #include "spdlog/logger.h" -#include -#include -#include - namespace spdlog { // Async overflow policy - block by default. diff --git a/include/spdlog/common.h b/include/spdlog/common.h index ddb0d28c..aa339d3b 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -8,7 +8,26 @@ #include "spdlog/tweakme.h" #include -#include + +#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 +#else +# include +# include +# include +#endif +#if defined(SPDLOG_USE_BOOST_CHRONO) +# include +#else +# include +#endif + #include #include #include @@ -57,7 +76,36 @@ namespace sinks { 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; using sinks_init_list = std::initializer_list; using log_err_handler = std::function; diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 47659ddc..021f985f 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -10,9 +10,6 @@ #include "spdlog/details/thread_pool.h" -#include -#include -#include template inline spdlog::async_logger::async_logger( diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h index 4ac7f743..4bc15458 100644 --- a/include/spdlog/details/console_globals.h +++ b/include/spdlog/details/console_globals.h @@ -6,7 +6,6 @@ #include "spdlog/details/null_mutex.h" #include -#include namespace spdlog { namespace details { @@ -40,7 +39,7 @@ struct console_stderr struct console_mutex { - using mutex_t = std::mutex; + using mutex_t = mutex; static mutex_t &mutex() { static mutex_t s_mutex; diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 65b3560f..5263a8b0 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -13,12 +13,6 @@ #include "../details/log_msg.h" #include "../details/os.h" -#include -#include -#include -#include -#include -#include namespace spdlog { namespace details { diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index eabf93b5..be45515e 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -4,7 +4,6 @@ #pragma once -#include "chrono" #include "spdlog/fmt/fmt.h" // 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 &dest) template inline ToDuration time_fraction(const log_clock::time_point &tp) { - using namespace std::chrono; + using namespace chrono; auto duration = tp.time_since_epoch(); auto secs = duration_cast(duration); return duration_cast(duration) - duration_cast(secs); diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index 3272dd5d..be2b80ca 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -8,8 +8,6 @@ #include "spdlog/common.h" #include "spdlog/details/os.h" -#include -#include namespace spdlog { namespace details { diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 0a7c5b6b..82b9a086 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -5,8 +5,6 @@ #pragma once -#include -#include // create logger with given name, sinks and the default pattern formatter // 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 { { - std::lock_guard lock(wstring_converter_mutex_); + lock_guard lock(wstring_converter_mutex_); utf8_string = wstring_converter_.to_bytes(fmt); } log(lvl, utf8_string.c_str(), args...); diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index d607da2c..65bf7fda 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -14,8 +14,6 @@ #include "spdlog/details/circular_q.h" -#include -#include namespace spdlog { namespace details { @@ -35,7 +33,7 @@ public: void enqueue(T &&item) { { - std::unique_lock lock(queue_mutex_); + std::unique_lock lock(queue_mutex_); pop_cv_.wait(lock, [this] { return !this->q_.full(); }); q_.push_back(std::move(item)); } @@ -46,7 +44,7 @@ public: void enqueue_nowait(T &&item) { { - std::unique_lock lock(queue_mutex_); + std::unique_lock lock(queue_mutex_); q_.push_back(std::move(item)); } push_cv_.notify_one(); @@ -54,10 +52,10 @@ public: // try to dequeue item. if no item found. wait upto timeout and try again // 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 lock(queue_mutex_); + std::unique_lock lock(queue_mutex_); if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) { return false; @@ -75,7 +73,7 @@ public: // try to enqueue and block if no room left void enqueue(T &&item) { - std::unique_lock lock(queue_mutex_); + std::unique_lock lock(queue_mutex_); pop_cv_.wait(lock, [this] { return !this->q_.full(); }); q_.push_back(std::move(item)); push_cv_.notify_one(); @@ -84,16 +82,16 @@ public: // enqueue immediately. overrun oldest message in the queue if no room left. void enqueue_nowait(T &&item) { - std::unique_lock lock(queue_mutex_); + std::unique_lock lock(queue_mutex_); q_.push_back(std::move(item)); push_cv_.notify_one(); } // try to dequeue item. if no item found. wait upto timeout and try again // 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 lock(queue_mutex_); + std::unique_lock lock(queue_mutex_); if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) { return false; @@ -106,9 +104,9 @@ public: #endif private: - std::mutex queue_mutex_; - std::condition_variable push_cv_; - std::condition_variable pop_cv_; + mutex queue_mutex_; + condition_variable push_cv_; + condition_variable pop_cv_; spdlog::details::circular_q q_; }; } // namespace details diff --git a/include/spdlog/details/null_mutex.h b/include/spdlog/details/null_mutex.h index 3f495bd9..e0dd220e 100644 --- a/include/spdlog/details/null_mutex.h +++ b/include/spdlog/details/null_mutex.h @@ -5,7 +5,6 @@ #pragma once -#include // null, no cost dummy "mutex" and dummy "atomic" int namespace spdlog { diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index d7f7430f..b4220e2c 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -7,7 +7,6 @@ #include "../common.h" #include -#include #include #include #include @@ -16,7 +15,6 @@ #include #include #include -#include #ifdef _WIN32 @@ -63,8 +61,8 @@ inline spdlog::log_clock::time_point now() #if defined __linux__ && defined SPDLOG_CLOCK_COARSE timespec ts; ::clock_gettime(CLOCK_REALTIME_COARSE, &ts); - return std::chrono::time_point( - std::chrono::duration_cast(std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec))); + return chrono::time_point( + chrono::duration_cast(chrono::seconds(ts.tv_sec) + chrono::nanoseconds(ts.tv_nsec))); #else return log_clock::now(); @@ -341,7 +339,7 @@ inline size_t _thread_id() pthread_threadid_np(nullptr, &tid); return static_cast(tid); #else // Default to standard C++11 (other Unix) - return static_cast(std::hash()(std::this_thread::get_id())); + return static_cast(std::hash()(std::this_thread::get_id())); #endif } @@ -364,7 +362,7 @@ inline void sleep_for_millis(int milliseconds) #if defined(_WIN32) ::Sleep(milliseconds); #else - std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); + std::this_thread::sleep_for(chrono::milliseconds(milliseconds)); #endif } diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index dd352ae5..8fd351f8 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -11,15 +11,6 @@ #include "spdlog/fmt/fmt.h" #include "spdlog/formatter.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include namespace spdlog { 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 { - auto millis = fmt_helper::time_fraction(msg.time); + auto millis = fmt_helper::time_fraction(msg.time); fmt_helper::pad3(static_cast(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 { - auto micros = fmt_helper::time_fraction(msg.time); + auto micros = fmt_helper::time_fraction(msg.time); fmt_helper::pad6(static_cast(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 { - auto ns = fmt_helper::time_fraction(msg.time); + auto ns = fmt_helper::time_fraction(msg.time); 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 { auto duration = msg.time.time_since_epoch(); - auto seconds = std::chrono::duration_cast(duration).count(); + auto seconds = chrono::duration_cast(duration).count(); 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 { public: - const std::chrono::seconds cache_refresh = std::chrono::seconds(5); + const chrono::seconds cache_refresh = chrono::seconds(5); z_formatter() = default; z_formatter(const z_formatter &) = delete; @@ -354,7 +345,7 @@ public: } private: - log_clock::time_point last_update_{std::chrono::seconds(0)}; + log_clock::time_point last_update_{chrono::seconds(0)}; #ifdef _WIN32 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 { - using namespace std::chrono; + using namespace chrono; #ifndef SPDLOG_NO_DATETIME // cache the date/time part for the next second. @@ -522,7 +513,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter } private: - std::chrono::seconds cache_timestamp_{0}; + chrono::seconds cache_timestamp_{0}; fmt::basic_memory_buffer cached_datetime_; }; @@ -553,7 +544,7 @@ public: void format(const details::log_msg &msg, fmt::memory_buffer &dest) override { #ifndef SPDLOG_NO_DATETIME - auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); + auto secs = chrono::duration_cast(msg.time.time_since_epoch()); if (secs != last_log_secs_) { cached_tm_ = get_time_(msg); @@ -573,7 +564,7 @@ private: std::string eol_; pattern_time_type pattern_time_type_; std::tm cached_tm_; - std::chrono::seconds last_log_secs_; + chrono::seconds last_log_secs_; std::vector> formatters_; diff --git a/include/spdlog/details/periodic_worker.h b/include/spdlog/details/periodic_worker.h index 57e5fa77..66b869f2 100644 --- a/include/spdlog/details/periodic_worker.h +++ b/include/spdlog/details/periodic_worker.h @@ -12,29 +12,25 @@ // creates the thread on construction. // stops and joins the thread on destruction. -#include -#include -#include -#include -#include namespace spdlog { namespace details { + class periodic_worker { public: - periodic_worker(const std::function &callback_fun, std::chrono::seconds interval) + periodic_worker(const std::function &callback_fun, chrono::seconds interval) { - active_ = (interval > std::chrono::seconds::zero()); + active_ = (interval > chrono::seconds::zero()); if (!active_) { return; } - worker_thread_ = std::thread([this, callback_fun, interval]() { + worker_thread_ = thread([this, callback_fun, interval]() { for (;;) { - std::unique_lock lock(this->mutex_); + unique_lock lock(this->mutex_); if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; })) { return; // active_ == false, so exit this thread @@ -53,7 +49,7 @@ public: if (worker_thread_.joinable()) { { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); active_ = false; } cv_.notify_one(); @@ -63,9 +59,9 @@ public: private: bool active_; - std::thread worker_thread_; - std::mutex mutex_; - std::condition_variable cv_; + thread worker_thread_; + mutex mutex_; + condition_variable cv_; }; } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 3988552a..1f55966f 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -14,11 +14,6 @@ #include "spdlog/details/periodic_worker.h" #include "spdlog/logger.h" -#include -#include -#include -#include -#include namespace spdlog { namespace details { @@ -32,7 +27,7 @@ public: void register_logger(std::shared_ptr new_logger) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); auto logger_name = new_logger->name(); throw_if_exists_(logger_name); loggers_[logger_name] = std::move(new_logger); @@ -40,7 +35,7 @@ public: void register_and_init(std::shared_ptr new_logger) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); auto logger_name = new_logger->name(); throw_if_exists_(logger_name); @@ -61,27 +56,27 @@ public: std::shared_ptr get(const std::string &logger_name) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); auto found = loggers_.find(logger_name); return found == loggers_.end() ? nullptr : found->second; } void set_tp(std::shared_ptr tp) { - std::lock_guard lock(tp_mutex_); + lock_guard lock(tp_mutex_); tp_ = std::move(tp); } std::shared_ptr get_tp() { - std::lock_guard lock(tp_mutex_); + lock_guard lock(tp_mutex_); return tp_; } // Set global formatter. Each sink in each logger will get a clone of this object void set_formatter(std::unique_ptr formatter) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); formatter_ = std::move(formatter); for (auto &l : loggers_) { @@ -91,7 +86,7 @@ public: void set_level(level::level_enum log_level) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) { l.second->set_level(log_level); @@ -101,7 +96,7 @@ public: void flush_on(level::level_enum log_level) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) { l.second->flush_on(log_level); @@ -109,16 +104,16 @@ public: flush_level_ = log_level; } - void flush_every(std::chrono::seconds interval) + void flush_every(chrono::seconds interval) { - std::lock_guard lock(flusher_mutex_); + lock_guard lock(flusher_mutex_); std::function clbk = std::bind(®istry::flush_all, this); periodic_flusher_.reset(new periodic_worker(clbk, interval)); } void set_error_handler(log_err_handler handler) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) { l.second->set_error_handler(handler); @@ -128,7 +123,7 @@ public: void apply_all(const std::function)> &fun) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) { fun(l.second); @@ -137,7 +132,7 @@ public: void flush_all() { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) { l.second->flush(); @@ -146,13 +141,13 @@ public: void drop(const std::string &logger_name) { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); loggers_.erase(logger_name); } void drop_all() { - std::lock_guard lock(logger_map_mutex_); + lock_guard lock(logger_map_mutex_); loggers_.clear(); } @@ -160,19 +155,19 @@ public: void shutdown() { { - std::lock_guard lock(flusher_mutex_); + lock_guard lock(flusher_mutex_); periodic_flusher_.reset(); } drop_all(); { - std::lock_guard lock(tp_mutex_); + lock_guard lock(tp_mutex_); tp_.reset(); } } - std::recursive_mutex &tp_mutex() + recursive_mutex &tp_mutex() { return tp_mutex_; } @@ -199,8 +194,8 @@ private: } } - std::mutex logger_map_mutex_, flusher_mutex_; - std::recursive_mutex tp_mutex_; + mutex logger_map_mutex_, flusher_mutex_; + recursive_mutex tp_mutex_; std::unordered_map> loggers_; std::unique_ptr formatter_; level::level_enum level_ = level::info; diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index b45b6d2b..d6ed6968 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -4,10 +4,8 @@ #include "spdlog/details/mpmc_blocking_q.h" #include "spdlog/details/os.h" -#include -#include -#include -#include +// #include +// #include namespace spdlog { namespace details { @@ -160,7 +158,7 @@ public: private: q_type q_; - std::vector threads_; + std::vector threads_; void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) { @@ -185,7 +183,7 @@ private: bool process_next_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) { return true; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 91eec4bc..e130d4af 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -22,9 +22,6 @@ #include "spdlog/formatter.h" #include "spdlog/sinks/sink.h" -#include -#include -#include namespace spdlog { @@ -154,7 +151,7 @@ protected: #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT std::wstring_convert> wstring_converter_; - std::mutex wstring_converter_mutex_; + mutex wstring_converter_mutex_; #endif }; } // namespace spdlog diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 3f6c63b6..8a960fac 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -9,10 +9,6 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/details/os.h" -#include -#include -#include -#include namespace spdlog { namespace sinks { @@ -50,7 +46,7 @@ public: void set_color(level::level_enum color_level, const std::string &color) { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); colors_[color_level] = color; } @@ -84,11 +80,11 @@ public: const std::string on_cyan = "\033[46m"; 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. // If color is not supported in the terminal, log as is instead. - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); fmt::memory_buffer formatted; formatter_->format(msg, formatted); @@ -112,19 +108,19 @@ public: void flush() override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); fflush(target_file_); } void set_pattern(const std::string &pattern) SPDLOG_FINAL { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } void set_formatter(std::unique_ptr sink_formatter) override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index 43863eae..8d4623fb 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -32,25 +32,25 @@ public: void log(const details::log_msg &msg) SPDLOG_FINAL { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); sink_it_(msg); } void flush() SPDLOG_FINAL override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); flush_(); } void set_pattern(const std::string &pattern) SPDLOG_FINAL override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } void set_formatter(std::unique_ptr sink_formatter) SPDLOG_FINAL override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index 5db38e8a..d4727f30 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -9,8 +9,6 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/spdlog.h" -#include -#include namespace spdlog { namespace sinks { @@ -43,7 +41,7 @@ private: details::file_helper file_helper_; }; -using basic_file_sink_mt = basic_file_sink; +using basic_file_sink_mt = basic_file_sink; using basic_file_sink_st = basic_file_sink; } // namespace sinks diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 940d55d4..093f3ad7 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -10,11 +10,6 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/spdlog.h" -#include -#include -#include -#include -#include namespace spdlog { namespace sinks { @@ -97,7 +92,7 @@ private: { return rotation_time; } - return {rotation_time + std::chrono::hours(24)}; + return {rotation_time + chrono::hours(24)}; } filename_t base_filename_; @@ -108,7 +103,7 @@ private: bool truncate_; }; -using daily_file_sink_mt = daily_file_sink; +using daily_file_sink_mt = daily_file_sink; using daily_file_sink_st = daily_file_sink; } // namespace sinks diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 8d2f6f9e..6bb53471 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -9,10 +9,6 @@ #include "spdlog/details/log_msg.h" #include "spdlog/details/null_mutex.h" -#include -#include -#include -#include // Distribution sink (mux). Stores a vector of sinks which get called when log // is called @@ -30,13 +26,13 @@ public: void add_sink(std::shared_ptr sink) { - std::lock_guard lock(base_sink::mutex_); + lock_guard lock(base_sink::mutex_); sinks_.push_back(sink); } void remove_sink(std::shared_ptr sink) { - std::lock_guard lock(base_sink::mutex_); + lock_guard lock(base_sink::mutex_); sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); } @@ -61,7 +57,7 @@ protected: std::vector> sinks_; }; -using dist_sink_mt = dist_sink; +using dist_sink_mt = dist_sink; using dist_sink_st = dist_sink; } // namespace sinks diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index 73b8c2b7..92c40d41 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -8,7 +8,6 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" -#include namespace spdlog { namespace sinks { @@ -21,7 +20,7 @@ protected: void flush_() override {} }; -using null_sink_mt = null_sink; +using null_sink_mt = null_sink; using null_sink_st = null_sink; } // namespace sinks diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h index 5f31993c..6bfe409f 100644 --- a/include/spdlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -8,8 +8,6 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" -#include -#include namespace spdlog { namespace sinks { @@ -44,7 +42,7 @@ protected: bool force_flush_; }; -using ostream_sink_mt = ostream_sink; +using ostream_sink_mt = ostream_sink; using ostream_sink_st = ostream_sink; } // namespace sinks diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 7ed2385a..3708d43d 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -4,18 +4,12 @@ // #pragma once +#include "spdlog/spdlog.h" #include "spdlog/details/file_helper.h" #include "spdlog/details/null_mutex.h" #include "spdlog/fmt/fmt.h" #include "spdlog/sinks/base_sink.h" -#include "spdlog/spdlog.h" -#include -#include -#include -#include -#include -#include namespace spdlog { namespace sinks { @@ -119,7 +113,7 @@ private: details::file_helper file_helper_; }; -using rotating_file_sink_mt = rotating_file_sink; +using rotating_file_sink_mt = rotating_file_sink; using rotating_file_sink_st = rotating_file_sink; } // namespace sinks diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index e12739c1..6c282311 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -5,14 +5,11 @@ #pragma once +#include "spdlog/spdlog.h" #include "spdlog/details/console_globals.h" #include "spdlog/details/null_mutex.h" -#include "spdlog/spdlog.h" -#include -#include -#include -#include +#include "spdlog/details/console_globals.h" namespace spdlog { @@ -35,7 +32,7 @@ public: void log(const details::log_msg &msg) override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); fmt::memory_buffer formatted; formatter_->format(msg, formatted); fwrite(formatted.data(), sizeof(char), formatted.size(), file_); @@ -44,19 +41,19 @@ public: void flush() override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); fflush(file_); } void set_pattern(const std::string &pattern) override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } void set_formatter(std::unique_ptr sink_formatter) override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 151e7a11..171eb4bd 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -8,8 +8,6 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/spdlog.h" -#include -#include #include namespace spdlog { @@ -70,7 +68,7 @@ private: } }; -using syslog_sink_mt = syslog_sink; +using syslog_sink_mt = syslog_sink; using syslog_sink_st = syslog_sink; } // namespace sinks diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 8c63e7fc..d3c0dfd2 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -10,10 +10,6 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/sink.h" -#include -#include -#include -#include #include namespace spdlog { @@ -57,13 +53,13 @@ public: // change the color for the given level void set_color(level::level_enum level, WORD color) { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); colors_[level] = color; } void log(const details::log_msg &msg) SPDLOG_FINAL override { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); fmt::memory_buffer formatted; formatter_->format(msg, formatted); if (msg.color_range_end > msg.color_range_start) @@ -92,13 +88,13 @@ public: void set_pattern(const std::string &pattern) override SPDLOG_FINAL { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } void set_formatter(std::unique_ptr sink_formatter) override SPDLOG_FINAL { - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 40640ab9..57a53584 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -12,10 +12,6 @@ #include "spdlog/logger.h" #include "spdlog/version.h" -#include -#include -#include -#include namespace spdlog { @@ -81,7 +77,7 @@ inline void flush_on(level::level_enum log_level) // Start/Restart a periodic flusher thread // 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); }