Skip to content

Commit 257ac80

Browse files
committed
feat: add timestamped logging with version and git hash
1 parent 2bfb486 commit 257ac80

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/main.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ const Handle = @import("message").Handle;
1111
const build_options = @import("build_options");
1212

1313
const version = "0.1.0";
14+
const git_hash = build_options.git_hash;
15+
16+
pub const std_options: std.Options = .{
17+
.logFn = customLog,
18+
};
19+
20+
fn customLog(
21+
comptime level: std.log.Level,
22+
comptime scope: @TypeOf(.enum_literal),
23+
comptime format: []const u8,
24+
args: anytype,
25+
) void {
26+
_ = scope;
27+
const prefix = comptime switch (level) {
28+
.err => "ERROR",
29+
.warn => "WARN ",
30+
.info => "INFO ",
31+
.debug => "DEBUG",
32+
};
33+
34+
// Get wall clock time
35+
const epoch = std.time.timestamp();
36+
const es = std.time.epoch.EpochSeconds{ .secs = @intCast(epoch) };
37+
const day_secs = es.getDaySeconds();
38+
const h = day_secs.getHoursIntoDay();
39+
const m = day_secs.getMinutesIntoHour();
40+
const s = day_secs.getSecondsIntoMinute();
41+
42+
const stderr = std.fs.File.stderr().deprecatedWriter();
43+
nosuspend stderr.print("{d:0>2}:{d:0>2}:{d:0>2} {s} [{s}-{s}] " ++ format ++ "\n", .{h, m, s, prefix, version, git_hash} ++ args) catch {};
44+
}
1445

1546
var running: std.atomic.Value(bool) = std.atomic.Value(bool).init(true);
1647
var signal_pipe: [2]posix.fd_t = .{ -1, -1 };
@@ -60,6 +91,8 @@ pub fn main() !void {
6091
defer _ = gpa.deinit();
6192
const allocator = gpa.allocator();
6293

94+
std.log.info("starting", .{});
95+
6396
// Load config
6497
var config = Config.load(allocator);
6598
defer config.deinit();

0 commit comments

Comments
 (0)