mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 03:34:48 +00:00
Better error handling and debug logging
This commit is contained in:
31
src/main.zig
31
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user