Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
0cb38085
Commit
0cb38085
authored
Nov 10, 2019
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated ringbuffer sink
parent
cff6644b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
44 deletions
+65
-44
include/spdlog/details/circular_q.h
include/spdlog/details/circular_q.h
+3
-3
include/spdlog/details/log_msg_buffer-inl.h
include/spdlog/details/log_msg_buffer-inl.h
+1
-3
include/spdlog/sinks/ringbuffer_sink-inl.h
include/spdlog/sinks/ringbuffer_sink-inl.h
+24
-12
include/spdlog/sinks/ringbuffer_sink.h
include/spdlog/sinks/ringbuffer_sink.h
+37
-26
No files found.
include/spdlog/details/circular_q.h
View file @
0cb38085
...
...
@@ -77,11 +77,11 @@ public:
{
if
(
tail_
>=
head_
)
{
return
tail_
-
head_
;
return
tail_
-
head_
;
}
else
{
return
max_items_
-
(
head_
-
tail_
);
return
max_items_
-
(
head_
-
tail_
);
}
}
...
...
@@ -90,7 +90,7 @@ public:
const
T
&
at
(
size_t
i
)
const
{
assert
(
i
<
size
());
return
v_
[(
head_
+
i
)
%
max_items_
];
return
v_
[(
head_
+
i
)
%
max_items_
];
}
// Pop item from front.
...
...
include/spdlog/details/log_msg_buffer-inl.h
View file @
0cb38085
...
...
@@ -26,9 +26,7 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
update_string_views
();
}
SPDLOG_INLINE
log_msg_buffer
::
log_msg_buffer
(
log_msg_buffer
&&
other
)
SPDLOG_NOEXCEPT
:
log_msg
{
other
}
,
buffer
{
std
::
move
(
other
.
buffer
)}
SPDLOG_INLINE
log_msg_buffer
::
log_msg_buffer
(
log_msg_buffer
&&
other
)
SPDLOG_NOEXCEPT
:
log_msg
{
other
},
buffer
{
std
::
move
(
other
.
buffer
)}
{
update_string_views
();
}
...
...
include/spdlog/sinks/ringbuffer_sink-inl.h
View file @
0cb38085
...
...
@@ -3,20 +3,19 @@
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/sinks/ringbuffer_sink.h"
#endif
//
#ifndef SPDLOG_HEADER_ONLY
//
#include "spdlog/sinks/ringbuffer_sink.h"
//
#endif
#include "spdlog/common.h"
#include "spdlog/details/os.h"
namespace
spdlog
{
namespace
sinks
{
template
<
typename
Mutex
>
SPDLOG_INLINE
ringbuffer_sink
<
Mutex
>::
ringbuffer_sink
(
size_t
buf_size
)
SPDLOG_INLINE
ringbuffer_sink
<
Mutex
>::
ringbuffer_sink
(
size_t
n_items
)
{
buf_
=
details
::
circular_q
<
details
::
log_msg_buffer
>
(
buf_size
);
buf_
=
details
::
circular_q
<
details
::
log_msg_buffer
>
(
n_items
);
}
template
<
typename
Mutex
>
...
...
@@ -26,18 +25,31 @@ SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg)
}
template
<
typename
Mutex
>
SPDLOG_INLINE
std
::
vector
<
std
::
string
>
ringbuffer_sink
<
Mutex
>::
last
(
size_t
lim
)
SPDLOG_INLINE
std
::
vector
<
std
::
string
>
ringbuffer_sink
<
Mutex
>::
formatted_messages
(
size_t
lim
)
{
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
auto
n_items
=
lim
>
0
?
(
std
::
min
)(
lim
,
buf_
.
size
())
:
buf_
.
size
();
std
::
vector
<
std
::
string
>
ret
;
ret
.
reserve
(
lim
);
size_t
num
=
0
;
for
(
size_t
i
=
0
;
i
<
buf_
.
size
();
i
++
){
num
++
;
ret
.
reserve
(
n_items
);
for
(
size_t
i
=
0
;
i
<
n_items
;
i
++
)
{
memory_buf_t
formatted
;
base_sink
<
Mutex
>::
formatter_
->
format
(
buf_
.
at
(
i
),
formatted
);
ret
.
push_back
(
fmt
::
to_string
(
formatted
));
if
(
lim
>
0
&&
num
==
lim
)
break
;
}
return
ret
;
}
template
<
typename
Mutex
>
SPDLOG_INLINE
std
::
vector
<
details
::
log_msg_buffer
>
ringbuffer_sink
<
Mutex
>::
raw_messages
(
size_t
lim
)
{
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
auto
n_items
=
lim
>
0
?
(
std
::
min
)(
lim
,
buf_
.
size
())
:
buf_
.
size
();
std
::
vector
<
details
::
log_msg_buffer
>
ret
;
ret
.
reserve
(
n_items
);
for
(
size_t
i
=
0
;
i
<
n_items
;
i
++
)
{
ret
.
push_back
(
buf_
.
at
(
i
));
}
return
ret
;
}
...
...
include/spdlog/sinks/ringbuffer_sink.h
View file @
0cb38085
...
...
@@ -3,9 +3,7 @@
#pragma once
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/details/synchronous_factory.h"
#include "spdlog/details/circular_q.h"
#include "spdlog/details/log_msg_buffer.h"
...
...
@@ -22,15 +20,47 @@ template<typename Mutex>
class
ringbuffer_sink
final
:
public
base_sink
<
Mutex
>
{
public:
explicit
ringbuffer_sink
(
size_t
buf_size
);
std
::
vector
<
std
::
string
>
last
(
size_t
lim
=
0
);
explicit
ringbuffer_sink
(
size_t
n_items
)
:
q_
{
n_items
}
{}
std
::
vector
<
details
::
log_msg_buffer
>
last_raw
(
size_t
lim
=
0
)
{
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
auto
n_items
=
lim
>
0
?
(
std
::
min
)(
lim
,
q_
.
size
())
:
q_
.
size
();
std
::
vector
<
details
::
log_msg_buffer
>
ret
;
ret
.
reserve
(
n_items
);
for
(
size_t
i
=
0
;
i
<
n_items
;
i
++
)
{
ret
.
push_back
(
q_
.
at
(
i
));
}
return
ret
;
}
std
::
vector
<
std
::
string
>
last_formatted
(
size_t
lim
=
0
)
{
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
auto
n_items
=
lim
>
0
?
(
std
::
min
)(
lim
,
q_
.
size
())
:
q_
.
size
();
std
::
vector
<
std
::
string
>
ret
;
ret
.
reserve
(
n_items
);
for
(
size_t
i
=
0
;
i
<
n_items
;
i
++
)
{
memory_buf_t
formatted
;
base_sink
<
Mutex
>::
formatter_
->
format
(
q_
.
at
(
i
),
formatted
);
ret
.
push_back
(
fmt
::
to_string
(
formatted
));
}
return
ret
;
}
protected:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
;
void
flush_
()
override
{};
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
{
q_
.
push_back
(
details
::
log_msg_buffer
{
msg
});
}
void
flush_
()
override
{}
private:
details
::
circular_q
<
details
::
log_msg_buffer
>
buf
_
;
details
::
circular_q
<
details
::
log_msg_buffer
>
q
_
;
};
using
ringbuffer_sink_mt
=
ringbuffer_sink
<
std
::
mutex
>
;
...
...
@@ -38,23 +68,4 @@ using ringbuffer_sink_st = ringbuffer_sink<details::null_mutex>;
}
// namespace sinks
//
// factory functions
//
template
<
typename
Factory
=
spdlog
::
synchronous_factory
>
inline
std
::
shared_ptr
<
logger
>
basic_logger_mt
(
const
std
::
string
&
logger_name
,
size_t
buf_size
)
{
return
Factory
::
template
create
<
sinks
::
ringbuffer_sink_mt
>(
logger_name
,
buf_size
);
}
template
<
typename
Factory
=
spdlog
::
synchronous_factory
>
inline
std
::
shared_ptr
<
logger
>
basic_logger_st
(
const
std
::
string
&
logger_name
,
size_t
buf_size
)
{
return
Factory
::
template
create
<
sinks
::
ringbuffer_sink_st
>(
logger_name
,
buf_size
);
}
}
// namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "ringbuffer_sink-inl.h"
#endif
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