DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
logbuf.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file logbuf.h
6 * @brief Fixed-RAM rotating log buffer with severity traps (DETWS_ENABLE_LOGBUF).
7 *
8 * Keeps the last DETWS_LOG_LINES log lines in a fixed ring (the oldest is pruned
9 * on overflow - no heap, bounded latency), each line stored as `<L> message`
10 * where L is the severity letter. Dump the ring oldest-first for a `/logs`
11 * endpoint, and register a trap callback that fires when a line is logged at or
12 * above a severity threshold (forward criticals as an SNMP trap / webhook). Pure
13 * and fully host-tested - no ESP32 dependency.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef DETERMINISTICESPASYNCWEBSERVER_LOGBUF_H
20#define DETERMINISTICESPASYNCWEBSERVER_LOGBUF_H
21
22#include "ServerConfig.h"
23#include <stddef.h>
24#include <stdint.h>
25
26#if DETWS_ENABLE_LOGBUF
27
28/** @brief Severity levels (ordered low -> high). Compared (level >= threshold) and passed through the
29 * uint8_t trap-callback ABI, so integer constants in a namespacing struct - cast-free. */
30struct DetwsLogLevel
31{
32 static constexpr uint8_t DETWS_LOG_DEBUG = 0;
33 static constexpr uint8_t DETWS_LOG_INFO = 1;
34 static constexpr uint8_t DETWS_LOG_WARN = 2;
35 static constexpr uint8_t DETWS_LOG_ERROR = 3;
36};
37
38/** @brief Trap callback: fired for a line logged at level >= the threshold. */
39typedef void (*detws_log_trap_fn)(uint8_t level, const char *line);
40
41/** @brief Empty the ring (and clear the line count). */
42void detws_logbuf_reset(void);
43
44/** @brief Append @p msg at @p level (stored as `<L> msg`, truncated to fit). */
45void detws_log(uint8_t level, const char *msg);
46
47/** @brief Number of lines currently held (0 .. DETWS_LOG_LINES). */
48uint16_t detws_log_count(void);
49
50/** @brief Line @p i (0 = oldest .. count-1 = newest), or nullptr if out of range. */
51const char *detws_log_at(uint16_t i);
52
53/**
54 * @brief Dump all held lines, oldest-first, newline-separated, into @p out.
55 * @return characters written, or 0 if @p cap is too small (fail-closed).
56 */
57int detws_log_dump(char *out, size_t cap);
58
59/** @brief Install a trap callback that fires when a line is logged at level >= @p threshold. */
60void detws_log_set_trap(uint8_t threshold, detws_log_trap_fn cb);
61
62#endif // DETWS_ENABLE_LOGBUF
63#endif // DETERMINISTICESPASYNCWEBSERVER_LOGBUF_H
User-facing configuration for DeterministicESPAsyncWebServer.