dist_sink.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015 David Schury, Gabi Melman
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
11 #include "opc/spdlog/sinks/sink.h"
12 
13 #include <algorithm>
14 #include <mutex>
15 #include <memory>
16 #include <vector>
17 
18 // Distribution sink (mux). Stores a vector of sinks which get called when log is called
19 
20 namespace spdlog
21 {
22 namespace sinks
23 {
24 template<class Mutex>
25 class dist_sink: public base_sink<Mutex>
26 {
27 public:
28  explicit dist_sink() :_sinks() {}
29  dist_sink(const dist_sink&) = delete;
30  dist_sink& operator=(const dist_sink&) = delete;
31  virtual ~dist_sink() = default;
32 
33 protected:
34  std::vector<std::shared_ptr<sink>> _sinks;
35 
36  void _sink_it(const details::log_msg& msg) override
37  {
38  for (auto &sink : _sinks)
39  {
40  if( sink->should_log( msg.level))
41  {
42  sink->log(msg);
43  }
44  }
45  }
46 
47  void _flush() override
48  {
49  std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
50  for (auto &sink : _sinks)
51  sink->flush();
52  }
53 
54 public:
55 
56 
57  void add_sink(std::shared_ptr<sink> sink)
58  {
59  std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
60  _sinks.push_back(sink);
61  }
62 
63  void remove_sink(std::shared_ptr<sink> sink)
64  {
65  std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
66  _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
67  }
68 };
69 
72 }
73 }
void _flush() override
Definition: dist_sink.h:47
bool should_log(level::level_enum msg_level) const
Definition: sink.h:36
int remove(const filename_t &filename)
Definition: os.h:181
dist_sink< std::mutex > dist_sink_mt
Definition: dist_sink.h:70
dist_sink & operator=(const dist_sink &)=delete
std::vector< std::shared_ptr< sink > > _sinks
Definition: dist_sink.h:34
level::level_enum level
Definition: log_msg.h:42
virtual void log(const details::log_msg &msg)=0
void _sink_it(const details::log_msg &msg) override
Definition: dist_sink.h:36
virtual void flush()=0
void add_sink(std::shared_ptr< sink > sink)
Definition: dist_sink.h:57
virtual ~dist_sink()=default
void remove_sink(std::shared_ptr< sink > sink)
Definition: dist_sink.h:63
dist_sink< details::null_mutex > dist_sink_st
Definition: dist_sink.h:71


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04