From 3a3c5873c3d503887409a315e9c9c05957c2ff14 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 11 Apr 2026 08:21:59 +0900 Subject: [PATCH] Add dlogf printf formatting, ipv4::to_string helper, clean up string building --- firmware/include/debug_log.h | 14 +++++++++++++- firmware/include/dispatch.h | 7 +++++-- firmware/include/ipv4.h | 8 ++++++++ firmware/lib/dispatch.cpp | 6 +++++- firmware/lib/test_handlers.cpp | 4 +--- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/firmware/include/debug_log.h b/firmware/include/debug_log.h index c0bc581..68be752 100644 --- a/firmware/include/debug_log.h +++ b/firmware/include/debug_log.h @@ -1,4 +1,6 @@ #pragma once +#include +#include #include #include #include @@ -17,12 +19,22 @@ inline void dlog(std::string_view msg) { g_debug_log.push_overwrite(log_entry{static_cast(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 fn) { uint32_t t0 = time_us_32(); fn(); uint32_t elapsed = time_us_32() - t0; if (elapsed > threshold_us) - dlog(std::string(label) + " " + std::to_string(elapsed) + "us"); + dlogf("%.*s %luus", static_cast(label.size()), label.data(), static_cast(elapsed)); } inline std::vector dlog_drain() { diff --git a/firmware/include/dispatch.h b/firmware/include/dispatch.h index db83a62..d9817b3 100644 --- a/firmware/include/dispatch.h +++ b/firmware/include/dispatch.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -32,8 +33,10 @@ void typed_handler(responder resp, std::span payload) { auto tup = req.as_tuple(); auto r = msgpack::unpack(p, tup); if (!r) { - resp.respond(DeviceError{1, "decode request ext_id=" + - std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast(r.error()))}); + char err[64]; + snprintf(err, sizeof(err), "decode request ext_id=%d: msgpack error %d", + Req::ext_id, static_cast(r.error())); + resp.respond(DeviceError{1, err}); return; } auto result = Fn(resp, req); diff --git a/firmware/include/ipv4.h b/firmware/include/ipv4.h index 4d875a0..c2a7b70 100644 --- a/firmware/include/ipv4.h +++ b/firmware/include/ipv4.h @@ -1,8 +1,10 @@ #pragma once #include #include +#include #include #include +#include #include "eth.h" #include "span_writer.h" @@ -10,6 +12,12 @@ namespace ipv4 { using ip4_addr = std::array; +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 { eth::header eth; uint8_t ver_ihl; diff --git a/firmware/lib/dispatch.cpp b/firmware/lib/dispatch.cpp index 2efb799..4009070 100644 --- a/firmware/lib/dispatch.cpp +++ b/firmware/lib/dispatch.cpp @@ -72,7 +72,11 @@ void dispatch_schedule_ms(uint32_t ms, std::function fn) { uint8_t err_buf[256]; span_writer err_out(err_buf, sizeof(err_buf)); 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{err_buf, *err}); } }); diff --git a/firmware/lib/test_handlers.cpp b/firmware/lib/test_handlers.cpp index e10295b..eab26ca 100644 --- a/firmware/lib/test_handlers.cpp +++ b/firmware/lib/test_handlers.cpp @@ -44,9 +44,7 @@ static void test_ping(const responder& resp, ipv4::ip4_addr dst_ip) { return; } *done = true; - std::string ip_str = std::to_string(src_ip[0]) + "." + std::to_string(src_ip[1]) + "." + - std::to_string(src_ip[2]) + "." + std::to_string(src_ip[3]); - resp.respond(ResponseTest{true, {"reply from " + ip_str}}); + resp.respond(ResponseTest{true, {"reply from " + ipv4::to_string(src_ip)}}); }; net_add_frame_callback(*cb);