remove generic

This commit is contained in:
2025-10-30 19:33:56 +01:00
parent 57cfca97cb
commit fa61b79252

View File

@@ -25,46 +25,52 @@ pub const Options = struct {
disabled_in_test: bool = true, disabled_in_test: bool = true,
}; };
/// Used to create a logger from. pub const empty_logger = Logger{ .base_tags = &.{} };
/// A Logger. Examples:
/// ///
/// Examples:
/// ```zig /// ```zig
/// const logger = Logger(&.{.my_module}); /// pub const module_log = Logger{ .base_tags = &.{.module} };
/// const foo_bar_logger = Logger(&.{.foo, .bar}); /// module_log.log(.debug, "Hello {s}!", .{"World"});
/// const foo_bar_log = foo_bar_logger.log;
/// ///
/// pub const sub_module_log = module_log.scope(.bar);
/// sub_module_log.log(.perf, "Operation took {} ns", .{time_ns});
/// ``` /// ```
pub fn Logger(comptime base_tags: []const @Type(.enum_literal)) type { pub const Logger = @This();
return struct {
base_tags: []const @Type(.enum_literal),
/// Creates a new logger with an extended tag prefix. /// Creates a new logger with an extended tag prefix.
pub fn scope(comptime tag: @Type(.enum_literal)) type { pub fn scope(comptime logger: Logger, comptime tag: @TypeOf(.enum_literal)) Logger {
return Logger(base_tags ++ [_]@Type(.enum_literal){tag}); return .{
.base_tags = logger.base_tags ++ [_]@Type(.enum_literal){tag},
};
} }
/// Log with one `tag`. Checks whether the log is enabled. /// Log with one `tag`. Checks whether the log is enabled.
pub fn log( pub fn log(
comptime logger: Logger,
comptime tag: @Type(.enum_literal), comptime tag: @Type(.enum_literal),
comptime format: []const u8, comptime format: []const u8,
args: anytype, args: anytype,
) void { ) void {
logTags(&.{tag}, format, args); logger.logTags(&.{tag}, format, args);
} }
/// Log with multiple `tags`. Checks whether the log is enabled. /// Log with multiple `tags`. Checks whether the log is enabled.
pub fn logTags( pub fn logTags(
comptime logger: Logger,
comptime tags: []const @Type(.enum_literal), comptime tags: []const @Type(.enum_literal),
comptime format: []const u8, comptime format: []const u8,
args: anytype, args: anytype,
) void { ) void {
if (builtin.is_test and options.disabled_in_test) return; if (builtin.is_test and options.disabled_in_test) return;
const all_tags = comptime base_tags ++ tags; const all_tags = comptime logger.base_tags ++ tags;
if (!options.enabled(all_tags)) return; if (!options.enabled(all_tags)) return;
options.function(all_tags, format, args); options.function(all_tags, format, args);
} }
};
}
/// Return `false` if `as` and `bs` have no common member. Else return `true`. /// Return `false` if `as` and `bs` have no common member. Else return `true`.
fn intersect(comptime as: []const @Type(.enum_literal), comptime bs: []const @Type(.enum_literal)) bool { fn intersect(comptime as: []const @Type(.enum_literal), comptime bs: []const @Type(.enum_literal)) bool {