Add dlogf printf formatting, ipv4::to_string helper, clean up string building
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#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)});
|
||||
}
|
||||
|
||||
__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) {
|
||||
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<int>(label.size()), label.data(), static_cast<unsigned long>(elapsed));
|
||||
}
|
||||
|
||||
inline std::vector<log_entry> dlog_drain() {
|
||||
|
||||
Reference in New Issue
Block a user