1919const std = @import ("std" );
2020const builtin = @import ("builtin" );
2121
22- const JsObject = @import ( "../env.zig" ). Env . JsObject ;
22+ const Allocator = std . mem . Allocator ;
2323const Page = @import ("../page.zig" ).Page ;
24+ const JsObject = @import ("../env.zig" ).Env .JsObject ;
2425
2526const log = if (builtin .is_test ) & test_capture else @import ("../../log.zig" );
2627
@@ -29,6 +30,13 @@ pub const Console = struct {
2930 timers : std .StringHashMapUnmanaged (u32 ) = .{},
3031 counts : std .StringHashMapUnmanaged (u32 ) = .{},
3132
33+ pub fn _lp (_ : * const Console , values : []JsObject , page : * Page ) ! void {
34+ if (values .len == 0 ) {
35+ return ;
36+ }
37+ log .fatal (.console , "lightpanda" , .{ .args = try serializeValues (values , page ) });
38+ }
39+
3240 pub fn _log (_ : * const Console , values : []JsObject , page : * Page ) ! void {
3341 if (values .len == 0 ) {
3442 return ;
@@ -134,12 +142,19 @@ pub const Console = struct {
134142 }
135143
136144 fn serializeValues (values : []JsObject , page : * Page ) ! []const u8 {
145+ if (values .len == 0 ) {
146+ return "" ;
147+ }
148+
137149 const arena = page .call_arena ;
150+ const separator = log .separator ();
138151 var arr : std .ArrayListUnmanaged (u8 ) = .{};
139- try arr .appendSlice (arena , try values [0 ].toString ());
140- for (values [1.. ]) | value | {
141- try arr .append (arena , ' ' );
142- try arr .appendSlice (arena , try value .toString ());
152+
153+ for (values , 1.. ) | value , i | {
154+ try arr .appendSlice (arena , separator );
155+ try arr .writer (arena ).print ("{d}: " , .{i });
156+ const serialized = if (builtin .mode == .Debug ) value .toDetailString () else value .toString ();
157+ try arr .appendSlice (arena , try serialized );
143158 }
144159 return arr .items ;
145160 }
@@ -165,8 +180,8 @@ test "Browser.Console" {
165180 }, .{});
166181
167182 const captured = test_capture .captured .items ;
168- try testing .expectEqual ("[info] args=a" , captured [0 ]);
169- try testing .expectEqual ("[warn] args=hello world 23 true [object Object] " , captured [1 ]);
183+ try testing .expectEqual ("[info] args= 1: a" , captured [0 ]);
184+ try testing .expectEqual ("[warn] args= 1: hello world 2: 23 3: true 4: #< Object> " , captured [1 ]);
170185 }
171186
172187 {
@@ -207,13 +222,17 @@ test "Browser.Console" {
207222
208223 const captured = test_capture .captured .items ;
209224 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 ]);
225+ try testing .expectEqual ("[assertion failed] values= 1: x 2: true" , captured [1 ]);
226+ try testing .expectEqual ("[assertion failed] values= 1: x" , captured [2 ]);
212227 }
213228}
214229const TestCapture = struct {
215230 captured : std .ArrayListUnmanaged ([]const u8 ) = .{},
216231
232+ fn separator (_ : * const TestCapture ) []const u8 {
233+ return " " ;
234+ }
235+
217236 fn reset (self : * TestCapture ) void {
218237 self .captured = .{};
219238 }
@@ -254,6 +273,15 @@ const TestCapture = struct {
254273 self .capture (scope , msg , args );
255274 }
256275
276+ fn fatal (
277+ self : * TestCapture ,
278+ comptime scope : @Type (.enum_literal ),
279+ comptime msg : []const u8 ,
280+ args : anytype ,
281+ ) void {
282+ self .capture (scope , msg , args );
283+ }
284+
257285 fn capture (
258286 self : * TestCapture ,
259287 comptime scope : @Type (.enum_literal ),
@@ -278,7 +306,7 @@ const TestCapture = struct {
278306 inline for (@typeInfo (@TypeOf (args )).@"struct" .fields ) | f | {
279307 try buf .appendSlice (allocator , f .name );
280308 try buf .append (allocator , '=' );
281- try @import ("../../log.zig" ).writeValue (false , @field (args , f .name ), buf .writer (allocator ));
309+ try @import ("../../log.zig" ).writeValue (.pretty , @field (args , f .name ), buf .writer (allocator ));
282310 try buf .append (allocator , ' ' );
283311 }
284312 self .captured .append (testing .arena_allocator , std .mem .trimRight (u8 , buf .items , " " )) catch unreachable ;
0 commit comments