Agora  1.2.0
Agora project
dup_filter_sink.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 #include "dist_sink.h"
9 
10 #include <cstdio>
11 #include <mutex>
12 #include <string>
13 #include <chrono>
14 
15 // Duplicate message removal sink.
16 // Skip the message if previous one is identical and less than "max_skip_duration" have passed
17 //
18 // Example:
19 //
20 // #include <spdlog/sinks/dup_filter_sink.h>
21 //
22 // int main() {
23 // auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5));
24 // dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
25 // spdlog::logger l("logger", dup_filter);
26 // l.info("Hello");
27 // l.info("Hello");
28 // l.info("Hello");
29 // l.info("Different Hello");
30 // }
31 //
32 // Will produce:
33 // [2019-06-25 17:50:56.511] [logger] [info] Hello
34 // [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages..
35 // [2019-06-25 17:50:56.512] [logger] [info] Different Hello
36 
37 namespace spdlog {
38 namespace sinks {
39 template<typename Mutex>
40 class dup_filter_sink : public dist_sink<Mutex>
41 {
42 public:
43  template<class Rep, class Period>
44  explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration)
45  : max_skip_duration_{max_skip_duration}
46  {}
47 
48 protected:
49  std::chrono::microseconds max_skip_duration_;
50  log_clock::time_point last_msg_time_;
51  std::string last_msg_payload_;
52  size_t skip_counter_ = 0;
53 
54  void sink_it_(const details::log_msg &msg) override
55  {
56  bool filtered = filter_(msg);
57  if (!filtered)
58  {
59  skip_counter_ += 1;
60  return;
61  }
62 
63  // log the "skipped.." message
64  if (skip_counter_ > 0)
65  {
66  char buf[64];
67  auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", static_cast<unsigned>(skip_counter_));
68  if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf))
69  {
70  details::log_msg skipped_msg{msg.logger_name, level::info, string_view_t{buf, static_cast<size_t>(msg_size)}};
71  dist_sink<Mutex>::sink_it_(skipped_msg);
72  }
73  }
74 
75  // log current message
77  last_msg_time_ = msg.time;
78  skip_counter_ = 0;
79  last_msg_payload_.assign(msg.payload.data(), msg.payload.data() + msg.payload.size());
80  }
81 
82  // return whether the log msg should be displayed (true) or skipped (false)
83  bool filter_(const details::log_msg &msg)
84  {
85  auto filter_duration = msg.time - last_msg_time_;
86  return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_);
87  }
88 };
89 
92 
93 } // namespace sinks
94 } // namespace spdlog
spdlog::sinks::dist_sink
Definition: dist_sink.h:23
spdlog::sinks::dup_filter_sink::dup_filter_sink
dup_filter_sink(std::chrono::duration< Rep, Period > max_skip_duration)
Definition: dup_filter_sink.h:44
spdlog::details::log_msg::payload
string_view_t payload
Definition: log_msg.h:30
spdlog::sinks::dup_filter_sink::skip_counter_
size_t skip_counter_
Definition: dup_filter_sink.h:52
fmt::v8::basic_string_view
Definition: core.h:448
fmt::v8::basic_string_view::size
constexpr auto size() const -> size_t
Definition: core.h:495
spdlog::level::info
@ info
Definition: common.h:215
spdlog::sinks::dup_filter_sink::last_msg_time_
log_clock::time_point last_msg_time_
Definition: dup_filter_sink.h:50
spdlog::sinks::dup_filter_sink::filter_
bool filter_(const details::log_msg &msg)
Definition: dup_filter_sink.h:83
null_mutex.h
spdlog::sinks::dup_filter_sink::last_msg_payload_
std::string last_msg_payload_
Definition: dup_filter_sink.h:51
spdlog::sinks::dup_filter_sink
Definition: dup_filter_sink.h:40
spdlog
Definition: async.h:25
spdlog::details::log_msg::logger_name
string_view_t logger_name
Definition: log_msg.h:20
spdlog::sinks::dup_filter_sink::max_skip_duration_
std::chrono::microseconds max_skip_duration_
Definition: dup_filter_sink.h:49
fmt::v8::basic_string_view::data
constexpr auto data() const -> const Char *
Definition: core.h:492
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::dist_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: dist_sink.h:58
dist_sink.h
log_msg.h
spdlog::details::log_msg::time
log_clock::time_point time
Definition: log_msg.h:22
spdlog::sinks::dup_filter_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: dup_filter_sink.h:54