Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ pub const websocket_blocking = true;

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

pub const std_options = .{
// Set the log level to info
.log_level = .debug,

// Define logFn to override the std implementation
.logFn = logFn,
};

const usage =
\\usage: {s} [options] [URL]
\\
Expand All @@ -49,6 +57,7 @@ const usage =
\\ * otherwhise the browser starts a CDP server
\\
\\ -h, --help Print this help message and exit.
\\ --verbose Display all logs. By default only info, warn and err levels are displayed.
\\ --host Host of the CDP server (default "127.0.0.1")
\\ --port Port of the CDP server (default "9222")
\\ --timeout Timeout for incoming connections of the CDP server (in seconds, default "3")
Expand Down Expand Up @@ -110,6 +119,10 @@ const CliMode = union(CliModeTag) {
if (std.mem.eql(u8, "-h", opt) or std.mem.eql(u8, "--help", opt)) {
return printUsageExit(execname, 0);
}
if (std.mem.eql(u8, "--verbose", opt)) {
verbose = true;
continue;
}
if (std.mem.eql(u8, "--dump", opt)) {
_fetch.dump = true;
continue;
Expand Down Expand Up @@ -334,3 +347,18 @@ pub fn main() !void {
},
}
}

var verbose: bool = builtin.mode == .Debug; // In debug mode, force verbose.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big fan of this global variable but I guess it's not possible to do without, at least if we want to keep using the std lib log function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think have a global var in the main.zig is not perfect, but ok.

fn logFn(
comptime level: std.log.Level,
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
if (!verbose) {
// hide all messages with level greater of equal to debug level.
if (@intFromEnum(level) >= @intFromEnum(std.log.Level.debug)) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As debug is the last level I think checking an equality should be more clear. But of course like that you can't handle addition lowers levels. So it's also good to me like.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But Since we filter here, I do prefer the >= to avoid more verbose messages if a lower level is added by Zig later.

}
// default std log function.
std.log.defaultLog(level, scope, format, args);
}
Loading