mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 10:29:02 +08:00
no exceptions while logging
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include <exception>
|
||||
#include<functional>
|
||||
|
||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
@@ -58,6 +60,7 @@ using level_t = details::null_atomic_int;
|
||||
using level_t = std::atomic_int;
|
||||
#endif
|
||||
|
||||
using log_err_handler = std::function<void(const std::string &err_msg)>;
|
||||
|
||||
//Log level enum
|
||||
namespace level
|
||||
|
@@ -120,6 +120,7 @@ public:
|
||||
async_log_helper(formatter_ptr formatter,
|
||||
const 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,
|
||||
const std::function<void()>& worker_warmup_cb = nullptr,
|
||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||
@@ -142,14 +143,13 @@ private:
|
||||
// queue of messages to log
|
||||
q_type _q;
|
||||
|
||||
log_err_handler _err_handler;
|
||||
|
||||
bool _flush_requested;
|
||||
|
||||
bool _terminate_requested;
|
||||
|
||||
|
||||
// last exception thrown from the worker thread
|
||||
std::shared_ptr<spdlog_ex> _last_workerthread_ex;
|
||||
|
||||
|
||||
// overflow policy
|
||||
const async_overflow_policy _overflow_policy;
|
||||
|
||||
@@ -166,10 +166,7 @@ private:
|
||||
std::thread _worker_thread;
|
||||
|
||||
void push_msg(async_msg&& new_msg);
|
||||
|
||||
// throw last worker thread exception or if worker thread is not active
|
||||
void throw_if_bad_worker();
|
||||
|
||||
|
||||
// worker thread main loop
|
||||
void worker_loop();
|
||||
|
||||
@@ -181,7 +178,7 @@ private:
|
||||
|
||||
// sleep,yield or return immediatly using the time passed since last message as a hint
|
||||
static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time);
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -193,6 +190,7 @@ inline spdlog::details::async_log_helper::async_log_helper(
|
||||
formatter_ptr formatter,
|
||||
const std::vector<sink_ptr>& sinks,
|
||||
size_t queue_size,
|
||||
log_err_handler err_handler,
|
||||
const async_overflow_policy overflow_policy,
|
||||
const std::function<void()>& worker_warmup_cb,
|
||||
const std::chrono::milliseconds& flush_interval_ms,
|
||||
@@ -200,6 +198,7 @@ inline spdlog::details::async_log_helper::async_log_helper(
|
||||
_formatter(formatter),
|
||||
_sinks(sinks),
|
||||
_q(queue_size),
|
||||
_err_handler(err_handler),
|
||||
_flush_requested(false),
|
||||
_terminate_requested(false),
|
||||
_overflow_policy(overflow_policy),
|
||||
@@ -232,8 +231,7 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
|
||||
}
|
||||
|
||||
inline void spdlog::details::async_log_helper::push_msg(details::async_log_helper::async_msg&& new_msg)
|
||||
{
|
||||
throw_if_bad_worker();
|
||||
{
|
||||
if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_overflow_policy::discard_log_msg)
|
||||
{
|
||||
auto last_op_time = details::os::now();
|
||||
@@ -263,14 +261,12 @@ inline void spdlog::details::async_log_helper::worker_loop()
|
||||
while(process_next_msg(last_pop, last_flush));
|
||||
if (_worker_teardown_cb) _worker_teardown_cb();
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
_last_workerthread_ex = std::make_shared<spdlog_ex>(std::string("async_logger worker thread exception: ") + ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_last_workerthread_ex = std::make_shared<spdlog_ex>("async_logger worker thread exception");
|
||||
}
|
||||
catch (const std::exception &ex) {
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...) {
|
||||
_err_handler("Unknown exception");
|
||||
}
|
||||
}
|
||||
|
||||
// process next message in the queue
|
||||
@@ -362,15 +358,6 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
|
||||
return sleep_for(milliseconds(200));
|
||||
}
|
||||
|
||||
// throw if the worker thread threw an exception or not active
|
||||
inline void spdlog::details::async_log_helper::throw_if_bad_worker()
|
||||
{
|
||||
if (_last_workerthread_ex)
|
||||
{
|
||||
auto ex = std::move(_last_workerthread_ex);
|
||||
throw *ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -26,7 +26,7 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
||||
const std::chrono::milliseconds& flush_interval_ms,
|
||||
const std::function<void()>& worker_teardown_cb) :
|
||||
logger(logger_name, begin, end),
|
||||
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
||||
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -73,5 +73,14 @@ inline void spdlog::async_logger::_set_pattern(const std::string& pattern)
|
||||
|
||||
inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
|
||||
{
|
||||
_async_log_helper->log(msg);
|
||||
try
|
||||
{
|
||||
_async_log_helper->log(msg);
|
||||
}
|
||||
catch (const std::exception &ex) {
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...) {
|
||||
_err_handler("Unknown exception");
|
||||
}
|
||||
}
|
||||
|
@@ -6,35 +6,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/logger.h>
|
||||
#include <spdlog/sinks/stdout_sinks.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
|
||||
// create logger with given name, sinks and the default pattern formatter
|
||||
// all other ctors will call this one
|
||||
template<class It>
|
||||
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
|
||||
_name(logger_name),
|
||||
_sinks(begin, end),
|
||||
_formatter(std::make_shared<pattern_formatter>("%+"))
|
||||
{
|
||||
|
||||
// no support under vs2013 for member initialization for std::atomic
|
||||
_level = level::info;
|
||||
_flush_level = level::off;
|
||||
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end):
|
||||
_name(logger_name),
|
||||
_sinks(begin, end),
|
||||
_formatter(std::make_shared<pattern_formatter>("%+"))
|
||||
{
|
||||
_level = level::info;
|
||||
_flush_level = level::off;
|
||||
_last_err_time = 0;
|
||||
_err_handler = [this](const std::string &msg) { this->_default_err_handler(msg);};
|
||||
}
|
||||
|
||||
// ctor with sinks as init list
|
||||
inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) :
|
||||
logger(logger_name, sinks_list.begin(), sinks_list.end()) {}
|
||||
inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list):
|
||||
logger(logger_name, sinks_list.begin(), sinks_list.end())
|
||||
{}
|
||||
|
||||
|
||||
// ctor with single sink
|
||||
inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) :
|
||||
logger(logger_name,
|
||||
{
|
||||
single_sink
|
||||
}) {}
|
||||
inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink):
|
||||
logger(logger_name,
|
||||
{
|
||||
single_sink
|
||||
})
|
||||
{}
|
||||
|
||||
|
||||
inline spdlog::logger::~logger() = default;
|
||||
@@ -42,131 +46,143 @@ inline spdlog::logger::~logger() = default;
|
||||
|
||||
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
|
||||
{
|
||||
_set_formatter(msg_formatter);
|
||||
_set_formatter(msg_formatter);
|
||||
}
|
||||
|
||||
inline void spdlog::logger::set_pattern(const std::string& pattern)
|
||||
{
|
||||
_set_pattern(pattern);
|
||||
_set_pattern(pattern);
|
||||
}
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Args&... args)
|
||||
{
|
||||
if (!should_log(lvl)) return;
|
||||
|
||||
details::log_msg log_msg(&_name, lvl);
|
||||
try
|
||||
{
|
||||
log_msg.raw.write(fmt, args...);
|
||||
}
|
||||
catch (fmt::FormatError &ex)
|
||||
{
|
||||
throw spdlog::spdlog_ex(std::string("format error in \"") + fmt + "\": " + ex.what());
|
||||
}
|
||||
|
||||
_sink_it(log_msg);
|
||||
if (!should_log(lvl)) return;
|
||||
|
||||
try {
|
||||
details::log_msg log_msg(&_name, lvl);
|
||||
log_msg.raw.write(fmt, args...);
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex) {
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...) {
|
||||
_err_handler("Unknown exception");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::log(level::level_enum lvl, const char* msg)
|
||||
{
|
||||
if (!should_log(lvl)) return;
|
||||
|
||||
details::log_msg log_msg(&_name, lvl);
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
if (!should_log(lvl)) return;
|
||||
try {
|
||||
details::log_msg log_msg(&_name, lvl);
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex) {
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...) {
|
||||
_err_handler("Unknown exception");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::log(level::level_enum lvl, const T& msg)
|
||||
{
|
||||
if (!should_log(lvl)) return;
|
||||
|
||||
details::log_msg log_msg(&_name, lvl);
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
|
||||
if (!should_log(lvl)) return;
|
||||
try {
|
||||
details::log_msg log_msg(&_name, lvl);
|
||||
log_msg.raw << msg;
|
||||
_sink_it(log_msg);
|
||||
}
|
||||
catch (const std::exception &ex) {
|
||||
_err_handler(ex.what());
|
||||
}
|
||||
catch (...) {
|
||||
_err_handler("Unknown exception");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::trace(const char* fmt, const Args&... args)
|
||||
{
|
||||
log(level::trace, fmt, args...);
|
||||
log(level::trace, fmt, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::debug(const char* fmt, const Args&... args)
|
||||
{
|
||||
log(level::debug, fmt, args...);
|
||||
log(level::debug, fmt, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::info(const char* fmt, const Args&... args)
|
||||
{
|
||||
log(level::info, fmt, args...);
|
||||
log(level::info, fmt, args...);
|
||||
}
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::warn(const char* fmt, const Args&... args)
|
||||
{
|
||||
log(level::warn, fmt, args...);
|
||||
log(level::warn, fmt, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::error(const char* fmt, const Args&... args)
|
||||
{
|
||||
log(level::err, fmt, args...);
|
||||
log(level::err, fmt, args...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void spdlog::logger::critical(const char* fmt, const Args&... args)
|
||||
{
|
||||
log(level::critical, fmt, args...);
|
||||
log(level::critical, fmt, args...);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::trace(const T& msg)
|
||||
{
|
||||
log(level::trace, msg);
|
||||
log(level::trace, msg);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::debug(const T& msg)
|
||||
{
|
||||
log(level::debug, msg);
|
||||
log(level::debug, msg);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::info(const T& msg)
|
||||
{
|
||||
log(level::info, msg);
|
||||
log(level::info, msg);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::warn(const T& msg)
|
||||
{
|
||||
log(level::warn, msg);
|
||||
log(level::warn, msg);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::error(const T& msg)
|
||||
{
|
||||
log(level::err, msg);
|
||||
log(level::err, msg);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void spdlog::logger::critical(const T& msg)
|
||||
{
|
||||
log(level::critical, msg);
|
||||
log(level::critical, msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -177,27 +193,38 @@ inline void spdlog::logger::critical(const T& msg)
|
||||
//
|
||||
inline const std::string& spdlog::logger::name() const
|
||||
{
|
||||
return _name;
|
||||
return _name;
|
||||
}
|
||||
|
||||
inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
|
||||
{
|
||||
_level.store(log_level);
|
||||
_level.store(log_level);
|
||||
}
|
||||
|
||||
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
|
||||
{
|
||||
_err_handler = err_handler;
|
||||
}
|
||||
|
||||
inline spdlog::log_err_handler spdlog::logger::error_handler()
|
||||
{
|
||||
return _err_handler;
|
||||
}
|
||||
|
||||
|
||||
inline void spdlog::logger::flush_on(level::level_enum log_level)
|
||||
{
|
||||
_flush_level.store(log_level);
|
||||
_flush_level.store(log_level);
|
||||
}
|
||||
|
||||
inline spdlog::level::level_enum spdlog::logger::level() const
|
||||
{
|
||||
return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
|
||||
return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= _level.load(std::memory_order_relaxed);
|
||||
return msg_level >= _level.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -205,26 +232,41 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
|
||||
//
|
||||
inline void spdlog::logger::_sink_it(details::log_msg& msg)
|
||||
{
|
||||
_formatter->format(msg);
|
||||
for (auto &sink : _sinks)
|
||||
sink->log(msg);
|
||||
|
||||
const auto flush_level = _flush_level.load(std::memory_order_relaxed);
|
||||
if (msg.level >= flush_level)
|
||||
flush();
|
||||
_formatter->format(msg);
|
||||
for (auto &sink : _sinks)
|
||||
sink->log(msg);
|
||||
|
||||
const auto flush_level = _flush_level.load(std::memory_order_relaxed);
|
||||
if (msg.level >= flush_level)
|
||||
flush();
|
||||
}
|
||||
|
||||
inline void spdlog::logger::_set_pattern(const std::string& pattern)
|
||||
{
|
||||
_formatter = std::make_shared<pattern_formatter>(pattern);
|
||||
_formatter = std::make_shared<pattern_formatter>(pattern);
|
||||
}
|
||||
inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
|
||||
{
|
||||
_formatter = msg_formatter;
|
||||
_formatter = msg_formatter;
|
||||
}
|
||||
|
||||
inline void spdlog::logger::flush()
|
||||
{
|
||||
for (auto& sink : _sinks)
|
||||
sink->flush();
|
||||
for (auto& sink : _sinks)
|
||||
sink->flush();
|
||||
}
|
||||
|
||||
inline void spdlog::logger::_default_err_handler(const std::string &msg)
|
||||
{
|
||||
auto now = time(nullptr);
|
||||
if (now - _last_err_time < 60)
|
||||
return;
|
||||
auto tm_time = details::os::localtime(now);
|
||||
char date_buf[100];
|
||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||
details::log_msg err_msg;
|
||||
err_msg.formatted.write("[*** LOG ERROR ***] [{}] [{}] [{}]{}", name(), msg, date_buf, details::os::eol);
|
||||
sinks::stderr_sink_mt::instance()->log(err_msg);
|
||||
_last_err_time = now;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -60,7 +60,12 @@ public:
|
||||
if (_formatter)
|
||||
new_logger->set_formatter(_formatter);
|
||||
|
||||
if (_err_handler)
|
||||
new_logger->set_error_handler(_err_handler);
|
||||
|
||||
new_logger->set_level(_level);
|
||||
|
||||
|
||||
//Add to registry
|
||||
_loggers[logger_name] = new_logger;
|
||||
return new_logger;
|
||||
@@ -112,6 +117,13 @@ public:
|
||||
_level = log_level;
|
||||
}
|
||||
|
||||
void set_error_handler(log_err_handler handler)
|
||||
{
|
||||
for (auto& l : _loggers)
|
||||
l.second->set_error_handler(handler);
|
||||
_err_handler = handler;
|
||||
}
|
||||
|
||||
void set_async_mode(size_t q_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(_mutex);
|
||||
@@ -149,6 +161,7 @@ private:
|
||||
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
||||
formatter_ptr _formatter;
|
||||
level::level_enum _level = level::info;
|
||||
log_err_handler _err_handler;
|
||||
bool _async_mode = false;
|
||||
size_t _async_q_size = 0;
|
||||
async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
|
||||
|
@@ -147,6 +147,13 @@ inline void spdlog::set_level(level::level_enum log_level)
|
||||
return details::registry::instance().set_level(log_level);
|
||||
}
|
||||
|
||||
inline void spdlog::set_error_handler(log_err_handler handler)
|
||||
{
|
||||
return details::registry::instance().set_error_handler(handler);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
|
||||
{
|
||||
|
@@ -19,7 +19,6 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
|
||||
@@ -52,14 +51,17 @@ public:
|
||||
template <typename T> void warn(const T&);
|
||||
template <typename T> void error(const T&);
|
||||
template <typename T> void critical(const T&);
|
||||
|
||||
|
||||
|
||||
bool should_log(level::level_enum) const;
|
||||
void set_level(level::level_enum);
|
||||
level::level_enum level() const;
|
||||
const std::string& name() const;
|
||||
void set_pattern(const std::string&);
|
||||
void set_formatter(formatter_ptr);
|
||||
void set_formatter(formatter_ptr);
|
||||
|
||||
// error handler
|
||||
void set_error_handler(log_err_handler);
|
||||
log_err_handler error_handler();
|
||||
|
||||
// automatically call flush() if message level >= log_level
|
||||
void flush_on(level::level_enum log_level);
|
||||
@@ -70,11 +72,16 @@ protected:
|
||||
virtual void _set_pattern(const std::string&);
|
||||
virtual void _set_formatter(formatter_ptr);
|
||||
|
||||
// default error handler: print the error to stderr with the max rate of 1 message/minute
|
||||
virtual void _default_err_handler(const std::string &msg);
|
||||
|
||||
const std::string _name;
|
||||
std::vector<sink_ptr> _sinks;
|
||||
formatter_ptr _formatter;
|
||||
spdlog::level_t _level;
|
||||
spdlog::level_t _flush_level;
|
||||
log_err_handler _err_handler;
|
||||
std::atomic<time_t> _last_err_time;
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -41,6 +41,11 @@ void set_formatter(formatter_ptr f);
|
||||
//
|
||||
void set_level(level::level_enum log_level);
|
||||
|
||||
//
|
||||
// Set global error handler
|
||||
//
|
||||
void set_error_handler(log_err_handler);
|
||||
|
||||
//
|
||||
// Turn on async mode (off by default) and set the queue size for each async_logger.
|
||||
// effective only for loggers created after this call.
|
||||
|
Reference in New Issue
Block a user