From cbc9245de829c58d6069a93d1cb596f372b8028c Mon Sep 17 00:00:00 2001 From: xydone Date: Tue, 22 Apr 2025 15:32:23 +0300 Subject: [PATCH] chore: make shard logging respect session log setting --- src/internal.zig | 7 +++++++ src/shard.zig | 19 ++++++++++--------- src/sharder.zig | 6 ++---- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/internal.zig b/src/internal.zig index b0be037..d785617 100644 --- a/src/internal.zig +++ b/src/internal.zig @@ -88,6 +88,13 @@ pub const debug = std.log.scoped(.@"discord.zig"); pub const Log = union(enum) { yes, no }; +pub inline fn logif(log: Log, comptime format: []const u8, args: anytype) void { + switch (log) { + .yes => debug.info(format, args), + .no => {}, + } +} + pub const default_identify_properties = IdentifyProperties{ .os = @tagName(builtin.os.tag), .browser = "discord.zig", diff --git a/src/shard.zig b/src/shard.zig index ee8d5a7..db00037 100644 --- a/src/shard.zig +++ b/src/shard.zig @@ -35,6 +35,7 @@ const GatewayInfo = @import("internal.zig").GatewayInfo; const GatewayBotInfo = @import("internal.zig").GatewayBotInfo; const GatewaySessionStartLimit = @import("internal.zig").GatewaySessionStartLimit; const ShardDetails = @import("internal.zig").ShardDetails; +const internalLogif = @import("internal.zig").logif; const Log = @import("internal.zig").Log; const GatewayDispatchEvent = @import("internal.zig").GatewayDispatchEvent; @@ -250,9 +251,8 @@ pub fn Shard(comptime Table: TableTemplate) type { std.debug.assert(std.json.validate(self.allocator, decompressed) catch @panic("Invalid JSON")); - // for some reason std.json breaks when you use a generic - const GatewayPayloadType = struct{ + const GatewayPayloadType = struct { /// opcode for the payload op: isize, /// Event data @@ -408,8 +408,8 @@ pub fn Shard(comptime Table: TableTemplate) type { }, else => { // log that the connection died, but don't stop the bot - std.debug.print("Shard {d} closed with error: {s}\n", .{self.id, @errorName(err)}); - std.debug.print("Attempting to reconnect...\n", .{}); + self.logif("Shard {d} closed with error: {s}\n", .{self.id, @errorName(err)}); + self.logif("Attempting to reconnect...\n", .{}); // reconnect self.reconnect() catch unreachable; } @@ -429,7 +429,7 @@ pub fn Shard(comptime Table: TableTemplate) type { .reason = reason, //[]const u8 }) catch { // log that the connection died, but don't stop the bot - std.debug.print("Shard {d} closed with error: {s} # {d}\n", .{self.id, reason, @intFromEnum(code)}); + self.logif("Shard {d} closed with error: {s} # {d}\n", .{self.id, reason, @intFromEnum(code)}); }; } @@ -446,10 +446,10 @@ pub fn Shard(comptime Table: TableTemplate) type { pub fn handleEventNoError(self: *Self, name: []const u8, payload_ptr: *json.Value) void { // log to make sure this executes - std.debug.print("Shard {d} dispatching {s}\n", .{self.id, name}); + self.logif("Shard {d} dispatching {s}\n", .{self.id, name}); self.handleEvent(name, payload_ptr.*) catch |err| { - std.debug.print("Shard {d} error: {s}\n", .{self.id, @errorName(err)}); + self.logif("Shard {d} error: {s}\n", .{self.id, @errorName(err)}); }; } @@ -2903,7 +2903,8 @@ pub fn Shard(comptime Table: TableTemplate) type { return req.delete(path); } - + inline fn logif(self: *Self, comptime format: []const u8, args: anytype) void { + internalLogif(self.log, format, args); + } }; } - diff --git a/src/sharder.zig b/src/sharder.zig index c7f5c4b..078ad6c 100644 --- a/src/sharder.zig +++ b/src/sharder.zig @@ -18,6 +18,7 @@ const Intents = @import("./structures/types.zig").Intents; const Snowflake = @import("./structures/snowflake.zig").Snowflake; const GatewayBotInfo = @import("internal.zig").GatewayBotInfo; const IdentifyProperties = @import("internal.zig").IdentifyProperties; +const internalLogif = @import("internal.zig").logif; const ShardDetails = @import("internal.zig").ShardDetails; const ConnectQueue = @import("internal.zig").ConnectQueue; const GatewayDispatchEvent = @import("internal.zig").GatewayDispatchEvent; @@ -254,10 +255,7 @@ pub fn ShardManager(comptime Table: TableTemplate) type { // inline fn logif(self: *Self, comptime format: []const u8, args: anytype) void { - switch (self.log) { - .yes => debug.info(format, args), - .no => {}, - } + internalLogif(self.log, format, args); } }; }