mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
limit default error handler to 1 message/second to avoid flood
This commit is contained in:
parent
bcb6484062
commit
3c30f77d31
@ -10,6 +10,8 @@
|
|||||||
#include "spdlog/sinks/sink.h"
|
#include "spdlog/sinks/sink.h"
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include "spdlog/details/pattern_formatter.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
// public methods
|
// public methods
|
||||||
@ -203,18 +205,32 @@ SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
|||||||
return (msg.level >= flush_level) && (msg.level != level::off);
|
return (msg.level >= flush_level) && (msg.level != level::off);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (custom_err_handler_)
|
if (custom_err_handler_)
|
||||||
{
|
{
|
||||||
custom_err_handler_(msg);
|
custom_err_handler_(msg);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto tm_time = details::os::localtime();
|
using std::chrono::system_clock;
|
||||||
|
static std::mutex mutex;
|
||||||
|
static std::chrono::system_clock::time_point last_report_time;
|
||||||
|
static size_t err_counter = 0;
|
||||||
|
std::lock_guard<std::mutex> lk{mutex};
|
||||||
|
auto now = system_clock::now();
|
||||||
|
err_counter++;
|
||||||
|
if(now - last_report_time < std::chrono::seconds(1))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
last_report_time = now;
|
||||||
|
auto tm_time = details::os::localtime(system_clock::to_time_t(now));
|
||||||
char date_buf[64];
|
char date_buf[64];
|
||||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||||
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
|
fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -317,8 +317,8 @@ protected:
|
|||||||
virtual void flush_();
|
virtual void flush_();
|
||||||
bool should_flush_(const details::log_msg &msg);
|
bool should_flush_(const details::log_msg &msg);
|
||||||
|
|
||||||
// default error handler.
|
// handle errors during logging.
|
||||||
// print the error to stderr with the max rate of 1 message/minute.
|
// default handler prints the error to stderr at max rate of 1 message/sec.
|
||||||
void err_handler_(const std::string &msg);
|
void err_handler_(const std::string &msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user