Zig implementation: use std.log + grooming.

master
Philippe Pittoli 2023-02-04 09:20:57 +01:00
parent 745b8c12ca
commit 0c8fc284d3
8 changed files with 29 additions and 43 deletions

View File

@ -1,5 +1,5 @@
const std = @import("std");
const print = std.debug.print;
const log = std.log.scoped(.libipc_bindings);
const ipc = @import("./ipc.zig");
const Context = ipc.Context;
const Message = ipc.Message;
@ -9,7 +9,7 @@ export fn ipc_context_init (ptr: **Context) callconv(.C) i32 {
ptr.* = std.heap.c_allocator.create(Context) catch return -1;
ptr.*.* = Context.init(std.heap.c_allocator) catch |err| {
print ("libipc: error while init context: {}\n", .{err});
log.warn("error while init context: {}\n", .{err});
return -1;
};
return 0;

View File

@ -1,10 +1,7 @@
const std = @import("std");
const hexdump = @import("./hexdump.zig");
const net = std.net;
const fmt = std.fmt;
const print = std.debug.print;
const print_eq = @import("./util.zig").print_eq;
pub const Connections = std.ArrayList(Connection);

View File

@ -1,16 +1,15 @@
const std = @import("std");
// const hexdump = @import("./hexdump.zig");
const testing = std.testing;
const net = std.net;
const os = std.os;
const fmt = std.fmt;
const log = std.log.scoped(.libipc_context);
const receive_fd = @import("./exchange-fd.zig").receive_fd;
const Timer = std.time.Timer;
const print = std.debug.print;
const CBEvent = @import("./callback.zig").CBEvent;
const Connection = @import("./connection.zig").Connection;
const Message = @import("./message.zig").Message;
@ -76,7 +75,7 @@ pub const Context = struct {
pub fn deinit(self: *Self) void {
self.close_all() catch |err| switch(err){
error.IndexOutOfBounds => {
print("context.deinit(): IndexOutOfBounds\n", .{});
log.err("context.deinit(): IndexOutOfBounds", .{});
},
};
self.allocator.free(self.rundir);
@ -119,7 +118,7 @@ pub const Context = struct {
var network_envvar = std.process.getEnvVarOwned(allocator, "IPC_NETWORK") catch |err| switch(err) {
// error{ OutOfMemory, EnvironmentVariableNotFound, InvalidUtf8 } (ErrorSet)
error.EnvironmentVariableNotFound => {
print("no IPC_NETWORK envvar: IPCd won't be contacted\n", .{});
log.debug("no IPC_NETWORK envvar: IPCd won't be contacted", .{});
return null;
}, // no need to contact IPCd
else => { return err; },
@ -200,7 +199,7 @@ pub const Context = struct {
pub fn connect_ipc (self: *Self, service_name: []const u8) !i32 {
// First, try ipcd.
if (try self.connect_ipcd (service_name, Connection.Type.IPC)) |fd| {
print("Connected via IPCd, fd is {}\n", .{fd});
log.debug("Connected via IPCd, fd is {}", .{fd});
return fd;
}
// In case this doesn't work, connect directly.
@ -342,8 +341,8 @@ pub const Context = struct {
var current_event: Event = Event.init(Event.Type.ERROR, 0, 0, null);
var wait_duration: i32 = -1; // -1 == unlimited
if (self.timer) |t| { wait_duration = t; }
else { print("listening (no timer)\n", .{}); }
if (self.timer) |t| { log.debug("listening (timer: {} ms)", .{t}); wait_duration = t; }
else { log.debug("listening (no timer)", .{}); }
// Make sure we listen to the right file descriptors,
// setting POLLIN & POLLOUT flags.
@ -368,7 +367,7 @@ pub const Context = struct {
count = try os.poll(self.pollfd.items, wait_duration);
if (count < 0) {
print("there is a problem: poll < 0\n", .{});
log.err("there is a problem: poll < 0", .{});
current_event = Event.init(Event.Type.ERROR, 0, 0, null);
return current_event;
}
@ -404,20 +403,20 @@ pub const Context = struct {
},
.DISCONNECTION => {
var dest = try self.switchdb.getDest(fd.fd);
print("disconnection from {} -> removing {}, too\n", .{fd.fd, dest});
log.debug("disconnection from {} -> removing {}, too", .{fd.fd, dest});
self.switchdb.nuke(fd.fd);
self.safe_close_fd(fd.fd);
self.safe_close_fd(dest);
},
.ERROR => {
var dest = try self.switchdb.getDest(fd.fd);
print("error from {} -> removing {}, too\n", .{fd.fd, dest});
log.warn("error from {} -> removing {}, too", .{fd.fd, dest});
self.switchdb.nuke(fd.fd);
self.safe_close_fd(fd.fd);
self.safe_close_fd(dest);
},
else => {
print("switch rx incoherent error: {}\n", .{current_event.t});
log.warn("switch rx incoherent error: {}", .{current_event.t});
return error.incoherentSwitchError;
},
}
@ -431,7 +430,7 @@ pub const Context = struct {
else {
var maybe_message = self.read(i) catch |err| switch(err) {
error.ConnectionResetByPeer => {
print("connection reset by peer\n", .{});
log.warn("connection reset by peer", .{});
try self.close(i);
return Event.init(Event.Type.DISCONNECTION, i, fd.fd, null);
},
@ -470,13 +469,13 @@ pub const Context = struct {
},
.ERROR => {
var dest = try self.switchdb.getDest(fd.fd);
print("error from {} -> removing {}, too\n", .{fd.fd, dest});
log.warn("error from {} -> removing {}, too", .{fd.fd, dest});
self.switchdb.nuke(fd.fd);
self.safe_close_fd(fd.fd);
self.safe_close_fd(dest);
},
else => {
print("switch tx incoherent error: {}\n", .{current_event.t});
log.warn("switch tx incoherent error: {}", .{current_event.t});
return error.incoherentSwitchError;
},
}
@ -608,7 +607,7 @@ test "Context - creation, display and memory check" {
// SERVER SIDE: creating a service.
var server = c.server_init("simple-context-test") catch |err| switch(err) {
error.FileNotFound => {
print ("\nError: cannot init server at {s}\n", .{path});
log.err("cannot init server at {s}", .{path});
return err;
},
else => return err,
@ -684,7 +683,7 @@ test "Context - creation, echo once" {
// SERVER SIDE: creating a service.
var server = c.server_init("simple-context-test") catch |err| switch(err) {
error.FileNotFound => {
print ("\nError: cannot init server at {s}\n", .{path});
log.err("cannot init server at {s}", .{path});
return err;
},
else => return err,

View File

@ -1,7 +1,7 @@
const std = @import("std");
const testing = std.testing;
const os = std.os;
const print = std.debug.print;
const log = std.log.scoped(.libipc_exchangefd);
const builtin = @import("builtin");
const windows = std.os.windows;
@ -97,14 +97,13 @@ pub fn send_fd(sockfd: os.socket_t, msg: []const u8, fd: os.fd_t) void {
.controllen = @sizeOf(@TypeOf(cmsg)),
.flags = 0,
}, 0) catch |err| {
print("error sendmsg failed with {s}", .{@errorName(err)});
log.err("error sendmsg failed with {s}", .{@errorName(err)});
return;
};
if (len != msg.len) {
// we don't have much choice but to exit here
// log.err(@src(), "expected sendmsg to return {} but got {}", .{msg.len, len});
print("expected sendmsg to return {} but got {}", .{msg.len, len});
// We don't have much choice but to exit here.
log.err("expected sendmsg to return {} but got {}", .{msg.len, len});
os.exit(0xff);
}
}
@ -215,7 +214,7 @@ pub fn receive_fd(sockfd: os.socket_t, buffer: []u8, msg_size: *usize) !os.fd_t
};
var msglen = recvmsg(sockfd, msg, 0) catch |err| {
print("error recvmsg failed with {s}", .{@errorName(err)});
log.err("error recvmsg failed with {s}", .{@errorName(err)});
return 0;
};

View File

@ -1,5 +1,4 @@
const std = @import("std");
const print = std.debug.print;
pub fn hexdump(stream: anytype, header: [] const u8, buffer: [] const u8) std.os.WriteError!void {
// Print a header.
@ -71,6 +70,8 @@ pub fn hexdump(stream: anytype, header: [] const u8, buffer: [] const u8) std.os
try stream.writeAll("\n");
}
const print = std.debug.print;
test "36-byte hexdump test" {
print("\nPrint hexdump, NO AUTOMATIC VERIFICATION, READ SOURCE CODE\n", .{});
@ -80,7 +81,6 @@ test "36-byte hexdump test" {
var hexwriter = hexfbs.writer();
try hexdump(hexwriter, "Hello World", buffer);
print("{s}\n", .{hexfbs.getWritten()});
}
test "32-byte hexdump test" {
@ -92,7 +92,6 @@ test "32-byte hexdump test" {
var hexwriter = hexfbs.writer();
try hexdump(hexwriter, "Hello World", buffer);
print("{s}\n", .{hexfbs.getWritten()});
}
test "26-byte hexdump test" {
@ -104,7 +103,6 @@ test "26-byte hexdump test" {
var hexwriter = hexfbs.writer();
try hexdump(hexwriter, "Hello World", buffer);
print("{s}\n", .{hexfbs.getWritten()});
}
test "1-byte hexdump test" {
@ -116,7 +114,6 @@ test "1-byte hexdump test" {
var hexwriter = hexfbs.writer();
try hexdump(hexwriter, "Hello World", buffer);
print("{s}\n", .{hexfbs.getWritten()});
}
test "0-byte hexdump test" {
@ -128,5 +125,4 @@ test "0-byte hexdump test" {
var hexwriter = hexfbs.writer();
try hexdump(hexwriter, "Hello World", buffer);
print("{s}\n", .{hexfbs.getWritten()});
}

View File

@ -1,10 +1,8 @@
const std = @import("std");
// const hexdump = @import("./hexdump.zig");
const testing = std.testing;
const net = std.net;
const fmt = std.fmt;
const print = std.debug.print;
const print_eq = @import("./util.zig").print_eq;
pub const Messages = std.ArrayList(Message);

View File

@ -1,5 +1,4 @@
const std = @import("std");
const hexdump = @import("./hexdump.zig");
const testing = std.testing;
const fmt = std.fmt;
@ -13,7 +12,7 @@ const Allocator = std.mem.Allocator;
const util = @import("./util.zig");
const print_eq = util.print_eq;
const print = std.debug.print;
const log = std.log.scoped(.libipc_switch);
const Event = ipc.Event;
@ -301,7 +300,7 @@ fn default_in (origin: i32, mcontent: [*]u8, mlen: *u32) CBEventType {
// Let's handle this as a disconnection.
if (packet_size < 4) {
// print("message is less than 4 bytes ({} bytes)\n", .{packet_size});
log.debug("message is less than 4 bytes ({} bytes)", .{packet_size});
return CBEventType.FD_CLOSING;
}

View File

@ -2,7 +2,7 @@ const std = @import("std");
const hexdump = @import("./hexdump.zig");
const testing = std.testing;
const print = std.debug.print;
const log = std.log.scoped(.libipc_util);
const Message = @import("./message.zig").Message;
/// A VERY LIGHT and INCOMPLETE way of decoding URI.
@ -42,7 +42,7 @@ pub fn print_buffer (header: []const u8, buffer: []const u8) void {
var hexfbs = std.io.fixedBufferStream(&hexbuf);
var hexwriter = hexfbs.writer();
hexdump.hexdump(hexwriter, header, buffer) catch unreachable;
print("{s}\n", .{hexfbs.getWritten()});
log.debug("{s}", .{hexfbs.getWritten()});
}
pub fn print_message (header: []const u8, m: Message) void {
@ -55,8 +55,6 @@ pub fn print_eq(expected: anytype, obj: anytype) !void {
var writer = fbs.writer();
try writer.print("{}", .{obj});
// print("print_eq, expected: {s}\n", .{expected});
// print("print_eq: {s}\n", .{fbs.getWritten()});
// typing workaround
var secbuffer: [4096]u8 = undefined;