logfire client for zig
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add background flush thread

flushes pending data every batch_timeout_ms (default 500ms).
stops cleanly on shutdown().

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

zzstoatzz 560715ef 5d88022f

+29
+29
src/root.zig
··· 66 66 current_trace_id: ?[16]u8, 67 67 span_id_counter: std.atomic.Value(u64), 68 68 69 + /// background flush thread 70 + flush_thread: ?std.Thread, 71 + running: std.atomic.Value(bool), 72 + 69 73 pub fn init(allocator: std.mem.Allocator, config: Config) !*Logfire { 70 74 const resolved = config.resolve(); 71 75 ··· 80 84 .pending_mutex = .{}, 81 85 .current_trace_id = null, 82 86 .span_id_counter = std.atomic.Value(u64).init(1), 87 + .flush_thread = null, 88 + .running = std.atomic.Value(bool).init(true), 83 89 }; 84 90 85 91 // generate initial trace id 86 92 self.current_trace_id = generateTraceId(); 87 93 94 + // start background flush thread 95 + if (resolved.batch_timeout_ms > 0) { 96 + self.flush_thread = std.Thread.spawn(.{}, flushLoop, .{self}) catch null; 97 + } 98 + 88 99 return self; 89 100 } 90 101 102 + fn flushLoop(self: *Logfire) void { 103 + const interval_ns = self.config.batch_timeout_ms * std.time.ns_per_ms; 104 + while (self.running.load(.acquire)) { 105 + std.Thread.sleep(interval_ns); 106 + if (!self.running.load(.acquire)) break; 107 + self.flush() catch |e| { 108 + std.log.warn("logfire: background flush failed: {}", .{e}); 109 + }; 110 + } 111 + } 112 + 91 113 pub fn deinit(self: *Logfire) void { 92 114 self.pending_spans.deinit(self.allocator); 93 115 self.pending_logs.deinit(self.allocator); ··· 97 119 } 98 120 99 121 pub fn shutdown(self: *Logfire) void { 122 + // stop flush thread 123 + self.running.store(false, .release); 124 + if (self.flush_thread) |t| { 125 + t.join(); 126 + } 127 + 128 + // final flush 100 129 self.flush() catch |e| { 101 130 std.log.warn("logfire: flush failed during shutdown: {}", .{e}); 102 131 };