Merge pull request 'chore: make shard logging respect session log setting' (#3) from xydone/discord.zig:master into master

Reviewed-on: https://codeberg.org/yuzu/discord.zig/pulls/3
This commit is contained in:
yuzu 2025-04-22 22:17:01 +00:00
commit 84bf0bac63
3 changed files with 19 additions and 13 deletions

View File

@ -88,6 +88,13 @@ pub const debug = std.log.scoped(.@"discord.zig");
pub const Log = union(enum) { yes, no }; 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{ pub const default_identify_properties = IdentifyProperties{
.os = @tagName(builtin.os.tag), .os = @tagName(builtin.os.tag),
.browser = "discord.zig", .browser = "discord.zig",

View File

@ -35,6 +35,7 @@ const GatewayInfo = @import("internal.zig").GatewayInfo;
const GatewayBotInfo = @import("internal.zig").GatewayBotInfo; const GatewayBotInfo = @import("internal.zig").GatewayBotInfo;
const GatewaySessionStartLimit = @import("internal.zig").GatewaySessionStartLimit; const GatewaySessionStartLimit = @import("internal.zig").GatewaySessionStartLimit;
const ShardDetails = @import("internal.zig").ShardDetails; const ShardDetails = @import("internal.zig").ShardDetails;
const internalLogif = @import("internal.zig").logif;
const Log = @import("internal.zig").Log; const Log = @import("internal.zig").Log;
const GatewayDispatchEvent = @import("internal.zig").GatewayDispatchEvent; 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 std.debug.assert(std.json.validate(self.allocator, decompressed) catch
@panic("Invalid JSON")); @panic("Invalid JSON"));
// for some reason std.json breaks when you use a generic // for some reason std.json breaks when you use a generic
const GatewayPayloadType = struct{ const GatewayPayloadType = struct {
/// opcode for the payload /// opcode for the payload
op: isize, op: isize,
/// Event data /// Event data
@ -408,8 +408,8 @@ pub fn Shard(comptime Table: TableTemplate) type {
}, },
else => { else => {
// log that the connection died, but don't stop the bot // 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)}); self.logif("Shard {d} closed with error: {s}\n", .{self.id, @errorName(err)});
std.debug.print("Attempting to reconnect...\n", .{}); self.logif("Attempting to reconnect...\n", .{});
// reconnect // reconnect
self.reconnect() catch unreachable; self.reconnect() catch unreachable;
} }
@ -429,7 +429,7 @@ pub fn Shard(comptime Table: TableTemplate) type {
.reason = reason, //[]const u8 .reason = reason, //[]const u8
}) catch { }) catch {
// log that the connection died, but don't stop the bot // 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 { pub fn handleEventNoError(self: *Self, name: []const u8, payload_ptr: *json.Value) void {
// log to make sure this executes // 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| { 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); return req.delete(path);
} }
inline fn logif(self: *Self, comptime format: []const u8, args: anytype) void {
internalLogif(self.log, format, args);
}
}; };
} }

View File

@ -18,6 +18,7 @@ const Intents = @import("./structures/types.zig").Intents;
const Snowflake = @import("./structures/snowflake.zig").Snowflake; const Snowflake = @import("./structures/snowflake.zig").Snowflake;
const GatewayBotInfo = @import("internal.zig").GatewayBotInfo; const GatewayBotInfo = @import("internal.zig").GatewayBotInfo;
const IdentifyProperties = @import("internal.zig").IdentifyProperties; const IdentifyProperties = @import("internal.zig").IdentifyProperties;
const internalLogif = @import("internal.zig").logif;
const ShardDetails = @import("internal.zig").ShardDetails; const ShardDetails = @import("internal.zig").ShardDetails;
const ConnectQueue = @import("internal.zig").ConnectQueue; const ConnectQueue = @import("internal.zig").ConnectQueue;
const GatewayDispatchEvent = @import("internal.zig").GatewayDispatchEvent; 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 { inline fn logif(self: *Self, comptime format: []const u8, args: anytype) void {
switch (self.log) { internalLogif(self.log, format, args);
.yes => debug.info(format, args),
.no => {},
}
} }
}; };
} }