Skip to content

Commit be942e0

Browse files
committed
Limit error handler rate to 1/sec
1 parent af7b061 commit be942e0

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

include/spdlog/details/err_helper.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
#pragma once
55

66
#include <string>
7-
#include <mutex>
7+
#include <chrono>
88
#include <exception>
9+
#include <mutex>
910
#include "spdlog/common.h"
1011

11-
// by default, prints the error to stderr, thread safe
12+
// by default, prints the error to stderr, at max rate of 1/sec thread safe
1213
namespace spdlog {
1314
namespace details {
1415
class SPDLOG_API err_helper {
1516
err_handler custom_err_handler_;
17+
std::chrono::steady_clock::time_point last_report_time_;
18+
std::mutex mutex_;
1619
public:
17-
void handle_ex(const std::string& origin, const source_loc& loc, const std::exception& ex) const noexcept;
18-
void handle_unknown_ex(const std::string& origin, const source_loc& loc) const noexcept;
20+
err_helper() = default;
21+
err_helper(const err_helper& other);
22+
err_helper(err_helper&& other);
23+
void handle_ex(const std::string& origin, const source_loc& loc, const std::exception& ex) noexcept;
24+
void handle_unknown_ex(const std::string& origin, const source_loc& loc) noexcept;
1925
void set_err_handler(err_handler handler);
2026
};
2127
} // namespace details

src/details/err_helper.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,34 @@
33

44
#include "spdlog/details/err_helper.h"
55

6-
#include "iostream"
76
#include "spdlog/details/os.h"
87

98
namespace spdlog {
109
namespace 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

Comments
 (0)