ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
log.cpp
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 log.cpp
6 * @brief The emitted half of the PC_LOG* macros (see log.h).
7 *
8 * Nothing here is compiled when PC_LOG_LEVEL is PC_NONE - not even the sink pointer - so a build that
9 * logs nothing links no logging code and spends no BSS on it.
10 */
11
13
14#if PC_LOG_LEVEL < PC_LOG_LEVEL_NONE
15
16#include <stdarg.h>
17
18#if PC_ENABLE_LOGBUF
20#endif
21
22/** @brief Owned state: just the sink the formatted line is handed to. */
23struct LogCtx
24{
25 pc_log_sink_fn sink;
26};
27static LogCtx s_log = {nullptr};
28
29void pc_log_set_sink(pc_log_sink_fn cb)
30{
31 s_log.sink = cb;
32}
33
34void pc_log_frame(uint8_t level, const pc_field *spec, ...)
35{
36 if (!spec)
37 {
38 return;
39 }
40
41 // One line's worth of stack, matching what the ring can store. A spec whose worst case exceeds
42 // it is a spec that was written wrong, not a runtime condition to absorb: every log frame
43 // states its literals' lengths and bounds its string fields, so whether a message fits is
44 // settled when the frame is declared. Nothing here decides it from the data.
45 char line[PC_LOG_LINE_LEN];
46 va_list ap;
47 va_start(ap, spec);
48 (void)pc_frame_vbuild(line, sizeof(line), spec, ap);
49 va_end(ap);
50
51#if PC_ENABLE_LOGBUF
52 pc_log(level, line);
53#endif
54 if (s_log.sink)
55 {
56 s_log.sink(level, line);
57 }
58}
59
60#endif // PC_LOG_LEVEL < PC_LOG_LEVEL_NONE
PC_OPTIMIZE_O2 size_t pc_frame_vbuild(char *out, size_t cap, const pc_field *spec, va_list ap)
va_list form, for a caller that already has one.
Definition frame.cpp:25
Abstract logging whose disabled levels cost nothing at all (PC_LOG_LEVEL).
void(* pc_log_sink_fn)(uint8_t level, const char *line)
Receives an emitted line, already formatted. level is a PC_LOG_LEVEL_* value.
Definition log.h:35
Fixed-RAM rotating log buffer with severity traps (PC_ENABLE_LOGBUF).
#define PC_LOG_LINE_LEN
Maximum length of one stored log line (bytes, including null).
One field of a frame. Frames are static const pc_field[], so they live in rodata.
Definition frame.h:80