re-enable log tests

This commit is contained in:
Karl Seguin
2025-05-27 19:57:25 +08:00
parent fe9344ce57
commit 8d3cf04324
2 changed files with 172 additions and 182 deletions

View File

@@ -22,8 +22,7 @@ const builtin = @import("builtin");
const JsObject = @import("../env.zig").Env.JsObject; const JsObject = @import("../env.zig").Env.JsObject;
const SessionState = @import("../env.zig").SessionState; const SessionState = @import("../env.zig").SessionState;
// const log = if (builtin.is_test) &test_capture else @import("../../log.zig"); const log = if (builtin.is_test) &test_capture else @import("../../log.zig");
const log = @import("../../log.zig");
pub const Console = struct { pub const Console = struct {
// TODO: configurable writer // TODO: configurable writer
@@ -151,137 +150,137 @@ fn timestamp() u32 {
return @intCast(ts.sec); return @intCast(ts.sec);
} }
// var test_capture = TestCapture{}; var test_capture = TestCapture{};
// const testing = @import("../../testing.zig"); const testing = @import("../../testing.zig");
// test "Browser.Console" { test "Browser.Console" {
// defer testing.reset(); defer testing.reset();
// var runner = try testing.jsRunner(testing.tracking_allocator, .{}); var runner = try testing.jsRunner(testing.tracking_allocator, .{});
// defer runner.deinit(); defer runner.deinit();
// { {
// try runner.testCases(&.{ try runner.testCases(&.{
// .{ "console.log('a')", "undefined" }, .{ "console.log('a')", "undefined" },
// .{ "console.warn('hello world', 23, true, new Object())", "undefined" }, .{ "console.warn('hello world', 23, true, new Object())", "undefined" },
// }, .{}); }, .{});
// const captured = test_capture.captured.items; const captured = test_capture.captured.items;
// try testing.expectEqual("[info] args=a", captured[0]); try testing.expectEqual("[info] args=a", captured[0]);
// try testing.expectEqual("[warn] args=hello world 23 true [object Object]", captured[1]); try testing.expectEqual("[warn] args=hello world 23 true [object Object]", captured[1]);
// } }
// { {
// test_capture.reset(); test_capture.reset();
// try runner.testCases(&.{ try runner.testCases(&.{
// .{ "console.countReset()", "undefined" }, .{ "console.countReset()", "undefined" },
// .{ "console.count()", "undefined" }, .{ "console.count()", "undefined" },
// .{ "console.count('teg')", "undefined" }, .{ "console.count('teg')", "undefined" },
// .{ "console.count('teg')", "undefined" }, .{ "console.count('teg')", "undefined" },
// .{ "console.count('teg')", "undefined" }, .{ "console.count('teg')", "undefined" },
// .{ "console.count()", "undefined" }, .{ "console.count()", "undefined" },
// .{ "console.countReset('teg')", "undefined" }, .{ "console.countReset('teg')", "undefined" },
// .{ "console.countReset()", "undefined" }, .{ "console.countReset()", "undefined" },
// .{ "console.count()", "undefined" }, .{ "console.count()", "undefined" },
// }, .{}); }, .{});
// const captured = test_capture.captured.items; const captured = test_capture.captured.items;
// try testing.expectEqual("[invalid counter] label=default", captured[0]); try testing.expectEqual("[invalid counter] label=default", captured[0]);
// try testing.expectEqual("[count] label=default count=1", captured[1]); try testing.expectEqual("[count] label=default count=1", captured[1]);
// try testing.expectEqual("[count] label=teg count=1", captured[2]); try testing.expectEqual("[count] label=teg count=1", captured[2]);
// try testing.expectEqual("[count] label=teg count=2", captured[3]); try testing.expectEqual("[count] label=teg count=2", captured[3]);
// try testing.expectEqual("[count] label=teg count=3", captured[4]); try testing.expectEqual("[count] label=teg count=3", captured[4]);
// try testing.expectEqual("[count] label=default count=2", captured[5]); try testing.expectEqual("[count] label=default count=2", captured[5]);
// try testing.expectEqual("[count reset] label=teg count=3", captured[6]); try testing.expectEqual("[count reset] label=teg count=3", captured[6]);
// try testing.expectEqual("[count reset] label=default count=2", captured[7]); try testing.expectEqual("[count reset] label=default count=2", captured[7]);
// try testing.expectEqual("[count] label=default count=1", captured[8]); try testing.expectEqual("[count] label=default count=1", captured[8]);
// } }
// { {
// test_capture.reset(); test_capture.reset();
// try runner.testCases(&.{ try runner.testCases(&.{
// .{ "console.assert(true)", "undefined" }, .{ "console.assert(true)", "undefined" },
// .{ "console.assert('a', 2, 3, 4)", "undefined" }, .{ "console.assert('a', 2, 3, 4)", "undefined" },
// .{ "console.assert('')", "undefined" }, .{ "console.assert('')", "undefined" },
// .{ "console.assert('', 'x', true)", "undefined" }, .{ "console.assert('', 'x', true)", "undefined" },
// .{ "console.assert(false, 'x')", "undefined" }, .{ "console.assert(false, 'x')", "undefined" },
// }, .{}); }, .{});
// const captured = test_capture.captured.items; const captured = test_capture.captured.items;
// try testing.expectEqual("[assertion failed] values=", captured[0]); try testing.expectEqual("[assertion failed] values=", captured[0]);
// try testing.expectEqual("[assertion failed] values=x true", captured[1]); try testing.expectEqual("[assertion failed] values=x true", captured[1]);
// try testing.expectEqual("[assertion failed] values=x", captured[2]); try testing.expectEqual("[assertion failed] values=x", captured[2]);
// } }
// } }
// const TestCapture = struct { const TestCapture = struct {
// captured: std.ArrayListUnmanaged([]const u8) = .{}, captured: std.ArrayListUnmanaged([]const u8) = .{},
// fn reset(self: *TestCapture) void { fn reset(self: *TestCapture) void {
// self.captured = .{}; self.captured = .{};
// } }
// fn debug( fn debug(
// self: *TestCapture, self: *TestCapture,
// comptime scope: @Type(.enum_literal), comptime scope: @Type(.enum_literal),
// comptime msg: []const u8, comptime msg: []const u8,
// args: anytype, args: anytype,
// ) void { ) void {
// self.capture(scope, msg, args); self.capture(scope, msg, args);
// } }
// fn info( fn info(
// self: *TestCapture, self: *TestCapture,
// comptime scope: @Type(.enum_literal), comptime scope: @Type(.enum_literal),
// comptime msg: []const u8, comptime msg: []const u8,
// args: anytype, args: anytype,
// ) void { ) void {
// self.capture(scope, msg, args); self.capture(scope, msg, args);
// } }
// fn warn( fn warn(
// self: *TestCapture, self: *TestCapture,
// comptime scope: @Type(.enum_literal), comptime scope: @Type(.enum_literal),
// comptime msg: []const u8, comptime msg: []const u8,
// args: anytype, args: anytype,
// ) void { ) void {
// self.capture(scope, msg, args); self.capture(scope, msg, args);
// } }
// fn err( fn err(
// self: *TestCapture, self: *TestCapture,
// comptime scope: @Type(.enum_literal), comptime scope: @Type(.enum_literal),
// comptime msg: []const u8, comptime msg: []const u8,
// args: anytype, args: anytype,
// ) void { ) void {
// self.capture(scope, msg, args); self.capture(scope, msg, args);
// } }
// fn capture( fn capture(
// self: *TestCapture, self: *TestCapture,
// comptime scope: @Type(.enum_literal), comptime scope: @Type(.enum_literal),
// comptime msg: []const u8, comptime msg: []const u8,
// args: anytype, args: anytype,
// ) void { ) void {
// self._capture(scope, msg, args) catch unreachable; self._capture(scope, msg, args) catch unreachable;
// } }
// fn _capture( fn _capture(
// self: *TestCapture, self: *TestCapture,
// comptime scope: @Type(.enum_literal), comptime scope: @Type(.enum_literal),
// comptime msg: []const u8, comptime msg: []const u8,
// args: anytype, args: anytype,
// ) !void { ) !void {
// std.debug.assert(scope == .console); std.debug.assert(scope == .console);
// const allocator = testing.arena_allocator; const allocator = testing.arena_allocator;
// var buf: std.ArrayListUnmanaged(u8) = .empty; var buf: std.ArrayListUnmanaged(u8) = .empty;
// try buf.appendSlice(allocator, "[" ++ msg ++ "] "); try buf.appendSlice(allocator, "[" ++ msg ++ "] ");
// inline for (@typeInfo(@TypeOf(args)).@"struct".fields) |f| { inline for (@typeInfo(@TypeOf(args)).@"struct".fields) |f| {
// try buf.appendSlice(allocator, f.name); try buf.appendSlice(allocator, f.name);
// try buf.append(allocator, '='); try buf.append(allocator, '=');
// try @import("../../log.zig").writeValue(allocator, &buf, false, @field(args, f.name)); try @import("../../log.zig").writeValue(false, @field(args, f.name), buf.writer(allocator));
// try buf.append(allocator, ' '); try buf.append(allocator, ' ');
// } }
// self.captured.append(testing.arena_allocator, std.mem.trimRight(u8, buf.items, " ")) catch unreachable; self.captured.append(testing.arena_allocator, std.mem.trimRight(u8, buf.items, " ")) catch unreachable;
// } }
// }; };

View File

@@ -81,12 +81,16 @@ pub fn log(comptime scope: @Type(.enum_literal), comptime level: Level, comptime
if (comptime enabled(scope, level) == false) { if (comptime enabled(scope, level) == false) {
return; return;
} }
tryLog(scope, level, msg, data) catch |log_err| {
std.debug.lockStdErr();
defer std.debug.unlockStdErr();
logTo(scope, level, msg, data, std.io.getStdErr().writer()) catch |log_err| {
std.debug.print("$time={d} $level=fatal $scope={s} $msg=\"log err\" err={s} log_msg=\"{s}\"", .{ timestamp(), @errorName(log_err), @tagName(scope), msg }); std.debug.print("$time={d} $level=fatal $scope={s} $msg=\"log err\" err={s} log_msg=\"{s}\"", .{ timestamp(), @errorName(log_err), @tagName(scope), msg });
}; };
} }
fn tryLog(comptime scope: @Type(.enum_literal), comptime level: Level, comptime msg: []const u8, data: anytype) !void { fn logTo(comptime scope: @Type(.enum_literal), comptime level: Level, comptime msg: []const u8, data: anytype, out: anytype) !void {
comptime { comptime {
if (msg.len > 30) { if (msg.len > 30) {
@compileError("log msg cannot be more than 30 characters: '" ++ msg ++ "'"); @compileError("log msg cannot be more than 30 characters: '" ++ msg ++ "'");
@@ -102,12 +106,7 @@ fn tryLog(comptime scope: @Type(.enum_literal), comptime level: Level, comptime
} }
} }
out_lock.lock(); var bw = std.io.bufferedWriter(out);
defer out_lock.unlock();
const stderr = std.io.getStdErr().writer();
var bw = std.io.bufferedWriter(stderr);
switch (format) { switch (format) {
.logfmt => try logLogfmt(scope, level, msg, data, bw.writer()), .logfmt => try logLogfmt(scope, level, msg, data, bw.writer()),
.pretty => try logPretty(scope, level, msg, data, bw.writer()), .pretty => try logPretty(scope, level, msg, data, bw.writer()),
@@ -292,64 +291,56 @@ fn elapsed() i64 {
return now - previous; return now - previous;
} }
// const testing = @import("testing.zig"); const testing = @import("testing.zig");
// test "log: data" { test "log: data" {
// var buf: std.ArrayListUnmanaged(u8) = .{}; var buf: std.ArrayListUnmanaged(u8) = .{};
// defer buf.deinit(testing.allocator); defer buf.deinit(testing.allocator);
// var logger = try TestLogger.init(testing.allocator, .{ .format = .logfmt }, buf.writer(testing.allocator)); {
// defer logger.deinit(); try logTo(.t_scope, .err, "nope", .{}, buf.writer(testing.allocator));
try testing.expectEqual("$time=1739795092929 $scope=t_scope $level=error $msg=nope\n", buf.items);
}
// { {
// try logger.log(.t_scope, .err, "nope", .{}); buf.clearRetainingCapacity();
// try testing.expectEqual("$time=1739795092929 $scope=t_scope $level=error $msg=nope\n", buf.items); const string = try testing.allocator.dupe(u8, "spice_must_flow");
// } defer testing.allocator.free(string);
// { try logTo(.scope_2, .warn, "a msg", .{
// buf.clearRetainingCapacity(); .cint = 5,
// const string = try testing.allocator.dupe(u8, "spice_must_flow"); .cfloat = 3.43,
// defer testing.allocator.free(string); .int = @as(i16, -49),
.float = @as(f32, 0.0003232),
.bt = true,
.bf = false,
.nn = @as(?i32, 33),
.n = @as(?i32, null),
.lit = "over9000!",
.slice = string,
.err = error.Nope,
.level = Level.warn,
}, buf.writer(testing.allocator));
// try logger.log(.scope_2, .warn, "a msg", .{ try testing.expectEqual("$time=1739795092929 $scope=scope_2 $level=warn $msg=\"a msg\" " ++
// .cint = 5, "cint=5 cfloat=3.43 int=-49 float=0.0003232 bt=true bf=false " ++
// .cfloat = 3.43, "nn=33 n=null lit=over9000! slice=spice_must_flow " ++
// .int = @as(i16, -49), "err=Nope level=warn\n", buf.items);
// .float = @as(f32, 0.0003232), }
// .bt = true, }
// .bf = false,
// .nn = @as(?i32, 33),
// .n = @as(?i32, null),
// .lit = "over9000!",
// .slice = string,
// .err = error.Nope,
// .level = Level.warn,
// });
// try testing.expectEqual("$time=1739795092929 $scope=scope_2 $level=warn $msg=\"a msg\" " ++ test "log: string escape" {
// "cint=5 cfloat=3.43 int=-49 float=0.0003232 bt=true bf=false " ++ var buf: std.ArrayListUnmanaged(u8) = .{};
// "nn=33 n=null lit=over9000! slice=spice_must_flow " ++ defer buf.deinit(testing.allocator);
// "err=Nope level=warn\n", buf.items);
// }
// }
// test "log: string escape" { const prefix = "$time=1739795092929 $scope=scope $level=error $msg=test ";
// var buf: std.ArrayListUnmanaged(u8) = .{}; {
// defer buf.deinit(testing.allocator); try logTo(.scope, .err, "test", .{ .string = "hello world" }, buf.writer(testing.allocator));
try testing.expectEqual(prefix ++ "string=\"hello world\"\n", buf.items);
}
// var logger = try TestLogger.init(testing.allocator, .{ .format = .logfmt }, buf.writer(testing.allocator)); {
// defer logger.deinit(); buf.clearRetainingCapacity();
try logTo(.scope, .err, "test", .{ .string = "\n \thi \" \" " }, buf.writer(testing.allocator));
// const prefix = "$time=1739795092929 $scope=scope $level=error $msg=test "; try testing.expectEqual(prefix ++ "string=\"\\n \thi \\\" \\\" \"\n", buf.items);
// { }
// try logger.log(.scope, .err, "test", .{ .string = "hello world" }); }
// try testing.expectEqual(prefix ++ "string=\"hello world\"\n", buf.items);
// }
// {
// buf.clearRetainingCapacity();
// try logger.log(.scope, .err, "test", .{ .string = "\n \thi \" \" " });
// try testing.expectEqual(prefix ++ "string=\"\\n \thi \\\" \\\" \"\n", buf.items);
// }
// }
// const TestLogger = LogT(std.ArrayListUnmanaged(u8).Writer);