-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlogger.hpp
243 lines (221 loc) · 8.18 KB
/
logger.hpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once
#include <atomic>
#include <chrono>
#include <string>
#include <string_view>
#include "format.hpp"
namespace espp {
/**
* @brief Logger provides a wrapper around nicer / more robust formatting than
* standard ESP_LOG* macros with the ability to change the log level at
* run-time. Logger currently is a light wrapper around libfmt (future
* std::format).
*
* \section logger_ex1 Basic Example
* \snippet logger_example.cpp Logger example
* \section logger_ex2 Threaded Logging and Verbosity Example
* \snippet logger_example.cpp MultiLogger example
*/
class Logger {
public:
/**
* Verbosity levels for the logger, in order of increasing priority.
*/
enum class Verbosity {
DEBUG, /**< Debug level verbosity. */
INFO, /**< Info level verbosity. */
WARN, /**< Warn level verbosity. */
ERROR, /**< Error level verbosity. */
NONE, /**< No verbosity - logger will not print anything. */
};
/**
* @brief Configuration struct for the logger.
*/
struct Config {
std::string_view tag; /**< The TAG that will be prepended to all logs. */
std::chrono::duration<float> rate_limit{
0}; /**< The rate limit for the logger. Optional, if <= 0 no rate limit. @note Only calls
that have _rate_limited suffixed will be rate limited. */
Verbosity level = Verbosity::WARN; /**< The verbosity level for the logger. */
};
/**
* @brief Construct a new Logger object
*
* @param config configuration for the logger.
*/
Logger(const Config &config)
: tag_(config.tag), rate_limit_(config.rate_limit), level_(config.level) {}
/**
* @brief Change the verbosity for the logger. \sa Logger::Verbosity
* @param level new verbosity level
*/
void set_verbosity(const Verbosity level) { level_ = level; }
/**
* @brief Change the tag for the logger.
* @param tag The new tag.
*/
void set_tag(const std::string_view tag) {
std::lock_guard<std::mutex> lock(tag_mutex_);
tag_ = tag;
}
/**
* @brief Format args into string according to format string. From:
* https://en.cppreference.com/w/cpp/utility/format/format
*
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
* @return formatted std::string
*/
template <typename... Args> std::string format(std::string_view rt_fmt_str, Args &&...args) {
return fmt::vformat(rt_fmt_str, fmt::make_format_args(args...));
}
/**
* @brief Print log in GRAY if level is Verbosity::DEBUG or greater.
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void debug(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::DEBUG)
return;
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::color::gray), "[{}/D]:{}\n", tag_, msg);
}
/**
* @brief Print log in GREEN if level is Verbosity::INFO or greater
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void info(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::INFO)
return;
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::terminal_color::green), "[{}/I]:{}\n", tag_, msg);
}
/**
* @brief Print log in YELLOW if level is Verbosity::WARN or greater
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void warn(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::WARN)
return;
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::terminal_color::yellow), "[{}/W]:{}\n", tag_, msg);
}
/**
* @brief Print log in RED if level is Verbosity::ERROR or greater
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void error(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::ERROR)
return;
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::terminal_color::red), "[{}/E]:{}\n", tag_, msg);
}
/**
* @brief Print log in GRAY if level is Verbosity::DEBUG or greater.
* This function is rate limited by the rate specified in the
* constructor.
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void debug_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::DEBUG)
return;
if (rate_limit_ > std::chrono::duration<float>::zero()) {
auto now = std::chrono::high_resolution_clock::now();
if (now - last_print_ < rate_limit_)
return;
last_print_ = now;
}
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::color::gray), "[{}/D]:{}\n", tag_, msg);
}
/**
* @brief Print log in GREEN if level is Verbosity::INFO or greater
* This function is rate limited by the rate specified in the
* constructor.
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void info_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::INFO)
return;
if (rate_limit_ > std::chrono::duration<float>::zero()) {
auto now = std::chrono::high_resolution_clock::now();
if (now - last_print_ < rate_limit_)
return;
last_print_ = now;
}
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::terminal_color::green), "[{}/I]:{}\n", tag_, msg);
}
/**
* @brief Print log in YELLOW if level is Verbosity::WARN or greater
* This function is rate limited by the rate specified in the
* constructor.
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void warn_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::WARN)
return;
if (rate_limit_ > std::chrono::duration<float>::zero()) {
auto now = std::chrono::high_resolution_clock::now();
if (now - last_print_ < rate_limit_)
return;
last_print_ = now;
}
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::terminal_color::yellow), "[{}/W]:{}\n", tag_, msg);
}
/**
* @brief Print log in RED if level is Verbosity::ERROR or greater
* This function is rate limited by the rate specified in the
* constructor.
* @param rt_fmt_str format string
* @param args optional arguments passed to be formatted.
*/
template <typename... Args> void error_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
if (level_ > Verbosity::ERROR)
return;
if (rate_limit_ > std::chrono::duration<float>::zero()) {
auto now = std::chrono::high_resolution_clock::now();
if (now - last_print_ < rate_limit_)
return;
last_print_ = now;
}
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
std::lock_guard<std::mutex> lock(tag_mutex_);
fmt::print(fg(fmt::terminal_color::red), "[{}/E]:{}\n", tag_, msg);
}
protected:
std::mutex tag_mutex_;
/**
* Name given to the logger to be prepended to all logs.
*/
std::string tag_;
/**
* Rate limit for the logger. If set to 0, no rate limiting will be
* performed.
*/
std::chrono::duration<float> rate_limit_{0.0f};
/**
* Last time a log was printed. Used for rate limiting.
*/
std::chrono::high_resolution_clock::time_point last_print_{};
/**
* Current verbosity of the logger. Determines what will be printed to
* console.
*/
std::atomic<Verbosity> level_;
};
} // namespace espp