SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp::LogBuffer Class Reference

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 LogBufferinstance ()
 Accessor used by the static vprintf trampoline.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ LogBuffer()

sensesp::LogBuffer::LogBuffer ( size_t  max_lines = 100,
uint32_t  max_age_ms = 2000,
size_t  max_line_length = kLogCaptureLineMax,
size_t  min_headroom_bytes = kLogCaptureMinHeadroomBytes 
)
explicit

Definition at line 22 of file log_buffer.cpp.

Member Function Documentation

◆ install()

void sensesp::LogBuffer::install ( )

Install the chained vprintf hook. Call once, early in startup.

Definition at line 32 of file log_buffer.cpp.

Here is the caller graph for this function:

◆ instance()

static LogBuffer * sensesp::LogBuffer::instance ( )
inlinestatic

Accessor used by the static vprintf trampoline.

Definition at line 137 of file log_buffer.h.

Here is the caller graph for this function:

◆ push_line()

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.

◆ session_id()

uint32_t sensesp::LogBuffer::session_id ( ) const
inline

Definition at line 131 of file log_buffer.h.

◆ size()

size_t sensesp::LogBuffer::size ( )

Test/inspection helper: current number of retained records.

Definition at line 294 of file log_buffer.cpp.

◆ snapshot_since() [1/2]

LogSnapshot sensesp::LogBuffer::snapshot_since ( uint32_t  since,
bool  has_since 
)

Definition at line 290 of file log_buffer.cpp.

Here is the call graph for this function:

◆ snapshot_since() [2/2]

LogSnapshot sensesp::LogBuffer::snapshot_since ( uint32_t  since,
bool  has_since,
uint32_t  now_ms 
)

Return retained lines newer than since.

Parameters
sinceCursor from the client's previous poll (the prior next).
has_sinceFalse on the initial load (no cursor) — returns the whole retained buffer and never reports a gap.
now_msReference time for age eviction (testing override).

Definition at line 255 of file log_buffer.cpp.

Here is the caller graph for this function:

The documentation for this class was generated from the following files: