Better logging

Define default log level (debug for debug builds, info for everything else)
Use log scopes
This commit is contained in:
xeon 2024-06-26 19:37:19 +03:00
parent c66d8726ed
commit e8b50bf352
5 changed files with 29 additions and 5 deletions

View file

@ -1,10 +1,18 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
const httpz = @import("httpz"); const httpz = @import("httpz");
const protocol = @import("protocol"); const protocol = @import("protocol");
const authentication = @import("authentication.zig"); const authentication = @import("authentication.zig");
const dispatch = @import("dispatch.zig"); const dispatch = @import("dispatch.zig");
pub const std_options = .{
.log_level = switch (builtin.mode) {
.Debug => .debug,
else => .info,
},
};
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();

View file

@ -9,6 +9,7 @@ const Stream = std.net.Stream;
const Address = std.net.Address; const Address = std.net.Address;
const Self = @This(); const Self = @This();
const log = std.log.scoped(.session);
address: Address, address: Address,
stream: Stream, stream: Stream,
@ -42,7 +43,6 @@ pub fn send(self: *Self, cmd_id: protocol.CmdID, proto: anytype) !void {
defer self.allocator.free(packet); defer self.allocator.free(packet);
_ = try self.stream.write(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 { 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); defer self.allocator.free(packet);
_ = try self.stream.write(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});
} }

View file

@ -14,6 +14,8 @@ const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator; const ArenaAllocator = std.heap.ArenaAllocator;
const CmdID = protocol.CmdID; const CmdID = protocol.CmdID;
const log = std.log.scoped(.handlers);
const Action = *const fn (*Session, *const Packet, Allocator) anyerror!void; const Action = *const fn (*Session, *const Packet, Allocator) anyerror!void;
const HandlerList = [_]struct { CmdID, Action }{ const HandlerList = [_]struct { CmdID, Action }{
.{ CmdID.CmdPlayerGetTokenCsReq, login.onPlayerGetToken }, .{ CmdID.CmdPlayerGetTokenCsReq, login.onPlayerGetToken },
@ -68,6 +70,8 @@ const DummyCmdList = [_]struct { CmdID, CmdID }{
.{ CmdID.CmdGetMainMissionCustomValueCsReq, CmdID.CmdGetMainMissionCustomValueScRsp }, .{ CmdID.CmdGetMainMissionCustomValueCsReq, CmdID.CmdGetMainMissionCustomValueScRsp },
}; };
const SupressLogList = [_]CmdID{CmdID.CmdSceneEntityMoveCsReq};
pub fn handle(session: *Session, packet: *const Packet) !void { pub fn handle(session: *Session, packet: *const Packet) !void {
var arena = ArenaAllocator.init(session.allocator); var arena = ArenaAllocator.init(session.allocator);
defer arena.deinit(); defer arena.deinit();
@ -77,7 +81,9 @@ pub fn handle(session: *Session, packet: *const Packet) !void {
inline for (HandlerList) |handler| { inline for (HandlerList) |handler| {
if (handler[0] == cmd_id) { if (handler[0] == cmd_id) {
try handler[1](session, packet, arena.allocator()); 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; 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});
} }

View file

@ -1,7 +1,15 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
const network = @import("network.zig"); const network = @import("network.zig");
const handlers = @import("handlers.zig"); const handlers = @import("handlers.zig");
pub const std_options = .{
.log_level = switch (builtin.mode) {
.Debug => .debug,
else => .info,
},
};
pub fn main() !void { pub fn main() !void {
try network.listen(); try network.listen();
} }

View file

@ -5,6 +5,8 @@ const Session = @import("../Session.zig");
const Packet = @import("../Packet.zig"); const Packet = @import("../Packet.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const log = std.log.scoped(.scene_service);
pub fn onGetCurSceneInfo(session: *Session, _: *const Packet, allocator: Allocator) !void { pub fn onGetCurSceneInfo(session: *Session, _: *const Packet, allocator: Allocator) !void {
var scene_info = protocol.SceneInfo.init(allocator); var scene_info = protocol.SceneInfo.init(allocator);
scene_info.game_mode_type = 1; 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| { for (req.entity_motion_list.items) |entity_motion| {
if (entity_motion.motion) |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 });
} }
} }