code format
Some checks failed
macos / macOS Clang (C++17, Release) (push) Waiting to run
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Waiting to run
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Waiting to run
windows / build (map[BUILD_EXAMPLE:ON BUILD_SHARED:OFF BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Waiting to run
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019]) (push) Waiting to run
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019]) (push) Waiting to run
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[asan:ON build_type:Debug … (push) Failing after 4s
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Debug compiler… (push) Failing after 2s
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Release compil… (push) Failing after 2s

This commit is contained in:
gabime 2025-01-17 20:59:46 +02:00
parent b9f0243405
commit 47fe6ef92a
23 changed files with 68 additions and 76 deletions

View File

@ -6,14 +6,14 @@
// //
// bench.cpp : spdlog benchmarks // bench.cpp : spdlog benchmarks
// //
#include <algorithm>
#include <atomic> #include <atomic>
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
#include <locale>
#include <memory> #include <memory>
#include <string> #include <string>
#include <thread> #include <thread>
#include <locale>
#include <algorithm>
#include "spdlog/sinks/async_sink.h" #include "spdlog/sinks/async_sink.h"
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
@ -103,7 +103,7 @@ int main(int argc, char *argv[]) {
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink)); auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
bench_mt(howmany, std::move(logger), threads); 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("");
spdlog::info("*********************************"); spdlog::info("*********************************");

View File

@ -124,7 +124,8 @@ int main(int argc, char *argv[]) {
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime(); benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
// rotating st // rotating st
auto rotating_st = spdlog::create<rotating_file_sink_st>("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files); auto rotating_st =
spdlog::create<rotating_file_sink_st>("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime(); benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
// daily st // 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(); benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
// rotating mt // rotating mt
auto rotating_mt = spdlog::create<rotating_file_sink_mt>("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files); auto rotating_mt =
spdlog::create<rotating_file_sink_mt>("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(); benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
// daily mt // daily mt
@ -151,7 +153,8 @@ int main(int argc, char *argv[]) {
} }
using spdlog::sinks::async_sink; using spdlog::sinks::async_sink;
async_sink::config config; async_sink::config config;
config.queue_size = 3 * 1024 * 1024;; config.queue_size = 3 * 1024 * 1024;
;
config.sinks.push_back(std::make_shared<null_sink_st>()); config.sinks.push_back(std::make_shared<null_sink_st>());
config.policy = async_sink::overflow_policy::overrun_oldest; config.policy = async_sink::overflow_policy::overrun_oldest;
auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::make_shared<async_sink>(config)); auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::make_shared<async_sink>(config));

View File

@ -88,7 +88,7 @@ void stdout_logger_example() {
// Create color multithreading logger. // Create color multithreading logger.
auto console = spdlog::create<stdout_color_sink_mt>("console"); auto console = spdlog::create<stdout_color_sink_mt>("console");
// or for stderr: // or for stderr:
//auto console = spdlog::create<stderr_color_sink_mt>("console"); // auto console = spdlog::create<stderr_color_sink_mt>("console");
} }
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"

View File

@ -6,13 +6,13 @@
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <cstdint>
#include <exception> #include <exception>
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <cstdint>
#include "./source_loc.h" #include "./source_loc.h"
#include "fmt/base.h" #include "fmt/base.h"

View File

@ -4,29 +4,27 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include "./log_msg.h" #include "./log_msg.h"
namespace spdlog { namespace spdlog {
namespace details { namespace details {
// Extend log_msg with internal buffer to store its payload. // Extend log_msg with internal buffer to store its payload.
// This is needed since log_msg holds string_views that points to stack data. // This is needed since log_msg holds string_views that points to stack data.
class SPDLOG_API async_log_msg : public log_msg { class SPDLOG_API async_log_msg : public log_msg {
public: public:
enum class type:std::uint8_t { log, flush, terminate }; enum class type : std::uint8_t { log, flush, terminate };
async_log_msg() = default; async_log_msg() = default;
explicit async_log_msg(type type); explicit async_log_msg(type type);
async_log_msg(type type, const log_msg &orig_msg); async_log_msg(type type, const log_msg &orig_msg);
~async_log_msg() = default; ~async_log_msg() = default;
async_log_msg(const async_log_msg &other); async_log_msg(const async_log_msg &other);
async_log_msg(async_log_msg &&other) noexcept; async_log_msg(async_log_msg &&other) noexcept;
async_log_msg &operator=(const async_log_msg &other); async_log_msg &operator=(const async_log_msg &other);
async_log_msg &operator=(async_log_msg &&other) noexcept; 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: private:
type msg_type_{type::log}; type msg_type_{type::log};
memory_buf_t buffer_; memory_buf_t buffer_;

View File

@ -3,10 +3,11 @@
#pragma once #pragma once
#include <string>
#include <chrono> #include <chrono>
#include <exception> #include <exception>
#include <mutex> #include <mutex>
#include <string>
#include "spdlog/common.h" #include "spdlog/common.h"
// by default, prints the error to stderr, at max rate of 1/sec thread safe // 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_; err_handler custom_err_handler_;
std::chrono::steady_clock::time_point last_report_time_; std::chrono::steady_clock::time_point last_report_time_;
mutable std::mutex mutex_; mutable std::mutex mutex_;
public: public:
err_helper() = default; err_helper() = default;
~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 handle_unknown_ex(const std::string& origin, const source_loc& loc) noexcept;
void set_err_handler(err_handler handler); void set_err_handler(err_handler handler);
}; };
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@ -42,7 +42,7 @@ public:
logger(std::string name, sinks_init_list sinks) logger(std::string name, sinks_init_list sinks)
: logger(std::move(name), sinks.begin(), sinks.end()) {} : logger(std::move(name), sinks.begin(), sinks.end()) {}
logger(const logger &other) ; logger(const logger &other);
logger(logger &&other) noexcept; logger(logger &&other) noexcept;
~logger() = default; ~logger() = default;

View File

@ -91,14 +91,13 @@ public:
return std::make_shared<async_sink>(cfg); return std::make_shared<async_sink>(cfg);
} }
private: private:
using async_log_msg = details::async_log_msg; using async_log_msg = details::async_log_msg;
using queue_t = details::mpmc_blocking_queue<async_log_msg>; using queue_t = details::mpmc_blocking_queue<async_log_msg>;
void enqueue_message_(details::async_log_msg &&msg) const; void enqueue_message_(details::async_log_msg &&msg) const;
void backend_loop_(); void backend_loop_();
void backend_log_(const details::log_msg &msg) ; void backend_log_(const details::log_msg &msg);
void backend_flush_(); void backend_flush_();
config config_; config config_;

View File

@ -16,11 +16,11 @@
#include <mongocxx/client.hpp> #include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp> #include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp> #include <mongocxx/uri.hpp>
#include <mutex> #include <mutex>
#include "../details/null_mutex.h"
#include "../common.h" #include "../common.h"
#include "../details/log_msg.h" #include "../details/log_msg.h"
#include "../details/null_mutex.h"
#include "./base_sink.h" #include "./base_sink.h"
namespace spdlog { namespace spdlog {

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <mutex> #include <mutex>
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "./base_sink.h" #include "./base_sink.h"

View File

@ -46,7 +46,9 @@ public:
} }
protected: 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 {} void flush_() override {}
private: private:

View File

@ -17,8 +17,8 @@ public:
virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0; virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0;
void set_level(level level) { level_.store(level, std::memory_order_relaxed); } void set_level(level level) { level_.store(level, std::memory_order_relaxed); }
level log_level() const { return 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);} bool should_log(level msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); }
protected: protected:
// sink log level - default is all // sink log level - default is all

View File

@ -8,8 +8,8 @@
#include <syslog.h> #include <syslog.h>
#include <array> #include <array>
#include <string>
#include <mutex> #include <mutex>
#include <string>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -19,7 +19,7 @@ namespace sinks {
template <typename Mutex> template <typename Mutex>
class syslog_sink final : public base_sink<Mutex> { class syslog_sink final : public base_sink<Mutex> {
public: 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}, : enable_formatting_{enable_formatting},
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
/* spdlog::level::debug */ LOG_DEBUG, /* spdlog::level::debug */ LOG_DEBUG,

View File

@ -6,7 +6,6 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
async_log_msg::async_log_msg(const type type) async_log_msg::async_log_msg(const type type)
: msg_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__) // are compiler generated const chars* (__FILE__, __LINE__, __FUNCTION__)
// if you pass custom strings to source location, make sure they outlive the async_log_msg // 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) 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(logger_name);
buffer_.append(payload); buffer_.append(payload);
update_string_views(); update_string_views();
} }
async_log_msg::async_log_msg(const async_log_msg &other) 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(logger_name);
buffer_.append(payload); buffer_.append(payload);
update_string_views(); update_string_views();
} }
async_log_msg::async_log_msg(async_log_msg &&other) noexcept 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(); update_string_views();
} }

View File

@ -109,7 +109,6 @@ template <typename Mutex>
ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode) ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
: ansicolor_sink<Mutex>(stderr, mode) {} : ansicolor_sink<Mutex>(stderr, mode) {}
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

View File

@ -33,26 +33,21 @@ async_sink::async_sink(config async_config)
}); });
} }
async_sink::~async_sink() { async_sink::~async_sink() {
try { try {
q_->enqueue(async_log_msg(async_log_msg::type::terminate)); q_->enqueue(async_log_msg(async_log_msg::type::terminate));
worker_thread_.join(); worker_thread_.join();
} catch (...) { } catch (...) {
terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag. terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag.
#ifndef NDEBUG #ifndef NDEBUG
printf("Exception in ~async_sink()\n"); printf("Exception in ~async_sink()\n");
#endif #endif
} }
} }
void async_sink::log(const details::log_msg &msg) { void async_sink::log(const details::log_msg &msg) { enqueue_message_(async_log_msg(async_log_msg::type::log, msg)); }
enqueue_message_(async_log_msg(async_log_msg::type::log, msg));
}
void async_sink::flush() { void async_sink::flush() { enqueue_message_(details::async_log_msg(async_log_msg::type::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_formatter>(pattern)); } void async_sink::set_pattern(const std::string &pattern) { set_formatter(std::make_unique<pattern_formatter>(pattern)); }
@ -83,7 +78,8 @@ bool async_sink::wait_all(const std::chrono::milliseconds timeout) const {
} }
void async_sink::wait_all() 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(); } size_t async_sink::get_overrun_counter() const { return q_->overrun_counter(); }

View File

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/pattern_formatter.h" #include "spdlog/pattern_formatter.h"
@ -53,8 +54,8 @@ void base_sink<Mutex>::set_formatter_(std::unique_ptr<formatter> sink_formatter)
formatter_ = std::move(sink_formatter); formatter_ = std::move(sink_formatter);
} }
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
// template instantiations // template instantiations
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"

View File

@ -2,9 +2,11 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/common.h"
#include <mutex> #include <mutex>
#include "spdlog/common.h"
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -34,7 +36,6 @@ void basic_file_sink<Mutex>::flush_() {
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
// template instantiations // template instantiations
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>; template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>;

View File

@ -8,6 +8,7 @@
// clang-format on // clang-format on
#include "spdlog/sinks/wincolor_sink.h" #include "spdlog/sinks/wincolor_sink.h"
#include "spdlog/common.h" #include "spdlog/common.h"
namespace spdlog { namespace spdlog {
@ -131,7 +132,6 @@ template <typename Mutex>
wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode) wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode)
: wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {} : wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

View File

@ -13,21 +13,17 @@
namespace spdlog { namespace spdlog {
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER #ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
static std::shared_ptr<logger> s_logger = std::make_shared<logger>("", std::make_shared<sinks::stdout_color_sink_mt>()); static std::shared_ptr<logger> s_logger = std::make_shared<logger>("", std::make_shared<sinks::stdout_color_sink_mt>());
#else #else
static std::short_ptr<logger> s_logger = nullptr; static std::short_ptr<logger> s_logger = nullptr;
#endif #endif
std::shared_ptr<logger> global_logger() { return s_logger; } std::shared_ptr<logger> global_logger() { return s_logger; }
void set_global_logger(std::shared_ptr<logger> global_logger) { s_logger = std::move(global_logger); } void set_global_logger(std::shared_ptr<logger> global_logger) { s_logger = std::move(global_logger); }
logger *global_logger_raw() noexcept { logger *global_logger_raw() noexcept { return s_logger.get(); }
return s_logger.get();
}
void set_formatter(std::unique_ptr<formatter> formatter) { global_logger()->set_formatter(std::move(formatter)); } void set_formatter(std::unique_ptr<formatter> formatter) { global_logger()->set_formatter(std::move(formatter)); }

View File

@ -71,7 +71,6 @@ TEST_CASE("discard policy discard_new ", "[async]") {
auto as = std::make_shared<async_sink>(config); auto as = std::make_shared<async_sink>(config);
auto logger = std::make_shared<spdlog::logger>("async_logger", as); auto logger = std::make_shared<spdlog::logger>("async_logger", as);
REQUIRE(as->get_config().policy == async_sink::overflow_policy::discard_new); REQUIRE(as->get_config().policy == async_sink::overflow_policy::discard_new);
REQUIRE(as->get_discard_counter() == 0); REQUIRE(as->get_discard_counter() == 0);
REQUIRE(as->get_overrun_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))); REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
} }
TEST_CASE("bad_ctor", "[async]") { TEST_CASE("bad_ctor", "[async]") {
async_sink::config cfg; async_sink::config cfg;
cfg.queue_size = 0; cfg.queue_size = 0;
@ -303,9 +301,9 @@ TEST_CASE("custom_err_handler", "[async]") {
test_sink->set_exception(std::runtime_error("test backend exception")); test_sink->set_exception(std::runtime_error("test backend exception"));
async_sink::config config; async_sink::config config;
config.sinks.push_back(std::move(test_sink)); 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<async_sink>(config); auto asink = std::make_shared<async_sink>(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 // lvalue logger so will be destructed here already so all messages were processed
REQUIRE(error_called); REQUIRE(error_called);
} }
@ -334,9 +332,9 @@ TEST_CASE("wait_all", "[async]") {
REQUIRE(elapsed < delay * 3); REQUIRE(elapsed < delay * 3);
// wait enough time for all messages to be processed // 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(messages * delay)));
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(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
} }
// test wait_all without timeout // test wait_all without timeout

View File

@ -18,7 +18,6 @@ protected:
}; };
struct custom_ex {}; struct custom_ex {};
using namespace spdlog::sinks; using namespace spdlog::sinks;
TEST_CASE("default_error_handler", "[errors]") { TEST_CASE("default_error_handler", "[errors]") {
prepare_logdir(); prepare_logdir();
@ -36,9 +35,7 @@ TEST_CASE("custom_error_handler", "[errors]") {
prepare_logdir(); prepare_logdir();
auto logger = spdlog::create<basic_file_sink_mt>("test-format-error", log_filename); auto logger = spdlog::create<basic_file_sink_mt>("test-format-error", log_filename);
logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string & msg) { logger->set_error_handler([=](const std::string &msg) { REQUIRE(msg == "argument not found"); });
REQUIRE(msg == "argument not found");
});
logger->info("Good message #1"); logger->info("Good message #1");
REQUIRE_NOTHROW(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx")); REQUIRE_NOTHROW(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"));
logger->info("Good message #2"); logger->info("Good message #2");

View File

@ -6,9 +6,9 @@
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <exception>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include <exception>
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
@ -37,13 +37,9 @@ public:
delay_ = delay; delay_ = delay;
} }
void set_exception(const std::runtime_error& ex) { void set_exception(const std::runtime_error& ex) { exception_ptr_ = std::make_exception_ptr(ex); }
exception_ptr_ = std::make_exception_ptr(ex);
}
void clear_exception() { void clear_exception() { exception_ptr_ = nullptr; }
exception_ptr_ = nullptr;
}
// return last output without the eol // return last output without the eol
std::vector<std::string> lines() { std::vector<std::string> lines() {
@ -52,7 +48,7 @@ public:
} }
protected: protected:
void sink_it_(const details::log_msg &msg) override { void sink_it_(const details::log_msg& msg) override {
if (exception_ptr_) { if (exception_ptr_) {
std::rethrow_exception(exception_ptr_); std::rethrow_exception(exception_ptr_);
} }
@ -78,7 +74,7 @@ protected:
size_t flush_counter_{0}; size_t flush_counter_{0};
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()}; std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
std::vector<std::string> lines_; std::vector<std::string> 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<std::mutex>; using test_sink_mt = test_sink<std::mutex>;