DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
syslog.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 syslog.h
6 * @brief Zero-heap RFC 5424 syslog client over UDP.
7 *
8 * Ships device log lines to a remote syslog server as RFC 5424 UDP datagrams via
9 * the transport-layer UDP service (det_udp_sendto). Split, like the other
10 * network services, into a pure host-testable formatter and an ESP32-only send:
11 *
12 * - syslog_format() builds one RFC 5424 line into a caller buffer (no sockets,
13 * no heap) - unit-tested on the host (env:native_syslog).
14 * - syslog_log() formats into a static scratch buffer and sends it to the
15 * configured server (a no-op stub off-target).
16 *
17 * Emitted line: `<PRI>1 - HOSTNAME APP-NAME - - - MSG`, where PRI = facility*8 +
18 * severity. TIMESTAMP/PROCID/MSGID/STRUCTURED-DATA are the RFC 5424 NILVALUE
19 * ("-"); the server stamps its own receipt time. Strings are copied into fixed
20 * BSS buffers at init, so nothing must outlive the call.
21 */
22
23#ifndef DETERMINISTICESPASYNCWEBSERVER_SYSLOG_H
24#define DETERMINISTICESPASYNCWEBSERVER_SYSLOG_H
25
26#include "ServerConfig.h"
27#include <stddef.h>
28#include <stdint.h>
29
30#if DETWS_ENABLE_SYSLOG
31
32/** @brief RFC 5424 §6.2.1 severity levels (numerically lower = more severe). */
33enum class SyslogSeverity : uint8_t
34{
35 SYSLOG_EMERG = 0, ///< system is unusable
36 SYSLOG_ALERT = 1, ///< action must be taken immediately
37 SYSLOG_CRIT = 2, ///< critical conditions
38 SYSLOG_ERR = 3, ///< error conditions
39 SYSLOG_WARNING = 4, ///< warning conditions
40 SYSLOG_NOTICE = 5, ///< normal but significant
41 SYSLOG_INFO = 6, ///< informational
42 SYSLOG_DEBUG = 7, ///< debug-level messages
43};
44
45/** @brief Common RFC 5424 §6.2.1 facilities (the default is LOCAL0). */
46enum class SyslogFacility : uint8_t
47{
48 SYSLOG_FAC_USER = 1, ///< user-level messages
49 SYSLOG_FAC_DAEMON = 3, ///< system daemons
50 SYSLOG_FAC_LOCAL0 = 16, ///< local use 0 (default)
51 SYSLOG_FAC_LOCAL1 = 17,
52 SYSLOG_FAC_LOCAL7 = 23,
53};
54
55/**
56 * @brief Configure the syslog client (call after WiFi is up).
57 *
58 * @param server_ip dotted-quad IPv4 of the syslog server (e.g. "192.168.1.10").
59 * @param port server UDP port (514 is the IANA syslog port).
60 * @param hostname this device's HOSTNAME field (copied; pass nullptr/"" for "-").
61 * @param appname APP-NAME field (copied; pass nullptr/"" for "-").
62 * @param facility syslog facility (default LOCAL0).
63 */
64void syslog_init(const char *server_ip, uint16_t port, const char *hostname, const char *appname,
65 SyslogFacility facility = SyslogFacility::SYSLOG_FAC_LOCAL0);
66
67/**
68 * @brief Format one RFC 5424 line into @p out (host-testable; no sockets/heap).
69 *
70 * @return number of bytes written (excl. NUL), or 0 if it would not fit @p cap.
71 */
72size_t syslog_format(char *out, size_t cap, SyslogFacility facility, SyslogSeverity severity, const char *hostname,
73 const char *appname, const char *msg);
74
75/**
76 * @brief Format @p msg at @p severity and send it to the configured server.
77 *
78 * @return true if the datagram was queued; false if not yet configured, the line
79 * overflowed DETWS_SYSLOG_MSG_MAX, or the send failed (host build).
80 */
81bool syslog_log(SyslogSeverity severity, const char *msg);
82
83#endif // DETWS_ENABLE_SYSLOG
84
85#endif // DETERMINISTICESPASYNCWEBSERVER_SYSLOG_H
User-facing configuration for DeterministicESPAsyncWebServer.