diff --git a/bench/async_bench.cpp b/bench/async_bench.cpp index 18370328..77307495 100644 --- a/bench/async_bench.cpp +++ b/bench/async_bench.cpp @@ -6,14 +6,14 @@ // // bench.cpp : spdlog benchmarks // +#include #include -#include #include +#include +#include #include #include #include -#include -#include #include "spdlog/sinks/async_sink.h" #include "spdlog/sinks/basic_file_sink.h" @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) { auto logger = std::make_shared("async_logger", std::move(async_sink)); bench_mt(howmany, std::move(logger), threads); } - //verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written + // verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written } spdlog::info(""); spdlog::info("*********************************"); diff --git a/bench/latency.cpp b/bench/latency.cpp index 55e324fe..ac15974a 100644 --- a/bench/latency.cpp +++ b/bench/latency.cpp @@ -124,7 +124,8 @@ int main(int argc, char *argv[]) { benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime(); // rotating st - auto rotating_st = spdlog::create("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files); + auto rotating_st = + spdlog::create("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files); benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime(); // daily st @@ -142,7 +143,8 @@ int main(int argc, char *argv[]) { benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime(); // rotating mt - auto rotating_mt = spdlog::create("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files); + auto rotating_mt = + spdlog::create("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files); benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime(); // daily mt @@ -151,7 +153,8 @@ int main(int argc, char *argv[]) { } using spdlog::sinks::async_sink; async_sink::config config; - config.queue_size = 3 * 1024 * 1024;; + config.queue_size = 3 * 1024 * 1024; + ; config.sinks.push_back(std::make_shared()); config.policy = async_sink::overflow_policy::overrun_oldest; auto async_logger = std::make_shared("async_logger", std::make_shared(config)); diff --git a/example/example.cpp b/example/example.cpp index f1dafa2c..8f645ff0 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -88,7 +88,7 @@ void stdout_logger_example() { // Create color multithreading logger. auto console = spdlog::create("console"); // or for stderr: - //auto console = spdlog::create("console"); + // auto console = spdlog::create("console"); } #include "spdlog/sinks/basic_file_sink.h" diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 2d69fb79..07f09a63 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -6,13 +6,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include "./source_loc.h" #include "fmt/base.h" diff --git a/include/spdlog/details/async_log_msg.h b/include/spdlog/details/async_log_msg.h index 52dee73d..e5f006c0 100644 --- a/include/spdlog/details/async_log_msg.h +++ b/include/spdlog/details/async_log_msg.h @@ -4,29 +4,27 @@ #pragma once #include + #include "./log_msg.h" namespace spdlog { namespace details { - // Extend log_msg with internal buffer to store its payload. // This is needed since log_msg holds string_views that points to stack data. - class SPDLOG_API async_log_msg : public log_msg { public: - enum class type:std::uint8_t { log, flush, terminate }; + enum class type : std::uint8_t { log, flush, terminate }; async_log_msg() = default; explicit async_log_msg(type type); async_log_msg(type type, const log_msg &orig_msg); - ~async_log_msg() = default; async_log_msg(const async_log_msg &other); async_log_msg(async_log_msg &&other) noexcept; async_log_msg &operator=(const async_log_msg &other); async_log_msg &operator=(async_log_msg &&other) noexcept; + [[nodiscard]] type message_type() const { return msg_type_; } - type message_type() const {return msg_type_;} private: type msg_type_{type::log}; memory_buf_t buffer_; diff --git a/include/spdlog/details/err_helper.h b/include/spdlog/details/err_helper.h index 887948ef..25c23339 100644 --- a/include/spdlog/details/err_helper.h +++ b/include/spdlog/details/err_helper.h @@ -3,10 +3,11 @@ #pragma once -#include #include #include #include +#include + #include "spdlog/common.h" // by default, prints the error to stderr, at max rate of 1/sec thread safe @@ -16,6 +17,7 @@ class SPDLOG_API err_helper { err_handler custom_err_handler_; std::chrono::steady_clock::time_point last_report_time_; mutable std::mutex mutex_; + public: err_helper() = default; ~err_helper() = default; @@ -25,5 +27,5 @@ public: void handle_unknown_ex(const std::string& origin, const source_loc& loc) noexcept; void set_err_handler(err_handler handler); }; -} // namespace details -} // namespace spdlog +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 68e149b7..dbd932fa 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -42,7 +42,7 @@ public: logger(std::string name, sinks_init_list sinks) : logger(std::move(name), sinks.begin(), sinks.end()) {} - logger(const logger &other) ; + logger(const logger &other); logger(logger &&other) noexcept; ~logger() = default; diff --git a/include/spdlog/sinks/async_sink.h b/include/spdlog/sinks/async_sink.h index 4e756491..f11e108a 100644 --- a/include/spdlog/sinks/async_sink.h +++ b/include/spdlog/sinks/async_sink.h @@ -91,14 +91,13 @@ public: return std::make_shared(cfg); } - private: using async_log_msg = details::async_log_msg; using queue_t = details::mpmc_blocking_queue; void enqueue_message_(details::async_log_msg &&msg) const; void backend_loop_(); - void backend_log_(const details::log_msg &msg) ; + void backend_log_(const details::log_msg &msg); void backend_flush_(); config config_; diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h index 36d8945c..2c55649b 100644 --- a/include/spdlog/sinks/mongo_sink.h +++ b/include/spdlog/sinks/mongo_sink.h @@ -16,11 +16,11 @@ #include #include #include - #include -#include "../details/null_mutex.h" + #include "../common.h" #include "../details/log_msg.h" +#include "../details/null_mutex.h" #include "./base_sink.h" namespace spdlog { diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 6aa7d036..e2e0dd13 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -4,6 +4,7 @@ #pragma once #include + #include "../details/null_mutex.h" #include "./base_sink.h" diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index 677f76dd..b6964190 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -46,7 +46,9 @@ public: } protected: - void sink_it_(const details::log_msg &msg) override { q_.push_back(details::async_log_msg{details::async_log_msg::type::log, msg}); } + void sink_it_(const details::log_msg &msg) override { + q_.push_back(details::async_log_msg{details::async_log_msg::type::log, msg}); + } void flush_() override {} private: diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index cce83299..79f748c3 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -17,8 +17,8 @@ public: virtual void set_formatter(std::unique_ptr sink_formatter) = 0; void set_level(level level) { level_.store(level, std::memory_order_relaxed); } - level log_level() const { return level_.load(std::memory_order_relaxed);} - bool should_log(level msg_level) const {return msg_level >= level_.load(std::memory_order_relaxed);} + level log_level() const { return level_.load(std::memory_order_relaxed); } + bool should_log(level msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); } protected: // sink log level - default is all diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 52a486e1..28390ab3 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -8,8 +8,8 @@ #include #include -#include #include +#include namespace spdlog { namespace sinks { @@ -19,7 +19,7 @@ namespace sinks { template class syslog_sink final : public base_sink { public: - syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting=false) + syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting = false) : enable_formatting_{enable_formatting}, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, /* spdlog::level::debug */ LOG_DEBUG, diff --git a/src/details/async_log_msg.cpp b/src/details/async_log_msg.cpp index 62c95cab..c763e4dd 100644 --- a/src/details/async_log_msg.cpp +++ b/src/details/async_log_msg.cpp @@ -6,7 +6,6 @@ namespace spdlog { namespace details { - async_log_msg::async_log_msg(const type type) : msg_type_{type} {} @@ -15,21 +14,25 @@ async_log_msg::async_log_msg(const type type) // are compiler generated const chars* (__FILE__, __LINE__, __FUNCTION__) // if you pass custom strings to source location, make sure they outlive the async_log_msg async_log_msg::async_log_msg(const type type, const log_msg &orig_msg) - : log_msg{orig_msg}, msg_type_(type) { + : log_msg{orig_msg}, + msg_type_(type) { buffer_.append(logger_name); buffer_.append(payload); update_string_views(); } async_log_msg::async_log_msg(const async_log_msg &other) - : log_msg{other}, msg_type_{other.msg_type_} { + : log_msg{other}, + msg_type_{other.msg_type_} { buffer_.append(logger_name); buffer_.append(payload); update_string_views(); } async_log_msg::async_log_msg(async_log_msg &&other) noexcept - : log_msg{other}, msg_type_{other.msg_type_}, buffer_{std::move(other.buffer_)} { + : log_msg{other}, + msg_type_{other.msg_type_}, + buffer_{std::move(other.buffer_)} { update_string_views(); } diff --git a/src/sinks/ansicolor_sink.cpp b/src/sinks/ansicolor_sink.cpp index c4d02b64..ce6e7154 100644 --- a/src/sinks/ansicolor_sink.cpp +++ b/src/sinks/ansicolor_sink.cpp @@ -109,7 +109,6 @@ template ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) : ansicolor_sink(stderr, mode) {} - } // namespace sinks } // namespace spdlog diff --git a/src/sinks/async_sink.cpp b/src/sinks/async_sink.cpp index 51832c28..be6ec84d 100644 --- a/src/sinks/async_sink.cpp +++ b/src/sinks/async_sink.cpp @@ -33,26 +33,21 @@ async_sink::async_sink(config async_config) }); } - async_sink::~async_sink() { try { q_->enqueue(async_log_msg(async_log_msg::type::terminate)); worker_thread_.join(); } catch (...) { - terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag. - #ifndef NDEBUG - printf("Exception in ~async_sink()\n"); - #endif + terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag. +#ifndef NDEBUG + printf("Exception in ~async_sink()\n"); +#endif } } -void async_sink::log(const details::log_msg &msg) { - enqueue_message_(async_log_msg(async_log_msg::type::log, msg)); -} +void async_sink::log(const details::log_msg &msg) { enqueue_message_(async_log_msg(async_log_msg::type::log, msg)); } -void async_sink::flush() { - enqueue_message_(details::async_log_msg(async_log_msg::type::flush)); -} +void async_sink::flush() { enqueue_message_(details::async_log_msg(async_log_msg::type::flush)); } void async_sink::set_pattern(const std::string &pattern) { set_formatter(std::make_unique(pattern)); } @@ -83,7 +78,8 @@ bool async_sink::wait_all(const std::chrono::milliseconds timeout) const { } void async_sink::wait_all() const { - while (!wait_all(std::chrono::milliseconds(10))) { /* empty */ } + while (!wait_all(std::chrono::milliseconds(10))) { /* empty */ + } } size_t async_sink::get_overrun_counter() const { return q_->overrun_counter(); } diff --git a/src/sinks/base_sink.cpp b/src/sinks/base_sink.cpp index 3ac28a46..2db52457 100644 --- a/src/sinks/base_sink.cpp +++ b/src/sinks/base_sink.cpp @@ -5,6 +5,7 @@ #include #include + #include "spdlog/common.h" #include "spdlog/pattern_formatter.h" @@ -53,8 +54,8 @@ void base_sink::set_formatter_(std::unique_ptr sink_formatter) formatter_ = std::move(sink_formatter); } -} // namespace sinks -} // namespace spdlog +} // namespace sinks +} // namespace spdlog // template instantiations #include "spdlog/details/null_mutex.h" diff --git a/src/sinks/basic_file_sink.cpp b/src/sinks/basic_file_sink.cpp index a48d3a90..d324427f 100644 --- a/src/sinks/basic_file_sink.cpp +++ b/src/sinks/basic_file_sink.cpp @@ -2,9 +2,11 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) #include "spdlog/sinks/basic_file_sink.h" -#include "spdlog/common.h" + #include +#include "spdlog/common.h" + namespace spdlog { namespace sinks { @@ -34,7 +36,6 @@ void basic_file_sink::flush_() { } // namespace sinks } // namespace spdlog - // template instantiations #include "spdlog/details/null_mutex.h" template class SPDLOG_API spdlog::sinks::basic_file_sink; diff --git a/src/sinks/wincolor_sink.cpp b/src/sinks/wincolor_sink.cpp index 5fa8542e..20c0a4ed 100644 --- a/src/sinks/wincolor_sink.cpp +++ b/src/sinks/wincolor_sink.cpp @@ -8,6 +8,7 @@ // clang-format on #include "spdlog/sinks/wincolor_sink.h" + #include "spdlog/common.h" namespace spdlog { @@ -131,7 +132,6 @@ template wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} - } // namespace sinks } // namespace spdlog diff --git a/src/spdlog.cpp b/src/spdlog.cpp index 83a2c9a4..415cecad 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -13,21 +13,17 @@ namespace spdlog { - #ifndef SPDLOG_DISABLE_GLOBAL_LOGGER - static std::shared_ptr s_logger = std::make_shared("", std::make_shared()); +static std::shared_ptr s_logger = std::make_shared("", std::make_shared()); #else - static std::short_ptr s_logger = nullptr; +static std::short_ptr s_logger = nullptr; #endif - std::shared_ptr global_logger() { return s_logger; } void set_global_logger(std::shared_ptr global_logger) { s_logger = std::move(global_logger); } -logger *global_logger_raw() noexcept { - return s_logger.get(); -} +logger *global_logger_raw() noexcept { return s_logger.get(); } void set_formatter(std::unique_ptr formatter) { global_logger()->set_formatter(std::move(formatter)); } diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 062c66d6..0de44bdb 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -71,7 +71,6 @@ TEST_CASE("discard policy discard_new ", "[async]") { auto as = std::make_shared(config); auto logger = std::make_shared("async_logger", as); - REQUIRE(as->get_config().policy == async_sink::overflow_policy::discard_new); REQUIRE(as->get_discard_counter() == 0); REQUIRE(as->get_overrun_counter() == 0); @@ -167,7 +166,6 @@ TEST_CASE("to_file", "[async]") { REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol))); } - TEST_CASE("bad_ctor", "[async]") { async_sink::config cfg; cfg.queue_size = 0; @@ -303,9 +301,9 @@ TEST_CASE("custom_err_handler", "[async]") { test_sink->set_exception(std::runtime_error("test backend exception")); async_sink::config config; config.sinks.push_back(std::move(test_sink)); - config.custom_err_handler = [&error_called](const std::string &) { error_called = true;}; + config.custom_err_handler = [&error_called](const std::string &) { error_called = true; }; auto asink = std::make_shared(config); - spdlog::logger ("async_logger", std::move(asink)).info("Test"); + spdlog::logger("async_logger", std::move(asink)).info("Test"); // lvalue logger so will be destructed here already so all messages were processed REQUIRE(error_called); } @@ -334,9 +332,9 @@ TEST_CASE("wait_all", "[async]") { REQUIRE(elapsed < delay * 3); // wait enough time for all messages to be processed REQUIRE(as->wait_all(std::chrono::milliseconds(messages * delay))); - REQUIRE(as->wait_all(std::chrono::milliseconds(-10))); // no more messages - REQUIRE(as->wait_all(std::chrono::milliseconds(0))); // no more messages - REQUIRE(as->wait_all(std::chrono::milliseconds(10))); // no more messages + REQUIRE(as->wait_all(std::chrono::milliseconds(-10))); // no more messages + REQUIRE(as->wait_all(std::chrono::milliseconds(0))); // no more messages + REQUIRE(as->wait_all(std::chrono::milliseconds(10))); // no more messages } // test wait_all without timeout diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index 9985f4e8..602c6372 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -18,7 +18,6 @@ protected: }; struct custom_ex {}; - using namespace spdlog::sinks; TEST_CASE("default_error_handler", "[errors]") { prepare_logdir(); @@ -36,9 +35,7 @@ TEST_CASE("custom_error_handler", "[errors]") { prepare_logdir(); auto logger = spdlog::create("test-format-error", log_filename); logger->flush_on(spdlog::level::info); - logger->set_error_handler([=](const std::string & msg) { - REQUIRE(msg == "argument not found"); - }); + logger->set_error_handler([=](const std::string &msg) { REQUIRE(msg == "argument not found"); }); logger->info("Good message #1"); REQUIRE_NOTHROW(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx")); logger->info("Good message #2"); diff --git a/tests/test_sink.h b/tests/test_sink.h index 5bbfd434..ae7da237 100644 --- a/tests/test_sink.h +++ b/tests/test_sink.h @@ -6,9 +6,9 @@ #pragma once #include +#include #include #include -#include #include "spdlog/details/null_mutex.h" #include "spdlog/details/os.h" @@ -37,13 +37,9 @@ public: delay_ = delay; } - void set_exception(const std::runtime_error& ex) { - exception_ptr_ = std::make_exception_ptr(ex); - } + void set_exception(const std::runtime_error& ex) { exception_ptr_ = std::make_exception_ptr(ex); } - void clear_exception() { - exception_ptr_ = nullptr; - } + void clear_exception() { exception_ptr_ = nullptr; } // return last output without the eol std::vector lines() { @@ -52,7 +48,7 @@ public: } protected: - void sink_it_(const details::log_msg &msg) override { + void sink_it_(const details::log_msg& msg) override { if (exception_ptr_) { std::rethrow_exception(exception_ptr_); } @@ -78,7 +74,7 @@ protected: size_t flush_counter_{0}; std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()}; std::vector lines_; - std::exception_ptr exception_ptr_; // will be thrown on next log or flush if not null + std::exception_ptr exception_ptr_; // will be thrown on next log or flush if not null }; using test_sink_mt = test_sink;