extract debug_log into its own static library; expose log_view iterator instead of raw ring_buffer
This commit is contained in:
7
debug_log/CMakeLists.txt
Normal file
7
debug_log/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
add_library(debug_log STATIC debug_log.cpp)
|
||||
|
||||
target_include_directories(debug_log PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_compile_options(debug_log PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
target_link_libraries(debug_log PUBLIC pico_stdlib)
|
||||
28
debug_log/debug_log.cpp
Normal file
28
debug_log/debug_log.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "debug_log.h"
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include "ring_buffer.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint16_t LOG_CAP = 32;
|
||||
ring_buffer<log_entry, LOG_CAP> buf;
|
||||
|
||||
} // namespace
|
||||
|
||||
void dlog(std::string_view msg) {
|
||||
buf.push_overwrite(log_entry{static_cast<uint32_t>(time_us_32()), std::string(msg)});
|
||||
}
|
||||
|
||||
void dlogf(const char* fmt, ...) {
|
||||
char b[128];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsnprintf(b, sizeof(b), fmt, args);
|
||||
va_end(args);
|
||||
dlog(b);
|
||||
}
|
||||
|
||||
log_view log_entries() {
|
||||
return log_view{buf.data.data(), buf.head, LOG_CAP, buf.used()};
|
||||
}
|
||||
52
debug_log/debug_log.h
Normal file
52
debug_log/debug_log.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include "pico/time.h"
|
||||
|
||||
struct log_entry {
|
||||
uint32_t timestamp_us;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
class log_iterator {
|
||||
const log_entry* data;
|
||||
uint16_t base;
|
||||
uint16_t cap;
|
||||
uint16_t pos;
|
||||
public:
|
||||
log_iterator(const log_entry* d, uint16_t b, uint16_t c, uint16_t p)
|
||||
: data(d), base(b), cap(c), pos(p) {}
|
||||
const log_entry& operator*() const { return data[(base + pos) % cap]; }
|
||||
log_iterator& operator++() { ++pos; return *this; }
|
||||
bool operator!=(const log_iterator& o) const { return pos != o.pos; }
|
||||
};
|
||||
|
||||
class log_view {
|
||||
const log_entry* data;
|
||||
uint16_t base;
|
||||
uint16_t cap;
|
||||
uint16_t count;
|
||||
public:
|
||||
log_view(const log_entry* d, uint16_t b, uint16_t c, uint16_t n)
|
||||
: data(d), base(b), cap(c), count(n) {}
|
||||
log_iterator begin() const { return {data, base, cap, 0}; }
|
||||
log_iterator end() const { return {data, base, cap, count}; }
|
||||
};
|
||||
|
||||
log_view log_entries();
|
||||
|
||||
void dlog(std::string_view msg);
|
||||
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
void dlogf(const char* fmt, ...);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
67
debug_log/ring_buffer.h
Normal file
67
debug_log/ring_buffer.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#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()}; }
|
||||
};
|
||||
Reference in New Issue
Block a user