JDevlieghere created this revision. JDevlieghere added reviewers: mib, bulbazord, labath. Herald added a project: All. JDevlieghere requested review of this revision.
Add the ability to remove diagnostic callbacks. https://reviews.llvm.org/D143548 Files: lldb/include/lldb/Utility/Diagnostics.h lldb/source/Utility/Diagnostics.cpp Index: lldb/source/Utility/Diagnostics.cpp =================================================================== --- lldb/source/Utility/Diagnostics.cpp +++ lldb/source/Utility/Diagnostics.cpp @@ -43,9 +43,19 @@ Diagnostics::~Diagnostics() {} -void Diagnostics::AddCallback(Callback callback) { +Diagnostics::CallbackID Diagnostics::AddCallback(Callback callback) { std::lock_guard<std::mutex> guard(m_callbacks_mutex); - m_callbacks.push_back(callback); + CallbackID id = m_callback_id++; + m_callbacks.emplace_back(id, callback); + return id; +} + +void Diagnostics::RemoveCallback(CallbackID id) { + std::lock_guard<std::mutex> guard(m_callbacks_mutex); + m_callbacks.erase( + std::remove_if(m_callbacks.begin(), m_callbacks.end(), + [id](const CallbackEntry &e) { return e.id == id; }), + m_callbacks.end()); } bool Diagnostics::Dump(raw_ostream &stream) { @@ -84,8 +94,8 @@ if (Error err = DumpDiangosticsLog(dir)) return err; - for (Callback c : m_callbacks) { - if (Error err = c(dir)) + for (CallbackEntry e : m_callbacks) { + if (Error err = e.callback(dir)) return err; } Index: lldb/include/lldb/Utility/Diagnostics.h =================================================================== --- lldb/include/lldb/Utility/Diagnostics.h +++ lldb/include/lldb/Utility/Diagnostics.h @@ -42,8 +42,10 @@ void Report(llvm::StringRef message); using Callback = std::function<llvm::Error(const FileSpec &)>; + using CallbackID = uint64_t; - void AddCallback(Callback callback); + CallbackID AddCallback(Callback callback); + void RemoveCallback(CallbackID id); static Diagnostics &Instance(); @@ -61,7 +63,21 @@ RotatingLogHandler m_log_handler; - llvm::SmallVector<Callback, 4> m_callbacks; + struct CallbackEntry { + CallbackEntry(CallbackID id, Callback callback) + : id(id), callback(std::move(callback)) {} + CallbackID id; + Callback callback; + }; + + /// Monotonically increasing callback identifier. Unique per Diagnostic + /// instance. + CallbackID m_callback_id; + + /// List of callback entries. + llvm::SmallVector<CallbackEntry, 4> m_callbacks; + + /// Mutex to protect callback list and callback identifier. std::mutex m_callbacks_mutex; };
Index: lldb/source/Utility/Diagnostics.cpp =================================================================== --- lldb/source/Utility/Diagnostics.cpp +++ lldb/source/Utility/Diagnostics.cpp @@ -43,9 +43,19 @@ Diagnostics::~Diagnostics() {} -void Diagnostics::AddCallback(Callback callback) { +Diagnostics::CallbackID Diagnostics::AddCallback(Callback callback) { std::lock_guard<std::mutex> guard(m_callbacks_mutex); - m_callbacks.push_back(callback); + CallbackID id = m_callback_id++; + m_callbacks.emplace_back(id, callback); + return id; +} + +void Diagnostics::RemoveCallback(CallbackID id) { + std::lock_guard<std::mutex> guard(m_callbacks_mutex); + m_callbacks.erase( + std::remove_if(m_callbacks.begin(), m_callbacks.end(), + [id](const CallbackEntry &e) { return e.id == id; }), + m_callbacks.end()); } bool Diagnostics::Dump(raw_ostream &stream) { @@ -84,8 +94,8 @@ if (Error err = DumpDiangosticsLog(dir)) return err; - for (Callback c : m_callbacks) { - if (Error err = c(dir)) + for (CallbackEntry e : m_callbacks) { + if (Error err = e.callback(dir)) return err; } Index: lldb/include/lldb/Utility/Diagnostics.h =================================================================== --- lldb/include/lldb/Utility/Diagnostics.h +++ lldb/include/lldb/Utility/Diagnostics.h @@ -42,8 +42,10 @@ void Report(llvm::StringRef message); using Callback = std::function<llvm::Error(const FileSpec &)>; + using CallbackID = uint64_t; - void AddCallback(Callback callback); + CallbackID AddCallback(Callback callback); + void RemoveCallback(CallbackID id); static Diagnostics &Instance(); @@ -61,7 +63,21 @@ RotatingLogHandler m_log_handler; - llvm::SmallVector<Callback, 4> m_callbacks; + struct CallbackEntry { + CallbackEntry(CallbackID id, Callback callback) + : id(id), callback(std::move(callback)) {} + CallbackID id; + Callback callback; + }; + + /// Monotonically increasing callback identifier. Unique per Diagnostic + /// instance. + CallbackID m_callback_id; + + /// List of callback entries. + llvm::SmallVector<CallbackEntry, 4> m_callbacks; + + /// Mutex to protect callback list and callback identifier. std::mutex m_callbacks_mutex; };
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits