|
SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
|
Captures ESP_LOGx output into a bounded RAM buffer for the web UI. More...
#include <sensesp/system/log_buffer.h>
Public Member Functions | |
| LogBuffer (size_t max_lines=100, uint32_t max_age_ms=2000, size_t max_line_length=kLogCaptureLineMax, size_t min_headroom_bytes=kLogCaptureMinHeadroomBytes) | |
| void | install () |
| Install the chained vprintf hook. Call once, early in startup. | |
| void | push_line (const char *text, size_t length, uint32_t now_ms) |
| Append a pre-formatted log line to the buffer. | |
| LogSnapshot | snapshot_since (uint32_t since, bool has_since, uint32_t now_ms) |
Return retained lines newer than since. | |
| LogSnapshot | snapshot_since (uint32_t since, bool has_since) |
| uint32_t | session_id () const |
| size_t | size () |
| Test/inspection helper: current number of retained records. | |
Static Public Member Functions | |
| static LogBuffer * | instance () |
| Accessor used by the static vprintf trampoline. | |
Captures ESP_LOGx output into a bounded RAM buffer for the web UI.
install() chains an esp_log_set_vprintf() hook: every formatted log line is forwarded to the previously installed handler (so UART output is unchanged) and also appended to a bounded in-memory buffer. The buffer is a live tail, not a persistent log: it is bounded by age (~2 s) and a hard line count, and is never written to flash.
Capture only works when the firmware is built with -D USE_ESP_IDF_LOG; otherwise Arduino-ESP32 reroutes ESP_LOGx to log_printf, bypassing the vprintf hook (a compile-time #warning is emitted in that case).
Thread-safety: the vprintf hook runs in the context of whichever task logged, so it must stay cheap on stack. On ESP-IDF log V1 it formats the line once into a small stack buffer, emits it to the console itself (fwrite to stdout), and hands the same buffer to a fixed-size queue; a dedicated capture task drains the queue and does the heap-allocating buffer work (ANSI stripping, std::string, deque) on its own adequately sized stack, keeping the hook from overflowing small-stack caller tasks. Over-length lines, ISR / pre-install context, and log V2 fall back to the chained previous handler for console output. A FreeRTOS mutex guards the records against the HTTP reader, which copies lines out while holding it and must not log while holding it.
Definition at line 97 of file log_buffer.h.
|
explicit |
Definition at line 22 of file log_buffer.cpp.
| void sensesp::LogBuffer::install | ( | ) |
Install the chained vprintf hook. Call once, early in startup.
Definition at line 32 of file log_buffer.cpp.
|
inlinestatic |
Accessor used by the static vprintf trampoline.
Definition at line 137 of file log_buffer.h.
| void sensesp::LogBuffer::push_line | ( | const char * | text, |
| size_t | length, | ||
| uint32_t | now_ms | ||
| ) |
Append a pre-formatted log line to the buffer.
Trailing CR/LF is stripped and the text is truncated to max_line_length. now_ms is the capture time (esp_log_timestamp() in production); tests pass an explicit value to exercise age-based eviction.
Definition at line 203 of file log_buffer.cpp.
|
inline |
Definition at line 131 of file log_buffer.h.
| size_t sensesp::LogBuffer::size | ( | ) |
Test/inspection helper: current number of retained records.
Definition at line 294 of file log_buffer.cpp.
| LogSnapshot sensesp::LogBuffer::snapshot_since | ( | uint32_t | since, |
| bool | has_since | ||
| ) |
| LogSnapshot sensesp::LogBuffer::snapshot_since | ( | uint32_t | since, |
| bool | has_since, | ||
| uint32_t | now_ms | ||
| ) |
Return retained lines newer than since.
| since | Cursor from the client's previous poll (the prior next). |
| has_since | False on the initial load (no cursor) — returns the whole retained buffer and never reports a gap. |
| now_ms | Reference time for age eviction (testing override). |
Definition at line 255 of file log_buffer.cpp.