mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 10:23:50 +01:00
Added "removeSink" method and related functionality to LogWorker API
This commit is contained in:
parent
6c1698c4f7
commit
c9cee5d9a0
@ -53,6 +53,7 @@ namespace g3 {
|
||||
class LogWorker final {
|
||||
LogWorker() = default;
|
||||
void addWrappedSink(std::shared_ptr<g3::internal::SinkWrapper> wrapper);
|
||||
void removeWrappedSink(std::shared_ptr<g3::internal::SinkWrapper> wrapper);
|
||||
|
||||
LogWorkerImpl _impl;
|
||||
LogWorker(const LogWorker&) = delete;
|
||||
@ -103,6 +104,15 @@ namespace g3 {
|
||||
|
||||
|
||||
|
||||
/// Removes a sink
|
||||
/// @param
|
||||
template<typename T>
|
||||
void removeSink(std::unique_ptr<SinkHandle<T>> sink_handle) {
|
||||
removeWrappedSink(sink_handle.get()->sink());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// internal:
|
||||
/// pushes in background thread (asynchronously) input messages to log file
|
||||
void save(LogMessagePtr entry);
|
||||
|
@ -24,6 +24,7 @@ namespace g3 {
|
||||
// in the resulting future. Ref: SinkHandle::call
|
||||
template<class T>
|
||||
class SinkHandle {
|
||||
friend class LogWorker;
|
||||
std::weak_ptr<internal::Sink<T>> _sink;
|
||||
|
||||
public:
|
||||
@ -48,6 +49,11 @@ namespace g3 {
|
||||
return std::move(promise.get_future());
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::shared_ptr<internal::Sink<T>> sink() {
|
||||
return _sink.lock();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -116,6 +116,20 @@ namespace g3 {
|
||||
token_done.wait();
|
||||
}
|
||||
|
||||
void LogWorker::removeWrappedSink(std::shared_ptr<g3::internal::SinkWrapper> sink) {
|
||||
auto bg_removesink_call = [this, sink] {
|
||||
for (auto it = _impl._sinks.begin(); it != _impl._sinks.end(); ) {
|
||||
if ((*it).get() == sink.get()) {
|
||||
it = _impl._sinks.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
};
|
||||
auto token_done = g3::spawn_task(bg_removesink_call, _impl._bg.get());
|
||||
token_done.wait();
|
||||
}
|
||||
|
||||
std::unique_ptr<LogWorker> LogWorker::createLogWorker() {
|
||||
return std::unique_ptr<LogWorker>(new LogWorker);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user