extract debug_log into its own static library; expose log_view iterator instead of raw ring_buffer

This commit is contained in:
Ian Gulliver
2026-05-01 10:33:39 -07:00
parent b82c038091
commit 7000c2e825
7 changed files with 91 additions and 40 deletions

View File

@@ -1,39 +0,0 @@
#pragma once
#include <cstdarg>
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>
#include "pico/time.h"
#include "ring_buffer.h"
struct log_entry {
uint32_t timestamp_us;
std::string message;
};
inline ring_buffer<log_entry, 32> g_debug_log;
inline void dlog(std::string_view msg) {
g_debug_log.push_overwrite(log_entry{static_cast<uint32_t>(time_us_32()), std::string(msg)});
}
__attribute__((format(printf, 1, 2)))
inline void dlogf(const char* fmt, ...) {
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
dlog(buf);
}
template <typename F>
inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, F&& fn) {
uint32_t t0 = time_us_32();
fn();
uint32_t elapsed = time_us_32() - t0;
if (elapsed > threshold_us)
dlogf("%.*s %luus", static_cast<int>(label.size()), label.data(), static_cast<unsigned long>(elapsed));
}

View File

@@ -1,67 +0,0 @@
#pragma once
#include <array>
#include <cstdint>
#include <span>
template <typename T, uint16_t N>
struct ring_buffer {
std::array<T, N> data = {};
uint16_t head = 0;
uint16_t tail = 0;
uint16_t used() const { return tail - head; }
uint16_t free() const { return N - used(); }
bool empty() const { return head == tail; }
bool push(std::span<const T> src) {
if (src.size() > free()) return false;
for (auto& v : src)
data[(tail++) % N] = v;
return true;
}
bool push(const T& v) {
if (free() == 0) return false;
data[(tail++) % N] = v;
return true;
}
void push_overwrite(const T& v) {
if (free() == 0) head++;
data[(tail++) % N] = v;
}
uint16_t peek(std::span<T> dst) const {
uint16_t len = dst.size() < used() ? dst.size() : used();
for (uint16_t i = 0; i < len; i++)
dst[i] = data[(head + i) % N];
return len;
}
void consume(uint16_t len) {
head += len;
if (head >= N) {
head -= N;
tail -= N;
}
}
std::span<const T> read_contiguous() const {
uint16_t offset = head % N;
uint16_t contig = N - offset;
uint16_t pending = used();
uint16_t len = pending < contig ? pending : contig;
return {data.data() + offset, len};
}
struct iterator {
const ring_buffer* rb;
uint16_t index;
const T& operator*() const { return rb->data[(rb->head + index) % N]; }
iterator& operator++() { index++; return *this; }
bool operator!=(const iterator& o) const { return index != o.index; }
};
iterator begin() const { return {this, 0}; }
iterator end() const { return {this, used()}; }
};