Skip to content

Commit 81bfda0

Browse files
authored
Merge pull request #62 from esp-cpp/feature/logger-rate-limit
feat(logger): allow rate limiting for logger
2 parents 5f0ef54 + 10fdbce commit 81bfda0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+901
-232
lines changed

components/logger/example/main/logger_example.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ extern "C" void app_main(void) {
3434
{
3535
//! [MultiLogger example]
3636
// create loggers
37-
auto logger1 = espp::Logger({.tag = "Thread 1", .level = espp::Logger::Verbosity::INFO});
38-
auto logger2 = espp::Logger({.tag = "Thread 2", .level = espp::Logger::Verbosity::DEBUG});
37+
auto logger1 = espp::Logger(
38+
{.tag = "Thread 1", .rate_limit = 500ms, .level = espp::Logger::Verbosity::INFO});
39+
auto logger2 = espp::Logger(
40+
{.tag = "Thread 2", .rate_limit = 1s, .level = espp::Logger::Verbosity::DEBUG});
3941
// lambda for logging to those two loggers from multiple threads
4042
auto logger_fn = [](espp::Logger *logger) {
4143
size_t loop_iteration{0};
@@ -45,6 +47,7 @@ extern "C" void app_main(void) {
4547
logger->info("some info: {}", loop_iteration);
4648
logger->warn("some warning: {}", loop_iteration);
4749
logger->error("some error: {}", loop_iteration);
50+
logger->info_rate_limited("some rate limited info: {}", loop_iteration);
4851
// update loop variables
4952
loop_iteration++;
5053
// sleep
@@ -59,7 +62,7 @@ extern "C" void app_main(void) {
5962
while (true) {
6063
// update the loggers' verbosity
6164
level++;
62-
if (level > static_cast<uint8_t>(espp::Logger::Verbosity::ERROR)) {
65+
if (level > static_cast<uint8_t>(espp::Logger::Verbosity::NONE)) {
6366
level = static_cast<uint8_t>(espp::Logger::Verbosity::DEBUG);
6467
}
6568
espp::Logger::Verbosity verbosity = static_cast<espp::Logger::Verbosity>(level);

components/logger/include/logger.hpp

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <atomic>
4+
#include <chrono>
45
#include <string>
56
#include <string_view>
67

@@ -25,17 +26,31 @@ class Logger {
2526
* Verbosity levels for the logger, in order of increasing priority.
2627
*/
2728
enum class Verbosity {
28-
DEBUG,
29-
INFO,
30-
WARN,
31-
ERROR,
29+
DEBUG, /**< Debug level verbosity. */
30+
INFO, /**< Info level verbosity. */
31+
WARN, /**< Warn level verbosity. */
32+
ERROR, /**< Error level verbosity. */
33+
NONE, /**< No verbosity - logger will not print anything. */
3234
};
3335

36+
/**
37+
* @brief Configuration struct for the logger.
38+
*/
3439
struct Config {
35-
std::string_view tag; /**< The TAG that will be prepended to all logs. */
40+
std::string_view tag; /**< The TAG that will be prepended to all logs. */
41+
std::chrono::duration<float> rate_limit{
42+
0}; /**< The rate limit for the logger. Optional, if <= 0 no rate limit. @note Only calls
43+
that have _rate_limited suffixed will be rate limited. */
3644
Verbosity level = Verbosity::WARN; /**< The verbosity level for the logger. */
3745
};
38-
Logger(const Config &config) : tag_(config.tag), level_(config.level) {}
46+
47+
/**
48+
* @brief Construct a new Logger object
49+
*
50+
* @param config configuration for the logger.
51+
*/
52+
Logger(const Config &config)
53+
: tag_(config.tag), rate_limit_(config.rate_limit), level_(config.level) {}
3954

4055
/**
4156
* @brief Change the verbosity for the logger. \sa Logger::Verbosity
@@ -103,12 +118,103 @@ class Logger {
103118
fmt::print(fg(fmt::terminal_color::red), "[{}/E]:{}\n", tag_, msg);
104119
}
105120

121+
/**
122+
* @brief Print log in GRAY if level is Verbosity::DEBUG or greater.
123+
* This function is rate limited by the rate specified in the
124+
* constructor.
125+
* @param rt_fmt_str format string
126+
* @param args optional arguments passed to be formatted.
127+
*/
128+
template <typename... Args> void debug_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
129+
if (level_ > Verbosity::DEBUG)
130+
return;
131+
if (rate_limit_ > std::chrono::duration<float>::zero()) {
132+
auto now = std::chrono::high_resolution_clock::now();
133+
if (now - last_print_ < rate_limit_)
134+
return;
135+
last_print_ = now;
136+
}
137+
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
138+
fmt::print(fg(fmt::color::gray), "[{}/D]:{}\n", tag_, msg);
139+
}
140+
141+
/**
142+
* @brief Print log in GREEN if level is Verbosity::INFO or greater
143+
* This function is rate limited by the rate specified in the
144+
* constructor.
145+
* @param rt_fmt_str format string
146+
* @param args optional arguments passed to be formatted.
147+
*/
148+
template <typename... Args> void info_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
149+
if (level_ > Verbosity::INFO)
150+
return;
151+
if (rate_limit_ > std::chrono::duration<float>::zero()) {
152+
auto now = std::chrono::high_resolution_clock::now();
153+
if (now - last_print_ < rate_limit_)
154+
return;
155+
last_print_ = now;
156+
}
157+
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
158+
fmt::print(fg(fmt::terminal_color::green), "[{}/I]:{}\n", tag_, msg);
159+
}
160+
161+
/**
162+
* @brief Print log in YELLOW if level is Verbosity::WARN or greater
163+
* This function is rate limited by the rate specified in the
164+
* constructor.
165+
* @param rt_fmt_str format string
166+
* @param args optional arguments passed to be formatted.
167+
*/
168+
template <typename... Args> void warn_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
169+
if (level_ > Verbosity::WARN)
170+
return;
171+
if (rate_limit_ > std::chrono::duration<float>::zero()) {
172+
auto now = std::chrono::high_resolution_clock::now();
173+
if (now - last_print_ < rate_limit_)
174+
return;
175+
last_print_ = now;
176+
}
177+
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
178+
fmt::print(fg(fmt::terminal_color::yellow), "[{}/W]:{}\n", tag_, msg);
179+
}
180+
181+
/**
182+
* @brief Print log in RED if level is Verbosity::ERROR or greater
183+
* This function is rate limited by the rate specified in the
184+
* constructor.
185+
* @param rt_fmt_str format string
186+
* @param args optional arguments passed to be formatted.
187+
*/
188+
template <typename... Args> void error_rate_limited(std::string_view rt_fmt_str, Args &&...args) {
189+
if (level_ > Verbosity::ERROR)
190+
return;
191+
if (rate_limit_ > std::chrono::duration<float>::zero()) {
192+
auto now = std::chrono::high_resolution_clock::now();
193+
if (now - last_print_ < rate_limit_)
194+
return;
195+
last_print_ = now;
196+
}
197+
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
198+
fmt::print(fg(fmt::terminal_color::red), "[{}/E]:{}\n", tag_, msg);
199+
}
200+
106201
protected:
107202
/**
108203
* Name given to the logger to be prepended to all logs.
109204
*/
110205
std::string tag_;
111206

207+
/**
208+
* Rate limit for the logger. If set to 0, no rate limiting will be
209+
* performed.
210+
*/
211+
std::chrono::duration<float> rate_limit_{0.0f};
212+
213+
/**
214+
* Last time a log was printed. Used for rate limiting.
215+
*/
216+
std::chrono::high_resolution_clock::time_point last_print_{};
217+
112218
/**
113219
* Current verbosity of the logger. Determines what will be printed to
114220
* console.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
BLDC Haptics
2+
************
3+
4+
The `BldcHaptics` class is a high-level interface for controlling a BLDC motor
5+
with a haptic feedback loop. It is designed to be used to provide haptic
6+
feedback as part of a rotary input device (the BLDC motor). The component
7+
provides a `DetentConfig` interface for configuring the input / haptic feedback
8+
profile for the motor dynamically with configuration of:
9+
10+
- Range of motion (min/max position + width of each position)
11+
- Width of each position in the range (which will be used to calculate the
12+
actual range of motion based on the number of positions)
13+
- Strength of the haptic feedback at each position (detent)
14+
- Strength of the haptic feedback at the edges of the range of motion
15+
- Specific positions to provide haptic feedback at (detents)
16+
- Snap point (percentage of position width which will trigger a snap to the
17+
nearest position)
18+
19+
20+
The component also provides a `HapticConfig` interface for configuring the
21+
haptic feedback loop with configuration of:
22+
23+
- Strength of the haptic feedback
24+
- Frequency of the haptic feedback [currently not implemented]
25+
- Duration of the haptic feedback [currently not implemented]
26+
27+
.. ---------------------------- API Reference ----------------------------------
28+
29+
API Reference
30+
-------------
31+
32+
.. include-build-file:: inc/detent_config.inc
33+
.. include-build-file:: inc/haptic_config.inc
34+
.. include-build-file:: inc/bldc_haptics.inc

docs/adc/adc_types.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
<li><a href="index.html">ADC APIs</a> &raquo;</li>
136136
<li>ADC Types</li>
137137
<li class="wy-breadcrumbs-aside">
138-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
138+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
139139
</li>
140140
</ul>
141141
<hr/>
@@ -152,7 +152,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
152152
<section id="header-file">
153153
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
154154
<ul class="simple">
155-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/6590917/components/adc/include/adc_types.hpp">components/adc/include/adc_types.hpp</a></p></li>
155+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/dde3d42/components/adc/include/adc_types.hpp">components/adc/include/adc_types.hpp</a></p></li>
156156
</ul>
157157
</section>
158158
</section>

docs/adc/ads1x15.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<li><a href="index.html">ADC APIs</a> &raquo;</li>
137137
<li>ADS1x15 I2C ADC</li>
138138
<li class="wy-breadcrumbs-aside">
139-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
139+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
140140
</li>
141141
</ul>
142142
<hr/>
@@ -153,7 +153,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
153153
<section id="header-file">
154154
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
155155
<ul class="simple">
156-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
156+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
157157
</ul>
158158
</section>
159159
<section id="classes">

docs/adc/continuous_adc.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<li><a href="index.html">ADC APIs</a> &raquo;</li>
137137
<li>Continuous ADC</li>
138138
<li class="wy-breadcrumbs-aside">
139-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
139+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
140140
</li>
141141
</ul>
142142
<hr/>
@@ -158,7 +158,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
158158
<section id="header-file">
159159
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
160160
<ul class="simple">
161-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
161+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
162162
</ul>
163163
</section>
164164
<section id="classes">

docs/adc/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
129129
<li>ADC APIs</li>
130130
<li class="wy-breadcrumbs-aside">
131-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
131+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
132132
</li>
133133
</ul>
134134
<hr/>

docs/adc/oneshot_adc.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<li><a href="index.html">ADC APIs</a> &raquo;</li>
137137
<li>Oneshot ADC</li>
138138
<li class="wy-breadcrumbs-aside">
139-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
139+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
140140
</li>
141141
</ul>
142142
<hr/>
@@ -157,7 +157,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
157157
<section id="header-file">
158158
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
159159
<ul class="simple">
160-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
160+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
161161
</ul>
162162
</section>
163163
<section id="classes">

docs/bldc/bldc_driver.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
135135
<li>BLDC Driver</li>
136136
<li class="wy-breadcrumbs-aside">
137-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
137+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
138138
</li>
139139
</ul>
140140
<hr/>
@@ -151,7 +151,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
151151
<section id="header-file">
152152
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
153153
<ul class="simple">
154-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
154+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
155155
</ul>
156156
</section>
157157
<section id="classes">

docs/bldc/bldc_motor.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
135135
<li>BLDC Motor</li>
136136
<li class="wy-breadcrumbs-aside">
137-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
137+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
138138
</li>
139139
</ul>
140140
<hr/>
@@ -165,7 +165,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
165165
<section id="header-file">
166166
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
167167
<ul class="simple">
168-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
168+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
169169
</ul>
170170
</section>
171171
<section id="classes">

docs/bldc/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
127127
<li>BLDC APIs</li>
128128
<li class="wy-breadcrumbs-aside">
129-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
129+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
130130
</li>
131131
</ul>
132132
<hr/>

docs/cli.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
133133
<li>Command Line Interface (CLI) APIs</li>
134134
<li class="wy-breadcrumbs-aside">
135-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/cli.rst" class="fa fa-github"> Edit on GitHub</a>
135+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/cli.rst" class="fa fa-github"> Edit on GitHub</a>
136136
</li>
137137
</ul>
138138
<hr/>
@@ -175,7 +175,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
175175
<section id="header-file">
176176
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
177177
<ul class="simple">
178-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/cli/include/cli.hpp">components/cli/include/cli.hpp</a></p></li>
178+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/cli/include/cli.hpp">components/cli/include/cli.hpp</a></p></li>
179179
</ul>
180180
</section>
181181
<section id="macros">
@@ -324,7 +324,7 @@ <h4>Oneshot CLI Example<a class="headerlink" href="#classespp_1_1_cli_1cli_ex1"
324324
<section id="id1">
325325
<h3>Header File<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
326326
<ul class="simple">
327-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/cli/include/line_input.hpp">components/cli/include/line_input.hpp</a></p></li>
327+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/cli/include/line_input.hpp">components/cli/include/line_input.hpp</a></p></li>
328328
</ul>
329329
</section>
330330
<section id="id2">

docs/color.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
130130
<li>Color APIs</li>
131131
<li class="wy-breadcrumbs-aside">
132-
<a href="https://github.com/esp-cpp/espp/blob/13e6595/docs/en/color.rst" class="fa fa-github"> Edit on GitHub</a>
132+
<a href="https://github.com/esp-cpp/espp/blob/5f0ef54/docs/en/color.rst" class="fa fa-github"> Edit on GitHub</a>
133133
</li>
134134
</ul>
135135
<hr/>
@@ -152,7 +152,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
152152
<section id="header-file">
153153
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
154154
<ul class="simple">
155-
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/13e6595/components/color/include/color.hpp">components/color/include/color.hpp</a></p></li>
155+
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/5f0ef54/components/color/include/color.hpp">components/color/include/color.hpp</a></p></li>
156156
</ul>
157157
</section>
158158
<section id="classes">

0 commit comments

Comments
 (0)