extract debug_log into its own static library; expose log_view iterator instead of raw ring_buffer
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
add_subdirectory(w6300)
|
||||
add_subdirectory(debug_log)
|
||||
|
||||
add_library(limen STATIC
|
||||
src/arp.cpp
|
||||
@@ -23,6 +24,7 @@ target_compile_options(limen PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
target_link_libraries(limen PUBLIC
|
||||
w6300
|
||||
debug_log
|
||||
pico_stdlib
|
||||
pico_sha256
|
||||
pico_unique_id
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -45,8 +45,9 @@ std::optional<ResponseInfo> handle_info(const responder&, const RequestInfo&) {
|
||||
|
||||
std::optional<ResponseLog> handle_log(const responder&, const RequestLog&) {
|
||||
ResponseLog resp;
|
||||
for (auto& e : g_debug_log)
|
||||
for (auto& e : log_entries()) {
|
||||
resp.entries.push_back(LogEntry{e.timestamp_us, e.message});
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user