logger fixes

This commit is contained in:
2026-10-12 15:51:58 +02:00
parent eabce70866
commit 350e65837b
5 changed files with 69 additions and 25 deletions
+29 -11
View File
@@ -430,13 +430,39 @@ static const char* const log_level_colors[] = {
[LOG_PANIC] = "\e[1;31m",
};
static void log_console_printf(const char* fmt, ...) {
static void log_cb_printf(log_fn_t callback, void* ctx, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
print_fmt(printf_console, nullptr, fmt, args);
print_fmt(callback, ctx, fmt, args);
va_end(args);
}
// Formats a single record and passes it to `callback`.
static void log_emit(log_fn_t callback, void* ctx, const struct log_record* rec, const char* msg) {
log_cb_printf(
callback,
ctx,
"[%5lu.%06lu] [%5lu] %s",
rec->time_ns / 1'000'000'000,
rec->time_ns / 1'000 % 1'000'000,
(uint64_t)rec->tid,
log_level_colors[rec->level]
);
callback(ctx, msg, rec->len);
callback(ctx, "\e[0m\n", 5);
}
void log_replay(log_fn_t callback, void* ctx) {
char msg[LOG_MESSAGE_MAX];
for (size_t offset = log_tail; offset != log_head;) {
struct log_record rec;
log_ring_peek(offset, &rec, sizeof(rec));
log_ring_peek(offset + sizeof(rec), msg, rec.len);
log_emit(callback, ctx, &rec, msg);
offset += sizeof(rec) + rec.len;
}
}
void log_vwrite(enum log_level level, const char* message, va_list args) {
log_vwrite_prefixed(level, "", message, args);
}
@@ -458,15 +484,7 @@ void log_vwrite_prefixed(enum log_level level, const char* prefix, const char* m
spin_lock(&print_lock);
log_store(&rec, line.buf);
log_console_printf(
"[%5lu.%06lu] [%5lu] %s",
rec.time_ns / 1'000'000'000,
rec.time_ns / 1'000 % 1'000'000,
(uint64_t)rec.tid,
log_level_colors[level]
);
console_write(line.buf, line.len);
console_write("\e[0m\n", 5);
log_emit(printf_console, nullptr, &rec, line.buf);
spin_unlock(&print_lock);
}