SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
log_buffer.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_SYSTEM_LOG_BUFFER_H_
2#define SENSESP_SRC_SENSESP_SYSTEM_LOG_BUFFER_H_
3
4#include <freertos/FreeRTOS.h>
5#include <freertos/queue.h>
6#include <freertos/semphr.h>
7#include <freertos/task.h>
8
9#include <cstdarg>
10#include <cstdint>
11#include <deque>
12#include <string>
13#include <vector>
14
15namespace sensesp {
16
20struct LogRecord {
21 uint32_t seq;
22 uint32_t timestamp_ms;
23 std::string text;
24};
25
27inline constexpr size_t kLogCaptureLineMax = 256;
30inline constexpr size_t kLogCaptureQueueDepth = 8;
36inline constexpr uint32_t kLogCaptureTaskStackSize = 3072;
37inline constexpr UBaseType_t kLogCaptureTaskPriority = 1;
47inline constexpr size_t kLogCaptureMinHeadroomBytes = 8192;
48
52 uint32_t timestamp_ms;
53 uint16_t length;
55};
56
67 uint32_t session_id;
68 uint32_t next;
69 bool gap;
70 std::vector<std::string> lines;
71};
72
97class LogBuffer {
98 public:
99 // max_line_length defaults to kLogCaptureLineMax to match the capture buffer
100 // in vprintf_trampoline(); a larger value cannot recover more than that
101 // buffer already captured.
102 explicit LogBuffer(size_t max_lines = 100, uint32_t max_age_ms = 2000,
103 size_t max_line_length = kLogCaptureLineMax,
104 size_t min_headroom_bytes = kLogCaptureMinHeadroomBytes);
105
109 void install();
110
118 void push_line(const char* text, size_t length, uint32_t now_ms);
119
128 LogSnapshot snapshot_since(uint32_t since, bool has_since, uint32_t now_ms);
129 LogSnapshot snapshot_since(uint32_t since, bool has_since);
130
131 uint32_t session_id() const { return session_id_; }
132
134 size_t size();
135
137 static LogBuffer* instance() { return instance_; }
138
139 private:
140 static int vprintf_trampoline(const char* format, va_list args);
141
144 static void capture_task(void* arg);
145
149 void enqueue_capture(LogQueueItem& item, int n);
150
153 void prune_locked(uint32_t now_ms);
154
155 size_t max_lines_;
156 uint32_t max_age_ms_;
157 size_t max_line_length_;
158 size_t min_headroom_bytes_;
159
160 std::deque<LogRecord> records_;
161 uint32_t next_seq_ = 0;
162 uint32_t session_id_;
163
164 int (*previous_vprintf_)(const char*, va_list) = nullptr;
165
166 // The capture task is created once in install() and runs for the process
167 // lifetime (never torn down), so its handle is not retained.
168 QueueHandle_t capture_queue_ = nullptr;
169
170 SemaphoreHandle_t mutex_;
171 StaticSemaphore_t mutex_buffer_;
172
173 static LogBuffer* instance_;
174};
175
176} // namespace sensesp
177
178#endif // SENSESP_SRC_SENSESP_SYSTEM_LOG_BUFFER_H_
Captures ESP_LOGx output into a bounded RAM buffer for the web UI.
Definition log_buffer.h:97
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.
void install()
Install the chained vprintf hook. Call once, early in startup.
size_t size()
Test/inspection helper: current number of retained records.
uint32_t session_id() const
Definition log_buffer.h:131
static LogBuffer * instance()
Accessor used by the static vprintf trampoline.
Definition log_buffer.h:137
constexpr size_t kLogCaptureQueueDepth
Definition log_buffer.h:30
constexpr size_t kLogCaptureMinHeadroomBytes
Definition log_buffer.h:47
constexpr UBaseType_t kLogCaptureTaskPriority
Definition log_buffer.h:37
constexpr uint32_t kLogCaptureTaskStackSize
Definition log_buffer.h:36
constexpr size_t kLogCaptureLineMax
Maximum captured line length, and the size of each queue slot's text.
Definition log_buffer.h:27
char text[kLogCaptureLineMax]
Definition log_buffer.h:54
A single captured log line.
Definition log_buffer.h:20
std::string text
Definition log_buffer.h:23
uint32_t timestamp_ms
Definition log_buffer.h:22
Result of a snapshot_since() query, ready to serialize for the web UI.
Definition log_buffer.h:66
std::vector< std::string > lines
Definition log_buffer.h:70