-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.cpp
More file actions
119 lines (101 loc) · 3.35 KB
/
Copy pathdemo.cpp
File metadata and controls
119 lines (101 loc) · 3.35 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
#include <cmath>
#include <map>
#include <thread>
#include <vector>
#include "ysl.hpp"
#include "stl_emitter.hpp"
int main(int /*argc*/, char* argv[])
{
// setup glog
FLAGS_logtostderr = true;
FLAGS_colorlogtostderr = true;
FLAGS_minloglevel = 0;
FLAGS_v = 2;
// FLAGS_log_prefix = false;
google::InitGoogleLogging(argv[0]);
// setup YSL for current thread: use 4 sapces as indent
YSL::StreamLogger::set_thread_format(YSL::LoggerFormat::Indent, 4);
// plain glog LOG
LOG(INFO) << "This is a plain glog record";
// simple key-value scope
// YSL(INFO) << key << value;
{
YSL_FSCOPE(INFO, "A Simple YSL Frame");
YSL(INFO) << "name" // key
<< "Mark McGwire"; // value
YSL(INFO) << "hr" << 65;
YSL(INFO) << "avg" << 0.278f;
}
// stl and flow emission, explicit end-of-document
{
// manually start a frame
YSL(INFO) << YSL::ThreadFrame("Some STL Containers");
// setup local precision
YSL(INFO) << YSL::FloatPrecision(3);
// start a mapping
YSL(INFO) << YSL::BeginMap;
YSL(INFO) << "hello" << std::map<int, float>{{1, 3.4f}, {2, 6.78f}, {3, 9.0f}};
YSL(INFO) << "PI";
YSL(INFO) << YSL::Flow << std::vector<int>{3, 1, 4, 1, 5, 9, 2, 6};
YSL(INFO) << "empty tuple" << std::tuple<>();
// end the mapping and the frame(explicitly)
YSL(INFO) << YSL::EndMap << YSL::EndDoc;
}
// comment, literal and implicit/explicit new line
{
YSL_FSCOPE(INFO, "About Comment, Literal And Newline");
YSL(INFO) << YSL::Comment("This is a comment");
LOGC(INFO) << "This is a comment by LOGC";
YSL(INFO) << "glog newline" << nullptr;
LOG(INFO) << std::endl;
YSL(INFO) << "ysl newline" << true;
YSL(INFO) << YSL::Newline;
YSL(INFO) << "newline and literal" << YSL::Literal << "multi\nline\nliteral";
}
// LOG_AT_LEVEL like
YSL_AT_LEVEL(2) << YSL::ThreadFrame("At level 2") << YSL::EndDoc;
// logging level and LOG_IF like
for (int loop = 0; loop < 10; ++loop)
{
YSL_IF(ERROR, loop & 1) << YSL::ThreadFrame("Loop " + std::to_string(loop));
YSL_IF(ERROR, loop & 1) << YSL::BeginSeq;
YSL_IF(WARNING, loop & 1) << YSL::Flow << std::vector<int>(loop, loop);
YSL_IF(ERROR, loop & 1) << YSL::EndSeq;
VYSL_IF(1, loop & 3) << std::vector<int>(loop, loop);
}
// threaded logging
const int n = 4;
const int m = 1000;
const auto worker = [](int idx) {
// use quoted string style for better performance
YSL::StreamLogger::set_thread_format(YSL::DoubleQuoted);
YSL::StreamLogger::set_thread_format(YSL::LoggerFormat::FloatPrecision, 3);
// interval logging
for (int loop = 0; loop < m; ++loop)
{
YSL(INFO) << YSL::ThreadFrame("Thread " + std::to_string(idx)) << YSL::Flow
<< YSL::BeginMap;
auto phase = static_cast<float>(idx) * .1f + static_cast<float>(loop) * .2f;
YSL(INFO) << "cos" << std::cos(phase) << "sin" << std::sin(phase);
YSL(INFO) << "log" << std::log(phase) << "exp" << std::exp(phase);
YSL(INFO) << YSL::EndMap;
// C++14
// using namespace std::chrono_literals;
// std::this_thread::sleep_for(10ms);
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
YSL(INFO) << YSL::EndDoc; // explicit
};
std::vector<std::thread> threads;
threads.reserve(n);
for (int idx = 0; idx < n; ++idx)
{
threads.emplace_back(std::thread{worker, idx});
}
for (auto& thread : threads)
{
thread.join();
}
LOG(INFO); // HINT: extra flush
return 0;
}