spdlog_sample.cpp
3.63 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "spdlog/spdlogwarp.hh"
#include <thread>
#include <iostream>
#include "spdlog/fmt/fmt.h"
#include "spdlog/fmt/bin_to_hex.h"
void thd()
{
// trace日志会缓存在内存
while (1) {
auto tr = SLog::get_trace("trace1");
tr->trace("1");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tr->trace("2");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tr->trace("3");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tr->trace("4");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tr->trace("5");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tr->trace("6");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tr->trace("7");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
void thd2()
{
while (1) {
auto tr = SLog::get("log2");
tr->error("th2 1");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
tr->error("th2 2");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
tr->info("th2 3");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
tr->info("th2 4");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
tr->info("th2 5");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
tr->info("th2 6");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
tr->info("th2 7");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void thd3()
{
while (1) {
SLog::get("log2")->info("th3 1");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
SLog::get("log2")->info("th3 2");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
SLog::get("log2")->info("th3 3");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void thd4()
{
while (1) {
SLog::global()->info("th4 1");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
SLog::global()->info("th4 2");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
SLog::global()->info("th4 3");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
int main()
{
//初始化前确认是否已经初始化过了。避免主程序已经初始化过了。子模块建议使用专属日志
if (!SLog::has_inited()) {
SLog::init("./", "sample");
}
// 公共日志
SLog::global()->info("I'm global {} {}", 1, "string");
// 此函数是为了方便替换原有代码中的printf
cslog("I'm global log too %d %s", 1, "string");
// 专属日志
SLog::get("subject")->error("I'm {}", "subject");
auto sublog = SLog::get("subject");
sublog->error("I'm {}", "subject");
SLog::get("./2222/subject")->error("I'm {}", "2222");
// 带行号文件名
SPDLOG_LOGGER_INFO(sublog, "with line filename");
// 额外赠送功能,string的format,fmtlib库
std::string fmtstr;
fmt::format(fmtstr, "format {} {} {}", 2, 2.9, "string");
std::cout << fmtstr;
unsigned char buffer[1024];
//memset(buffer, 1024, 0);
// 打印二进制数据
sublog->info("binary: {}", spdlog::to_hex(std::begin(buffer), std::begin(buffer)+1024));
SLog::cleanup();
SLog::get("subject")->error("after drop 1");
SLog::global()->info("after drop global");
//return 0;
auto th = std::thread(thd);
th.detach();
auto th2 = std::thread(thd2);
th2.detach();
auto th3 = std::thread(thd3);
th3.detach();
auto th4 = std::thread(thd4);
th4.detach();
SLog::global()->info("输入dump回车,将trace日志打印到文件中");
while (1) {
std::string input;
std::cin >> input;
if (input == "dump") {
SLog::dump_trace();
}
}
}