Skip to content

Commit cbfd4c2

Browse files
authored
Merge pull request #38 from justagist/feature/dup_filter_sink
add bindings for mux (distribution) sink; and duplication filter sink
2 parents 4723d51 + 9e013f9 commit cbfd4c2

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

src/pyspdlog.cpp

+84-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <pybind11/functional.h>
77
#include <pybind11/pybind11.h>
88
#include <pybind11/stl.h>
9-
//#include <pybind11/chrono.h>
9+
#include <pybind11/chrono.h>
1010

1111
using namespace pybind11::literals;
1212

@@ -20,6 +20,9 @@ using namespace pybind11::literals;
2020
#include <spdlog/sinks/stdout_color_sinks.h>
2121
#include <spdlog/sinks/stdout_sinks.h>
2222
#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>
2326
#ifndef _WIN32
2427
#include <spdlog/sinks/syslog_sink.h>
2528
#endif
@@ -235,6 +238,64 @@ class rotating_file_sink_st : public Sink {
235238
}
236239
};
237240

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+
238299
class null_sink_st : public Sink {
239300
public:
240301
null_sink_st()
@@ -759,6 +820,28 @@ PYBIND11_MODULE(spdlog, m)
759820
py::arg("max_size"),
760821
py::arg("max_files"));
761822

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+
762845
py::class_<null_sink_st, Sink>(m, "null_sink_st")
763846
.def(py::init<>());
764847

0 commit comments

Comments
 (0)