Files
picomap/firmware/include/debug_log.h

40 lines
980 B
C++

#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));
}