cli: allow setting log level with '-l' flag

This commit is contained in:
Isaac Freund
2020-06-17 02:01:07 +02:00
parent d74323bbbf
commit 0efc04508b
2 changed files with 22 additions and 4 deletions

View File

@ -28,6 +28,7 @@ const usage: []const u8 =
\\
\\ -h Print this help message and exit.
\\ -c <command> Run `sh -c <command>` on startup.
\\ -l <level> Set the log level to a value from 0 to 7.
\\
;
@ -46,9 +47,15 @@ pub fn main() !void {
if (it.nextPosix()) |command| {
startup_command = command;
} else {
const stderr = std.io.getStdErr().outStream();
try stderr.print("Error: flag '-c' requires exactly one argument\n", .{});
std.os.exit(1);
printErrorExit("Error: flag '-c' requires exactly one argument", .{});
}
} else if (std.mem.eql(u8, arg, "-l")) {
if (it.nextPosix()) |level_str| {
const level = std.fmt.parseInt(u3, level_str, 10) catch
printErrorExit("Error: invalid log level '{}'", .{level_str});
log.level = @intToEnum(log.Level, level);
} else {
printErrorExit("Error: flag '-l' requires exactly one argument", .{});
}
} else {
const stderr = std.io.getStdErr().outStream();
@ -81,3 +88,9 @@ pub fn main() !void {
log.info(.server, "shutting down", .{});
}
fn printErrorExit(comptime format: []const u8, args: var) noreturn {
const stderr = std.io.getStdErr().outStream();
stderr.print(format ++ "\n", args) catch std.os.exit(1);
std.os.exit(1);
}