remove generic
This commit is contained in:
70
src/root.zig
70
src/root.zig
@@ -25,47 +25,53 @@ 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 {
|
|
||||||
/// 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});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Log with one `tag`. Checks whether the log is enabled.
|
base_tags: []const @Type(.enum_literal),
|
||||||
pub fn log(
|
|
||||||
comptime tag: @Type(.enum_literal),
|
|
||||||
comptime format: []const u8,
|
|
||||||
args: anytype,
|
|
||||||
) void {
|
|
||||||
logTags(&.{tag}, format, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Log with multiple `tags`. Checks whether the log is enabled.
|
/// Creates a new logger with an extended tag prefix.
|
||||||
pub fn logTags(
|
pub fn scope(comptime logger: Logger, comptime tag: @TypeOf(.enum_literal)) Logger {
|
||||||
comptime tags: []const @Type(.enum_literal),
|
return .{
|
||||||
comptime format: []const u8,
|
.base_tags = logger.base_tags ++ [_]@Type(.enum_literal){tag},
|
||||||
args: anytype,
|
|
||||||
) void {
|
|
||||||
if (builtin.is_test and options.disabled_in_test) return;
|
|
||||||
|
|
||||||
const all_tags = comptime base_tags ++ tags;
|
|
||||||
if (!options.enabled(all_tags)) return;
|
|
||||||
|
|
||||||
options.function(all_tags, format, args);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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 {
|
||||||
|
logger.logTags(&.{tag}, format, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 {
|
||||||
|
if (builtin.is_test and options.disabled_in_test) return;
|
||||||
|
|
||||||
|
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`.
|
/// 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 {
|
||||||
for (as) |a| {
|
for (as) |a| {
|
||||||
|
|||||||
Reference in New Issue
Block a user