Removed statics from default error handler

This commit is contained in:
gabime 2024-11-09 11:16:30 +02:00
parent 7cee026baa
commit fd6cb2c7a7
2 changed files with 10 additions and 17 deletions

View File

@ -173,24 +173,16 @@ SPDLOG_INLINE void logger::err_handler_(const std::string &msg) {
custom_err_handler_(msg); custom_err_handler_(msg);
} else { } else {
using std::chrono::system_clock; 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(); auto now = system_clock::now();
err_counter++; auto err_counter_val = err_counter.fetch_add(1, std::memory_order_relaxed)+1;
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)); 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);
#if defined(USING_R) && defined(R_R_H) // if in R environment #if defined(USING_R) && defined(R_R_H) // if in R environment
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n",
msg.c_str()); err_counter_val, date_buf, name().c_str(), msg.c_str());
#else #else
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter_val, date_buf,
name().c_str(), msg.c_str()); name().c_str(), msg.c_str());
#endif #endif
} }

View File

@ -311,6 +311,7 @@ protected:
spdlog::level_t flush_level_{level::off}; spdlog::level_t flush_level_{level::off};
err_handler custom_err_handler_{nullptr}; err_handler custom_err_handler_{nullptr};
details::backtracer tracer_; details::backtracer tracer_;
std::atomic<size_t> err_counter{0};
// common implementation for after templated public api has been resolved // common implementation for after templated public api has been resolved
template <typename... Args> template <typename... Args>