Skip to content

Commit 769d99e

Browse files
committed
Tweak debug logging
1 - Add a log_level build option to control the default log level from the build (e.g. -Dlog_level=debug). Defaults to info 2 - Add a new boolean log_unknown_properties build option to enable logging unknown properties. Defautls to false. 3 - Remove the log debug for script eval - this can be a huge value (i.e. hundreds of KB), which makes the debug log unusable IMO.
1 parent f95defe commit 769d99e

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

build.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ pub fn build(b: *std.Build) !void {
4444
b.option([]const u8, "git_commit", "Current git commit") orelse "dev",
4545
);
4646

47+
opts.addOption(
48+
std.log.Level,
49+
"log_level",
50+
b.option(std.log.Level, "log_level", "The log level") orelse std.log.Level.info,
51+
);
52+
53+
opts.addOption(
54+
bool,
55+
"log_unknown_properties",
56+
b.option(bool, "log_unknown_properties", "Log access to unknown properties") orelse false,
57+
);
58+
4759
const target = b.standardTargetOptions(.{});
4860
const optimize = b.standardOptimizeOption(.{});
4961

@@ -175,7 +187,7 @@ fn common(b: *std.Build, opts: *std.Build.Step.Options, step: *std.Build.Step.Co
175187
else => {},
176188
}
177189

178-
mod.addImport("build_info", opts.createModule());
190+
mod.addImport("build_config", opts.createModule());
179191
mod.addObjectFile(mod.owner.path(lib_path));
180192
}
181193

src/browser/page.zig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,7 @@ pub const Page = struct {
647647
}
648648
return error.JsErr;
649649
};
650-
651-
if (builtin.mode == .Debug) {
652-
const msg = try res.toString(page.arena);
653-
log.debug("eval script {s}: {s}", .{ src, msg });
654-
}
650+
_ = res;
655651

656652
if (self.onload) |onload| {
657653
_ = page.scope.exec(onload, "script_on_load") catch {

src/main.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ const App = @import("app.zig").App;
2525
const Platform = @import("runtime/js.zig").Platform;
2626
const Browser = @import("browser/browser.zig").Browser;
2727

28+
const build_config = @import("build_config");
2829
const parser = @import("browser/netsurf.zig");
29-
const version = @import("build_info").git_commit;
3030

3131
const log = std.log.scoped(.cli);
3232

3333
pub const std_options = std.Options{
3434
// Set the log level to info
35-
.log_level = .info,
35+
.log_level = @enumFromInt(@intFromEnum(build_config.log_level)),
3636

3737
// Define logFn to override the std implementation
3838
.logFn = logFn,
@@ -59,7 +59,7 @@ pub fn main() !void {
5959
return std.process.cleanExit();
6060
},
6161
.version => {
62-
std.debug.print("{s}\n", .{version});
62+
std.debug.print("{s}\n", .{build_config.git_commit});
6363
return std.process.cleanExit();
6464
},
6565
else => {},

src/runtime/js.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,9 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
14071407

14081408
fn generateNamedIndexer(comptime Struct: type, template_proto: v8.ObjectTemplate) void {
14091409
if (@hasDecl(Struct, "named_get") == false) {
1410+
if (comptime @import("build_config").log_unknown_properties) {
1411+
generateDebugNamedIndexer(Struct, template_proto);
1412+
}
14101413
return;
14111414
}
14121415
const configuration = v8.NamedPropertyHandlerConfiguration{
@@ -1441,6 +1444,31 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
14411444
template_proto.setNamedProperty(configuration, null);
14421445
}
14431446

1447+
fn generateDebugNamedIndexer(comptime Struct: type, template_proto: v8.ObjectTemplate) void {
1448+
const configuration = v8.NamedPropertyHandlerConfiguration{
1449+
.getter = struct {
1450+
fn callback(c_name: ?*const v8.C_Name, raw_info: ?*const v8.C_PropertyCallbackInfo) callconv(.c) u8 {
1451+
const info = v8.PropertyCallbackInfo.initFromV8(raw_info);
1452+
const isolate = info.getIsolate();
1453+
const context = isolate.getCurrentContext();
1454+
1455+
const scope: *Scope = @ptrFromInt(context.getEmbedderData(1).castTo(v8.BigInt).getUint64());
1456+
1457+
const property = valueToString(scope.call_arena, .{ .handle = c_name.? }, isolate, context) catch "???";
1458+
log.debug("unknwon named property {s}.{s}", .{ @typeName(Struct), property });
1459+
return v8.Intercepted.No;
1460+
}
1461+
}.callback,
1462+
1463+
// This is really cool. Without this, we'd intercept _all_ properties
1464+
// even those explicitly set. So, node.length for example would get routed
1465+
// to our `named_get`, rather than a `get_length`. This might be
1466+
// useful if we run into a type that we can't model properly in Zig.
1467+
.flags = v8.PropertyHandlerFlags.OnlyInterceptStrings | v8.PropertyHandlerFlags.NonMasking,
1468+
};
1469+
template_proto.setNamedProperty(configuration, null);
1470+
}
1471+
14441472
fn generateUndetectable(comptime Struct: type, template: v8.ObjectTemplate) void {
14451473
const has_js_call_as_function = @hasDecl(Struct, "jsCallAsFunction");
14461474

src/telemetry/lightpanda.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
3-
const build_info = @import("build_info");
3+
const build_config = @import("build_config");
44

55
const Thread = std.Thread;
66
const Allocator = std.mem.Allocator;
@@ -151,7 +151,7 @@ const LightPandaEvent = struct {
151151
try writer.write(builtin.cpu.arch);
152152

153153
try writer.objectField("version");
154-
try writer.write(build_info.git_commit);
154+
try writer.write(build_config.git_commit);
155155

156156
try writer.objectField("event");
157157
try writer.write(@tagName(std.meta.activeTag(self.event)));

0 commit comments

Comments
 (0)