ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
LogBuffer - a fixed-RAM rotating log buffer with severity traps

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); // trap on WARN and ERROR
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); // forward criticals here (SNMP trap / webhook)
}

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:

// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
#define PC_ENABLE_LOGBUF 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
// Fired for every line at or above the trap threshold.
static void on_trap(uint8_t level, const char *line)
{
Serial.printf("[trap] %s\n", line); // forward criticals here (SNMP trap / webhook)
(void)level;
}
void setup()
{
Serial.begin(115200);
init_wifi_physical(SSID, PASSWORD);
while (!wifi_ready())
delay(250);
Serial.print("IP: ");
uint32_t ip = pc_net_egress_ip(); // library egress IP (network byte order), no Arduino WiFi
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); // trap on WARN and ERROR
pc_log(PC_LOG_INFO, "boot complete");
server.on("/logs", HttpMethod::HTTP_GET, [](uint8_t id, HttpReq *) {
pc_log_dump(buf, sizeof(buf));
server.send(id, 200, "text/plain", buf);
});
server.begin(80);
}
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); // WARN trips the trap
}
server.handle();
}
Single-port HTTP server with deterministic, zero-allocation execution.
Definition protocore.h:348
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.
Definition physical.cpp:41
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
Definition physical.cpp:77
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Definition physical.cpp:45
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.