remove generic
This commit is contained in:
58
src/root.zig
58
src/root.zig
@@ -25,45 +25,51 @@ pub const Options = struct {
|
||||
disabled_in_test: bool = true,
|
||||
};
|
||||
|
||||
/// Used to create a logger from.
|
||||
///
|
||||
/// Examples:
|
||||
/// ```zig
|
||||
/// const logger = Logger(&.{.my_module});
|
||||
/// const foo_bar_logger = Logger(&.{.foo, .bar});
|
||||
/// const foo_bar_log = foo_bar_logger.log;
|
||||
///
|
||||
/// ```
|
||||
pub fn Logger(comptime base_tags: []const @Type(.enum_literal)) type {
|
||||
return struct {
|
||||
/// Creates a new logger with an extended tag prefix.
|
||||
pub fn scope(comptime tag: @Type(.enum_literal)) type {
|
||||
return Logger(base_tags ++ [_]@Type(.enum_literal){tag});
|
||||
}
|
||||
pub const empty_logger = Logger{ .base_tags = &.{} };
|
||||
|
||||
/// Log with one `tag`. Checks whether the log is enabled.
|
||||
pub fn log(
|
||||
/// A Logger. Examples:
|
||||
///
|
||||
/// ```zig
|
||||
/// pub const module_log = Logger{ .base_tags = &.{.module} };
|
||||
/// module_log.log(.debug, "Hello {s}!", .{"World"});
|
||||
///
|
||||
/// pub const sub_module_log = module_log.scope(.bar);
|
||||
/// sub_module_log.log(.perf, "Operation took {} ns", .{time_ns});
|
||||
/// ```
|
||||
pub const Logger = @This();
|
||||
|
||||
base_tags: []const @Type(.enum_literal),
|
||||
|
||||
/// Creates a new logger with an extended tag prefix.
|
||||
pub fn scope(comptime logger: Logger, comptime tag: @TypeOf(.enum_literal)) Logger {
|
||||
return .{
|
||||
.base_tags = logger.base_tags ++ [_]@Type(.enum_literal){tag},
|
||||
};
|
||||
}
|
||||
|
||||
/// Log with one `tag`. Checks whether the log is enabled.
|
||||
pub fn log(
|
||||
comptime logger: Logger,
|
||||
comptime tag: @Type(.enum_literal),
|
||||
comptime format: []const u8,
|
||||
args: anytype,
|
||||
) void {
|
||||
logTags(&.{tag}, format, args);
|
||||
}
|
||||
) void {
|
||||
logger.logTags(&.{tag}, format, args);
|
||||
}
|
||||
|
||||
/// Log with multiple `tags`. Checks whether the log is enabled.
|
||||
pub fn logTags(
|
||||
/// Log with multiple `tags`. Checks whether the log is enabled.
|
||||
pub fn logTags(
|
||||
comptime logger: Logger,
|
||||
comptime tags: []const @Type(.enum_literal),
|
||||
comptime format: []const u8,
|
||||
args: anytype,
|
||||
) void {
|
||||
) void {
|
||||
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;
|
||||
|
||||
options.function(all_tags, format, args);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Return `false` if `as` and `bs` have no common member. Else return `true`.
|
||||
|
||||
Reference in New Issue
Block a user