diff --git a/bench/Makefile b/bench/Makefile index bfd1f1b0..68fe7428 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -1,6 +1,6 @@ CXX ?= g++ CXXFLAGS = -march=native -Wall -Wextra -pedantic -std=c++11 -pthread -I../include -fmax-errors=1 -CXX_RELEASE_FLAGS = -O3 -flto -Wl,--no-as-needed +CXX_RELEASE_FLAGS = -O3 -flto -Wl,--no-as-needed binaries=bench latency diff --git a/example/example.cpp b/example/example.cpp index 62db7120..4222c829 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -23,6 +23,7 @@ void syslog_example(); int main(int, char *[]) { + try { // console logging example @@ -52,8 +53,8 @@ int main(int, char *[]) // apply some function on all registered loggers spdlog::apply_all([&](std::shared_ptr l) { l->info("End of example."); }); - // Release and close all loggers - spdlog::drop_all(); + // release any threads created by spdlog, and drop all loggers in the registry. + spdlog::shutdown(); } // Exceptions will only be thrown upon failed logger or sink construction (not during logging) catch (const spdlog::spdlog_ex &ex) @@ -69,7 +70,7 @@ void stdout_example() { // create color multi threaded logger auto console = spdlog::stdout_color_mt("console"); - console->info("Welcome to spdlog!"); + console->info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); console->error("Some error message with arg: {}", 1); auto err_logger = spdlog::stderr_color_mt("stderr"); diff --git a/include/spdlog/async.h b/include/spdlog/async.h index 2f4de8f3..c55440be 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -38,11 +38,11 @@ struct async_factory_impl template static std::shared_ptr create(const std::string &logger_name, SinkArgs &&... args) { - using details::registry; + using details::registry; auto sink = std::make_shared(std::forward(args)...); - // create default tp if not already exists. - auto tp = registry::instance().create_tp_once(details::default_async_q_size, 1); + // create default tp if not already exists. + auto tp = registry::instance().create_tp_once(details::default_async_q_size, 1); auto new_logger = std::make_shared(logger_name, std::move(sink), std::move(tp), OverflowPolicy); registry::instance().register_and_init(new_logger); return new_logger; @@ -66,9 +66,9 @@ inline std::shared_ptr create_async_nb(const std::string &logger // set global thread pool. inline void init_thread_pool(size_t q_size, size_t thread_count) -{ +{ auto tp = std::make_shared(q_size, thread_count); - details::registry::instance().set_tp(std::move(tp)); + details::registry::instance().set_tp(std::move(tp)); } // get the global thread pool. diff --git a/include/spdlog/details/periodic_worker.h b/include/spdlog/details/periodic_worker.h index 96ec8db3..acc13117 100644 --- a/include/spdlog/details/periodic_worker.h +++ b/include/spdlog/details/periodic_worker.h @@ -50,15 +50,15 @@ public: // stop the worker thread and join it ~periodic_worker() { - if (worker_thread_.joinable()) - { - { - std::lock_guard lock(mutex_); - active_ = false; - } - cv_.notify_one(); - worker_thread_.join(); - } + if (worker_thread_.joinable()) + { + { + std::lock_guard lock(mutex_); + active_ = false; + } + cv_.notify_one(); + worker_thread_.join(); + } } private: diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index d5244ce3..93f83ab1 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -72,16 +72,16 @@ public: tp_ = std::move(tp); } - // create tp only if not exists already - std::shared_ptr create_tp_once(size_t queue_size, size_t n_threads) - { - std::lock_guard lock(tp_mutex_); - if (tp_ == nullptr) - { - tp_ = std::make_shared(queue_size, n_threads); - } - return tp_; - } + // create tp only if not exists already + std::shared_ptr create_tp_once(size_t queue_size, size_t n_threads) + { + std::lock_guard lock(tp_mutex_); + if (tp_ == nullptr) + { + tp_ = std::make_shared(queue_size, n_threads); + } + return tp_; + } std::shared_ptr get_tp() { @@ -146,14 +146,14 @@ public: } } - void flush_all() - { - std::lock_guard lock(logger_map_mutex_); - for (auto &l : loggers_) - { - l.second->flush(); - } - } + void flush_all() + { + std::lock_guard lock(logger_map_mutex_); + for (auto &l : loggers_) + { + l.second->flush(); + } + } void drop(const std::string &logger_name) { @@ -175,14 +175,14 @@ public: periodic_flusher_.reset(); } - drop_all(); + drop_all(); { std::lock_guard lock(tp_mutex_); tp_.reset(); } } - + static registry &instance() { static registry s_instance; @@ -208,10 +208,10 @@ private: throw spdlog_ex("logger with name '" + logger_name + "' already exists"); } } - + std::mutex logger_map_mutex_, flusher_mutex_; std::mutex tp_mutex_; - std::unordered_map> loggers_; + std::unordered_map> loggers_; std::unique_ptr formatter_; level::level_enum level_ = level::info; level::level_enum flush_level_ = level::off; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 8d611895..2963a2a0 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -10,6 +10,7 @@ #include "spdlog/common.h" #include "spdlog/details/registry.h" #include "spdlog/logger.h" +#include "spdlog/version.h" #include #include @@ -120,7 +121,7 @@ inline void drop_all() // stop any running threads started by spdlog and clean registry loggers inline void shutdown() { - details::registry::instance().shutdown(); + details::registry::instance().shutdown(); } ///////////////////////////////////////////////////////////////////////////////