Skip to content

Commit 0782961

Browse files
authored
feat(logger): Use esp_timer_get_time on esp platform (#193)
* Updated logger component to use the esp_timer component (if ESP_PLATFORM) and the `esp_timer_get_time` function for time since boot in microseconds, using the original std::chrono::steady_clock otherwise.
1 parent bd9a9fc commit 0782961

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

Diff for: components/logger/CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
if (ESP_PLATFORM)
2+
# set component requirements for ESP32
3+
set(COMPONENT_REQUIRES "format esp_timer")
4+
else()
5+
# set component requirements for generic
6+
set(COMPONENT_REQUIRES "format")
7+
endif()
8+
19
idf_component_register(
210
INCLUDE_DIRS "include"
311
SRC_DIRS "src"
4-
REQUIRES format)
12+
REQUIRES ${COMPONENT_REQUIRES})

Diff for: components/logger/include/logger.hpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <string>
66
#include <string_view>
77

8+
#if defined(ESP_PLATFORM)
9+
#include <esp_timer.h>
10+
#endif
11+
812
#include "format.hpp"
913

1014
namespace espp {
@@ -108,7 +112,7 @@ class Logger {
108112
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
109113
if (include_time_) {
110114
std::lock_guard<std::mutex> lock(tag_mutex_);
111-
fmt::print(fg(fmt::color::gray), "[{}/D][{:.3f}]: {}\n", tag_, get_time(), msg);
115+
fmt::print(fg(fmt::color::gray), "[{}/D][{}]: {}\n", tag_, get_time(), msg);
112116
} else {
113117
std::lock_guard<std::mutex> lock(tag_mutex_);
114118
fmt::print(fg(fmt::color::gray), "[{}/D]:{}\n", tag_, msg);
@@ -126,7 +130,7 @@ class Logger {
126130
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
127131
if (include_time_) {
128132
std::lock_guard<std::mutex> lock(tag_mutex_);
129-
fmt::print(fg(fmt::terminal_color::green), "[{}/I][{:.3f}]: {}\n", tag_, get_time(), msg);
133+
fmt::print(fg(fmt::terminal_color::green), "[{}/I][{}]: {}\n", tag_, get_time(), msg);
130134
} else {
131135
std::lock_guard<std::mutex> lock(tag_mutex_);
132136
fmt::print(fg(fmt::terminal_color::green), "[{}/I]:{}\n", tag_, msg);
@@ -144,7 +148,7 @@ class Logger {
144148
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
145149
if (include_time_) {
146150
std::lock_guard<std::mutex> lock(tag_mutex_);
147-
fmt::print(fg(fmt::terminal_color::yellow), "[{}/W][{:.3f}]: {}\n", tag_, get_time(), msg);
151+
fmt::print(fg(fmt::terminal_color::yellow), "[{}/W][{}]: {}\n", tag_, get_time(), msg);
148152
} else {
149153
std::lock_guard<std::mutex> lock(tag_mutex_);
150154
fmt::print(fg(fmt::terminal_color::yellow), "[{}/W]:{}\n", tag_, msg);
@@ -162,7 +166,7 @@ class Logger {
162166
auto msg = format(rt_fmt_str, std::forward<Args>(args)...);
163167
if (include_time_) {
164168
std::lock_guard<std::mutex> lock(tag_mutex_);
165-
fmt::print(fg(fmt::terminal_color::red), "[{}/E][{:.3f}]: {}\n", tag_, get_time(), msg);
169+
fmt::print(fg(fmt::terminal_color::red), "[{}/E][{}]: {}\n", tag_, get_time(), msg);
166170
} else {
167171
std::lock_guard<std::mutex> lock(tag_mutex_);
168172
fmt::print(fg(fmt::terminal_color::red), "[{}/E]:{}\n", tag_, msg);
@@ -260,10 +264,19 @@ class Logger {
260264
* @return time in seconds since the start of the logging system.
261265
*/
262266
static auto get_time() {
267+
#if defined(ESP_PLATFORM)
268+
// use esp_timer_get_time to get the time in microseconds
269+
uint64_t time = esp_timer_get_time();
270+
uint64_t seconds = time / 1e6f;
271+
uint64_t milliseconds = (time % 1000000) / 1e3f;
272+
return fmt::format("{}.{:03}", seconds, milliseconds);
273+
#else
263274
// get the elapsed time since the start of the logging system as floating
264275
// point seconds
265276
auto now = std::chrono::steady_clock::now();
266-
return std::chrono::duration<float>(now - start_time_).count();
277+
auto seconds = std::chrono::duration<float>(now - start_time_).count();
278+
return fmt::format("{:.3f}", seconds);
279+
#endif
267280
}
268281

269282
/**

0 commit comments

Comments
 (0)