diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2026-01-24 18:53:42 -0500 |
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2026-01-24 19:17:43 -0500 |
| commit | c3b17f826740bed3f2785e97550f647cf38dfb7f (patch) | |
| tree | dfd9bfd78e745073c7df31775a494b9cab309d97 /src/main.zig | |
| parent | cf365673b5c78de4a3811cd35a79e9986123354a (diff) | |
Better error handling and debug logging
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/src/main.zig b/src/main.zig index 25ead5e..edbfb1b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,3 @@ -const is_debug = builtin.mode == .Debug; - const help = \\-h, --help Display this help and exit. \\-r, --relay <str> A relay message to send. @@ -43,7 +41,7 @@ pub fn main(init: std.process.Init) !void { if (to_option.get(args[i])) |opt| { switch (opt) { .help => { - std.debug.print("{s}\n", .{help}); + std.debug.print("{s}", .{help}); return; }, .relay => { @@ -84,7 +82,7 @@ pub fn main(init: std.process.Init) !void { return error.InvalidArguments; } - var client: SaprusClient = try .init(); + var client = try SaprusClient.init(); defer client.deinit(); if (flags.relay != null) { @@ -96,7 +94,7 @@ pub fn main(init: std.process.Init) !void { chunk_writer.end = 0; try chunk_writer.print("{b64}", .{chunk}); try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest)); - try init.io.sleep(.fromMilliseconds(40), .real); + try init.io.sleep(.fromMilliseconds(40), .boot); } } else { var stdin_file: std.Io.File = .stdin(); @@ -114,7 +112,7 @@ pub fn main(init: std.process.Init) !void { chunk_writer.end = 0; try chunk_writer.print("{b64}", .{stdin.buffered()}); try client.sendRelay(init.io, chunk_writer.buffered(), parseDest(flags.dest)); - try init.io.sleep(.fromMilliseconds(40), .real); + try init.io.sleep(.fromMilliseconds(40), .boot); try stdin.discardAll(stdin.end); } else |err| switch (err) { error.EndOfStream => { @@ -145,7 +143,7 @@ pub fn main(init: std.process.Init) !void { log.debug("Connection started", .{}); - while (true) { + next_message: while (true) { var res_buf: [2048]u8 = undefined; const next = connection.next(init.io, &res_buf) catch { try init.io.sleep(.fromSeconds(retry_seconds), .boot); @@ -156,7 +154,7 @@ pub fn main(init: std.process.Init) !void { var connection_payload_buf: [2048]u8 = undefined; const connection_payload = connection_payload_buf[0..try b64d.calcSizeForSlice(next)]; b64d.decode(connection_payload, next) catch { - // TODO: debug log + log.debug("Failed to decode message, skipping: '{s}'", .{connection_payload}); continue; }; @@ -171,17 +169,24 @@ pub fn main(init: std.process.Init) !void { var child_stderr: std.ArrayList(u8) = .empty; defer child_stderr.deinit(init.gpa); - try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize)); + child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize)) catch |err| { + log.debug("Failed to collect output: {t}", .{err}); + continue; + }; - var cmd_output_buf: [2048]u8 = undefined; + var cmd_output_buf: [SaprusClient.max_payload_len * 2]u8 = undefined; var cmd_output: Writer = .fixed(&cmd_output_buf); var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len); while (cmd_output_window_iter.next()) |chunk| { cmd_output.end = 0; - try cmd_output.print("{b64}", .{chunk}); - try connection.send(init.io, cmd_output.buffered()); - try init.io.sleep(.fromMilliseconds(40), .real); + // Unreachable because the cmd_output_buf is twice the size of the chunk. + cmd_output.print("{b64}", .{chunk}) catch unreachable; + connection.send(init.io, cmd_output.buffered()) catch |err| { + log.debug("Failed to send connection chunk: {t}", .{err}); + continue :next_message; + }; + try init.io.sleep(.fromMilliseconds(40), .boot); } } } |
