@@ -22,7 +22,7 @@ const builtin = @import("builtin");
2222const JsObject = @import ("../env.zig" ).Env .JsObject ;
2323const SessionState = @import ("../env.zig" ).SessionState ;
2424
25- const log = if (builtin .is_test ) & test_capture else std . log . scoped ( .console );
25+ const log = if (builtin .is_test ) & test_capture else @import ( "../../log.zig" );
2626
2727pub const Console = struct {
2828 // TODO: configurable writer
@@ -33,7 +33,7 @@ pub const Console = struct {
3333 if (values .len == 0 ) {
3434 return ;
3535 }
36- log .info ("{s} " , .{try serializeValues (values , state )});
36+ log .info (.console , "info " , .{ . args = try serializeValues (values , state ) });
3737 }
3838
3939 pub fn _info (console : * const Console , values : []JsObject , state : * SessionState ) ! void {
@@ -44,21 +44,21 @@ pub const Console = struct {
4444 if (values .len == 0 ) {
4545 return ;
4646 }
47- log .debug ("{s} " , .{try serializeValues (values , state )});
47+ log .debug (.console , "debug " , .{ . args = try serializeValues (values , state ) });
4848 }
4949
5050 pub fn _warn (_ : * const Console , values : []JsObject , state : * SessionState ) ! void {
5151 if (values .len == 0 ) {
5252 return ;
5353 }
54- log .warn ("{s} " , .{try serializeValues (values , state )});
54+ log .warn (.console , "warn " , .{ . args = try serializeValues (values , state ) });
5555 }
5656
5757 pub fn _error (_ : * const Console , values : []JsObject , state : * SessionState ) ! void {
5858 if (values .len == 0 ) {
5959 return ;
6060 }
61- log .err ( "{s} " , .{try serializeValues (values , state )});
61+ log .info ( .console , "error " , .{ . args = try serializeValues (values , state ) });
6262 }
6363
6464 pub fn _clear (_ : * const Console ) void {}
@@ -77,25 +77,24 @@ pub const Console = struct {
7777 const count = current + 1 ;
7878 gop .value_ptr .* = count ;
7979
80- log .info ("{s}: {d}" , .{ label , count });
80+ log .info (.console , "count" , .{ . label = label , . count = count });
8181 }
8282
8383 pub fn _countReset (self : * Console , label_ : ? []const u8 ) ! void {
8484 const label = label_ orelse "default" ;
8585 const kv = self .counts .fetchRemove (label ) orelse {
86- log .warn ( "Counter \" {s} \" doesn't exist. " , .{label });
86+ log .info ( .console , "invalid counter " , .{ . label = label });
8787 return ;
8888 };
89-
90- log .info ("{s}: {d}" , .{ label , kv .value });
89+ log .info (.console , "count reset" , .{ .label = label , .count = kv .value });
9190 }
9291
9392 pub fn _time (self : * Console , label_ : ? []const u8 , state : * SessionState ) ! void {
9493 const label = label_ orelse "default" ;
9594 const gop = try self .timers .getOrPut (state .arena , label );
9695
9796 if (gop .found_existing ) {
98- log .warn ( "Timer \" {s} \" already exists. " , .{label });
97+ log .info ( .console , "duplicate timer " , .{ . label = label });
9998 return ;
10099 }
101100 gop .key_ptr .* = try state .arena .dupe (u8 , label );
@@ -106,22 +105,21 @@ pub const Console = struct {
106105 const elapsed = timestamp ();
107106 const label = label_ orelse "default" ;
108107 const start = self .timers .get (label ) orelse {
109- log .warn ( "Timer \" {s} \" doesn't exist. " , .{label });
108+ log .info ( .console , "invalid timer " , .{ . label = label });
110109 return ;
111110 };
112-
113- log .info ("\" {s}\" : {d}ms" , .{ label , elapsed - start });
111+ log .info (.console , "timer" , .{ .label = label , .elapsed = elapsed - start });
114112 }
115113
116114 pub fn _timeStop (self : * Console , label_ : ? []const u8 ) void {
117115 const elapsed = timestamp ();
118116 const label = label_ orelse "default" ;
119117 const kv = self .timers .fetchRemove (label ) orelse {
120- log .warn ( "Timer \" {s} \" doesn't exist. " , .{label });
118+ log .info ( .console , "invalid timer " , .{ . label = label });
121119 return ;
122120 };
123121
124- log .info ( " \" {s} \" : {d}ms - timer ended " , .{ label , elapsed - kv .value });
122+ log .warn ( .console , " timer stop " , .{ . label = label , . elapsed = elapsed - kv .value });
125123 }
126124
127125 pub fn _assert (_ : * Console , assertion : JsObject , values : []JsObject , state : * SessionState ) ! void {
@@ -132,7 +130,7 @@ pub const Console = struct {
132130 if (values .len > 0 ) {
133131 serialized_values = try serializeValues (values , state );
134132 }
135- log .err ( "Assertion failed: {s} " , .{serialized_values });
133+ log .info ( .console , "assertion failed" , .{ . values = serialized_values });
136134 }
137135
138136 fn serializeValues (values : []JsObject , state : * SessionState ) ! []const u8 {
@@ -155,20 +153,20 @@ fn timestamp() u32 {
155153var test_capture = TestCapture {};
156154const testing = @import ("../../testing.zig" );
157155test "Browser.Console" {
156+ defer testing .reset ();
157+
158158 var runner = try testing .jsRunner (testing .tracking_allocator , .{});
159159 defer runner .deinit ();
160160
161- defer testing .reset ();
162-
163161 {
164162 try runner .testCases (&.{
165163 .{ "console.log('a')" , "undefined" },
166164 .{ "console.warn('hello world', 23, true, new Object())" , "undefined" },
167165 }, .{});
168166
169167 const captured = test_capture .captured .items ;
170- try testing .expectEqual ("a" , captured [0 ]);
171- try testing .expectEqual ("hello world 23 true [object Object]" , captured [1 ]);
168+ try testing .expectEqual ("[info] args= a" , captured [0 ]);
169+ try testing .expectEqual ("[warn] args= hello world 23 true [object Object]" , captured [1 ]);
172170 }
173171
174172 {
@@ -186,15 +184,15 @@ test "Browser.Console" {
186184 }, .{});
187185
188186 const captured = test_capture .captured .items ;
189- try testing .expectEqual ("Counter \" default \" doesn't exist. " , captured [0 ]);
190- try testing .expectEqual ("default: 1" , captured [1 ]);
191- try testing .expectEqual ("teg: 1" , captured [2 ]);
192- try testing .expectEqual ("teg: 2" , captured [3 ]);
193- try testing .expectEqual ("teg: 3" , captured [4 ]);
194- try testing .expectEqual ("default: 2" , captured [5 ]);
195- try testing .expectEqual ("teg: 3" , captured [6 ]);
196- try testing .expectEqual ("default: 2" , captured [7 ]);
197- try testing .expectEqual ("default: 1" , captured [8 ]);
187+ try testing .expectEqual ("[invalid counter] label=default " , captured [0 ]);
188+ try testing .expectEqual ("[count] label= default count= 1" , captured [1 ]);
189+ try testing .expectEqual ("[count] label= teg count= 1" , captured [2 ]);
190+ try testing .expectEqual ("[count] label= teg count= 2" , captured [3 ]);
191+ try testing .expectEqual ("[count] label= teg count= 3" , captured [4 ]);
192+ try testing .expectEqual ("[count] label= default count= 2" , captured [5 ]);
193+ try testing .expectEqual ("[count reset] label= teg count= 3" , captured [6 ]);
194+ try testing .expectEqual ("[count reset] label= default count= 2" , captured [7 ]);
195+ try testing .expectEqual ("[count] label= default count= 1" , captured [8 ]);
198196 }
199197
200198 {
@@ -208,33 +206,81 @@ test "Browser.Console" {
208206 }, .{});
209207
210208 const captured = test_capture .captured .items ;
211- try testing .expectEqual ("Assertion failed: " , captured [0 ]);
212- try testing .expectEqual ("Assertion failed: x true" , captured [1 ]);
213- try testing .expectEqual ("Assertion failed: x" , captured [2 ]);
209+ try testing .expectEqual ("[assertion failed] values= " , captured [0 ]);
210+ try testing .expectEqual ("[assertion failed] values= x true" , captured [1 ]);
211+ try testing .expectEqual ("[assertion failed] values= x" , captured [2 ]);
214212 }
215213}
216-
217214const TestCapture = struct {
218215 captured : std .ArrayListUnmanaged ([]const u8 ) = .{},
219216
220217 fn reset (self : * TestCapture ) void {
221218 self .captured = .{};
222219 }
223220
224- fn debug (self : * TestCapture , comptime fmt : []const u8 , args : anytype ) void {
225- const str = std .fmt .allocPrint (testing .arena_allocator , fmt , args ) catch unreachable ;
226- self .captured .append (testing .arena_allocator , str ) catch unreachable ;
221+ fn debug (
222+ self : * TestCapture ,
223+ comptime scope : @Type (.enum_literal ),
224+ comptime msg : []const u8 ,
225+ args : anytype ,
226+ ) void {
227+ self .capture (scope , msg , args );
228+ }
229+
230+ fn info (
231+ self : * TestCapture ,
232+ comptime scope : @Type (.enum_literal ),
233+ comptime msg : []const u8 ,
234+ args : anytype ,
235+ ) void {
236+ self .capture (scope , msg , args );
227237 }
228238
229- fn info (self : * TestCapture , comptime fmt : []const u8 , args : anytype ) void {
230- self .debug (fmt , args );
239+ fn warn (
240+ self : * TestCapture ,
241+ comptime scope : @Type (.enum_literal ),
242+ comptime msg : []const u8 ,
243+ args : anytype ,
244+ ) void {
245+ self .capture (scope , msg , args );
231246 }
232247
233- fn warn (self : * TestCapture , comptime fmt : []const u8 , args : anytype ) void {
234- self .debug (fmt , args );
248+ fn err (
249+ self : * TestCapture ,
250+ comptime scope : @Type (.enum_literal ),
251+ comptime msg : []const u8 ,
252+ args : anytype ,
253+ ) void {
254+ self .capture (scope , msg , args );
235255 }
236256
237- fn err (self : * TestCapture , comptime fmt : []const u8 , args : anytype ) void {
238- self .debug (fmt , args );
257+ fn capture (
258+ self : * TestCapture ,
259+ comptime scope : @Type (.enum_literal ),
260+ comptime msg : []const u8 ,
261+ args : anytype ,
262+ ) void {
263+ self ._capture (scope , msg , args ) catch unreachable ;
264+ }
265+
266+ fn _capture (
267+ self : * TestCapture ,
268+ comptime scope : @Type (.enum_literal ),
269+ comptime msg : []const u8 ,
270+ args : anytype ,
271+ ) ! void {
272+ std .debug .assert (scope == .console );
273+
274+ const allocator = testing .arena_allocator ;
275+ var buf : std .ArrayListUnmanaged (u8 ) = .empty ;
276+ try buf .appendSlice (allocator , "[" ++ msg ++ "] " );
277+
278+ inline for (@typeInfo (@TypeOf (args )).@"struct" .fields ) | f | {
279+ try buf .appendSlice (allocator , f .name );
280+ try buf .append (allocator , '=' );
281+ try @import ("../../log.zig" ).writeValue (allocator , & buf , false , @field (args , f .name ));
282+ try buf .append (allocator , ' ' );
283+ }
284+ self .captured .append (testing .arena_allocator , std .mem .trimRight (u8 , buf .items , " " )) catch unreachable ;
239285 }
240286};
0 commit comments