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
cff6644b
Unverified
Commit
cff6644b
authored
Nov 09, 2019
by
Gabi Melman
Committed by
GitHub
Nov 09, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1307 from eudoxos/ringbuffer-sink
Add ringbuffer sink
parents
653ec05c
63837530
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
127 additions
and
0 deletions
+127
-0
circular_q.h
include/spdlog/details/circular_q.h
+21
-0
ringbuffer_sink-inl.h
include/spdlog/sinks/ringbuffer_sink-inl.h
+46
-0
ringbuffer_sink.h
include/spdlog/sinks/ringbuffer_sink.h
+60
-0
No files found.
include/spdlog/details/circular_q.h
View file @
cff6644b
...
...
@@ -72,6 +72,27 @@ public:
return
v_
[
head_
];
}
// Return number of elements actually stored
size_t
size
()
const
{
if
(
tail_
>=
head_
)
{
return
tail_
-
head_
;
}
else
{
return
max_items_
-
(
head_
-
tail_
);
}
}
// Return const reference to item by index.
// If index is out of range 0…size()-1, the behavior is undefined.
const
T
&
at
(
size_t
i
)
const
{
assert
(
i
<
size
());
return
v_
[(
head_
+
i
)
%
max_items_
];
}
// Pop item from front.
// If there are no elements in the container, the behavior is undefined.
void
pop_front
()
...
...
include/spdlog/sinks/ringbuffer_sink-inl.h
0 → 100644
View file @
cff6644b
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#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
)
{
buf_
=
details
::
circular_q
<
details
::
log_msg_buffer
>
(
buf_size
);
}
template
<
typename
Mutex
>
SPDLOG_INLINE
void
ringbuffer_sink
<
Mutex
>::
sink_it_
(
const
details
::
log_msg
&
msg
)
{
buf_
.
push_back
(
details
::
log_msg_buffer
{
msg
});
}
template
<
typename
Mutex
>
SPDLOG_INLINE
std
::
vector
<
std
::
string
>
ringbuffer_sink
<
Mutex
>::
last
(
size_t
lim
)
{
std
::
lock_guard
<
Mutex
>
lock
(
base_sink
<
Mutex
>::
mutex_
);
std
::
vector
<
std
::
string
>
ret
;
ret
.
reserve
(
lim
);
size_t
num
=
0
;
for
(
size_t
i
=
0
;
i
<
buf_
.
size
();
i
++
){
num
++
;
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
;
}
}
// namespace sinks
}
// namespace spdlog
include/spdlog/sinks/ringbuffer_sink.h
0 → 100644
View file @
cff6644b
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#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"
#include <mutex>
#include <string>
#include <vector>
namespace
spdlog
{
namespace
sinks
{
/*
* Ring buffer sink
*/
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
);
protected
:
void
sink_it_
(
const
details
::
log_msg
&
msg
)
override
;
void
flush_
()
override
{};
private
:
details
::
circular_q
<
details
::
log_msg_buffer
>
buf_
;
};
using
ringbuffer_sink_mt
=
ringbuffer_sink
<
std
::
mutex
>
;
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