ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file log.h
6 * @brief Abstract logging whose disabled levels cost nothing at all (PROTOCORE_LOG_LEVEL).
7 *
8 * Instrumentation is only worth leaving in the source permanently if a build that does not want it
9 * pays nothing for it - not a branch, not a call, and not a format string sitting in flash. A
10 * runtime `if (level >= threshold)` fails that last part: every message is still linked in, and on
11 * a device flash is the scarce resource.
12 *
13 * So the filter is the preprocessor. A call below PROTOCORE_LOG_LEVEL expands to a form that names its
14 * arguments only inside `sizeof(...)` - an unevaluated context - which emits no code and no string
15 * literal, yet still runs the compiler's printf format checking over them and marks the arguments
16 * used (so a variable read only by a log does not warn). Enable the level and the same line starts
17 * logging, with no source change.
18 *
19 * Where an emitted line goes is the caller's choice: it is handed to server/logbuf's ring when
20 * PROTOCORE_ENABLE_LOGBUF is on, and to a sink callback registered with ::LogNs::set_sink (a serial port,
21 * syslog, a websocket console) if there is one.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef PROTOCORE_LOG_H
28#define PROTOCORE_LOG_H
29
30#include "numeros_scribo/numeros_scribo.h"
31#include "protocore_config.h"
32
33EMBED_BEGIN_DECLS
34
35/** @brief Receives an emitted line, already formatted. @p level is a PROTOCORE_LOG_LEVEL_* value. */
36typedef void (*protocore_log_sink_fn)(uint8_t level, const char *line);
37
38/**
39 * @brief Declared, never defined: only ever named inside `sizeof`, so no call is ever generated.
40 *
41 * It exists to mark a discarded statement's arguments as used, so a variable read only by a log
42 * does not warn its way into being deleted.
43 */
44int protocore_log_discard_args(const mmgr_field *spec, const mmgr_fval *v, size_t nv);
45
46/** @brief The discarded form: marks arguments used, emits nothing. */
47#define PROTOCORE_LOG_DISCARD(spec, v, nv) \
48 do \
49 { \
50 (void)sizeof(protocore_log_discard_args((spec), (v), (nv))); \
51 } while (0)
52
53/** @brief One line to emit: its level, and the spec that shapes it. */
54typedef struct
55{
56 uint8_t level; ///< the severity the line carries
57 const mmgr_field *spec; ///< the shape of the message
58 const mmgr_fval *v; ///< the values that fill it
59 size_t nv; ///< how many
61
62/**
63 * @brief The frame logger.
64 *
65 * A caller sets the members a call takes and invokes it through ::Log. The installed sink is behind
66 * @ref internal; the level macros below do the member-set and the call in one statement.
67 *
68 * @var LogNs::frame one line to emit: its level, and the spec that shapes it
69 * @var LogNs::sink the sink an install registers; NULL clears it
70 * @var LogNs::emit build the line and route it to the ring and/or the sink
71 * @var LogNs::set_sink install (or clear) the sink emitted lines are handed to
72 *
73 * A spec, not a format string: the shape of the message is decided when the code is written, so
74 * nothing here parses anything at runtime, and a build whose logs declare no float field links no
75 * float formatter. A build with every level compiled out still carries the handle, so a caller
76 * compiles either way and emit is the no-op.
77 */
83
84/** @brief The operands and the outcome. */
85extern LogVars LogV;
86
87/** @brief The entries. */
88typedef struct
89{
90 void (*const emit)(const uint8_t *work);
91 void (*const set_sink)(uint8_t *work);
92} LogNs;
93
94// What the table binds, defined once in the .c and taking one parameter each: everything
95// else an entry needs is an operand in LogV or a region of the borrow at a fixed offset.
96void protocore_log_emit(const uint8_t *work);
97void protocore_log_set_sink(uint8_t *work);
98
99// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
100// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
101// `Log.emit(work)` resolves to a named function and becomes a DIRECT call. An extern table
102// leaves the call indirect and the symbol live at every level, -O2 -flto included.
103static const LogNs Log __attribute__((unused)) = {
104 .emit = protocore_log_emit,
105 .set_sink = protocore_log_set_sink,
106};
107
108/**
109 * @brief The PROTOCORE_LOG_BORROW bytes this module's state lives in.
110 *
111 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
112 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
113 * walks, so the state lasts the life of the program.
114 *
115 * @return the span.
116 */
117uint8_t *protocore_log_span(void);
118
119/** @brief Set the frame members and emit, as one statement. */
120#define PROTOCORE_LOG_EMIT(lvl, spec_, v_, nv_) \
121 do \
122 { \
123 LogV.frame.level = (lvl); \
124 LogV.frame.spec = (spec_); \
125 LogV.frame.v = (v_); \
126 LogV.frame.nv = (nv_); \
127 Log.emit(protocore_log_span()); \
128 } while (0)
129
130#if PROTOCORE_LOG_LEVEL <= PROTOCORE_LOG_LEVEL_DEBUG
131#define PROTOCORE_LOGD(spec, v, nv) PROTOCORE_LOG_EMIT(PROTOCORE_LOG_LEVEL_DEBUG, (spec), (v), (nv))
132#else
133#define PROTOCORE_LOGD(spec, v, nv) PROTOCORE_LOG_DISCARD((spec), (v), (nv))
134#endif
135
136#if PROTOCORE_LOG_LEVEL <= PROTOCORE_LOG_LEVEL_INFO
137#define PROTOCORE_LOGI(spec, v, nv) PROTOCORE_LOG_EMIT(PROTOCORE_LOG_LEVEL_INFO, (spec), (v), (nv))
138#else
139#define PROTOCORE_LOGI(spec, v, nv) PROTOCORE_LOG_DISCARD((spec), (v), (nv))
140#endif
141
142#if PROTOCORE_LOG_LEVEL <= PROTOCORE_LOG_LEVEL_WARN
143#define PROTOCORE_LOGW(spec, v, nv) PROTOCORE_LOG_EMIT(PROTOCORE_LOG_LEVEL_WARN, (spec), (v), (nv))
144#else
145#define PROTOCORE_LOGW(spec, v, nv) PROTOCORE_LOG_DISCARD((spec), (v), (nv))
146#endif
147
148#if PROTOCORE_LOG_LEVEL <= PROTOCORE_LOG_LEVEL_ERROR
149#define PROTOCORE_LOGE(spec, v, nv) PROTOCORE_LOG_EMIT(PROTOCORE_LOG_LEVEL_ERROR, (spec), (v), (nv))
150#else
151#define PROTOCORE_LOGE(spec, v, nv) PROTOCORE_LOG_DISCARD((spec), (v), (nv))
152#endif
153
154EMBED_END_DECLS
155
156#endif // PROTOCORE_LOG_H
int protocore_log_discard_args(const mmgr_field *spec, const mmgr_fval *v, size_t nv)
Declared, never defined: only ever named inside sizeof, so no call is ever generated.
EMBED_BEGIN_DECLS typedef void(* protocore_log_sink_fn)(uint8_t level, const char *line)
Receives an emitted line, already formatted. level is a PROTOCORE_LOG_LEVEL_* value.
Definition log.h:36
void protocore_log_set_sink(uint8_t *work)
uint8_t * protocore_log_span(void)
The PROTOCORE_LOG_BORROW bytes this module's state lives in.
void protocore_log_emit(const uint8_t *work)
LogVars LogV
The operands and the outcome.
One line to emit: its level, and the spec that shapes it.
Definition log.h:55
const mmgr_field * spec
the shape of the message
Definition log.h:57
size_t nv
how many
Definition log.h:59
const mmgr_fval * v
the values that fill it
Definition log.h:58
uint8_t level
the severity the line carries
Definition log.h:56
The entries.
Definition log.h:89
void(*const set_sink)(uint8_t *work)
Definition log.h:91
void(*const emit)(const uint8_t *work)
Definition log.h:90
Definition log.h:79
LogFrameArgs frame
Definition log.h:80
protocore_log_sink_fn sink
Definition log.h:81