33
44#include " spdlog/details/err_helper.h"
55
6- #include " iostream"
76#include " spdlog/details/os.h"
87
98namespace spdlog {
109namespace details {
1110
11+ err_helper::err_helper (const err_helper &other)
12+ : custom_err_handler_(other.custom_err_handler_),
13+ last_report_time_ (other.last_report_time_) {}
14+
15+ err_helper::err_helper (err_helper &&other)
16+ : custom_err_handler_(std::move(other.custom_err_handler_)),
17+ last_report_time_(other.last_report_time_) {};
18+
1219// Prints error to stderr with source location (if available). A stderr sink is not used because reaching
1320// this point might indicate a problem with the logging system itself so we use fputs() directly.
14- void err_helper::handle_ex (const std::string &origin, const source_loc &loc, const std::exception &ex) const noexcept {
21+ void err_helper::handle_ex (const std::string &origin, const source_loc &loc, const std::exception &ex) noexcept {
22+ std::lock_guard lock (mutex_);
1523 try {
1624 if (custom_err_handler_) {
1725 custom_err_handler_ (ex.what ());
1826 return ;
1927 }
28+
29+ const auto now = std::chrono::steady_clock::now ();
30+ if (now - last_report_time_ < std::chrono::seconds (1 )) {
31+ return ;
32+ }
33+ last_report_time_ = now;
2034 const auto tm_time = os::localtime ();
2135 char date_buf[32 ];
2236 std::strftime (date_buf, sizeof (date_buf), " %Y-%m-%d %H:%M:%S" , &tm_time);
@@ -29,17 +43,21 @@ void err_helper::handle_ex(const std::string &origin, const source_loc &loc, con
2943 }
3044 std::fputs (msg.c_str (), stderr);
3145 } catch (const std::exception &handler_ex) {
32- std::fprintf (stderr, " [*** LOG ERROR ***] [%s] caught exception during error handler: %s\n " , origin.c_str (), handler_ex.what ());
46+ std::fprintf (stderr, " [*** LOG ERROR ***] [%s] caught exception during error handler: %s\n " , origin.c_str (),
47+ handler_ex.what ());
3348 } catch (...) { // catch all exceptions
3449 std::fprintf (stderr, " [*** LOG ERROR ***] [%s] caught unknown exception during error handler\n " , origin.c_str ());
3550 }
3651}
3752
38- void err_helper::handle_unknown_ex (const std::string &origin, const source_loc &loc) const noexcept {
53+ void err_helper::handle_unknown_ex (const std::string &origin, const source_loc &loc) noexcept {
3954 handle_ex (origin, loc, std::runtime_error (" unknown exception" ));
4055}
4156
42- void err_helper::set_err_handler (err_handler handler) { custom_err_handler_ = std::move (handler); }
57+ void err_helper::set_err_handler (err_handler handler) {
58+ std::lock_guard lock (mutex_);
59+ custom_err_handler_ = std::move (handler);
60+ }
4361
4462} // namespace details
4563} // namespace spdlog
0 commit comments