mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-27 07:59:03 +08:00
catch potential flush exceptions
This commit is contained in:
parent
560df2878a
commit
f4db1c510c
@ -179,4 +179,6 @@ using filename_t = std::wstring;
|
||||
using filename_t = std::string;
|
||||
#endif
|
||||
|
||||
#define SPDLOG_CATCH_AND_HANDLE catch (const std::exception &ex) {_err_handler(ex.what());}\
|
||||
catch (...) {_err_handler("Unknown exeption in logger");}
|
||||
} // namespace spdlog
|
||||
|
@ -30,313 +30,317 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
namespace details {
|
||||
|
||||
class async_log_helper
|
||||
{
|
||||
// Async msg to move to/from the queue
|
||||
// Movable only. should never be copied
|
||||
enum class async_msg_type
|
||||
{
|
||||
log,
|
||||
flush,
|
||||
terminate
|
||||
};
|
||||
class async_log_helper
|
||||
{
|
||||
// Async msg to move to/from the queue
|
||||
// Movable only. should never be copied
|
||||
enum class async_msg_type
|
||||
{
|
||||
log,
|
||||
flush,
|
||||
terminate
|
||||
};
|
||||
|
||||
struct async_msg
|
||||
{
|
||||
level::level_enum level;
|
||||
log_clock::time_point time;
|
||||
size_t thread_id;
|
||||
std::string txt;
|
||||
async_msg_type msg_type;
|
||||
size_t msg_id;
|
||||
struct async_msg
|
||||
{
|
||||
level::level_enum level;
|
||||
log_clock::time_point time;
|
||||
size_t thread_id;
|
||||
std::string txt;
|
||||
async_msg_type msg_type;
|
||||
size_t msg_id;
|
||||
|
||||
async_msg() = default;
|
||||
~async_msg() = default;
|
||||
async_msg() = default;
|
||||
~async_msg() = default;
|
||||
|
||||
explicit async_msg(async_msg_type m_type)
|
||||
: level(level::info)
|
||||
, thread_id(0)
|
||||
, msg_type(m_type)
|
||||
, msg_id(0)
|
||||
{
|
||||
}
|
||||
explicit async_msg(async_msg_type m_type)
|
||||
: level(level::info)
|
||||
, thread_id(0)
|
||||
, msg_type(m_type)
|
||||
, msg_id(0)
|
||||
{
|
||||
}
|
||||
|
||||
async_msg(async_msg &&other) = default;
|
||||
async_msg &operator=(async_msg &&other) = default;
|
||||
async_msg(async_msg &&other) = default;
|
||||
async_msg &operator=(async_msg &&other) = default;
|
||||
|
||||
// never copy or assign. should only be moved..
|
||||
async_msg(const async_msg &) = delete;
|
||||
async_msg &operator=(const async_msg &other) = delete;
|
||||
// never copy or assign. should only be moved..
|
||||
async_msg(const async_msg &) = delete;
|
||||
async_msg &operator=(const async_msg &other) = delete;
|
||||
|
||||
// construct from log_msg
|
||||
explicit async_msg(const details::log_msg &m)
|
||||
: level(m.level)
|
||||
, time(m.time)
|
||||
, thread_id(m.thread_id)
|
||||
, txt(m.raw.data(), m.raw.size())
|
||||
, msg_type(async_msg_type::log)
|
||||
, msg_id(m.msg_id)
|
||||
{
|
||||
}
|
||||
// construct from log_msg
|
||||
explicit async_msg(const details::log_msg &m)
|
||||
: level(m.level)
|
||||
, time(m.time)
|
||||
, thread_id(m.thread_id)
|
||||
, txt(m.raw.data(), m.raw.size())
|
||||
, msg_type(async_msg_type::log)
|
||||
, msg_id(m.msg_id)
|
||||
{
|
||||
}
|
||||
|
||||
// copy into log_msg
|
||||
void fill_log_msg(log_msg &msg, std::string* logger_name)
|
||||
{
|
||||
msg.logger_name = logger_name;
|
||||
msg.level = level;
|
||||
msg.time = time;
|
||||
msg.thread_id = thread_id;
|
||||
msg.raw.clear();
|
||||
msg.raw << txt;
|
||||
msg.msg_id = msg_id;
|
||||
}
|
||||
};
|
||||
// copy into log_msg
|
||||
void fill_log_msg(log_msg &msg, std::string* logger_name)
|
||||
{
|
||||
msg.logger_name = logger_name;
|
||||
msg.level = level;
|
||||
msg.time = time;
|
||||
msg.thread_id = thread_id;
|
||||
msg.raw.clear();
|
||||
msg.raw << txt;
|
||||
msg.msg_id = msg_id;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
using item_type = async_msg;
|
||||
using q_type = details::mpmc_bounded_queue<item_type>;
|
||||
public:
|
||||
using item_type = async_msg;
|
||||
using q_type = details::mpmc_bounded_queue<item_type>;
|
||||
|
||||
using clock = std::chrono::steady_clock;
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
async_log_helper(std::string logger_name,
|
||||
formatter_ptr formatter,
|
||||
std::vector<sink_ptr> sinks,
|
||||
size_t queue_size,
|
||||
const log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||
std::function<void()> worker_warmup_cb = nullptr,
|
||||
const std::chrono::milliseconds &flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||
std::function<void()> worker_teardown_cb = nullptr);
|
||||
async_log_helper(std::string logger_name,
|
||||
formatter_ptr formatter,
|
||||
std::vector<sink_ptr> sinks,
|
||||
size_t queue_size,
|
||||
const log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||
std::function<void()> worker_warmup_cb = nullptr,
|
||||
const std::chrono::milliseconds &flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||
std::function<void()> worker_teardown_cb = nullptr);
|
||||
|
||||
void log(const details::log_msg &msg);
|
||||
void log(const details::log_msg &msg);
|
||||
|
||||
// stop logging and join the back thread
|
||||
~async_log_helper();
|
||||
// stop logging and join the back thread
|
||||
~async_log_helper();
|
||||
|
||||
async_log_helper(const async_log_helper &) = delete;
|
||||
async_log_helper &operator=(const async_log_helper &) = delete;
|
||||
async_log_helper(const async_log_helper &) = delete;
|
||||
async_log_helper &operator=(const async_log_helper &) = delete;
|
||||
|
||||
void set_formatter(formatter_ptr msg_formatter);
|
||||
void set_formatter(formatter_ptr msg_formatter);
|
||||
|
||||
void flush();
|
||||
void flush();
|
||||
|
||||
void set_error_handler(spdlog::log_err_handler err_handler);
|
||||
void set_error_handler(spdlog::log_err_handler err_handler);
|
||||
|
||||
private:
|
||||
std::string _logger_name;
|
||||
formatter_ptr _formatter;
|
||||
std::vector<std::shared_ptr<sinks::sink>> _sinks;
|
||||
private:
|
||||
std::string _logger_name;
|
||||
formatter_ptr _formatter;
|
||||
std::vector<std::shared_ptr<sinks::sink>> _sinks;
|
||||
|
||||
// queue of messages to log
|
||||
q_type _q;
|
||||
// queue of messages to log
|
||||
q_type _q;
|
||||
|
||||
log_err_handler _err_handler;
|
||||
log_err_handler _err_handler;
|
||||
|
||||
std::chrono::time_point<log_clock> _last_flush;
|
||||
std::chrono::time_point<log_clock> _last_flush;
|
||||
|
||||
// overflow policy
|
||||
const async_overflow_policy _overflow_policy;
|
||||
// overflow policy
|
||||
const async_overflow_policy _overflow_policy;
|
||||
|
||||
// worker thread warmup callback - one can set thread priority, affinity, etc
|
||||
const std::function<void()> _worker_warmup_cb;
|
||||
// worker thread warmup callback - one can set thread priority, affinity, etc
|
||||
const std::function<void()> _worker_warmup_cb;
|
||||
|
||||
// auto periodic sink flush parameter
|
||||
const std::chrono::milliseconds _flush_interval_ms;
|
||||
// auto periodic sink flush parameter
|
||||
const std::chrono::milliseconds _flush_interval_ms;
|
||||
|
||||
// worker thread teardown callback
|
||||
const std::function<void()> _worker_teardown_cb;
|
||||
// worker thread teardown callback
|
||||
const std::function<void()> _worker_teardown_cb;
|
||||
|
||||
std::mutex null_mutex_;
|
||||
// null_mutex null_mutex_;
|
||||
std::condition_variable_any not_empty_cv_;
|
||||
std::condition_variable_any not_full_cv_;
|
||||
std::mutex null_mutex_;
|
||||
// null_mutex null_mutex_;
|
||||
std::condition_variable_any not_empty_cv_;
|
||||
std::condition_variable_any not_full_cv_;
|
||||
|
||||
// worker thread
|
||||
std::thread _worker_thread;
|
||||
// worker thread
|
||||
std::thread _worker_thread;
|
||||
|
||||
void enqueue_msg(async_msg &&new_msg, async_overflow_policy policy);
|
||||
void enqueue_msg(async_msg &&new_msg, async_overflow_policy policy);
|
||||
|
||||
// worker thread main loop
|
||||
void worker_loop();
|
||||
// worker thread main loop
|
||||
void worker_loop();
|
||||
|
||||
// dequeue next message from the queue and process it.
|
||||
// return false if termination of the queue is required
|
||||
bool process_next_msg();
|
||||
// dequeue next message from the queue and process it.
|
||||
// return false if termination of the queue is required
|
||||
bool process_next_msg();
|
||||
|
||||
void handle_flush_interval();
|
||||
void handle_flush_interval();
|
||||
|
||||
void flush_sinks();
|
||||
};
|
||||
} // namespace details
|
||||
void flush_sinks();
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// async_sink class implementation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline spdlog::details::async_log_helper::async_log_helper(std::string logger_name,
|
||||
formatter_ptr formatter,
|
||||
std::vector<sink_ptr> sinks,
|
||||
size_t queue_size,
|
||||
log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy,
|
||||
std::function<void()> worker_warmup_cb,
|
||||
const std::chrono::milliseconds &flush_interval_ms,
|
||||
std::function<void()> worker_teardown_cb)
|
||||
: _logger_name(std::move(logger_name))
|
||||
inline spdlog::details::async_log_helper::async_log_helper(std::string logger_name,
|
||||
formatter_ptr formatter,
|
||||
std::vector<sink_ptr> sinks,
|
||||
size_t queue_size,
|
||||
log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy,
|
||||
std::function<void()> worker_warmup_cb,
|
||||
const std::chrono::milliseconds &flush_interval_ms,
|
||||
std::function<void()> worker_teardown_cb)
|
||||
: _logger_name(std::move(logger_name))
|
||||
, _formatter(std::move(formatter))
|
||||
, _sinks(std::move(sinks))
|
||||
, _q(queue_size)
|
||||
, _err_handler(std::move(err_handler))
|
||||
, _last_flush(os::now())
|
||||
, _overflow_policy(overflow_policy)
|
||||
, _worker_warmup_cb(std::move(worker_warmup_cb))
|
||||
, _flush_interval_ms(flush_interval_ms)
|
||||
, _worker_teardown_cb(std::move(worker_teardown_cb))
|
||||
, _sinks(std::move(sinks))
|
||||
, _q(queue_size)
|
||||
, _err_handler(std::move(err_handler))
|
||||
, _last_flush(os::now())
|
||||
, _overflow_policy(overflow_policy)
|
||||
, _worker_warmup_cb(std::move(worker_warmup_cb))
|
||||
, _flush_interval_ms(flush_interval_ms)
|
||||
, _worker_teardown_cb(std::move(worker_teardown_cb))
|
||||
{
|
||||
_worker_thread = std::thread(&async_log_helper::worker_loop, this);
|
||||
_worker_thread = std::thread(&async_log_helper::worker_loop, this);
|
||||
}
|
||||
|
||||
// send to the worker thread terminate message, and join it.
|
||||
inline spdlog::details::async_log_helper::~async_log_helper()
|
||||
{
|
||||
try
|
||||
{
|
||||
enqueue_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
||||
_worker_thread.join();
|
||||
}
|
||||
catch (...) // don't crash in destructor
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
enqueue_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
||||
_worker_thread.join();
|
||||
}
|
||||
catch (...) // don't crash in destructor
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// try to push and block until succeeded (if the policy is not to discard when the queue is full)
|
||||
inline void spdlog::details::async_log_helper::log(const details::log_msg &msg)
|
||||
{
|
||||
enqueue_msg(async_msg(msg), _overflow_policy);
|
||||
enqueue_msg(async_msg(msg), _overflow_policy);
|
||||
}
|
||||
|
||||
inline void spdlog::details::async_log_helper::enqueue_msg(details::async_log_helper::async_msg &&new_msg, async_overflow_policy policy)
|
||||
{
|
||||
|
||||
// block until succeeded pushing to the queue
|
||||
if (policy == async_overflow_policy::block_retry)
|
||||
{
|
||||
_q.enqueue(std::move(new_msg));
|
||||
}
|
||||
else
|
||||
{
|
||||
_q.enqueue_nowait(std::move(new_msg));
|
||||
}
|
||||
// block until succeeded pushing to the queue
|
||||
if (policy == async_overflow_policy::block_retry)
|
||||
{
|
||||
_q.enqueue(std::move(new_msg));
|
||||
}
|
||||
else
|
||||
{
|
||||
_q.enqueue_nowait(std::move(new_msg));
|
||||
}
|
||||
}
|
||||
|
||||
// optionally wait for the queue be empty and request flush from the sinks
|
||||
inline void spdlog::details::async_log_helper::flush()
|
||||
{
|
||||
enqueue_msg(async_msg(async_msg_type::flush), _overflow_policy);
|
||||
enqueue_msg(async_msg(async_msg_type::flush), _overflow_policy);
|
||||
}
|
||||
|
||||
inline void spdlog::details::async_log_helper::worker_loop()
|
||||
{
|
||||
if (_worker_warmup_cb)
|
||||
{
|
||||
_worker_warmup_cb();
|
||||
}
|
||||
auto active = true;
|
||||
while (active)
|
||||
{
|
||||
try
|
||||
{
|
||||
active = process_next_msg();
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exeption in async logger worker loop.");
|
||||
}
|
||||
}
|
||||
if (_worker_teardown_cb)
|
||||
{
|
||||
_worker_teardown_cb();
|
||||
}
|
||||
if (_worker_warmup_cb)
|
||||
{
|
||||
_worker_warmup_cb();
|
||||
}
|
||||
auto active = true;
|
||||
while (active)
|
||||
{
|
||||
try
|
||||
{
|
||||
active = process_next_msg();
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
if (_worker_teardown_cb)
|
||||
{
|
||||
_worker_teardown_cb();
|
||||
}
|
||||
}
|
||||
|
||||
// process next message in the queue
|
||||
// return true if this thread should still be active (while no terminate msg was received)
|
||||
inline bool spdlog::details::async_log_helper::process_next_msg()
|
||||
{
|
||||
async_msg incoming_async_msg;
|
||||
bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(2));
|
||||
if (!dequeued)
|
||||
{
|
||||
handle_flush_interval();
|
||||
return true;
|
||||
}
|
||||
async_msg incoming_async_msg;
|
||||
bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(2));
|
||||
if (!dequeued)
|
||||
{
|
||||
handle_flush_interval();
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (incoming_async_msg.msg_type)
|
||||
{
|
||||
case async_msg_type::flush:
|
||||
flush_sinks();
|
||||
return true;
|
||||
switch (incoming_async_msg.msg_type)
|
||||
{
|
||||
case async_msg_type::flush:
|
||||
flush_sinks();
|
||||
return true;
|
||||
|
||||
case async_msg_type::terminate:
|
||||
flush_sinks();
|
||||
return false;
|
||||
case async_msg_type::terminate:
|
||||
flush_sinks();
|
||||
return false;
|
||||
|
||||
default:
|
||||
log_msg incoming_log_msg;
|
||||
incoming_async_msg.fill_log_msg(incoming_log_msg, &_logger_name);
|
||||
_formatter->format(incoming_log_msg);
|
||||
for (auto &s : _sinks)
|
||||
{
|
||||
if (s->should_log(incoming_log_msg.level))
|
||||
{
|
||||
s->log(incoming_log_msg);
|
||||
}
|
||||
}
|
||||
handle_flush_interval();
|
||||
return true;
|
||||
}
|
||||
assert(false);
|
||||
return true; // should not be reached
|
||||
default:
|
||||
log_msg incoming_log_msg;
|
||||
incoming_async_msg.fill_log_msg(incoming_log_msg, &_logger_name);
|
||||
_formatter->format(incoming_log_msg);
|
||||
for (auto &s : _sinks)
|
||||
{
|
||||
if (s->should_log(incoming_log_msg.level))
|
||||
{
|
||||
try
|
||||
{
|
||||
s->log(incoming_log_msg);
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
}
|
||||
handle_flush_interval();
|
||||
return true;
|
||||
}
|
||||
assert(false);
|
||||
return true; // should not be reached
|
||||
}
|
||||
|
||||
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
|
||||
{
|
||||
_formatter = std::move(msg_formatter);
|
||||
_formatter = std::move(msg_formatter);
|
||||
}
|
||||
|
||||
inline void spdlog::details::async_log_helper::set_error_handler(spdlog::log_err_handler err_handler)
|
||||
{
|
||||
_err_handler = std::move(err_handler);
|
||||
_err_handler = std::move(err_handler);
|
||||
}
|
||||
|
||||
// flush all sinks if _flush_interval_ms has expired.
|
||||
inline void spdlog::details::async_log_helper::handle_flush_interval()
|
||||
{
|
||||
if (_flush_interval_ms == std::chrono::milliseconds::zero())
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto delta = details::os::now() - _last_flush;
|
||||
;
|
||||
if (delta >= _flush_interval_ms)
|
||||
{
|
||||
flush_sinks();
|
||||
}
|
||||
if (_flush_interval_ms == std::chrono::milliseconds::zero())
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto delta = details::os::now() - _last_flush;
|
||||
;
|
||||
if (delta >= _flush_interval_ms)
|
||||
{
|
||||
flush_sinks();
|
||||
}
|
||||
}
|
||||
|
||||
// flush all sinks if _flush_interval_ms has expired. only called if queue is empty
|
||||
inline void spdlog::details::async_log_helper::flush_sinks()
|
||||
{
|
||||
for (auto &s : _sinks)
|
||||
{
|
||||
s->flush();
|
||||
}
|
||||
_last_flush = os::now();
|
||||
|
||||
for (auto &s : _sinks)
|
||||
{
|
||||
try
|
||||
{
|
||||
s->flush();
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
_last_flush = os::now();
|
||||
}
|
||||
|
@ -68,15 +68,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
|
||||
#endif
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exception in logger " + _name);
|
||||
throw;
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
@ -92,15 +84,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exception in logger " + _name);
|
||||
throw;
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@ -116,15 +100,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_err_handler("Unknown exception in logger " + _name);
|
||||
throw;
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
template<typename Arg1, typename... Args>
|
||||
@ -331,10 +307,14 @@ inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
|
||||
|
||||
inline void spdlog::logger::flush()
|
||||
{
|
||||
for (auto &sink : _sinks)
|
||||
{
|
||||
sink->flush();
|
||||
}
|
||||
try
|
||||
{
|
||||
for (auto &sink : _sinks)
|
||||
{
|
||||
sink->flush();
|
||||
}
|
||||
}
|
||||
SPDLOG_CATCH_AND_HANDLE
|
||||
}
|
||||
|
||||
inline void spdlog::logger::_default_err_handler(const std::string &msg)
|
||||
|
@ -7,14 +7,17 @@
|
||||
|
||||
class failing_sink : public spdlog::sinks::sink
|
||||
{
|
||||
void log(const spdlog::details::log_msg &msg) override
|
||||
void log(const spdlog::details::log_msg &) override
|
||||
{
|
||||
throw std::runtime_error("some error happened during log");
|
||||
}
|
||||
|
||||
void flush() override {}
|
||||
void flush() override
|
||||
{
|
||||
throw std::runtime_error("some error happened during flush");
|
||||
}
|
||||
};
|
||||
|
||||
using namespace std;
|
||||
TEST_CASE("default_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@ -44,7 +47,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
std::string filename = "logs/simple_log.txt";
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||
logger->flush_on(spdlog::level::info);
|
||||
logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
logger->info("Good message #1");
|
||||
#if !defined(SPDLOG_FMT_PRINTF)
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
@ -57,12 +60,19 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
|
||||
TEST_CASE("default_error_handler2", "[errors]]")
|
||||
{
|
||||
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("flush_error_handler", "[errors]]")
|
||||
{
|
||||
spdlog::drop_all();
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("async_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@ -71,7 +81,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
std::string filename = "logs/simple_async_log.txt";
|
||||
{
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||
logger->set_error_handler([=](const std::string &msg) {
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err.txt");
|
||||
@ -99,15 +109,16 @@ TEST_CASE("async_error_handler2", "[errors]]")
|
||||
spdlog::set_async_mode(128);
|
||||
{
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &msg) {
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err2.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err2.txt");
|
||||
ofs << err_msg;
|
||||
});
|
||||
logger->info("Hello failure");
|
||||
logger->info("Hello failure");
|
||||
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
|
||||
spdlog::set_sync_mode();
|
||||
logger.reset();
|
||||
}
|
||||
|
||||
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
|
||||
|
@ -50,7 +50,7 @@ TEST_CASE("flush_on", "[flush_on]]")
|
||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string basename = "logs/rotating_log";
|
||||
std::string basename = "logs/rotating_log_A";
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0);
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
@ -70,8 +70,9 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string basename = "logs/rotating_log";
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 1);
|
||||
size_t max_size = 10 * 1024;
|
||||
std::string basename = "logs/rotating_log.txt";
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
logger->info("Test message {}", i);
|
||||
|
||||
@ -88,9 +89,9 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
REQUIRE(get_filesize(filename) <= 1024);
|
||||
auto filename1 = basename + ".1";
|
||||
REQUIRE(get_filesize(filename1) <= 1024);
|
||||
REQUIRE(get_filesize(filename) <= max_size);
|
||||
auto filename1 = "logs/rotating_log.1.txt";
|
||||
REQUIRE(get_filesize(filename1) <= max_size);
|
||||
}
|
||||
|
||||
TEST_CASE("daily_logger", "[daily_logger]]")
|
||||
|
@ -21,7 +21,7 @@ TEST_CASE("basic async test ", "[async]")
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
auto logger = spdlog::create_async("as", test_sink, 128, spdlog::async_overflow_policy::block_retry);
|
||||
auto logger = spdlog::create_async("as", test_sink, queue_size, spdlog::async_overflow_policy::block_retry);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
logger->info("Hello message #{}", i);
|
||||
|
@ -1,10 +1,83 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2015
|
||||
VisualStudioVersion = 14.0
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27428.2037
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests.vcxproj", "{59A07559-5F38-4DD6-A7FA-DB4153690B42}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "spdlog", "spdlog", "{7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\async_logger.h = ..\include\spdlog\async_logger.h
|
||||
..\include\spdlog\common.h = ..\include\spdlog\common.h
|
||||
..\include\spdlog\formatter.h = ..\include\spdlog\formatter.h
|
||||
..\include\spdlog\logger.h = ..\include\spdlog\logger.h
|
||||
..\include\spdlog\spdlog.h = ..\include\spdlog\spdlog.h
|
||||
..\include\spdlog\tweakme.h = ..\include\spdlog\tweakme.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "contrib", "contrib", "{AAD3C108-4E24-4AA8-BA09-4C9A75A881B3}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\contrib\README.md = ..\include\spdlog\contrib\README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{3FB7E4DF-2397-463E-BDAD-32854AB66DFD}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\contrib\sinks\.gitignore = ..\include\spdlog\contrib\sinks\.gitignore
|
||||
..\include\spdlog\contrib\sinks\step_file_sink.h = ..\include\spdlog\contrib\sinks\step_file_sink.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "details", "details", "{97DED9BF-821E-4A7A-8D13-ED9A739E1F55}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\details\async_log_helper.h = ..\include\spdlog\details\async_log_helper.h
|
||||
..\include\spdlog\details\async_logger_impl.h = ..\include\spdlog\details\async_logger_impl.h
|
||||
..\include\spdlog\details\file_helper.h = ..\include\spdlog\details\file_helper.h
|
||||
..\include\spdlog\details\log_msg.h = ..\include\spdlog\details\log_msg.h
|
||||
..\include\spdlog\details\logger_impl.h = ..\include\spdlog\details\logger_impl.h
|
||||
..\include\spdlog\details\mpmc_blocking_q.h = ..\include\spdlog\details\mpmc_blocking_q.h
|
||||
..\include\spdlog\details\null_mutex.h = ..\include\spdlog\details\null_mutex.h
|
||||
..\include\spdlog\details\os.h = ..\include\spdlog\details\os.h
|
||||
..\include\spdlog\details\pattern_formatter_impl.h = ..\include\spdlog\details\pattern_formatter_impl.h
|
||||
..\include\spdlog\details\registry.h = ..\include\spdlog\details\registry.h
|
||||
..\include\spdlog\details\spdlog_impl.h = ..\include\spdlog\details\spdlog_impl.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "fmt", "fmt", "{0B649723-CF78-47C0-B1CA-1F173DDBFED4}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\fmt\fmt.h = ..\include\spdlog\fmt\fmt.h
|
||||
..\include\spdlog\fmt\ostr.h = ..\include\spdlog\fmt\ostr.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "bundled", "bundled", "{1FBA69C4-7EAA-4D60-BCF9-3D59D5A88D32}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\fmt\bundled\format.cc = ..\include\spdlog\fmt\bundled\format.cc
|
||||
..\include\spdlog\fmt\bundled\format.h = ..\include\spdlog\fmt\bundled\format.h
|
||||
..\include\spdlog\fmt\bundled\LICENSE.rst = ..\include\spdlog\fmt\bundled\LICENSE.rst
|
||||
..\include\spdlog\fmt\bundled\ostream.cc = ..\include\spdlog\fmt\bundled\ostream.cc
|
||||
..\include\spdlog\fmt\bundled\ostream.h = ..\include\spdlog\fmt\bundled\ostream.h
|
||||
..\include\spdlog\fmt\bundled\posix.cc = ..\include\spdlog\fmt\bundled\posix.cc
|
||||
..\include\spdlog\fmt\bundled\posix.h = ..\include\spdlog\fmt\bundled\posix.h
|
||||
..\include\spdlog\fmt\bundled\printf.cc = ..\include\spdlog\fmt\bundled\printf.cc
|
||||
..\include\spdlog\fmt\bundled\printf.h = ..\include\spdlog\fmt\bundled\printf.h
|
||||
..\include\spdlog\fmt\bundled\time.h = ..\include\spdlog\fmt\bundled\time.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{278CDF3C-6E6D-4FAF-AF79-C1806101B4CB}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\include\spdlog\sinks\android_sink.h = ..\include\spdlog\sinks\android_sink.h
|
||||
..\include\spdlog\sinks\ansicolor_sink.h = ..\include\spdlog\sinks\ansicolor_sink.h
|
||||
..\include\spdlog\sinks\base_sink.h = ..\include\spdlog\sinks\base_sink.h
|
||||
..\include\spdlog\sinks\dist_sink.h = ..\include\spdlog\sinks\dist_sink.h
|
||||
..\include\spdlog\sinks\file_sinks.h = ..\include\spdlog\sinks\file_sinks.h
|
||||
..\include\spdlog\sinks\msvc_sink.h = ..\include\spdlog\sinks\msvc_sink.h
|
||||
..\include\spdlog\sinks\null_sink.h = ..\include\spdlog\sinks\null_sink.h
|
||||
..\include\spdlog\sinks\ostream_sink.h = ..\include\spdlog\sinks\ostream_sink.h
|
||||
..\include\spdlog\sinks\sink.h = ..\include\spdlog\sinks\sink.h
|
||||
..\include\spdlog\sinks\stdout_sinks.h = ..\include\spdlog\sinks\stdout_sinks.h
|
||||
..\include\spdlog\sinks\syslog_sink.h = ..\include\spdlog\sinks\syslog_sink.h
|
||||
..\include\spdlog\sinks\wincolor_sink.h = ..\include\spdlog\sinks\wincolor_sink.h
|
||||
..\include\spdlog\sinks\windebug_sink.h = ..\include\spdlog\sinks\windebug_sink.h
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
@ -25,4 +98,15 @@ Global
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{AAD3C108-4E24-4AA8-BA09-4C9A75A881B3} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
{3FB7E4DF-2397-463E-BDAD-32854AB66DFD} = {AAD3C108-4E24-4AA8-BA09-4C9A75A881B3}
|
||||
{97DED9BF-821E-4A7A-8D13-ED9A739E1F55} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
{0B649723-CF78-47C0-B1CA-1F173DDBFED4} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
{1FBA69C4-7EAA-4D60-BCF9-3D59D5A88D32} = {0B649723-CF78-47C0-B1CA-1F173DDBFED4}
|
||||
{278CDF3C-6E6D-4FAF-AF79-C1806101B4CB} = {7EFD7EC9-512F-4B35-ADFE-49863B2C04A5}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {25A6A4D3-9B25-4071-81B4-99DFDD066255}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
@ -30,9 +30,6 @@
|
||||
<ClCompile Include="utils.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="errors.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="test_macros.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
@ -42,6 +39,9 @@
|
||||
<ClCompile Include="test_misc.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="errors.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="test_async.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
Loading…
Reference in New Issue
Block a user