Skip to content

Commit 8e05f09

Browse files
server, cdp: improve logging
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 84c49fb commit 8e05f09

File tree

12 files changed

+179
-44
lines changed

12 files changed

+179
-44
lines changed

src/cdp/browser.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
2424
const result = cdp.result;
2525
const getMsg = cdp.getMsg;
2626

27+
const log = std.log.scoped(.cdp);
28+
2729
const Methods = enum {
2830
getVersion,
2931
setDownloadBehavior,
@@ -64,6 +66,7 @@ fn getVersion(
6466

6567
// input
6668
const msg = try getMsg(alloc, _id, void, scanner);
69+
log.debug("Req > id {d}, method {s}", .{ msg.id, "browser.getVersion" });
6770

6871
// ouput
6972
const Res = struct {
@@ -92,6 +95,7 @@ fn setDownloadBehavior(
9295
eventsEnabled: ?bool = null,
9396
};
9497
const msg = try getMsg(alloc, _id, Params, scanner);
98+
log.debug("REQ > id {d}, method {s}", .{ msg.id, "browser.setDownloadBehavior" });
9599

96100
// output
97101
return result(alloc, msg.id, null, null, null);
@@ -113,6 +117,7 @@ fn getWindowForTarget(
113117
};
114118
const msg = try cdp.getMsg(alloc, _id, ?Params, scanner);
115119
std.debug.assert(msg.sessionID != null);
120+
log.debug("Req > id {d}, method {s}", .{ msg.id, "browser.getWindowForTarget" });
116121

117122
// output
118123
const Resp = struct {
@@ -138,6 +143,7 @@ fn setWindowBounds(
138143

139144
// input
140145
const msg = try cdp.getMsg(alloc, _id, void, scanner);
146+
log.debug("Req > id {d}, method {s}", .{ msg.id, "browser.setWindowBounds" });
141147

142148
// output
143149
return result(alloc, msg.id, null, null, msg.sessionID);

src/cdp/cdp.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const emulation = @import("emulation.zig").emulation;
3131
const fetch = @import("fetch.zig").fetch;
3232
const performance = @import("performance.zig").performance;
3333

34+
const log_cdp = std.log.scoped(.cdp);
35+
3436
pub const Error = error{
3537
UnknonwDomain,
3638
UnknownMethod,
@@ -95,7 +97,6 @@ pub fn do(
9597
return error.WrongTokenType;
9698
}
9799
const method_name = method_token.string;
98-
std.log.debug("cmd: method {s}, id {any}", .{ method_name, id });
99100

100101
// retrieve domain from method
101102
var iter = std.mem.splitScalar(u8, method_name, '.');
@@ -154,7 +155,6 @@ pub fn dumpFile(
154155
std.debug.assert(nb == script.len);
155156
const p = try dir.realpathAlloc(alloc, name);
156157
defer alloc.free(p);
157-
std.log.debug("Script {d} saved at {s}", .{ id, p });
158158
}
159159

160160
fn checkKey(key: []const u8, token: []const u8) !void {
@@ -186,6 +186,10 @@ pub fn result(
186186
res: anytype,
187187
sessionID: ?[]const u8,
188188
) ![]const u8 {
189+
log_cdp.debug(
190+
"Res > id {d}, sessionID {?s}, result {any}",
191+
.{ id, sessionID, res },
192+
);
189193
if (T == null) {
190194
// No need to stringify a custom JSON msg, just use string templates
191195
if (sessionID) |sID| {
@@ -212,6 +216,7 @@ pub fn sendEvent(
212216
params: T,
213217
sessionID: ?[]const u8,
214218
) !void {
219+
log_cdp.debug("Event > method {s}, sessionID {?s}", .{ name, sessionID });
215220
const Resp = struct {
216221
method: []const u8,
217222
params: T,
@@ -221,7 +226,6 @@ pub fn sendEvent(
221226

222227
const event_msg = try stringify(alloc, resp);
223228
defer alloc.free(event_msg);
224-
std.log.debug("event {s}", .{event_msg});
225229
try server.sendSync(ctx, event_msg);
226230
}
227231

@@ -307,10 +311,6 @@ pub fn getMsg(
307311
}
308312

309313
// end
310-
std.log.debug(
311-
"id {any}, params {any}, sessionID: {any}, token {any}",
312-
.{ id, params, sessionID, t },
313-
);
314314
t = try scanner.next();
315315
if (t != .end_of_document) return error.CDPMsgEnd;
316316

src/cdp/emulation.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const result = cdp.result;
2525
const getMsg = cdp.getMsg;
2626
const stringify = cdp.stringify;
2727

28+
const log = std.log.scoped(.cdp);
29+
2830
const Methods = enum {
2931
setEmulatedMedia,
3032
setFocusEmulationEnabled,
@@ -68,6 +70,7 @@ fn setEmulatedMedia(
6870
features: ?[]MediaFeature = null,
6971
};
7072
const msg = try getMsg(alloc, _id, Params, scanner);
73+
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setEmulatedMedia" });
7174

7275
// output
7376
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -86,6 +89,7 @@ fn setFocusEmulationEnabled(
8689
enabled: bool,
8790
};
8891
const msg = try getMsg(alloc, _id, Params, scanner);
92+
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setFocusEmulationEnabled" });
8993

9094
// output
9195
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -101,6 +105,7 @@ fn setDeviceMetricsOverride(
101105

102106
// input
103107
const msg = try cdp.getMsg(alloc, _id, void, scanner);
108+
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setDeviceMetricsOverride" });
104109

105110
// output
106111
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -114,6 +119,7 @@ fn setTouchEmulationEnabled(
114119
_: *Ctx,
115120
) ![]const u8 {
116121
const msg = try cdp.getMsg(alloc, _id, void, scanner);
122+
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setTouchEmulationEnabled" });
117123

118124
return result(alloc, msg.id, null, null, msg.sessionID);
119125
}

src/cdp/fetch.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
2424
const result = cdp.result;
2525
const getMsg = cdp.getMsg;
2626

27+
const log = std.log.scoped(.cdp);
28+
2729
const Methods = enum {
2830
disable,
2931
};
@@ -51,6 +53,7 @@ fn disable(
5153
_: *Ctx,
5254
) ![]const u8 {
5355
const msg = try getMsg(alloc, _id, void, scanner);
56+
log.debug("Req > id {d}, method {s}", .{ msg.id, "fetch.disable" });
5457

5558
return result(alloc, msg.id, null, null, msg.sessionID);
5659
}

src/cdp/log.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const result = cdp.result;
2525
const getMsg = cdp.getMsg;
2626
const stringify = cdp.stringify;
2727

28+
const log_cdp = std.log.scoped(.cdp);
29+
2830
const Methods = enum {
2931
enable,
3032
};
@@ -51,6 +53,7 @@ fn enable(
5153
_: *Ctx,
5254
) ![]const u8 {
5355
const msg = try getMsg(alloc, _id, void, scanner);
56+
log_cdp.debug("Req > id {d}, method {s}", .{ msg.id, "log.enable" });
5457

5558
return result(alloc, msg.id, null, null, msg.sessionID);
5659
}

src/cdp/network.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
2424
const result = cdp.result;
2525
const getMsg = cdp.getMsg;
2626

27+
const log = std.log.scoped(.cdp);
28+
2729
const Methods = enum {
2830
enable,
2931
setCacheDisabled,
@@ -51,7 +53,10 @@ fn enable(
5153
scanner: *std.json.Scanner,
5254
_: *Ctx,
5355
) ![]const u8 {
56+
57+
// input
5458
const msg = try getMsg(alloc, _id, void, scanner);
59+
log.debug("Req > id {d}, method {s}", .{ msg.id, "network.enable" });
5560

5661
return result(alloc, msg.id, null, null, msg.sessionID);
5762
}
@@ -63,7 +68,10 @@ fn setCacheDisabled(
6368
scanner: *std.json.Scanner,
6469
_: *Ctx,
6570
) ![]const u8 {
71+
72+
// input
6673
const msg = try getMsg(alloc, _id, void, scanner);
74+
log.debug("Req > id {d}, method {s}", .{ msg.id, "network.setCacheDisabled" });
6775

6876
return result(alloc, msg.id, null, null, msg.sessionID);
6977
}

src/cdp/page.zig

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const getMsg = cdp.getMsg;
2626
const stringify = cdp.stringify;
2727
const sendEvent = cdp.sendEvent;
2828

29+
const log = std.log.scoped(.cdp);
30+
2931
const Runtime = @import("runtime.zig");
3032

3133
const Methods = enum {
@@ -62,7 +64,11 @@ fn enable(
6264
scanner: *std.json.Scanner,
6365
_: *Ctx,
6466
) ![]const u8 {
67+
68+
// input
6569
const msg = try getMsg(alloc, _id, void, scanner);
70+
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.enable" });
71+
6672
return result(alloc, msg.id, null, null, msg.sessionID);
6773
}
6874

@@ -87,13 +93,36 @@ fn getFrameTree(
8793
scanner: *std.json.Scanner,
8894
ctx: *Ctx,
8995
) ![]const u8 {
96+
97+
// input
9098
const msg = try cdp.getMsg(alloc, _id, void, scanner);
99+
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.getFrameTree" });
91100

101+
// output
92102
const FrameTree = struct {
93103
frameTree: struct {
94104
frame: Frame,
95105
},
96106
childFrames: ?[]@This() = null,
107+
108+
pub fn format(
109+
self: @This(),
110+
comptime _: []const u8,
111+
options: std.fmt.FormatOptions,
112+
writer: anytype,
113+
) !void {
114+
try writer.writeAll("cdp.page.getFrameTree { ");
115+
try writer.writeAll(".frameTree = { ");
116+
try writer.writeAll(".frame = { ");
117+
const frame = self.frameTree.frame;
118+
try writer.writeAll(".id = ");
119+
try std.fmt.formatText(frame.id, "s", options, writer);
120+
try writer.writeAll(", .loaderId = ");
121+
try std.fmt.formatText(frame.loaderId, "s", options, writer);
122+
try writer.writeAll(", .url = ");
123+
try std.fmt.formatText(frame.url, "s", options, writer);
124+
try writer.writeAll(" } } }");
125+
}
97126
};
98127
const frameTree = FrameTree{
99128
.frameTree = .{
@@ -121,6 +150,7 @@ fn setLifecycleEventsEnabled(
121150
enabled: bool,
122151
};
123152
const msg = try getMsg(alloc, _id, Params, scanner);
153+
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.setLifecycleEventsEnabled" });
124154

125155
ctx.state.page_life_cycle_events = true;
126156

@@ -151,10 +181,23 @@ fn addScriptToEvaluateOnNewDocument(
151181
runImmediately: bool = false,
152182
};
153183
const msg = try getMsg(alloc, _id, Params, scanner);
184+
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.addScriptToEvaluateOnNewDocument" });
154185

155186
// output
156187
const Res = struct {
157188
identifier: []const u8 = "1",
189+
190+
pub fn format(
191+
self: @This(),
192+
comptime _: []const u8,
193+
options: std.fmt.FormatOptions,
194+
writer: anytype,
195+
) !void {
196+
try writer.writeAll("cdp.page.addScriptToEvaluateOnNewDocument { ");
197+
try writer.writeAll(".identifier = ");
198+
try std.fmt.formatText(self.identifier, "s", options, writer);
199+
try writer.writeAll(" }");
200+
}
158201
};
159202
return result(alloc, msg.id, Res, Res{}, msg.sessionID);
160203
}
@@ -175,6 +218,7 @@ fn createIsolatedWorld(
175218
};
176219
const msg = try getMsg(alloc, _id, Params, scanner);
177220
std.debug.assert(msg.sessionID != null);
221+
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.createIsolatedWorld" });
178222
const params = msg.params.?;
179223

180224
// noop executionContextCreated event
@@ -219,6 +263,7 @@ fn navigate(
219263
};
220264
const msg = try getMsg(alloc, _id, Params, scanner);
221265
std.debug.assert(msg.sessionID != null);
266+
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.navigate" });
222267
const params = msg.params.?;
223268

224269
// change state
@@ -264,14 +309,29 @@ fn navigate(
264309
frameId: []const u8,
265310
loaderId: ?[]const u8,
266311
errorText: ?[]const u8 = null,
312+
313+
pub fn format(
314+
self: @This(),
315+
comptime _: []const u8,
316+
options: std.fmt.FormatOptions,
317+
writer: anytype,
318+
) !void {
319+
try writer.writeAll("cdp.page.navigate.Resp { ");
320+
try writer.writeAll(".frameId = ");
321+
try std.fmt.formatText(self.frameId, "s", options, writer);
322+
if (self.loaderId) |loaderId| {
323+
try writer.writeAll(", .loaderId = '");
324+
try std.fmt.formatText(loaderId, "s", options, writer);
325+
}
326+
try writer.writeAll(" }");
327+
}
267328
};
268329
const resp = Resp{
269330
.frameId = ctx.state.frameID,
270331
.loaderId = ctx.state.loaderID,
271332
};
272333
const res = try result(alloc, msg.id, Resp, resp, msg.sessionID);
273334
defer alloc.free(res);
274-
std.log.debug("res {s}", .{res});
275335
try server.sendSync(ctx, res);
276336

277337
// TODO: at this point do we need async the following actions to be async?

src/cdp/performance.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
2424
const result = cdp.result;
2525
const getMsg = cdp.getMsg;
2626

27+
const log = std.log.scoped(.cdp);
28+
2729
const Methods = enum {
2830
enable,
2931
};
@@ -49,7 +51,10 @@ fn enable(
4951
scanner: *std.json.Scanner,
5052
_: *Ctx,
5153
) ![]const u8 {
54+
55+
// input
5256
const msg = try getMsg(alloc, _id, void, scanner);
57+
log.debug("Req > id {d}, method {s}", .{ msg.id, "performance.enable" });
5358

5459
return result(alloc, msg.id, null, null, msg.sessionID);
5560
}

0 commit comments

Comments
 (0)