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
28e1c659
Commit
28e1c659
authored
Mar 30, 2014
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
async_sink use unique_ptr instead of shared
parent
3a1ac8b4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
19 deletions
+36
-19
example.cpp
example/example.cpp
+8
-6
blocking_queue.h
include/c11log/details/blocking_queue.h
+4
-3
async_sink.h
include/c11log/sinks/async_sink.h
+24
-10
No files found.
example/example.cpp
View file @
28e1c659
...
...
@@ -18,22 +18,24 @@ using namespace utils;
int
main
(
int
argc
,
char
*
argv
[])
{
const
unsigned
int
howmany
=
argc
<=
1
?
1000
:
atoi
(
argv
[
1
]);
const
unsigned
int
howmany
=
argc
<=
1
?
1000
000
:
atoi
(
argv
[
1
]);
logger
cout_logger
(
""
,
sinks
::
stdout_sink
());
cout_logger
.
info
()
<<
"Hello "
<<
"man"
;
//auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
auto
fsink
=
std
::
make_shared
<
sinks
::
rotating_file_sink
>
(
"log"
,
"txt"
,
1024
*
1024
*
50
,
5
,
0
);
auto
fsink2
=
std
::
make_shared
<
sinks
::
rotating_file_sink
>
(
"lllog"
,
"txt"
,
1024
*
1024
*
50
,
5
,
0
);
auto
as
=
std
::
make_shared
<
sinks
::
async_sink
>
(
1000
);
as
->
add_sink
(
sinks
::
null_sink
::
get
());
as
->
add_sink
(
sinks
::
null_sink
::
get
());
logger
my_logger
(
"my_logger"
,
as
);
auto
start
=
system_clock
::
now
();
for
(
unsigned
int
i
=
0
;
i
<
howmany
;
i
++
)
my_logger
.
info
()
<<
"Hello logger
"
;
for
(
unsigned
int
i
=
1
;
i
<=
howmany
;
++
i
)
my_logger
.
info
()
<<
"Hello logger
: "
<<
i
;
as
->
shutdown
(
milliseconds
(
500
0
));
as
->
shutdown
(
milliseconds
(
500
));
auto
delta
=
system_clock
::
now
()
-
start
;
auto
delta_d
=
duration_cast
<
duration
<
double
>>
(
delta
).
count
();
...
...
include/c11log/details/blocking_queue.h
View file @
28e1c659
...
...
@@ -19,8 +19,9 @@ template<typename T>
class
blocking_queue
{
public
:
using
queue_t
=
std
::
queue
<
T
>
;
using
size_type
=
typename
queue_t
::
size_type
;
using
queue_type
=
std
::
queue
<
T
>
;
using
item_type
=
T
;
using
size_type
=
typename
queue_type
::
size_type
;
using
clock
=
std
::
chrono
::
system_clock
;
explicit
blocking_queue
(
size_type
max_size
)
:
...
...
@@ -108,7 +109,7 @@ public:
{
{
std
::
unique_lock
<
std
::
mutex
>
ul
(
_mutex
);
queue_t
().
swap
(
_q
);
queue_t
ype
().
swap
(
_q
);
}
_item_popped_cond
.
notify_all
();
}
...
...
include/c11log/sinks/async_sink.h
View file @
28e1c659
...
...
@@ -3,27 +3,35 @@
#include <thread>
#include <chrono>
#include <atomic>
#include <functional>
#include "base_sink.h"
#include "../logger.h"
#include "../details/blocking_queue.h"
#include "../details/log_msg.h"
#include<iostream>
using
namespace
std
;
namespace
c11log
{
namespace
sinks
{
static
void
msg_deleter
(
details
::
log_msg
*
msg_to_delete
)
{
delete
[]
msg_to_delete
->
msg_buf
.
first
;
delete
msg_to_delete
;
}
class
async_sink
:
public
base_sink
{
public
:
using
queue_t
=
c11log
::
details
::
blocking_queue
<
std
::
shared_ptr
<
details
::
log_msg
>>
;
using
size_type
=
queue_t
::
size_type
;
explicit
async_sink
(
const
size_type
max_queue_size
);
using
queue_type
=
c11log
::
details
::
blocking_queue
<
std
::
unique_ptr
<
details
::
log_msg
,
std
::
function
<
void
(
details
::
log_msg
*
)
>>>
;
explicit
async_sink
(
const
queue_type
::
size_type
max_queue_size
);
//Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
...
...
@@ -42,7 +50,7 @@ protected:
private
:
c11log
::
logger
::
sinks_vector_t
_sinks
;
std
::
atomic
<
bool
>
_active
;
queue_t
_q
;
queue_t
ype
_q
;
std
::
thread
_back_thread
;
//Clear all remaining messages(if any), stop the _back_thread and join it
void
_shutdown
();
...
...
@@ -55,7 +63,7 @@ private:
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline
c11log
::
sinks
::
async_sink
::
async_sink
(
const
size_type
max_queue_size
)
inline
c11log
::
sinks
::
async_sink
::
async_sink
(
const
queue_type
::
size_type
max_queue_size
)
:
_sinks
(),
_active
(
true
),
_q
(
max_queue_size
),
...
...
@@ -66,6 +74,8 @@ inline c11log::sinks::async_sink::~async_sink()
{
_shutdown
();
}
inline
void
c11log
::
sinks
::
async_sink
::
_sink_it
(
const
details
::
log_msg
&
msg
)
{
auto
msg_size
=
msg
.
msg_buf
.
second
;
...
...
@@ -78,13 +88,15 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
char
*
buf
=
new
char
[
msg_size
];
std
::
memcpy
(
buf
,
msg
.
msg_buf
.
first
,
msg_size
);
new_msg
->
msg_buf
=
bufpair_t
(
buf
,
msg_size
);
auto
new_shared_msg
=
std
::
shared_ptr
<
details
::
log_msg
>
(
new_msg
,
[](
details
::
log_msg
*
msg_to_delete
)
/*
auto new_shared_msg =
queue_type::item_type(new_msg, [](const
details::log_msg* msg_to_delete)
{
delete []msg_to_delete->msg_buf.first;
delete msg_to_delete;
});
_q
.
push
(
new_shared_msg
);
* */
queue_type
::
item_type
new_shared_msg
(
new_msg
,
msg_deleter
);
_q
.
push
(
std
::
move
(
new_shared_msg
));
}
inline
void
c11log
::
sinks
::
async_sink
::
_thread_loop
()
...
...
@@ -92,12 +104,14 @@ inline void c11log::sinks::async_sink::_thread_loop()
static
std
::
chrono
::
seconds
pop_timeout
{
1
};
while
(
_active
)
{
std
::
shared_ptr
<
details
::
log_msg
>
msg
;
queue_type
::
item_type
msg
;
if
(
_q
.
pop
(
msg
,
pop_timeout
))
{
for
(
auto
&
sink
:
_sinks
)
{
sink
->
log
(
*
msg
);
if
(
!
_active
)
break
;
}
}
}
...
...
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