git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commitee5d48d442c4c5548059c0af00bd01b6a241183d
parent89a809bff1
authorIsaac Freund <[email protected]>
date2020-06-17 02:01
cli: allow setting log level with '-l' flag

 doc/river.1.scd |  7 ++++++-
 river/main.zig  | 19 ++++++++++++++++---
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/doc/river.1.scd b/doc/river.1.scd
index bc04b06..932273f 100644
--- a/doc/river.1.scd
+++ b/doc/river.1.scd
@@ -5,7 +5,7 @@ river - dynamic tiling Wayland compositor
 
 # SYNOPSIS
 
-*river* [*-c* _shell_command_]
+*river* [*-c* _shell_command_] [*-l* _log_level_]
 
 # DESCRIPTION
 
@@ -20,6 +20,11 @@ bspwm based on wlroots and written in Zig.
 	*riverctl*(1) and autostart programs. Make sure the script is
 	executable.
 
+*-l* _log_level_
+	Set the log level of river to a value from 0 to 7 with 0 being the
+	least verbose and 7 being the most verbose. The default log level of
+	release-safe builds is 5.
+
 # CONFIGURATION
 
 View border width, gap sizes, and programs which should float are
diff --git a/river/main.zig b/river/main.zig
index 2275efe..0060f8a 100644
--- a/river/main.zig
+++ b/river/main.zig
@@ -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);
+}