Lightpanda: the headless browser designed for AI and automation
apply formatting rule globally to all formtted values
Karl Seguinc74d176a8fa3
diff --git a/src/browser/Session.zig b/src/browser/Session.zigindex 9b8024268..a43fb2bb2 100644--- a/src/browser/Session.zig+++ b/src/browser/Session.zig@@ -153,7 +153,7 @@ pub fn isCancelled(self: *const Session) bool {return hook.check(hook.context);}-pub const max_console_bytes = 64 * 1024;+const max_console_bytes = 64 * 1024;pub fn init(self: *Session, browser: *Browser, notification: *Notification) !void {const allocator = browser.app.allocator;diff --git a/src/browser/webapi/Console.zig b/src/browser/webapi/Console.zigindex 644c1a742..91d68085c 100644--- a/src/browser/webapi/Console.zig+++ b/src/browser/webapi/Console.zig@@ -20,9 +20,7 @@ const std = @import("std");const lp = @import("lightpanda");const js = @import("../js/js.zig");-const Session = @import("../Session.zig");const Notification = @import("../../Notification.zig");-const LimitedWriter = @import("../../LimitedWriter.zig");const logger = lp.log;@@ -98,7 +96,7 @@ pub fn @"error"(values: []js.Value, exec: *js.Execution) void {}pub fn table(data: ?js.Value, columns: ?js.Value) void {- logger.info(.js, "console.table", .{ .data = LogValue{ .value = data }, .columns = LogValue{ .value = columns } });+ logger.info(.js, "console.table", .{ .data = data, .columns = columns });}pub fn dir(item_: ?js.Value, _: ?js.Value, exec: *js.Execution) void {@@ -189,75 +187,24 @@ pub fn groupCollapsed(values: []js.Value) void {}fn groupEnd() void {}--// Page values are logged as strings, so the logger escapes them. One console-// call writes at most max_console_bytes of them, like the session's capture.-const truncated_marker = "...[truncated]";--const LogBuffer = struct {- buf: [Session.max_console_bytes + truncated_marker.len]u8 = undefined,- used: usize = 0,- full: bool = false,-- // Null once an earlier value has used up the budget.- fn render(self: *LogBuffer, value: js.Value) !?[]const u8 {- if (self.full) {- return null;- }- var fixed: std.Io.Writer = .fixed(self.buf[self.used..]);- var limited: LimitedWriter = .init(&fixed, @intCast(Session.max_console_bytes - self.used));- value.format(&limited.writer) catch |err| {- if (!limited.truncated) {- return err;- }- self.full = true;- try fixed.writeAll(truncated_marker);- };- self.used += fixed.end;- return fixed.buffered();- }-};--const LogValue = struct {- value: ?js.Value,-- pub fn format(self: LogValue, writer: *std.Io.Writer) !void {- const value = self.value orelse return writer.writeAll("null");- var buf: LogBuffer = .{};- return logger.writePrettyString((try buf.render(value)).?, writer);- }-- pub fn logFmt(self: LogValue, key: []const u8, writer: anytype) !void {- const value = self.value orelse return writer.write(key, "null");- var buf: LogBuffer = .{};- return writer.write(key, (try buf.render(value)).?);- }-};-const ValueWriter = struct {values: []js.Value,stack: ?[]const u8 = null,pub fn format(self: ValueWriter, writer: *std.Io.Writer) !void {- var buf: LogBuffer = .{};for (self.valuesToLog(), 1..) |value, i| {- const text = try buf.render(value) orelse break;- try writer.print("\n arg({d}): ", .{i});- try logger.writePrettyString(text, writer);+ try writer.print("\n arg({d}): {f}", .{ i, value });}if (self.stack) |s| {- try writer.writeAll("\n stack: ");- try logger.writePrettyString(s, writer);+ try writer.print("\n stack: {s}", .{s});}}pub fn logFmt(self: ValueWriter, _: []const u8, writer: anytype) !void {- var buf: LogBuffer = .{};- var name_buf: [32]u8 = undefined;+ var buf: [32]u8 = undefined;for (self.valuesToLog(), 0..) |value, i| {- const text = try buf.render(value) orelse break;- const name = try std.mem.print(&name_buf, "param.{d}", .{i});- try writer.write(name, text);+ const name = try std.mem.print(&buf, "param.{d}", .{i});+ try writer.write(name, value);}}@@ -325,7 +272,7 @@ test "WebApi: Console" {try testing.htmlRunner("console", .{});}-test "WebApi: Console escapes and caps page values in the log" {+test "WebApi: Console escapes page values in the log" {const frame = try testing.createFrame();defer testing.test_session.closeAllPages();@@ -351,14 +298,4 @@ test "WebApi: Console escapes and caps page values in the log" {logger.opts.format = .pretty;try logger.logTo(.js, .info, "console.log", .{ValueWriter{ .values = &values }}, &aw.writer);try testing.expect(std.mem.endsWith(u8, aw.written(), "\n arg(1): one\nx level=fatal\n arg(2): \\x1b[31m\n\n"));-- // The cut keeps whole code points ('a' + 32767 two-byte ones) and drops- // later arguments.- aw.clearRetainingCapacity();- values[0] = try ls.local.exec("'a' + '\\u00e9'.repeat(100000)", null);- try logger.logTo(.js, .info, "console.log", .{ValueWriter{ .values = &values }}, &aw.writer);- const es: [(Session.max_console_bytes - 1) / 2][2]u8 = @splat("\u{e9}".*);- const expected = try std.mem.concat(testing.allocator, u8, &.{ "\n arg(1): a", std.mem.sliceAsBytes(&es), truncated_marker ++ "\n\n" });- defer testing.allocator.free(expected);- try testing.expect(std.mem.endsWith(u8, aw.written(), expected));}diff --git a/src/log.zig b/src/log.zigindex a6e6463ba..da0010299 100644--- a/src/log.zig+++ b/src/log.zig@@ -19,6 +19,8 @@const std = @import("std");const lp = @import("lightpanda");+const truncateUtf8 = @import("string.zig").truncateUtf8;+threadlocal var current_page: ?*PageContext = null;pub const Scope = enum {@@ -425,16 +427,6 @@ pub const KV = struct {const Colored = struct {code: []const u8,text: []const u8,-- pub fn logFmt(self: Colored, key: []const u8, writer: LogFormatWriter) !void {- return writer.write(key, self.text);- }-- pub fn format(self: Colored, writer: *std.Io.Writer) !void {- try writeColor(self.code, writer);- try writer.writeAll(self.text);- try clearColor(writer);- }};pub fn red(text: []const u8) Colored {@@ -453,6 +445,7 @@ const Value = union(enum) {float32: f32,float64: f64,boolean: bool,+ colored: *const Colored,formatter: Formatter,log_fmt: LogFmt,@@ -490,6 +483,10 @@ const Value = union(enum) {fn initRuntime(vp: anytype) Value {const T = @TypeOf(vp.*);+ if (comptime T == Colored) {+ return .{ .colored = vp };+ }+if (comptime std.meta.hasMethod(T, "logFmt")) {const Thunk = struct {fn logFmt(ptr: *const anyopaque, key: []const u8, writer: *std.Io.Writer) anyerror!void {@@ -582,9 +579,45 @@ fn writeErased(format: Format, value: Value, writer: *std.Io.Writer) !void {.float32 => |n| return writer.print("{d}", .{n}),.float64 => |n| return writer.print("{d}", .{n}),.boolean => |b| return writer.writeAll(if (b) "true" else "false"),- .formatter => |f| return f.writeFn(f.ptr, writer),- .log_fmt => |f| return f.writeFn(f.ptr, writer),+ .colored => |c| {+ if (format == .logfmt) {+ return writeString(format, c.text, writer);+ }+ try writeColor(c.code, writer);+ try writeString(format, c.text, writer);+ return clearColor(writer);+ },+ .formatter => |f| return writeFormatted(format, f.ptr, f.writeFn, writer),+ .log_fmt => |f| return writeFormatted(format, f.ptr, f.writeFn, writer),+ }+}++// Formatted values can hold page text (console arguments, URLs, exception+// messages), so they're rendered and then escaped like any other string.+const max_value_len = 8 * 1024;+const truncated_marker = "...[truncated]";++fn writeFormatted(+ format: Format,+ ptr: *const anyopaque,+ writeFn: *const fn (ptr: *const anyopaque, writer: *std.Io.Writer) anyerror!void,+ writer: *std.Io.Writer,+) !void {+ var buf: [max_value_len + truncated_marker.len]u8 = undefined;+ // One byte over the cap, so truncateUtf8 can tell whether the cut splits a code point.+ var fixed: std.Io.Writer = .fixed(buf[0 .. max_value_len + 1]);+ writeFn(ptr, &fixed) catch |e| {+ if (e != error.WriteFailed) {+ return e;+ }+ };+ const value = fixed.buffered();+ if (value.len <= max_value_len) {+ return writeString(format, value, writer);}+ const kept = truncateUtf8(value, max_value_len).len;+ @memcpy(buf[kept..][0..truncated_marker.len], truncated_marker);+ return writeString(format, buf[0 .. kept + truncated_marker.len], writer);}fn writeString(format: Format, value: []const u8, writer: *std.Io.Writer) !void {@@ -640,7 +673,7 @@ fn writeString(format: Format, value: []const u8, writer: *std.Io.Writer) !void// Control characters other than \n and \t are written as \xNN, so a logged// value can't emit terminal escape sequences.-pub fn writePrettyString(value: []const u8, writer: *std.Io.Writer) !void {+fn writePrettyString(value: []const u8, writer: *std.Io.Writer) !void {var start: usize = 0;for (value, 0..) |b, i| {if (!std.ascii.isControl(b) or b == '\n' or b == '\t') {@@ -836,6 +869,38 @@ test "log: string escape" {}}+test "log: formatted values are escaped and capped" {+ const Text = struct {+ text: []const u8,+ pub fn format(self: @This(), writer: *std.Io.Writer) !void {+ return writer.writeAll(self.text);+ }+ };++ opts.format = .logfmt;+ defer opts.format = .pretty;++ var aw = std.Io.Writer.Allocating.init(testing.allocator);+ defer aw.deinit();++ try logTo(.app, .err, "test", .{ .v = Text{ .text = "one\nx level=fatal" }, .esc = Text{ .text = "\x1b[31m" } }, &aw.writer);+ try testing.expectEqual("$time=1739795092929 $scope=app $level=error $msg=\"test\" v=\"one\\nx level=fatal\" esc=G1szMW0\n", aw.written());++ aw.clearRetainingCapacity();+ try writeValue(.pretty, Text{ .text = "a\x1b[31mb" }, &aw.writer);+ try testing.expectEqual("a\\x1b[31mb", aw.written());++ // The cut keeps whole code points: 'a' + two-byte ones.+ aw.clearRetainingCapacity();+ const es: [max_value_len / 2 + 10][2]u8 = @splat("\u{e9}".*);+ const long = try std.mem.concat(testing.allocator, u8, &.{ "a", std.mem.sliceAsBytes(&es) });+ defer testing.allocator.free(long);+ try writeValue(.pretty, Text{ .text = long }, &aw.writer);+ const out = aw.written();+ try testing.expectEqual(long[0 .. max_value_len - 1], out[0 .. out.len - truncated_marker.len]);+ try testing.expectEqual(truncated_marker, out[out.len - truncated_marker.len ..]);+}+test "log: page context" {opts.format = .logfmt;defer opts.format = .pretty;