28 uint8_t trap_threshold = 0xFF;
29 pc_log_trap_fn trap =
nullptr;
33char level_letter(uint8_t level)
37 case pc_log_level::PC_LOG_ERROR:
39 case pc_log_level::PC_LOG_WARN:
41 case pc_log_level::PC_LOG_INFO:
49void pc_logbuf_reset(
void)
55void pc_log(uint8_t level,
const char *msg)
60 slot = (uint16_t)((s_log.head + s_log.count) %
PC_LOG_LINES);
66 s_log.head = (uint16_t)((s_log.head + 1) %
PC_LOG_LINES);
69 pc_sb_ch(&sb67, (
char)(level_letter(level)));
76 s_log.level[slot] = level;
78 if (s_log.trap && level >= s_log.trap_threshold)
80 s_log.trap(level, s_log.lines[slot]);
84uint16_t pc_log_count(
void)
89const char *pc_log_at(uint16_t i)
98int pc_log_dump(
char *out,
size_t cap)
100 if (!out || cap == 0)
106 for (uint16_t i = 0; i < s_log.count; i++)
108 const char *line = s_log.lines[(s_log.head + i) %
PC_LOG_LINES];
109 size_t n = strnlen(line, cap);
110 size_t need = n + (i + 1 < s_log.count ? 1 : 0);
111 if (pos + need >= cap)
116 memcpy(out + pos, line, n);
118 if (i + 1 < s_log.count)
127void pc_log_set_trap(uint8_t threshold, pc_log_trap_fn cb)
129 s_log.trap_threshold = threshold;
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).
#define PC_LOG_LINES
Number of log lines retained in the ring.
Bounded no-heap string builder that fails closed on overflow (one shared copy).
size_t pc_sb_finish(pc_sb *b)
NUL-terminate and return the built length, or 0 if the build overflowed.
void pc_sb_ch(pc_sb *b, char c)
Append a single character.
void pc_sb_put(pc_sb *b, const char *s)
Append NUL-terminated s; leaves the buffer untouched and clears ok if it would not fit.
Bump-append target; ok latches false once an append would overflow cap.