allow more logging control

This commit is contained in:
Emil Lerch 2025-04-01 13:46:57 -07:00
parent aee9b34ec7
commit 74d9440bec
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -8,6 +8,24 @@ const Args = struct {
syncthing_url: ?[]const u8 = null, syncthing_url: ?[]const u8 = null,
}; };
pub const std_options: std.Options = .{
.logFn = logFn,
.log_level = .debug,
};
var log_level = std.log.default_level;
fn logFn(
comptime message_level: std.log.Level,
comptime scope: @TypeOf(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
if (@intFromEnum(message_level) <= @intFromEnum(log_level)) {
std.log.defaultLog(message_level, scope, format, args);
}
}
pub fn main() !u8 { pub fn main() !u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}).init; var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -104,6 +122,8 @@ fn parseArgs(allocator: std.mem.Allocator) !Args {
std.debug.print("Error: --url requires a URL argument\n", .{}); std.debug.print("Error: --url requires a URL argument\n", .{});
std.process.exit(1); std.process.exit(1);
} }
} else if (std.mem.eql(u8, arg, "-v")) {
moreVerbose();
} else if (std.mem.eql(u8, arg, "--help")) { } else if (std.mem.eql(u8, arg, "--help")) {
printUsage(); printUsage();
std.process.exit(0); std.process.exit(0);
@ -113,6 +133,14 @@ fn parseArgs(allocator: std.mem.Allocator) !Args {
return args; return args;
} }
fn moreVerbose() void {
log_level = switch (log_level) {
.info, .debug => .debug,
.warn => .info,
.err => .warn,
};
}
const FileType = enum { const FileType = enum {
json, json,
zon, zon,
@ -145,6 +173,7 @@ fn printUsage() void {
\\Options: \\Options:
\\ --config <path> Path to config file (default: config.json) \\ --config <path> Path to config file (default: config.json)
\\ --url <url> Override Syncthing URL from config \\ --url <url> Override Syncthing URL from config
\\ -v Increase logging verbosity (can be used multiple times)
\\ --help Show this help message \\ --help Show this help message
\\ \\
\\ST_EVENTS_AUTH environment variable must contain the auth token for \\ST_EVENTS_AUTH environment variable must contain the auth token for