From f9bc3c9d1834cb8bd5f872b749b057c33e8b0102 Mon Sep 17 00:00:00 2001 From: BobTheBob <32057864+BobTheBob9@users.noreply.github.com> Date: Wed, 24 Aug 2022 00:32:31 +0100 Subject: Adjust folder structure (#242) * Adjust folder structure * change launcher directory name --- .../include/spdlog/sinks/dup_filter_sink.h | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h (limited to 'NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h') diff --git a/NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h b/NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h new file mode 100644 index 00000000..c9a08d68 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h @@ -0,0 +1,90 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "dist_sink.h" +#include +#include + +#include +#include +#include + +// Duplicate message removal sink. +// Skip the message if previous one is identical and less than "max_skip_duration" have passed +// +// Example: +// +// #include +// +// int main() { +// auto dup_filter = std::make_shared(std::chrono::seconds(5)); +// dup_filter->add_sink(std::make_shared()); +// spdlog::logger l("logger", dup_filter); +// l.info("Hello"); +// l.info("Hello"); +// l.info("Hello"); +// l.info("Different Hello"); +// } +// +// Will produce: +// [2019-06-25 17:50:56.511] [logger] [info] Hello +// [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages.. +// [2019-06-25 17:50:56.512] [logger] [info] Different Hello + +namespace spdlog { +namespace sinks { +template +class dup_filter_sink : public dist_sink +{ +public: + template + explicit dup_filter_sink(std::chrono::duration max_skip_duration) + : max_skip_duration_{max_skip_duration} + {} + +protected: + std::chrono::microseconds max_skip_duration_; + log_clock::time_point last_msg_time_; + std::string last_msg_payload_; + size_t skip_counter_ = 0; + + void sink_it_(const details::log_msg &msg) override + { + bool filtered = filter_(msg); + if (!filtered) + { + skip_counter_ += 1; + return; + } + + // log the "skipped.." message + if (skip_counter_ > 0) + { + memory_buf_t buf; + fmt::format_to(buf, "Skipped {} duplicate messages..", skip_counter_); + details::log_msg skipped_msg{msg.logger_name, level::info, string_view_t{buf.data(), buf.size()}}; + dist_sink::sink_it_(skipped_msg); + } + + // log current message + dist_sink::sink_it_(msg); + last_msg_time_ = msg.time; + skip_counter_ = 0; + last_msg_payload_.assign(msg.payload.data(), msg.payload.data() + msg.payload.size()); + } + + // return whether the log msg should be displayed (true) or skipped (false) + bool filter_(const details::log_msg &msg) + { + auto filter_duration = msg.time - last_msg_time_; + return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_); + } +}; + +using dup_filter_sink_mt = dup_filter_sink; +using dup_filter_sink_st = dup_filter_sink; + +} // namespace sinks +} // namespace spdlog -- cgit v1.2.3