chore: make shard logging respect session log setting

This commit is contained in:
xydone 2025-04-22 15:32:23 +03:00
parent d449cdeea1
commit cbc9245de8
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 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",

View File

@ -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);
}
};
}

View File

@ -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);
}
};
}