From e710a59a435c81684717919df088385a40a0b30a Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 19 May 2024 08:59:57 -0500 Subject: [PATCH 1/2] os: log stderr from open command --- src/os/open.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/os/open.zig b/src/os/open.zig index 8bf8bb7ca..730b07e1e 100644 --- a/src/os/open.zig +++ b/src/os/open.zig @@ -13,5 +13,24 @@ pub fn open(alloc: Allocator, url: []const u8) !void { }; var exe = std.process.Child.init(argv, alloc); + + // Pipe stdout/stderr so we can collect output from the command + exe.stdout_behavior = .Pipe; + exe.stderr_behavior = .Pipe; + var stdout = std.ArrayList(u8).init(alloc); + var stderr = std.ArrayList(u8).init(alloc); + defer { + stdout.deinit(); + stderr.deinit(); + } + try exe.spawn(); + + // 50 KiB is the default value used by std.process.Child.run + try exe.collectOutput(&stdout, &stderr, 50 * 1024); + + _ = try exe.wait(); + if (stderr.items.len > 0) { + std.log.err("os.open: {s}", .{stderr.items}); + } } From 1e60f7186c4a1286cef6f4649ad5601928fdddf9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 20 May 2024 19:11:30 -0400 Subject: [PATCH 2/2] os: some stylistic changes, comments for stderr logging --- src/os/open.zig | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/os/open.zig b/src/os/open.zig index 730b07e1e..a907ee4f8 100644 --- a/src/os/open.zig +++ b/src/os/open.zig @@ -3,6 +3,9 @@ const builtin = @import("builtin"); const Allocator = std.mem.Allocator; /// Open a URL in the default handling application. +/// +/// Any output on stderr is logged as a warning in the application logs. +/// Output on stdout is ignored. pub fn open(alloc: Allocator, url: []const u8) !void { const argv = switch (builtin.os.tag) { .linux => &.{ "xdg-open", url }, @@ -24,13 +27,14 @@ pub fn open(alloc: Allocator, url: []const u8) !void { stderr.deinit(); } - try exe.spawn(); - // 50 KiB is the default value used by std.process.Child.run - try exe.collectOutput(&stdout, &stderr, 50 * 1024); + const output_max_size = 50 * 1024; + try exe.spawn(); + try exe.collectOutput(&stdout, &stderr, output_max_size); _ = try exe.wait(); - if (stderr.items.len > 0) { - std.log.err("os.open: {s}", .{stderr.items}); - } + + // If we have any stderr output we log it. This makes it easier for + // users to debug why some open commands may not work as expected. + if (stderr.items.len > 0) std.log.err("open stderr={s}", .{stderr.items}); }