add --verbose option

This commit is contained in:
Pierre Tachoire
2024-12-13 12:29:05 +01:00
parent 3b018e2a6d
commit ab4973ab6c

View File

@@ -40,6 +40,14 @@ pub const websocket_blocking = true;
const log = std.log.scoped(.cli); 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 = const usage =
\\usage: {s} [options] [URL] \\usage: {s} [options] [URL]
\\ \\
@@ -49,6 +57,7 @@ const usage =
\\ * otherwhise the browser starts a CDP server \\ * otherwhise the browser starts a CDP server
\\ \\
\\ -h, --help Print this help message and exit. \\ -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") \\ --host Host of the CDP server (default "127.0.0.1")
\\ --port Port of the CDP server (default "9222") \\ --port Port of the CDP server (default "9222")
\\ --timeout Timeout for incoming connections of the CDP server (in seconds, default "3") \\ --timeout Timeout for incoming connections of the CDP server (in seconds, default "3")
@@ -110,6 +119,10 @@ const CliMode = union(CliModeTag) {
if (std.mem.eql(u8, "-h", opt) or std.mem.eql(u8, "--help", opt)) { if (std.mem.eql(u8, "-h", opt) or std.mem.eql(u8, "--help", opt)) {
return printUsageExit(execname, 0); return printUsageExit(execname, 0);
} }
if (std.mem.eql(u8, "--verbose", opt)) {
verbose = true;
continue;
}
if (std.mem.eql(u8, "--dump", opt)) { if (std.mem.eql(u8, "--dump", opt)) {
_fetch.dump = true; _fetch.dump = true;
continue; continue;
@@ -334,3 +347,18 @@ pub fn main() !void {
}, },
} }
} }
var verbose: bool = builtin.mode == .Debug; // In debug mode, force verbose.
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;
}
// default std log function.
std.log.defaultLog(level, scope, format, args);
}