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

commit722358b7cdb87fa672483f4192b6fb972f6f9395
parent45b56c285f
authorIsaac Freund <[email protected]>
date2020-06-26 18:43
command: log output of commands run by mappings

 river/Control.zig | 10 +---------
 river/Seat.zig    | 21 ++++++++++++++++-----
 river/command.zig | 17 +++++++++++++++++
 3 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/river/Control.zig b/river/Control.zig
index a434a9c..3fd1cb5 100644
--- a/river/Control.zig
+++ b/river/Control.zig
@@ -138,15 +138,6 @@ fn runCommand(
     defer if (out) |s| util.gpa.free(s);
     command.run(util.gpa, seat, args, &out) catch |err| {
         const failure_message = switch (err) {
-            command.Error.NoCommand => "no command given",
-            command.Error.UnknownCommand => "unknown command",
-            command.Error.UnknownOption => "unknown option",
-            command.Error.NotEnoughArguments => "not enough arguments",
-            command.Error.TooManyArguments => "too many arguments",
-            command.Error.Overflow => "value out of bounds",
-            command.Error.InvalidCharacter => "invalid character in argument",
-            command.Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'",
-            command.Error.InvalidRgba => "invalid color format, must be #RRGGBB or #RRGGBBAA",
             command.Error.OutOfMemory => {
                 c.wl_client_post_no_memory(wl_client);
                 return;
@@ -155,6 +146,7 @@ fn runCommand(
                 c.wl_client_post_no_memory(wl_client);
                 return;
             },
+            else => command.errToMsg(err),
         };
         defer if (err == command.Error.Other) util.gpa.free(failure_message);
         c.zriver_command_callback_v1_send_failure(callback_resource, failure_message);
diff --git a/river/Seat.zig b/river/Seat.zig
index 64f6396..cdc08ed 100644
--- a/river/Seat.zig
+++ b/river/Seat.zig
@@ -21,6 +21,7 @@ const std = @import("std");
 
 const c = @import("c.zig");
 const command = @import("command.zig");
+const log = @import("log.zig");
 const util = @import("util.zig");
 
 const Cursor = @import("Cursor.zig");
@@ -275,12 +276,22 @@ pub fn handleMapping(self: *Self, keysym: c.xkb_keysym_t, modifiers: u32) bool {
     for (modes.items[self.mode_id].items) |mapping| {
         if (modifiers == mapping.modifiers and keysym == mapping.keysym) {
             // Execute the bound command
-            var failure_message: ?[]const u8 = null;
-            command.run(util.gpa, self, mapping.command_args, &failure_message) catch |err| {
-                // TODO: log the error
-                if (err == command.Error.Other)
-                    util.gpa.free(failure_message.?);
+            const args = mapping.command_args;
+            var out: ?[]const u8 = null;
+            defer if (out) |s| util.gpa.free(s);
+            command.run(util.gpa, self, args, &out) catch |err| {
+                const failure_message = switch (err) {
+                    command.Error.Other => out.?,
+                    else => command.errToMsg(err),
+                };
+                log.err(.command, "{}: {}", .{ args[0], failure_message });
+                return true;
             };
+            if (out) |s| {
+                const stdout = std.io.getStdOut().outStream();
+                stdout.print("{}", .{s}) catch
+                    |err| log.err(.command, "{}: write to stdout failed {}", .{ args[0], err });
+            }
             return true;
         }
     }
diff --git a/river/command.zig b/river/command.zig
index 7497f88..4194026 100644
--- a/river/command.zig
+++ b/river/command.zig
@@ -119,3 +119,20 @@ pub fn run(
 
     try impl_fn(allocator, seat, args, out);
 }
+
+/// Return a short error message for the given error. Passing Error.Other is UB
+pub fn errToMsg(err: Error) [:0]const u8 {
+    return switch (err) {
+        Error.NoCommand => "no command given",
+        Error.UnknownCommand => "unknown command",
+        Error.UnknownOption => "unknown option",
+        Error.NotEnoughArguments => "not enough arguments",
+        Error.TooManyArguments => "too many arguments",
+        Error.Overflow => "value out of bounds",
+        Error.InvalidCharacter => "invalid character in argument",
+        Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'",
+        Error.InvalidRgba => "invalid color format, must be #RRGGBB or #RRGGBBAA",
+        Error.OutOfMemory => "out of memory",
+        Error.Other => unreachable,
+    };
+}