testing: add LogFilter utility for scoped log suppression

This commit is contained in:
Adrià Arrufat
2026-03-12 13:56:53 +09:00
parent 1522c90294
commit 3d6d669a50
4 changed files with 29 additions and 3 deletions

View File

@@ -610,3 +610,23 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
unreachable;
}
/// LogFilter provides a scoped way to suppress specific log categories during tests.
/// This is useful for tests that trigger expected errors or warnings.
pub const LogFilter = struct {
old_filter: []const log.Scope,
/// Sets the log filter to only include the specified scope.
/// Returns a LogFilter that should be deinitialized to restore previous filters.
pub fn init(comptime scope: log.Scope) LogFilter {
const old_filter = log.opts.filter_scopes;
const new_filter = comptime &[_]log.Scope{scope};
log.opts.filter_scopes = new_filter;
return .{ .old_filter = old_filter };
}
/// Restores the log filters to their previous state.
pub fn deinit(self: LogFilter) void {
log.opts.filter_scopes = self.old_filter;
}
};