Add dlogf printf formatting, ipv4::to_string helper, clean up string building

This commit is contained in:
Ian Gulliver
2026-04-11 08:21:59 +09:00
parent c35c1de76a
commit 3a3c5873c3
5 changed files with 32 additions and 7 deletions

View File

@@ -1,4 +1,6 @@
#pragma once #pragma once
#include <cstdarg>
#include <cstdio>
#include <functional> #include <functional>
#include <string> #include <string>
#include <string_view> #include <string_view>
@@ -17,12 +19,22 @@ inline void dlog(std::string_view msg) {
g_debug_log.push_overwrite(log_entry{static_cast<uint32_t>(time_us_32()), std::string(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);
}
inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, std::function<void()> fn) { inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, std::function<void()> fn) {
uint32_t t0 = time_us_32(); uint32_t t0 = time_us_32();
fn(); fn();
uint32_t elapsed = time_us_32() - t0; uint32_t elapsed = time_us_32() - t0;
if (elapsed > threshold_us) if (elapsed > threshold_us)
dlog(std::string(label) + " " + std::to_string(elapsed) + "us"); dlogf("%.*s %luus", static_cast<int>(label.size()), label.data(), static_cast<unsigned long>(elapsed));
} }
inline std::vector<log_entry> dlog_drain() { inline std::vector<log_entry> dlog_drain() {

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <cstdio>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <span> #include <span>
@@ -32,8 +33,10 @@ void typed_handler(responder resp, std::span<const uint8_t> payload) {
auto tup = req.as_tuple(); auto tup = req.as_tuple();
auto r = msgpack::unpack(p, tup); auto r = msgpack::unpack(p, tup);
if (!r) { if (!r) {
resp.respond(DeviceError{1, "decode request ext_id=" + char err[64];
std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast<int>(r.error()))}); snprintf(err, sizeof(err), "decode request ext_id=%d: msgpack error %d",
Req::ext_id, static_cast<int>(r.error()));
resp.respond(DeviceError{1, err});
return; return;
} }
auto result = Fn(resp, req); auto result = Fn(resp, req);

View File

@@ -1,8 +1,10 @@
#pragma once #pragma once
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <cstdio>
#include <functional> #include <functional>
#include <span> #include <span>
#include <string>
#include "eth.h" #include "eth.h"
#include "span_writer.h" #include "span_writer.h"
@@ -10,6 +12,12 @@ namespace ipv4 {
using ip4_addr = std::array<uint8_t, 4>; using ip4_addr = std::array<uint8_t, 4>;
inline std::string to_string(const ip4_addr& ip) {
char buf[16];
snprintf(buf, sizeof(buf), "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
return buf;
}
struct __attribute__((packed)) header { struct __attribute__((packed)) header {
eth::header eth; eth::header eth;
uint8_t ver_ihl; uint8_t ver_ihl;

View File

@@ -72,7 +72,11 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
uint8_t err_buf[256]; uint8_t err_buf[256];
span_writer err_out(err_buf, sizeof(err_buf)); span_writer err_out(err_buf, sizeof(err_buf));
auto err = encode_response_into(err_out, msg->message_id, auto err = encode_response_into(err_out, msg->message_id,
DeviceError{2, "response too large: " + std::to_string(data.size())}); [&]{
char m[48];
snprintf(m, sizeof(m), "response too large: %zu", data.size());
return DeviceError{2, m};
}());
if (err) usb.send(std::span<const uint8_t>{err_buf, *err}); if (err) usb.send(std::span<const uint8_t>{err_buf, *err});
} }
}); });

View File

@@ -44,9 +44,7 @@ static void test_ping(const responder& resp, ipv4::ip4_addr dst_ip) {
return; return;
} }
*done = true; *done = true;
std::string ip_str = std::to_string(src_ip[0]) + "." + std::to_string(src_ip[1]) + "." + resp.respond(ResponseTest{true, {"reply from " + ipv4::to_string(src_ip)}});
std::to_string(src_ip[2]) + "." + std::to_string(src_ip[3]);
resp.respond(ResponseTest{true, {"reply from " + ip_str}});
}; };
net_add_frame_callback(*cb); net_add_frame_callback(*cb);