Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
7f3b5fb8
Commit
7f3b5fb8
authored
Jun 27, 2019
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix dup filter
parent
5dd260c3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
31 deletions
+34
-31
dup_filter_sink.h
include/spdlog/sinks/dup_filter_sink.h
+34
-31
No files found.
include/spdlog/sinks/dup_filter_sink.h
View file @
7f3b5fb8
...
...
@@ -7,24 +7,30 @@
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/log_msg.h"
#include <mutex>
#include <string>
#include <chrono>
// Duplicate
remove
sink.
//
Filter the message if previous one is identical and less than max max_skip_duration
have passed
// Duplicate
message removal
sink.
//
Skip the message if previous one is identical and less than "max_skip_duration"
have passed
//
// Example:
// auto d = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5));
// d->add_sink(std::make_shared<stdout_color_sink_mt>());
// spdlog::logger l("logger", d);
//
// #include "spdlog/sinks/dup_filter_sink.h"
//
// int main() {
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5));
// dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
// 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] 3 duplicate messages..
// [2019-06-25 17:50:56.512] [logger] [info]
Skipped
3 duplicate messages..
// [2019-06-25 17:50:56.512] [logger] [info] Different Hello
...
...
@@ -36,54 +42,51 @@ class dup_filter_sink : public dist_sink<Mutex>
{
public
:
template
<
class
Rep
,
class
Period
>
explicit
dup_filter_sink
(
std
::
chrono
::
duration
<
Rep
,
Period
>
max_ignore_duration
)
:
max_skip_duration_
{
max_ignore_duration
}
,
last_msg_time_
{
log_clock
::
now
()}
,
skip_counter_
{
0
}
explicit
dup_filter_sink
(
std
::
chrono
::
duration
<
Rep
,
Period
>
max_skip_duration
)
:
max_skip_duration_
{
max_skip_duration
}
{}
protected
:
std
::
chrono
::
microseconds
max_skip_duration_
;
log_clock
::
time_point
last_msg_time_
;
size_t
skip_counter_
;
std
::
string
last_msg_payload_
;
size_t
skip_counter_
=
0
;
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
auto
msg_time
=
msg
.
time
;
auto
delta_time
=
msg_time
-
last_msg_time_
;
last_msg_time_
=
msg_time
;
if
(
delta_time
<
max_skip_duration_
&&
last_msg_payload_
==
msg
.
payload
)
bool
filtered
=
filter_
(
msg
);
if
(
!
filtered
)
{
skip_counter_
++
;
skip_counter_
+=
1
;
return
;
}
//
got different message from the previous
if
(
skip_counter_
>
0
)
//
log the "skipped.." message
if
(
skip_counter_
>
0
)
{
fmt
::
basic_memory_buffer
<
char
,
80
>
buf
;
fmt
::
format_to
(
buf
,
"{} duplicate messages.."
,
skip_counter_
);
fmt
::
basic_memory_buffer
<
char
,
64
>
buf
;
fmt
::
format_to
(
buf
,
"
Skipped
{} duplicate messages.."
,
skip_counter_
);
details
::
log_msg
skipped_msg
{
msg
.
logger_name
,
msg
.
level
,
string_view_t
{
buf
.
data
(),
buf
.
size
()}};
// log the skip notification
for
(
auto
&
sink
:
dist_sink
<
Mutex
>::
sinks_
)
{
if
(
sink
->
should_log
(
msg
.
level
))
{
sink
->
log
(
skipped_msg
);
}
dist_sink
<
Mutex
>::
sink_it_
(
skipped_msg
);
}
// log current message
dist_sink
<
Mutex
>::
sink_it_
(
msg
);
last_msg_time_
=
msg
.
time
;
skip_counter_
=
0
;
last_msg_payload_
.
assign
(
msg
.
payload
.
data
(),
msg
.
payload
.
data
()
+
msg
.
payload
.
size
());
}
last_msg_payload_
.
assign
(
msg
.
payload
.
data
(),
msg
.
payload
.
data
()
+
msg
.
payload
.
size
());
dist_sink
<
Mutex
>::
sink_it_
(
msg
);
// 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
<
std
::
mutex
>
;
using
dup_filter_sink_st
=
dup_filter_sink
<
details
::
null_mutex
>
;
}
// namespace sinks
}
// namespace spdlog
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment