6
6
#include < pybind11/functional.h>
7
7
#include < pybind11/pybind11.h>
8
8
#include < pybind11/stl.h>
9
- // #include <pybind11/chrono.h>
9
+ #include < pybind11/chrono.h>
10
10
11
11
using namespace pybind11 ::literals;
12
12
@@ -20,6 +20,9 @@ using namespace pybind11::literals;
20
20
#include < spdlog/sinks/stdout_color_sinks.h>
21
21
#include < spdlog/sinks/stdout_sinks.h>
22
22
#include < spdlog/sinks/tcp_sink.h>
23
+ #include < spdlog/sinks/dist_sink.h>
24
+ #include < spdlog/sinks/dup_filter_sink.h>
25
+ #include < spdlog/details/null_mutex.h>
23
26
#ifndef _WIN32
24
27
#include < spdlog/sinks/syslog_sink.h>
25
28
#endif
@@ -235,6 +238,64 @@ class rotating_file_sink_st : public Sink {
235
238
}
236
239
};
237
240
241
+ template <typename Mutex>
242
+ class dist_sink : public Sink {
243
+ public:
244
+ dist_sink () { _sink = std::make_shared<spdlog::sinks::dist_sink<Mutex>>();}
245
+ dist_sink (std::vector<Sink> sinks)
246
+ {
247
+ std::vector<spd::sink_ptr> sink_vec;
248
+ for (uint i =0 ; i<sinks.size (); i++)
249
+ {
250
+ sink_vec.push_back (sinks.at (i).get_sink ());
251
+ }
252
+ _sink = std::make_shared<spdlog::sinks::dist_sink<Mutex>>(sink_vec);
253
+ }
254
+ void add_sink (const Sink& sink)
255
+ {
256
+ std::dynamic_pointer_cast<spdlog::sinks::dist_sink<Mutex>>(_sink)->add_sink (sink.get_sink ());
257
+ }
258
+
259
+ void remove_sink (const Sink& sink)
260
+ {
261
+ std::dynamic_pointer_cast<spdlog::sinks::dist_sink<Mutex>>(_sink)->remove_sink (sink.get_sink ());
262
+ }
263
+
264
+ void set_sinks (std::vector<Sink> sinks)
265
+ {
266
+ std::vector<spd::sink_ptr> sink_vec;
267
+ for (uint i =0 ; i<sinks.size (); i++)
268
+ {
269
+ sink_vec.push_back (sinks.at (i).get_sink ());
270
+ }
271
+ std::dynamic_pointer_cast<spdlog::sinks::dist_sink<Mutex>>(_sink)->set_sinks (sink_vec);
272
+ }
273
+
274
+ std::vector<spd::sink_ptr> &sinks ()
275
+ {
276
+ return std::dynamic_pointer_cast<spdlog::sinks::dist_sink<Mutex>>(_sink)->sinks ();
277
+ }
278
+ };
279
+
280
+ using dist_sink_mt = dist_sink<std::mutex>;
281
+ using dist_sink_st = dist_sink<spd::details::null_mutex>;
282
+
283
+ class dup_filter_sink_mt : public dist_sink_mt {
284
+ public:
285
+ dup_filter_sink_mt (float max_skip_duration_sec)
286
+ {
287
+ _sink = std::make_shared<spdlog::sinks::dup_filter_sink_mt>(std::chrono::milliseconds ((int )(max_skip_duration_sec*1000.0 )));
288
+ }
289
+ };
290
+
291
+ class dup_filter_sink_st : public dist_sink_st {
292
+ public:
293
+ dup_filter_sink_st (float max_skip_duration_sec)
294
+ {
295
+ _sink = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::milliseconds ((int )(max_skip_duration_sec*1000.0 )));
296
+ }
297
+ };
298
+
238
299
class null_sink_st : public Sink {
239
300
public:
240
301
null_sink_st ()
@@ -759,6 +820,28 @@ PYBIND11_MODULE(spdlog, m)
759
820
py::arg (" max_size" ),
760
821
py::arg (" max_files" ));
761
822
823
+ py::class_<dist_sink_mt, Sink>(m, " dist_sink_mt" )
824
+ .def (py::init<>())
825
+ .def (py::init<std::vector<Sink>>(), py::arg (" sinks" ))
826
+ .def (" add_sink" , &dist_sink_mt::add_sink, py::arg (" sink" ))
827
+ .def (" remove_sink" , &dist_sink_mt::remove_sink, py::arg (" sink" ))
828
+ .def (" set_sinks" , &dist_sink_mt::set_sinks, py::arg (" sinks" ))
829
+ .def (" sinks" , &dist_sink_mt::sinks);
830
+
831
+ py::class_<dist_sink_st, Sink>(m, " dist_sink_st" )
832
+ .def (py::init<>())
833
+ .def (py::init<std::vector<Sink>>(), py::arg (" sinks" ))
834
+ .def (" add_sink" , &dist_sink_st::add_sink, py::arg (" sink" ))
835
+ .def (" remove_sink" , &dist_sink_st::remove_sink, py::arg (" sink" ))
836
+ .def (" set_sinks" , &dist_sink_st::set_sinks, py::arg (" sinks" ))
837
+ .def (" sinks" , &dist_sink_st::sinks);
838
+
839
+ py::class_<dup_filter_sink_st, dist_sink_st>(m, " dup_filter_sink_st" )
840
+ .def (py::init<float >(), py::arg (" max_skip_duration_seconds" ));
841
+
842
+ py::class_<dup_filter_sink_mt, dist_sink_mt>(m, " dup_filter_sink_mt" )
843
+ .def (py::init<float >(), py::arg (" max_skip_duration_seconds" ));
844
+
762
845
py::class_<null_sink_st, Sink>(m, " null_sink_st" )
763
846
.def (py::init<>());
764
847
0 commit comments