-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async.cpp
More file actions
74 lines (58 loc) · 2.03 KB
/
test_async.cpp
File metadata and controls
74 lines (58 loc) · 2.03 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "TinyLog/include/LogConfig.h"
#include "TinyLog/include/SynLog.h"
#include "TinyLog/include/AsynLog.h"
#include "TinyLog/include/Logging.h"
#include "TinyLog/include/ThreadInfo.h"
#include <cstddef>
#include <functional>
#include <iostream>
#include <memory>
#include <mutex>
#include <sys/syscall.h>
#include <thread>
#include <unistd.h>
#include <vector>
using namespace std;
void func() {
for (int i = 0; i < 1e5; i++) {
auto curId = TinyLog::ThreadInfo::getTid();
LOG_TRACE("TRACE hello world, current thread id = %d\n", curId);
LOG_DEBUG("DEBUG hello world, current thread id = %d\n", curId);
LOG_INFO("INFO hello world, current thread id = %d\n", curId);
LOG_WARN("WARN hello world, current thread id = %d\n", curId);
LOG_ERROR("ERROR hello world, current thread id = %d\n", curId);
}
}
unique_ptr<TinyLog::AsynLog> asynLog;
/* 异步日志输出 */
void asynOutput(const char *_msg, size_t _len, size_t _kenLen = 0) {
asynLog->append(_msg, _len, _kenLen);
}
void AsynLogger() {
/* 设置日志等级和日志文件相关的设置 */
TinyLog::LogConfig config;
config.logLevel = TinyLog::Logger::TRACE;
config.fileOption.baseName = "main.cpp-AsynLog";
config.fileOption.fileWriter = TinyLog::NORMALFileWriter;
/* 500MB */
config.fileOption.rooSize = static_cast<size_t>(500 * 1024) * 1024;
TinyLog::Logger::setConfig(config);
/* 设置输出 */
TinyLog::Logger::setOutput(asynOutput);
asynLog =
make_unique<TinyLog::AsynLog>();
asynLog->start();
}
int main(int, char **) {
auto start_time = chrono::system_clock::now();
AsynLogger();
std::vector<std::thread> threads(8);
for (auto &t : threads)
t = std::thread(func);
for (auto &t : threads)
t.join();
auto end_time = chrono::system_clock::now();
chrono::milliseconds cost_time = chrono::duration_cast<chrono::milliseconds>(end_time - start_time);
std::cout << "AsynLogger cost time = " << cost_time.count() << endl;
return 0;
}