spdlog_sample.cpp
4.19 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
135
136
137
#include "spdlog/spdlogwarp.hh"
#include <thread>
#include <iostream>
#include "spdlog/fmt/fmt.h"
#include "spdlog/fmt/bin_to_hex.h"
#define SLP_MS 1000
void thd()
{
// trace日志会缓存在内存
while (1) {
auto tr = SLog::get_trace("trace1");
tr->trace("1");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->trace("2");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->trace("3");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->trace("4");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->trace("5");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->trace("6");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->trace("7");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
}
}
void thd2()
{
while (1) {
auto tr = SLog::get("error_info");
tr->error("th2 1");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->error("th2 2");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->info("th2 1");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
tr->info("th2 2");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
}
}
void thd3()
{
while (1) {
SLog::get("debuglog")->debug("th3 1");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
SLog::get("debuglog")->debug("th3 2");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
SLog::get("debuglog")->debug("th3 3");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
}
}
void thd4()
{
while (1) {
SLog::global()->info("th4 1");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
SLog::global()->debug("th4 2");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
SLog::global()->error("th4 3");
std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
}
}
int main()
{
//初始化前确认是否已经初始化过了。避免主程序已经初始化过了。子模块建议使用专属日志
if (!SLog::has_inited()) {
SLog::init("./", "sample");
}
// 公共日志
SLog::global()->info("I'm global {} {}", 1, "string");
// 此函数是为了方便替换原有代码中的printf。会输出日志到[appname].log中
cslog("I'm global log too %d %s", 1, "string");
// 专属日志,输出到subject.log
SLog::get("subject")->error("I'm {}", "subject");
auto sublog = SLog::get("subject");
sublog->error("I'm {}", "subject");
// 与sublog->info相比,写出的日志带行号文件名
SPDLOG_LOGGER_INFO(sublog, "with line filename");
SPDLOG_LOGGER_DEBUG(sublog, "with line filename");
// 专属日志,输出到./2222/subject.log
SLog::get("./2222/subject")->error("I'm {}", "2222");
// 额外赠送功能,string的format,fmtlib库
std::string fmtstr;
fmt::format(fmtstr, "format {} {} {}", 2, 2.9, "string");
std::cout << fmtstr;
// 打印二进制数据方法
unsigned char buffer[1024];
sublog->info("binary: {}", spdlog::to_hex(std::begin(buffer), std::begin(buffer)+1024));
//反初始化
SLog::cleanup();
//创建多个线程同时写日志,SLP_MS改小增加日志量,测试性能
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日志打印到文件中,输入debug、info、error动态更改日志等级");
while (1) {
std::string input;
std::cin >> input;
if (input == "dump") {
SLog::dump_trace();
} else if(input == "info") {
//动态变更日志等级至info
SLog::change_level(spdlog::level::info);
SLog::global()->critical("input:info");
} else if(input == "debug") {
//动态变更日志等级至debug
SLog::change_level(spdlog::level::debug);
SLog::global()->critical("input:debug");
} else if(input == "error") {
//动态变更日志等级至error
SLog::change_level(spdlog::level::err);
SLog::global()->critical("input:error");
}
}
}