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
911a2986
Commit
911a2986
authored
May 19, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed flush interval in async helper
parent
8321449c
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
340 additions
and
359 deletions
+340
-359
example/bench.cpp
example/bench.cpp
+10
-23
include/spdlog/contrib/sinks/step_file_sink.h
include/spdlog/contrib/sinks/step_file_sink.h
+10
-7
include/spdlog/details/async_log_helper.h
include/spdlog/details/async_log_helper.h
+244
-247
include/spdlog/details/mpmc_blocking_q.h
include/spdlog/details/mpmc_blocking_q.h
+67
-72
include/spdlog/sinks/test_sink.h
include/spdlog/sinks/test_sink.h
+9
-10
No files found.
example/bench.cpp
View file @
911a2986
...
...
@@ -7,7 +7,8 @@
// bench.cpp : spdlog benchmarks
//
#include "spdlog/async_logger.h"
#include "spdlog/sinks/test_sink.h"
#include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
...
...
@@ -29,10 +30,12 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
int
main
(
int
argc
,
char
*
argv
[])
{
int
queue_size
=
10
24
*
1024
;
int
queue_size
=
10
48576
;
int
howmany
=
1000000
;
int
threads
=
10
;
int
file_size
=
30
*
1024
*
1024
;
int
rotating_files
=
5
;
try
{
...
...
@@ -42,7 +45,7 @@ int main(int argc, char *argv[])
threads
=
atoi
(
argv
[
2
]);
if
(
argc
>
3
)
queue_size
=
atoi
(
argv
[
3
]);
/*
cout
<<
"*******************************************************************************
\n
"
;
cout
<<
"Single thread, "
<<
format
(
howmany
)
<<
" iterations"
<<
endl
;
cout
<<
"*******************************************************************************
\n
"
;
...
...
@@ -64,32 +67,17 @@ int main(int argc, char *argv[])
bench_mt
(
howmany
,
daily_mt
,
threads
);
bench
(
howmany
,
spdlog
::
create
<
null_sink_st
>
(
"null_mt"
));
*/
cout
<<
"
\n
*******************************************************************************
\n
"
;
cout
<<
"async logging.. "
<<
threads
<<
" threads sharing same logger, "
<<
format
(
howmany
)
<<
" iterations "
<<
endl
;
cout
<<
"*******************************************************************************
\n
"
;
spdlog
::
set_async_mode
(
queue_size
);
for
(
int
i
=
0
;
i
<
300
;
++
i
)
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
//auto as = spdlog::daily_logger_mt("as", "logs/daily_async.log");
auto
test_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
test_sink_mt
>
();
//auto as = spdlog::basic_logger_mt("as", "logs/async.log", true);
auto
as
=
std
::
make_shared
<
spdlog
::
async_logger
>
(
"as"
,
test_sink
,
queue_size
,
async_overflow_policy
::
block_retry
,
nullptr
,
std
::
chrono
::
milliseconds
(
2000
));
bench_mt
(
howmany
,
as
,
threads
);
as
.
reset
();
auto
as
=
spdlog
::
daily_logger_st
(
"as"
,
"logs/daily_async.log"
);
bench_mt
(
howmany
,
as
,
threads
);
spdlog
::
drop
(
"as"
);
auto
msg_counter
=
test_sink
->
msg_counter
();
cout
<<
"Count:"
<<
msg_counter
<<
endl
;
if
(
msg_counter
!=
howmany
)
{
cout
<<
"ERROR! Expected "
<<
howmany
;
exit
(
0
);
}
}
}
catch
(
std
::
exception
&
ex
)
...
...
@@ -131,7 +119,6 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
if
(
counter
>
howmany
)
break
;
log
->
info
(
"Hello logger: msg number {}"
,
counter
);
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}));
}
...
...
include/spdlog/contrib/sinks/step_file_sink.h
View file @
911a2986
...
...
@@ -17,8 +17,10 @@
//
// Create a file logger which creates new files with a specified time step and fixed file size:
//
// std::shared_ptr<logger> step_logger_mt(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max());
// std::shared_ptr<logger> step_logger_st(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max());
// std::shared_ptr<logger> step_logger_mt(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const
// filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max()); std::shared_ptr<logger>
// step_logger_st(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp",
// unsigned max_file_size = std::numeric_limits<unsigned>::max());
// Example for spdlog_impl.h
// Create a file logger that creates new files with a specified increment
...
...
@@ -76,7 +78,7 @@ public:
{
throw
spdlog_ex
(
"step_file_sink: Invalid max log size in ctor"
);
}
_tp
=
_next_tp
();
std
::
tie
(
_current_filename
,
_ext
)
=
FileNameCalc
::
calc_filename
(
_base_filename
,
_tmp_ext
);
...
...
@@ -88,7 +90,7 @@ public:
_file_helper
.
open
(
_current_filename
);
_current_size
=
_file_helper
.
size
();
// expensive. called only once
}
~
step_file_sink
()
{
try
...
...
@@ -96,7 +98,8 @@ public:
close_current_file
();
}
catch
(...)
{}
{
}
}
protected:
...
...
@@ -130,7 +133,7 @@ private:
{
using
details
::
os
::
filename_to_str
;
filename_t
src
=
_current_filename
,
target
;
filename_t
src
=
_current_filename
,
target
;
std
::
tie
(
target
,
std
::
ignore
)
=
details
::
file_helper
::
split_by_extenstion
(
src
);
target
+=
_ext
;
...
...
@@ -149,7 +152,7 @@ private:
filename_t
_current_filename
;
filename_t
_ext
;
unsigned
_current_size
;
details
::
file_helper
_file_helper
;
};
...
...
include/spdlog/details/async_log_helper.h
View file @
911a2986
This diff is collapsed.
Click to expand it.
include/spdlog/details/mpmc_blocking_q.h
View file @
911a2986
...
...
@@ -6,84 +6,79 @@
//
// async log helper :
// multi producer-multi consumer blocking queue
// multi producer-multi consumer blocking queue
// enqueue(..) - will block until room found to put the new message
// enqueue_nowait(..) - will return immediatly with false if no room left in the queue
// dequeue_for(..) - will block until the queue is not empty or timeout passed
#include <condition_variable>
#include <mutex>
#include <queue>
namespace
spdlog
{
namespace
details
{
template
<
typename
T
>
class
mpmc_bounded_queue
{
public:
using
item_type
=
T
;
explicit
mpmc_bounded_queue
(
size_t
max_items
)
:
max_items_
(
max_items
)
{}
// try to enqueue and block if no room left
void
enqueue
(
T
&&
item
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
pop_cv_
.
wait
(
lock
,
[
this
]
{
return
this
->
q_
.
size
()
<=
this
->
max_items_
;
});
q_
.
push
(
std
::
forward
<
T
>
(
item
));
}
push_cv_
.
notify_one
();
}
// try to enqueue and return immdeialty false if no room left
bool
enqueue_nowait
(
T
&&
item
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
if
(
this
->
q_
.
size
()
>=
this
->
max_items_
)
{
return
false
;
}
q_
.
push
(
std
::
forward
<
T
>
(
item
));
}
push_cv_
.
notify_one
();
return
true
;
}
// try to dequeue item. if no item found. wait upto timeout and try again
// Return true, if succeeded dequeue item, false otherwise
bool
dequeue_for
(
T
&
popped_item
,
std
::
chrono
::
milliseconds
wait_duration
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
//push_cv_.wait(lock, [this] {return this->q_.size() > 0; });
bool
found_msg
=
push_cv_
.
wait_for
(
lock
,
wait_duration
,
[
this
]
{
return
this
->
q_
.
size
()
>
0
;
});
if
(
!
found_msg
)
{
return
false
;
}
popped_item
=
std
::
move
(
q_
.
front
());
q_
.
pop
();
}
pop_cv_
.
notify_one
();
return
true
;
}
private:
size_t
max_items_
;
std
::
mutex
queue_mutex_
;
std
::
condition_variable
push_cv_
;
std
::
condition_variable
pop_cv_
;
std
::
queue
<
T
>
q_
;
};
}
}
namespace
details
{
template
<
typename
T
>
class
mpmc_bounded_queue
{
public:
using
item_type
=
T
;
explicit
mpmc_bounded_queue
(
size_t
max_items
)
:
max_items_
(
max_items
)
{
}
// try to enqueue and block if no room left
void
enqueue
(
T
&&
item
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
pop_cv_
.
wait
(
lock
,
[
this
]
{
return
this
->
q_
.
size
()
<
this
->
max_items_
;
});
q_
.
push
(
std
::
move
(
item
));
}
push_cv_
.
notify_one
();
}
// try to enqueue and return immdeialty false if no room left
bool
enqueue_nowait
(
T
&&
item
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
if
(
q_
.
size
()
==
this
->
max_items_
)
{
return
false
;
}
q_
.
push
(
std
::
forward
<
T
>
(
item
));
}
push_cv_
.
notify_one
();
return
true
;
}
// try to dequeue item. if no item found. wait upto timeout and try again
// Return true, if succeeded dequeue item, false otherwise
bool
dequeue_for
(
T
&
popped_item
,
std
::
chrono
::
milliseconds
wait_duration
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
if
(
!
push_cv_
.
wait_for
(
lock
,
wait_duration
,
[
this
]
{
return
this
->
q_
.
size
()
>
0
;
}))
{
return
false
;
}
popped_item
=
std
::
move
(
q_
.
front
());
q_
.
pop
();
}
pop_cv_
.
notify_one
();
return
true
;
}
private:
size_t
max_items_
;
std
::
mutex
queue_mutex_
;
std
::
condition_variable
push_cv_
;
std
::
condition_variable
pop_cv_
;
std
::
queue
<
T
>
q_
;
};
}
// namespace details
}
// namespace spdlog
include/spdlog/sinks/test_sink.h
View file @
911a2986
...
...
@@ -17,20 +17,19 @@ template<class Mutex>
class
test_sink
:
public
base_sink
<
Mutex
>
{
public:
size_t
msg_counter
()
{
return
msg_counter_
;
}
size_t
msg_counter
()
{
return
msg_counter_
;
}
protected:
void
_sink_it
(
const
details
::
log_msg
&
)
override
{
msg_counter_
++
;
}
void
_sink_it
(
const
details
::
log_msg
&
)
override
{
msg_counter_
++
;
}
void
_flush
()
override
{}
size_t
msg_counter_
{
0
};
size_t
msg_counter_
{
0
};
};
using
test_sink_mt
=
test_sink
<
std
::
mutex
>
;
...
...
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