diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2024-05-06 19:58:44 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2024-05-06 19:58:44 +0200 |
commit | ff44027b013cf7c771bf1f9c0cd3c7548c691048 (patch) | |
tree | 95de169eca7d752165b85dde02b3a6f7127e1c41 | |
parent | 54365b02ef3e36a631354aa1af3bd33f90db3cf7 (diff) | |
download | SouthRPC-ff44027b013cf7c771bf1f9c0cd3c7548c691048.tar.gz SouthRPC-ff44027b013cf7c771bf1f9c0cd3c7548c691048.zip |
include scope in logging, add logging fallback to stderr, etc.
-rw-r--r-- | src/interfaces/PluginId001.zig | 6 | ||||
-rw-r--r-- | src/server.zig | 12 | ||||
-rw-r--r-- | src/sys.zig | 23 |
3 files changed, 29 insertions, 12 deletions
diff --git a/src/interfaces/PluginId001.zig b/src/interfaces/PluginId001.zig index 2246b8b..7069228 100644 --- a/src/interfaces/PluginId001.zig +++ b/src/interfaces/PluginId001.zig @@ -46,9 +46,9 @@ pub fn GetString(self: *anyopaque, prop: PluginString) callconv(.C) ?[*:0]const _ = self; switch (prop) { - .ID_NAME => return @import("root").PLUGIN_NAME, - .ID_LOG_NAME => return @import("root").LOG_NAME, - .ID_DEPENDENCY_NAME => return @import("root").DEPENDENCY_NAME, + .ID_NAME => return @import("../main.zig").PLUGIN_NAME, + .ID_LOG_NAME => return @import("../main.zig").LOG_NAME, + .ID_DEPENDENCY_NAME => return @import("../main.zig").DEPENDENCY_NAME, else => return null, } } diff --git a/src/server.zig b/src/server.zig index 4dc6d42..fe44e83 100644 --- a/src/server.zig +++ b/src/server.zig @@ -15,6 +15,8 @@ var allocator = gpa.allocator(); const execute_command = @import("methods/execute_command.zig").method; const list_methods = @import("methods/list_methods.zig").method; +const server_log = std.log.scoped(.http_server); + pub const RpcMethods = .{ execute_command, list_methods, @@ -41,7 +43,7 @@ const JsonRpcResponse = struct { }; fn handleRequest(res: *Server.Response) !void { - std.log.info("{s} {s} {s}", .{ @tagName(res.request.method), @tagName(res.request.version), res.request.target }); + server_log.info("{s} {s} {s}", .{ @tagName(res.request.method), @tagName(res.request.version), res.request.target }); if (!std.mem.startsWith(u8, res.request.target, "/rpc")) { res.status = .not_found; @@ -58,7 +60,7 @@ fn handleRequest(res: *Server.Response) !void { }; const parsed = std.json.parseFromSlice(JsonRpcRequest, allocator, body, .{}) catch |err| { - std.log.err("Failed to parse request body {}", .{err}); + server_log.err("Failed to parse request body {}", .{err}); if (@errorReturnTrace()) |trace| { std.debug.dumpStackTrace(trace.*); @@ -151,7 +153,7 @@ fn serverThread(addr: std.net.Address) !void { defer _ = gpa.deinit(); runServer(&server) catch |err| { - std.log.err("server error: {}\n", .{err}); + server_log.err("server error: {}\n", .{err}); if (@errorReturnTrace()) |trace| { std.debug.dumpStackTrace(trace.*); @@ -169,7 +171,7 @@ pub fn start() !void { running = true; server_thread = try Thread.spawn(.{}, serverThread, .{addr}); - std.log.info("Started HTTP Server on {}", .{addr}); + server_log.info("Started HTTP Server on {}", .{addr}); } } @@ -178,6 +180,6 @@ pub fn stop() void { running = false; thread.join(); - std.log.info("Stopped HTTP Server", .{}); + server_log.info("Stopped HTTP Server", .{}); } } diff --git a/src/sys.zig b/src/sys.zig index 3c0afec..ea71ec2 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -17,7 +17,16 @@ var sys: ?*CSys = null; pub const LogLevel = enum(c_int) { LOG_INFO, LOG_WARN, LOG_ERR }; pub fn init() void { - sys = @ptrCast(@alignCast(northstar.create_interface.?("NSSys001", null))); + if (northstar.create_interface) |create_interface| { + var status: interface.InterfaceStatus = .IFACE_OK; + sys = @ptrCast(@alignCast(create_interface("NSSys001", &status))); + + if (status != .IFACE_OK) { + std.log.err("Failed to create NSSys001 interface: {}", .{status}); + } + } else { + std.log.err("Failed to create NSSys001 interface: {s}", .{"Failed to resolve CreateInterface"}); + } } pub fn log( @@ -26,7 +35,10 @@ pub fn log( comptime format: []const u8, args: anytype, ) void { - _ = scope; + const scope_prefix = switch (scope) { + std.log.default_log_scope => "", + else => "(" ++ @tagName(scope) ++ ")", + }; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); @@ -40,9 +52,12 @@ pub fn log( .debug => LogLevel.LOG_INFO, }; - const msg = std.fmt.allocPrintZ(allocator, format, args) catch unreachable; + const msg = std.fmt.allocPrintZ(allocator, scope_prefix ++ format, args) catch unreachable; if (sys) |s| { - s.*.vtable.log(s, northstar.data.handle, log_level, msg); + s.vtable.log(s, northstar.data.handle, log_level, msg); + } else { + // Northstar log has not been established, fallback to default log + std.log.defaultLog(level, scope, format, args); } } |