Layer: L7 Application ยท Build flags: PC_ENABLE_LOGBUF
What this example teaches
When there is no serial cable attached, you still want the last few log lines. This keeps the most recent PC_LOG_LINES lines in a fixed RAM ring (oldest pruned on overflow), serves them at GET /logs, and fires a trap callback on WARN+ lines - here it just prints, but a real app could forward an SNMP trap (SnmpTrap) or a webhook (Webhook).
Set a severity trap, then log by level:
pc_log_set_trap(PC_LOG_WARN, on_trap);
pc_log(PC_LOG_INFO, "boot complete");
The trap fires only for lines at or above the threshold, so criticals get pushed while routine INFO lines just accumulate in the ring:
static void on_trap(uint8_t level, const char *line) {
Serial.printf("[trap] %s\n", line);
}
Dump the ring on demand. pc_log_dump() writes the whole buffer into a caller-owned array sized PC_LOG_LINES * PC_LOG_LINE_LEN:
pc_log_dump(buf, sizeof(buf));
server.send(id, 200, "text/plain", buf);
#define PC_LOG_LINE_LEN
Maximum length of one stored log line (bytes, including null).
#define PC_LOG_LINES
Number of log lines retained in the ring.
The loop logs a heap/uptime line every five seconds, escalating to WARN (which trips the trap) when heap runs low.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_LOGBUF=1" \
--lib="." examples/L7-Application/LogBuffer/LogBuffer.ino
curl http://<ip>/logs # the last PC_LOG_LINES lines, oldest first
Annotated source
The complete sketch (LogBuffer.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_LOGBUF 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static void on_trap(uint8_t level, const char *line)
{
Serial.printf("[trap] %s\n", line);
(void)level;
}
void setup()
{
delay(250);
Serial.print("IP: ");
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
pc_log_set_trap(PC_LOG_WARN, on_trap);
pc_log(PC_LOG_INFO, "boot complete");
pc_log_dump(buf, sizeof(buf));
server.
send(
id, 200,
"text/plain", buf);
});
}
void loop()
{
static uint32_t last = 0;
if (millis() - last >= 5000)
{
last = millis();
char msg[64];
uint32_t heap = ESP.getFreeHeap();
snprintf(msg, sizeof(msg), "heap=%u uptime=%lus", (unsigned)heap, millis() / 1000);
pc_log(heap < 20000 ? PC_LOG_WARN : PC_LOG_INFO, msg);
}
}
Single-port HTTP server with deterministic, zero-allocation execution.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
Fixed-RAM rotating log buffer with severity traps (PC_ENABLE_LOGBUF).
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.