Added "removeSink" method and related functionality to LogWorker API

This commit is contained in:
Andrej Kislovskij 2016-05-08 22:12:11 +03:00
parent 6c1698c4f7
commit c9cee5d9a0
3 changed files with 30 additions and 0 deletions

View File

@ -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);

View File

@ -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();
}
};
}

View File

@ -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);
}