Skip to content

Commit

Permalink
Fixed copy ctor of err_helper to be thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Jan 17, 2025
1 parent 62bbd87 commit 80f0079
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/spdlog/details/err_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ namespace details {
class SPDLOG_API err_helper {
err_handler custom_err_handler_;
std::chrono::steady_clock::time_point last_report_time_;
std::mutex mutex_;
mutable std::mutex mutex_;
public:
err_helper() = default;
~err_helper() = default;
err_helper(const err_helper& other);
err_helper(err_helper&& other) noexcept;
void handle_ex(const std::string& origin, const source_loc& loc, const std::exception& ex) noexcept;
Expand Down
15 changes: 9 additions & 6 deletions src/details/err_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
namespace spdlog {
namespace details {

err_helper::err_helper(const err_helper &other)
: custom_err_handler_(other.custom_err_handler_),
last_report_time_(other.last_report_time_) {}
err_helper::err_helper(const err_helper &other) {
std::lock_guard lock(other.mutex_);
custom_err_handler_ = other.custom_err_handler_;
last_report_time_ = other.last_report_time_;
}

err_helper::err_helper(err_helper &&other) noexcept
: custom_err_handler_(std::move(other.custom_err_handler_)),
last_report_time_(other.last_report_time_) {}
err_helper::err_helper(err_helper &&other) noexcept {
custom_err_handler_ = std::move(other.custom_err_handler_);
last_report_time_ = std::move(other.last_report_time_);
}

// Prints error to stderr with source location (if available). A stderr sink is not used because reaching
// this point might indicate a problem with the logging system itself so we use fputs() directly.
Expand Down

0 comments on commit 80f0079

Please sign in to comment.