-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_logger_test.cpp
More file actions
59 lines (48 loc) · 1.61 KB
/
async_logger_test.cpp
File metadata and controls
59 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @file async_logger_test.cpp
* @brief 异步日志的单线程、多线程功能测试
*/
#include <gtest/gtest.h>
#include <Logger.h>
#include <Sinks/DefaultFileSink.h>
TEST(AsyncLoggerTest, SingleThreadAsyncLogger)
{
WW::Logger & logger = WW::Logger::getAsyncLogger("SingleThreadAsyncLogger");
// 文件输出
auto filename = "async_logger_test_single_thread_log.txt";
auto file_sink = std::make_shared<WW::DefaultFileSink>(filename);
logger.addSink(file_sink);
// 打印日志
logger.trace("This is a trace message.");
logger.debug("This is a debug message.");
logger.info("This is an info message.");
logger.warn("This is a warning message.");
logger.error("This is an error message.");
logger.fatal("This is a fatal message.");
// 刷新日志
logger.flush();
}
TEST(AsyncLoggerTest, MultiThreadAsyncLogger)
{
WW::Logger & logger = WW::Logger::getAsyncLogger("MultiThreadAsyncLogger");
// 文件输出
auto filename = "async_logger_test_multi_thread_log.txt";
auto file_sink = std::make_shared<WW::DefaultFileSink>(filename);
logger.addSink(file_sink);
// 创建线程
constexpr int threads_num = 8;
constexpr int logs_per_thread = 2000;
std::vector<std::thread> threads;
for (int i = 0; i < threads_num; i++) {
threads.emplace_back([&logger, i, logs_per_thread] {
for (int j = 0; j < logs_per_thread; j++) {
logger.info("This is an info message.");
}
});
}
for (auto & thread : threads) {
thread.join();
}
// 刷新日志
logger.flush();
}