server, cdp: improve logging

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-10-15 22:57:56 +02:00
parent 84c49fbe34
commit 8e05f09fc8
12 changed files with 180 additions and 45 deletions

View File

@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
const result = cdp.result;
const getMsg = cdp.getMsg;
const log = std.log.scoped(.cdp);
const Methods = enum {
getVersion,
setDownloadBehavior,
@@ -64,6 +66,7 @@ fn getVersion(
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "browser.getVersion" });
// ouput
const Res = struct {
@@ -92,6 +95,7 @@ fn setDownloadBehavior(
eventsEnabled: ?bool = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("REQ > id {d}, method {s}", .{ msg.id, "browser.setDownloadBehavior" });
// output
return result(alloc, msg.id, null, null, null);
@@ -113,6 +117,7 @@ fn getWindowForTarget(
};
const msg = try cdp.getMsg(alloc, _id, ?Params, scanner);
std.debug.assert(msg.sessionID != null);
log.debug("Req > id {d}, method {s}", .{ msg.id, "browser.getWindowForTarget" });
// output
const Resp = struct {
@@ -138,6 +143,7 @@ fn setWindowBounds(
// input
const msg = try cdp.getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "browser.setWindowBounds" });
// output
return result(alloc, msg.id, null, null, msg.sessionID);

View File

@@ -31,6 +31,8 @@ const emulation = @import("emulation.zig").emulation;
const fetch = @import("fetch.zig").fetch;
const performance = @import("performance.zig").performance;
const log_cdp = std.log.scoped(.cdp);
pub const Error = error{
UnknonwDomain,
UnknownMethod,
@@ -95,7 +97,6 @@ pub fn do(
return error.WrongTokenType;
}
const method_name = method_token.string;
std.log.debug("cmd: method {s}, id {any}", .{ method_name, id });
// retrieve domain from method
var iter = std.mem.splitScalar(u8, method_name, '.');
@@ -154,7 +155,6 @@ pub fn dumpFile(
std.debug.assert(nb == script.len);
const p = try dir.realpathAlloc(alloc, name);
defer alloc.free(p);
std.log.debug("Script {d} saved at {s}", .{ id, p });
}
fn checkKey(key: []const u8, token: []const u8) !void {
@@ -186,6 +186,10 @@ pub fn result(
res: anytype,
sessionID: ?[]const u8,
) ![]const u8 {
log_cdp.debug(
"Res > id {d}, sessionID {?s}, result {any}",
.{ id, sessionID, res },
);
if (T == null) {
// No need to stringify a custom JSON msg, just use string templates
if (sessionID) |sID| {
@@ -212,6 +216,7 @@ pub fn sendEvent(
params: T,
sessionID: ?[]const u8,
) !void {
log_cdp.debug("Event > method {s}, sessionID {?s}", .{ name, sessionID });
const Resp = struct {
method: []const u8,
params: T,
@@ -221,7 +226,6 @@ pub fn sendEvent(
const event_msg = try stringify(alloc, resp);
defer alloc.free(event_msg);
std.log.debug("event {s}", .{event_msg});
try server.sendSync(ctx, event_msg);
}
@@ -307,10 +311,6 @@ pub fn getMsg(
}
// end
std.log.debug(
"id {any}, params {any}, sessionID: {any}, token {any}",
.{ id, params, sessionID, t },
);
t = try scanner.next();
if (t != .end_of_document) return error.CDPMsgEnd;

View File

@@ -25,6 +25,8 @@ const result = cdp.result;
const getMsg = cdp.getMsg;
const stringify = cdp.stringify;
const log = std.log.scoped(.cdp);
const Methods = enum {
setEmulatedMedia,
setFocusEmulationEnabled,
@@ -68,6 +70,7 @@ fn setEmulatedMedia(
features: ?[]MediaFeature = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setEmulatedMedia" });
// output
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -86,6 +89,7 @@ fn setFocusEmulationEnabled(
enabled: bool,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setFocusEmulationEnabled" });
// output
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -101,6 +105,7 @@ fn setDeviceMetricsOverride(
// input
const msg = try cdp.getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setDeviceMetricsOverride" });
// output
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -114,6 +119,7 @@ fn setTouchEmulationEnabled(
_: *Ctx,
) ![]const u8 {
const msg = try cdp.getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "emulation.setTouchEmulationEnabled" });
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
const result = cdp.result;
const getMsg = cdp.getMsg;
const log = std.log.scoped(.cdp);
const Methods = enum {
disable,
};
@@ -51,6 +53,7 @@ fn disable(
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "fetch.disable" });
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -25,6 +25,8 @@ const result = cdp.result;
const getMsg = cdp.getMsg;
const stringify = cdp.stringify;
const log_cdp = std.log.scoped(.cdp);
const Methods = enum {
enable,
};
@@ -51,6 +53,7 @@ fn enable(
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, _id, void, scanner);
log_cdp.debug("Req > id {d}, method {s}", .{ msg.id, "log.enable" });
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
const result = cdp.result;
const getMsg = cdp.getMsg;
const log = std.log.scoped(.cdp);
const Methods = enum {
enable,
setCacheDisabled,
@@ -51,7 +53,10 @@ fn enable(
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "network.enable" });
return result(alloc, msg.id, null, null, msg.sessionID);
}
@@ -63,7 +68,10 @@ fn setCacheDisabled(
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "network.setCacheDisabled" });
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -26,6 +26,8 @@ const getMsg = cdp.getMsg;
const stringify = cdp.stringify;
const sendEvent = cdp.sendEvent;
const log = std.log.scoped(.cdp);
const Runtime = @import("runtime.zig");
const Methods = enum {
@@ -62,7 +64,11 @@ fn enable(
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.enable" });
return result(alloc, msg.id, null, null, msg.sessionID);
}
@@ -87,13 +93,36 @@ fn getFrameTree(
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
const msg = try cdp.getMsg(alloc, _id, void, scanner);
// input
const msg = try cdp.getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.getFrameTree" });
// output
const FrameTree = struct {
frameTree: struct {
frame: Frame,
},
childFrames: ?[]@This() = null,
pub fn format(
self: @This(),
comptime _: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
try writer.writeAll("cdp.page.getFrameTree { ");
try writer.writeAll(".frameTree = { ");
try writer.writeAll(".frame = { ");
const frame = self.frameTree.frame;
try writer.writeAll(".id = ");
try std.fmt.formatText(frame.id, "s", options, writer);
try writer.writeAll(", .loaderId = ");
try std.fmt.formatText(frame.loaderId, "s", options, writer);
try writer.writeAll(", .url = ");
try std.fmt.formatText(frame.url, "s", options, writer);
try writer.writeAll(" } } }");
}
};
const frameTree = FrameTree{
.frameTree = .{
@@ -121,6 +150,7 @@ fn setLifecycleEventsEnabled(
enabled: bool,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.setLifecycleEventsEnabled" });
ctx.state.page_life_cycle_events = true;
@@ -151,10 +181,23 @@ fn addScriptToEvaluateOnNewDocument(
runImmediately: bool = false,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.addScriptToEvaluateOnNewDocument" });
// output
const Res = struct {
identifier: []const u8 = "1",
pub fn format(
self: @This(),
comptime _: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
try writer.writeAll("cdp.page.addScriptToEvaluateOnNewDocument { ");
try writer.writeAll(".identifier = ");
try std.fmt.formatText(self.identifier, "s", options, writer);
try writer.writeAll(" }");
}
};
return result(alloc, msg.id, Res, Res{}, msg.sessionID);
}
@@ -175,6 +218,7 @@ fn createIsolatedWorld(
};
const msg = try getMsg(alloc, _id, Params, scanner);
std.debug.assert(msg.sessionID != null);
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.createIsolatedWorld" });
const params = msg.params.?;
// noop executionContextCreated event
@@ -219,6 +263,7 @@ fn navigate(
};
const msg = try getMsg(alloc, _id, Params, scanner);
std.debug.assert(msg.sessionID != null);
log.debug("Req > id {d}, method {s}", .{ msg.id, "page.navigate" });
const params = msg.params.?;
// change state
@@ -264,6 +309,22 @@ fn navigate(
frameId: []const u8,
loaderId: ?[]const u8,
errorText: ?[]const u8 = null,
pub fn format(
self: @This(),
comptime _: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
try writer.writeAll("cdp.page.navigate.Resp { ");
try writer.writeAll(".frameId = ");
try std.fmt.formatText(self.frameId, "s", options, writer);
if (self.loaderId) |loaderId| {
try writer.writeAll(", .loaderId = '");
try std.fmt.formatText(loaderId, "s", options, writer);
}
try writer.writeAll(" }");
}
};
const resp = Resp{
.frameId = ctx.state.frameID,
@@ -271,7 +332,6 @@ fn navigate(
};
const res = try result(alloc, msg.id, Resp, resp, msg.sessionID);
defer alloc.free(res);
std.log.debug("res {s}", .{res});
try server.sendSync(ctx, res);
// TODO: at this point do we need async the following actions to be async?

View File

@@ -24,6 +24,8 @@ const cdp = @import("cdp.zig");
const result = cdp.result;
const getMsg = cdp.getMsg;
const log = std.log.scoped(.cdp);
const Methods = enum {
enable,
};
@@ -49,7 +51,10 @@ fn enable(
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "performance.enable" });
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -28,6 +28,8 @@ const result = cdp.result;
const getMsg = cdp.getMsg;
const stringify = cdp.stringify;
const log = std.log.scoped(.cdp);
const Methods = enum {
enable,
runIfWaitingForDebugger,
@@ -80,6 +82,7 @@ fn sendInspector(
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s} (script saved on cache)", .{ msg.id, "runtime.evaluate" });
const params = msg.params.?;
script = params.expression;
id = msg.id;
@@ -98,6 +101,7 @@ fn sendInspector(
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s} (script saved on cache)", .{ msg.id, "runtime.callFunctionOn" });
const params = msg.params.?;
script = params.functionDeclaration;
id = msg.id;
@@ -166,7 +170,10 @@ fn runIfWaitingForDebugger(
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "runtime.runIfWaitingForDebugger" });
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -25,6 +25,8 @@ const result = cdp.result;
const getMsg = cdp.getMsg;
const stringify = cdp.stringify;
const log = std.log.scoped(.cdp);
const Methods = enum {
setDiscoverTargets,
setAutoAttach,
@@ -72,6 +74,7 @@ fn setDiscoverTargets(
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.setDiscoverTargets" });
// output
return result(alloc, msg.id, null, null, msg.sessionID);
@@ -112,7 +115,7 @@ fn setAutoAttach(
filter: ?[]TargetFilter = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
std.log.debug("params {any}", .{msg.params});
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.setAutoAttach" });
// attachedToTarget event
if (msg.sessionID == null) {
@@ -144,6 +147,7 @@ fn getTargetInfo(
targetId: ?[]const u8 = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.getTargetInfo" });
// output
const TargetInfo = struct {
@@ -178,6 +182,7 @@ fn getBrowserContexts(
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.getBrowserContexts" });
// ouptut
const Resp = struct {
@@ -212,12 +217,25 @@ fn createBrowserContext(
originsWithUniversalNetworkAccess: ?[][]const u8 = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.createBrowserContext" });
ctx.state.contextID = ContextID;
// output
const Resp = struct {
browserContextId: []const u8 = ContextID,
pub fn format(
self: @This(),
comptime _: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
try writer.writeAll("cdp.target.createBrowserContext { ");
try writer.writeAll(".browserContextId = ");
try std.fmt.formatText(self.browserContextId, "s", options, writer);
try writer.writeAll(" }");
}
};
return result(alloc, msg.id, Resp, Resp{}, msg.sessionID);
}
@@ -234,6 +252,7 @@ fn disposeBrowserContext(
browserContextId: []const u8,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.disposeBrowserContext" });
// output
const res = try result(alloc, msg.id, null, .{}, null);
@@ -266,6 +285,7 @@ fn createTarget(
forTab: ?bool = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.createTarget" });
// change CDP state
ctx.state.frameID = TargetID;
@@ -290,6 +310,18 @@ fn createTarget(
// output
const Resp = struct {
targetId: []const u8 = TargetID,
pub fn format(
self: @This(),
comptime _: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
try writer.writeAll("cdp.target.createTarget { ");
try writer.writeAll(".targetId = ");
try std.fmt.formatText(self.targetId, "s", options, writer);
try writer.writeAll(" }");
}
};
return result(alloc, msg.id, Resp, Resp{}, msg.sessionID);
}
@@ -306,6 +338,7 @@ fn closeTarget(
targetId: []const u8,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "target.closeTarget" });
// output
const Resp = struct {