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
8338b45b
Commit
8338b45b
authored
May 27, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added tp->wait_empty()
parent
0d0a841e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
16 deletions
+51
-16
include/spdlog/details/mpmc_blocking_q.h
include/spdlog/details/mpmc_blocking_q.h
+7
-0
include/spdlog/details/thread_pool.h
include/spdlog/details/thread_pool.h
+5
-0
tests/test_async.cpp
tests/test_async.cpp
+26
-12
tests/test_sink.h
tests/test_sink.h
+13
-4
No files found.
include/spdlog/details/mpmc_blocking_q.h
View file @
8338b45b
...
...
@@ -72,6 +72,13 @@ public:
return
true
;
}
// wait until the queue is empty
void
wait_empty
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
pop_cv_
.
wait
(
lock
,
[
this
]
{
return
this
->
q_
.
empty
();
});
}
private:
size_t
max_items_
;
std
::
mutex
queue_mutex_
;
...
...
include/spdlog/details/thread_pool.h
View file @
8338b45b
...
...
@@ -139,6 +139,11 @@ public:
return
msg_counter_
.
load
(
std
::
memory_order_relaxed
);
}
void
wait_empty
()
{
q_
.
wait_empty
();
}
private:
std
::
atomic
<
size_t
>
msg_counter_
;
// total # of messages processed in this pool
q_type
q_
;
...
...
tests/test_async.cpp
View file @
8338b45b
...
...
@@ -3,12 +3,6 @@
#include "spdlog/sinks/simple_file_sink.h"
#include "test_sink.h"
// std::unique_ptr<spdlog::async_logger> create_logger(size_t tp_queue_size, size_t tp_threads)
//{
// auto tp = std::make_shared<details::thread_pool>(8192, 1);
// auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
//}
TEST_CASE
(
"basic async test "
,
"[async]"
)
{
using
namespace
spdlog
;
...
...
@@ -25,7 +19,7 @@ TEST_CASE("basic async test ", "[async]")
logger
->
flush
();
}
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flush
ed_msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flush
_counter
()
==
1
);
}
TEST_CASE
(
"discard policy "
,
"[async]"
)
...
...
@@ -43,8 +37,7 @@ TEST_CASE("discard policy ", "[async]")
}
}
REQUIRE
(
test_sink
->
msg_counter
()
<
messages
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
<
messages
);
REQUIRE
(
test_sink
->
msg_counter
()
<
messages
);
}
TEST_CASE
(
"flush"
,
"[async]"
)
...
...
@@ -65,7 +58,27 @@ TEST_CASE("flush", "[async]")
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
250
));
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flush_counter
()
==
1
);
}
TEST_CASE
(
"tp->wait_empty() "
,
"[async]"
)
{
using
namespace
spdlog
;
auto
test_sink
=
std
::
make_shared
<
sinks
::
test_sink_mt
>
();
test_sink
->
set_delay
(
std
::
chrono
::
milliseconds
(
5
));
size_t
messages
=
50
;
auto
tp
=
std
::
make_shared
<
details
::
thread_pool
>
(
messages
,
2
);
auto
logger
=
std
::
make_shared
<
async_logger
>
(
"as"
,
test_sink
,
tp
,
async_overflow_policy
::
block_retry
);
for
(
size_t
i
=
0
;
i
<
messages
;
i
++
)
{
logger
->
info
(
"Hello message #{}"
,
i
);
}
logger
->
flush
();
tp
->
wait_empty
();
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flush_counter
()
==
1
);
}
TEST_CASE
(
"multi threads"
,
"[async]"
)
...
...
@@ -88,17 +101,18 @@ TEST_CASE("multi threads", "[async]")
logger
->
info
(
"Hello message #{}"
,
j
);
}
});
logger
->
flush
();
}
for
(
auto
&
t
:
threads
)
{
t
.
join
();
}
logger
->
flush
();
}
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
*
n_threads
);
REQUIRE
(
test_sink
->
flush
ed_msg_counter
()
==
messages
*
n_threads
);
REQUIRE
(
test_sink
->
flush
_counter
()
==
n_threads
);
}
TEST_CASE
(
"to_file"
,
"[async]"
)
...
...
tests/test_sink.h
View file @
8338b45b
...
...
@@ -8,7 +8,9 @@
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
#include <chrono>
#include <mutex>
#include <thread>
namespace
spdlog
{
namespace
sinks
{
...
...
@@ -22,23 +24,30 @@ public:
return
msg_counter_
;
}
size_t
flush
ed_msg
_counter
()
size_t
flush_counter
()
{
return
flushed_msg_counter_
;
return
flush_counter_
;
}
void
set_delay
(
std
::
chrono
::
milliseconds
delay
)
{
delay_
=
delay
;
}
protected:
void
_sink_it
(
const
details
::
log_msg
&
)
override
{
msg_counter_
++
;
std
::
this_thread
::
sleep_for
(
delay_
);
}
void
_flush
()
override
{
flush
ed_msg_counter_
+=
msg_counter_
;
flush
_counter_
++
;
}
size_t
msg_counter_
{
0
};
size_t
flushed_msg_counter_
{
0
};
size_t
flush_counter_
{
0
};
std
::
chrono
::
milliseconds
delay_
{
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