From e8b50bf3524688efc14d95532e13726759cf5915 Mon Sep 17 00:00:00 2001 From: xeon Date: Wed, 26 Jun 2024 19:37:19 +0300 Subject: [PATCH] Better logging Define default log level (debug for debug builds, info for everything else) Use log scopes --- dispatch/src/main.zig | 8 ++++++++ gameserver/src/Session.zig | 4 ++-- gameserver/src/handlers.zig | 10 ++++++++-- gameserver/src/main.zig | 8 ++++++++ gameserver/src/services/scene.zig | 4 +++- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/dispatch/src/main.zig b/dispatch/src/main.zig index 149ae11..e10d8a3 100644 --- a/dispatch/src/main.zig +++ b/dispatch/src/main.zig @@ -1,10 +1,18 @@ const std = @import("std"); +const builtin = @import("builtin"); const httpz = @import("httpz"); const protocol = @import("protocol"); const authentication = @import("authentication.zig"); const dispatch = @import("dispatch.zig"); +pub const std_options = .{ + .log_level = switch (builtin.mode) { + .Debug => .debug, + else => .info, + }, +}; + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); diff --git a/gameserver/src/Session.zig b/gameserver/src/Session.zig index 5e9f317..58fe724 100644 --- a/gameserver/src/Session.zig +++ b/gameserver/src/Session.zig @@ -9,6 +9,7 @@ const Stream = std.net.Stream; const Address = std.net.Address; const Self = @This(); +const log = std.log.scoped(.session); address: Address, stream: Stream, @@ -42,7 +43,6 @@ pub fn send(self: *Self, cmd_id: protocol.CmdID, proto: anytype) !void { defer self.allocator.free(packet); _ = try self.stream.write(packet); - std.log.debug("sent packet with id {}", .{cmd_id}); } pub fn send_empty(self: *Self, cmd_id: protocol.CmdID) !void { @@ -50,5 +50,5 @@ pub fn send_empty(self: *Self, cmd_id: protocol.CmdID) !void { defer self.allocator.free(packet); _ = try self.stream.write(packet); - std.log.debug("sent EMPTY packet with id {}", .{cmd_id}); + log.debug("sent EMPTY packet with id {}", .{cmd_id}); } diff --git a/gameserver/src/handlers.zig b/gameserver/src/handlers.zig index 1bbe5ce..6505a2c 100644 --- a/gameserver/src/handlers.zig +++ b/gameserver/src/handlers.zig @@ -14,6 +14,8 @@ const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const CmdID = protocol.CmdID; +const log = std.log.scoped(.handlers); + const Action = *const fn (*Session, *const Packet, Allocator) anyerror!void; const HandlerList = [_]struct { CmdID, Action }{ .{ CmdID.CmdPlayerGetTokenCsReq, login.onPlayerGetToken }, @@ -68,6 +70,8 @@ const DummyCmdList = [_]struct { CmdID, CmdID }{ .{ CmdID.CmdGetMainMissionCustomValueCsReq, CmdID.CmdGetMainMissionCustomValueScRsp }, }; +const SupressLogList = [_]CmdID{CmdID.CmdSceneEntityMoveCsReq}; + pub fn handle(session: *Session, packet: *const Packet) !void { var arena = ArenaAllocator.init(session.allocator); defer arena.deinit(); @@ -77,7 +81,9 @@ pub fn handle(session: *Session, packet: *const Packet) !void { inline for (HandlerList) |handler| { if (handler[0] == cmd_id) { try handler[1](session, packet, arena.allocator()); - std.log.debug("packet {} was handled", .{cmd_id}); + if (!std.mem.containsAtLeast(CmdID, &SupressLogList, 1, &[_]CmdID{cmd_id})) { + log.debug("packet {} was handled", .{cmd_id}); + } return; } } @@ -89,5 +95,5 @@ pub fn handle(session: *Session, packet: *const Packet) !void { } } - std.log.warn("packet {} was ignored", .{cmd_id}); + log.warn("packet {} was ignored", .{cmd_id}); } diff --git a/gameserver/src/main.zig b/gameserver/src/main.zig index 155efc3..0641735 100644 --- a/gameserver/src/main.zig +++ b/gameserver/src/main.zig @@ -1,7 +1,15 @@ const std = @import("std"); +const builtin = @import("builtin"); const network = @import("network.zig"); const handlers = @import("handlers.zig"); +pub const std_options = .{ + .log_level = switch (builtin.mode) { + .Debug => .debug, + else => .info, + }, +}; + pub fn main() !void { try network.listen(); } diff --git a/gameserver/src/services/scene.zig b/gameserver/src/services/scene.zig index 2f820ce..f6618a7 100644 --- a/gameserver/src/services/scene.zig +++ b/gameserver/src/services/scene.zig @@ -5,6 +5,8 @@ const Session = @import("../Session.zig"); const Packet = @import("../Packet.zig"); const Allocator = std.mem.Allocator; +const log = std.log.scoped(.scene_service); + pub fn onGetCurSceneInfo(session: *Session, _: *const Packet, allocator: Allocator) !void { var scene_info = protocol.SceneInfo.init(allocator); scene_info.game_mode_type = 1; @@ -64,7 +66,7 @@ pub fn onSceneEntityMove(session: *Session, packet: *const Packet, allocator: Al for (req.entity_motion_list.items) |entity_motion| { if (entity_motion.motion) |motion| { - std.log.debug("[POSITION] entity_id: {}, motion: {}", .{ entity_motion.entity_id, motion }); + log.debug("[POSITION] entity_id: {}, motion: {}", .{ entity_motion.entity_id, motion }); } }