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
457cc117
Commit
457cc117
authored
Mar 29, 2014
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
async_sink fixes
parent
d50efe9b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
27 deletions
+38
-27
example.cpp
example/example.cpp
+4
-2
stack_oss.h
include/c11log/details/stack_oss.h
+8
-8
logger.h
include/c11log/logger.h
+0
-4
async_sink.h
include/c11log/sinks/async_sink.h
+26
-13
No files found.
example/example.cpp
View file @
457cc117
...
...
@@ -23,9 +23,11 @@ int main(int argc, char* argv[])
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);
logger
my_logger
(
"my_logger"
,
sinks
::
null_sink
::
get
());
auto
as
=
std
::
make_shared
<
sinks
::
async_sink
>
(
howmany
);
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
++
)
...
...
include/c11log/details/stack_oss.h
View file @
457cc117
...
...
@@ -9,17 +9,17 @@ namespace c11log
namespace
details
{
class
st
r
_devicebuf
:
public
std
::
streambuf
class
st
ack
_devicebuf
:
public
std
::
streambuf
{
public
:
using
Base
=
std
::
streambuf
;
st
r
_devicebuf
()
=
default
;
~
st
r
_devicebuf
()
=
default
;
st
ack
_devicebuf
()
=
default
;
~
st
ack
_devicebuf
()
=
default
;
st
r_devicebuf
(
const
str
_devicebuf
&
other
)
=
delete
;
st
r_devicebuf
(
str
_devicebuf
&&
other
)
=
delete
;
st
r_devicebuf
&
operator
=
(
const
str
_devicebuf
&
)
=
delete
;
st
r_devicebuf
&
operator
=
(
str
_devicebuf
&&
)
=
delete
;
st
ack_devicebuf
(
const
stack
_devicebuf
&
other
)
=
delete
;
st
ack_devicebuf
(
stack
_devicebuf
&&
other
)
=
delete
;
st
ack_devicebuf
&
operator
=
(
const
stack
_devicebuf
&
)
=
delete
;
st
ack_devicebuf
&
operator
=
(
stack
_devicebuf
&&
)
=
delete
;
bufpair_t
buf
()
{
...
...
@@ -83,7 +83,7 @@ public:
}
private
:
st
r
_devicebuf
_dev
;
st
ack
_devicebuf
_dev
;
};
}
}
include/c11log/logger.h
View file @
457cc117
...
...
@@ -6,7 +6,6 @@
#include<memory>
#include<mutex>
#include<atomic>
#include <algorithm>
#include "common_types.h"
#include "sinks/base_sink.h"
...
...
@@ -60,7 +59,6 @@ public:
details
::
line_logger
fatal
();
private
:
friend
details
::
line_logger
;
...
...
@@ -99,7 +97,6 @@ inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_
logger
(
name
,
{
sink
},
f
)
{}
inline
c11log
::
details
::
line_logger
c11log
::
logger
::
log
(
c11log
::
level
::
level_enum
msg_level
)
{
return
details
::
line_logger
(
this
,
msg_level
,
msg_level
>=
_logger_level
);
...
...
@@ -133,7 +130,6 @@ inline c11log::details::line_logger c11log::logger::fatal()
}
inline
const
std
::
string
&
c11log
::
logger
::
get_name
()
const
{
return
_logger_name
;
...
...
include/c11log/sinks/async_sink.h
View file @
457cc117
...
...
@@ -24,12 +24,15 @@ public:
using
size_type
=
queue_t
::
size_type
;
explicit
async_sink
(
const
size_type
max_queue_size
);
//Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
~
async_sink
();
void
add_sink
(
logger
::
sink_ptr
sink
);
void
remove_sink
(
logger
::
sink_ptr
sink_ptr
);
//Wait to remaining items (if any) in the queue to be written and shutdown
void
shutdown
(
const
std
::
chrono
::
seconds
&
timeout
);
void
shutdown
(
const
std
::
chrono
::
milli
seconds
&
timeout
);
protected
:
...
...
@@ -43,6 +46,7 @@ private:
std
::
thread
_back_thread
;
//Clear all remaining messages(if any), stop the _back_thread and join it
void
_shutdown
();
std
::
mutex
_shutdown_mutex
;
};
}
}
...
...
@@ -64,19 +68,24 @@ inline c11log::sinks::async_sink::~async_sink()
}
inline
void
c11log
::
sinks
::
async_sink
::
_sink_it
(
const
details
::
log_msg
&
msg
)
{
auto
msg_size
=
msg
.
msg_buf
.
second
;
if
(
!
msg_size
)
return
;
//re allocate on the heap the (stack based) message
auto
new_msg
=
new
details
::
log_msg
();
*
new_msg
=
msg
;
auto
msg_size
=
msg
.
msg_buf
.
second
;
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
=
std
::
shared_ptr
<
details
::
log_msg
>
(
new_msg
,
[](
details
::
log_msg
*
msg_to_delete
)
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
)
{
delete
[]
msg_to_delete
->
msg_buf
.
first
;
delete
msg_to_delete
;
});
_q
.
push
(
new_shared
);
_q
.
push
(
new_shared
_msg
);
}
inline
void
c11log
::
sinks
::
async_sink
::
_thread_loop
()
...
...
@@ -106,23 +115,27 @@ inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
}
inline
void
c11log
::
sinks
::
async_sink
::
shutdown
(
const
std
::
chrono
::
seconds
&
timeout
)
inline
void
c11log
::
sinks
::
async_sink
::
shutdown
(
const
std
::
chrono
::
milliseconds
&
timeout
)
{
auto
until
=
std
::
chrono
::
system_clock
::
now
()
+
timeout
;
while
(
_q
.
size
()
>
0
&&
std
::
chrono
::
system_clock
::
now
()
<
until
)
{
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
200
));
}
if
(
timeout
>
std
::
chrono
::
milliseconds
::
zero
())
{
auto
until
=
log_clock
::
now
()
+
timeout
;
while
(
_q
.
size
()
>
0
&&
log_clock
::
now
()
<
until
)
{
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
2
));
}
}
_shutdown
();
}
inline
void
c11log
::
sinks
::
async_sink
::
_shutdown
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
_shutdown_mutex
);
if
(
_active
)
{
_active
=
false
;
if
(
_back_thread
.
joinable
())
_back_thread
.
join
();
_back_thread
.
join
();
}
}
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