command: log output of commands run by mappings

This commit is contained in:
Isaac Freund 2020-06-26 18:43:20 +02:00
parent 40597f184d
commit 3904275373
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
3 changed files with 34 additions and 14 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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,
};
}