fix: Consistent debug log spacing (#10349)

* fix: consistent debug log spacing

* refactor: simplify formatting

* refactor: more readable ternary

Co-Authored-By: Synbulat Biishev <contact@syjalo.dev>

* fix: modify parameters and types

---------

Co-authored-by: Synbulat Biishev <contact@syjalo.dev>
This commit is contained in:
Jiralite
2024-06-13 17:07:37 +01:00
committed by GitHub
parent c5d40d3807
commit 38c699bc8a
4 changed files with 30 additions and 43 deletions

View File

@@ -117,14 +117,14 @@ class WebSocketManager extends EventEmitter {
/** /**
* Emits a debug message. * Emits a debug message.
* @param {string} message The debug message * @param {string[]} messages The debug message
* @param {?number} [shardId] The id of the shard that emitted this message, if any * @param {?number} [shardId] The id of the shard that emitted this message, if any
* @private * @private
*/ */
debug(message, shardId) { debug(messages, shardId) {
this.client.emit( this.client.emit(
Events.Debug, Events.Debug,
`[WS => ${typeof shardId === 'number' ? `Shard ${shardId}` : 'Manager'}] ${message}`, `[WS => ${typeof shardId === 'number' ? `Shard ${shardId}` : 'Manager'}] ${messages.join('\n\t')}`,
); );
} }
@@ -170,15 +170,8 @@ class WebSocketManager extends EventEmitter {
}); });
const { total, remaining } = sessionStartLimit; const { total, remaining } = sessionStartLimit;
this.debug(['Fetched Gateway Information', `URL: ${gatewayURL}`, `Recommended Shards: ${recommendedShards}`]);
this.debug(`Fetched Gateway Information this.debug(['Session Limit Information', `Total: ${total}`, `Remaining: ${remaining}`]);
URL: ${gatewayURL}
Recommended Shards: ${recommendedShards}`);
this.debug(`Session Limit Information
Total: ${total}
Remaining: ${remaining}`);
this.gateway = `${gatewayURL}/`; this.gateway = `${gatewayURL}/`;
this.client.options.shardCount = await this._ws.getShardCount(); this.client.options.shardCount = await this._ws.getShardCount();
@@ -231,7 +224,7 @@ class WebSocketManager extends EventEmitter {
* @private * @private
*/ */
attachEvents() { attachEvents() {
this._ws.on(WSWebSocketShardEvents.Debug, ({ message, shardId }) => this.debug(message, shardId)); this._ws.on(WSWebSocketShardEvents.Debug, ({ message, shardId }) => this.debug([message], shardId));
this._ws.on(WSWebSocketShardEvents.Dispatch, ({ data, shardId }) => { this._ws.on(WSWebSocketShardEvents.Dispatch, ({ data, shardId }) => {
this.client.emit(Events.Raw, data, shardId); this.client.emit(Events.Raw, data, shardId);
this.emit(data.t, data.d, shardId); this.emit(data.t, data.d, shardId);
@@ -258,7 +251,7 @@ class WebSocketManager extends EventEmitter {
* @param {number} id The shard id that disconnected * @param {number} id The shard id that disconnected
*/ */
this.client.emit(Events.ShardDisconnect, { code, reason: reasonIsDeprecated, wasClean: true }, shardId); this.client.emit(Events.ShardDisconnect, { code, reason: reasonIsDeprecated, wasClean: true }, shardId);
this.debug(`Shard not resumable: ${code} (${GatewayCloseCodes[code] ?? CloseCodes[code]})`, shardId); this.debug([`Shard not resumable: ${code} (${GatewayCloseCodes[code] ?? CloseCodes[code]})`], shardId);
return; return;
} }
@@ -291,7 +284,7 @@ class WebSocketManager extends EventEmitter {
}); });
this._ws.on(WSWebSocketShardEvents.HeartbeatComplete, ({ heartbeatAt, latency, shardId }) => { this._ws.on(WSWebSocketShardEvents.HeartbeatComplete, ({ heartbeatAt, latency, shardId }) => {
this.debug(`Heartbeat acknowledged, latency of ${latency}ms.`, shardId); this.debug([`Heartbeat acknowledged, latency of ${latency}ms.`], shardId);
const shard = this.shards.get(shardId); const shard = this.shards.get(shardId);
shard.lastPingTimestamp = heartbeatAt; shard.lastPingTimestamp = heartbeatAt;
shard.ping = latency; shard.ping = latency;
@@ -324,7 +317,7 @@ class WebSocketManager extends EventEmitter {
async destroy() { async destroy() {
if (this.destroyed) return; if (this.destroyed) return;
// TODO: Make a util for getting a stack // TODO: Make a util for getting a stack
this.debug(Object.assign(new Error(), { name: 'Manager was destroyed:' }).stack); this.debug([Object.assign(new Error(), { name: 'Manager was destroyed:' }).stack]);
this.destroyed = true; this.destroyed = true;
await this._ws?.destroy({ code: CloseCodes.Normal, reason: 'Manager was destroyed' }); await this._ws?.destroy({ code: CloseCodes.Normal, reason: 'Manager was destroyed' });
} }

View File

@@ -85,11 +85,11 @@ class WebSocketShard extends EventEmitter {
/** /**
* Emits a debug event. * Emits a debug event.
* @param {string} message The debug message * @param {string[]} messages The debug message
* @private * @private
*/ */
debug(message) { debug(messages) {
this.manager.debug(message, this.id); this.manager.debug(messages, this.id);
} }
/** /**
@@ -110,10 +110,13 @@ class WebSocketShard extends EventEmitter {
wasClean: false, wasClean: false,
}, },
) { ) {
this.debug(`[CLOSE] this.debug([
Event Code: ${event.code} '[CLOSE]',
Clean : ${event.wasClean} `Event Code: ${event.code}`,
Reason : ${event.reason ?? 'No reason received'}`); `Clean : ${event.wasClean}`,
`Reason : ${event.reason ?? 'No reason received'}`,
]);
/** /**
* Emitted when a shard's WebSocket closes. * Emitted when a shard's WebSocket closes.
* @private * @private
@@ -130,7 +133,7 @@ class WebSocketShard extends EventEmitter {
*/ */
onReadyPacket(packet) { onReadyPacket(packet) {
if (!packet) { if (!packet) {
this.debug(`Received broken packet: '${packet}'.`); this.debug([`Received broken packet: '${packet}'.`]);
return; return;
} }
@@ -167,7 +170,7 @@ class WebSocketShard extends EventEmitter {
} }
// Step 1. If we don't have any other guilds pending, we are ready // Step 1. If we don't have any other guilds pending, we are ready
if (!this.expectedGuilds.size) { if (!this.expectedGuilds.size) {
this.debug('Shard received all its guilds. Marking as fully ready.'); this.debug(['Shard received all its guilds. Marking as fully ready.']);
this.status = Status.Ready; this.status = Status.Ready;
/** /**
@@ -191,12 +194,12 @@ class WebSocketShard extends EventEmitter {
this.readyTimeout = setTimeout( this.readyTimeout = setTimeout(
() => { () => {
this.debug( this.debug([
`Shard ${hasGuildsIntent ? 'did' : 'will'} not receive any more guild packets` + hasGuildsIntent
`${hasGuildsIntent ? ` in ${waitGuildTimeout} ms` : ''}.\nUnavailable guild count: ${ ? `Shard did not receive any guild packets in ${waitGuildTimeout} ms.`
this.expectedGuilds.size : 'Shard will not receive anymore guild packets.',
}`, `Unavailable guild count: ${this.expectedGuilds.size}`,
); ]);
this.readyTimeout = null; this.readyTimeout = null;
this.status = Status.Ready; this.status = Status.Ready;

View File

@@ -3653,7 +3653,7 @@ export class WebSocketManager extends EventEmitter {
public on(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this; public on(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this;
public once(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this; public once(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this;
private debug(message: string, shardId?: number): void; private debug(messages: readonly string[], shardId?: number): void;
private connect(): Promise<void>; private connect(): Promise<void>;
private broadcast(packet: unknown): void; private broadcast(packet: unknown): void;
private destroy(): Promise<void>; private destroy(): Promise<void>;
@@ -3684,7 +3684,7 @@ export class WebSocketShard extends EventEmitter {
public status: Status; public status: Status;
public ping: number; public ping: number;
private debug(message: string): void; private debug(messages: readonly string[]): void;
private onReadyPacket(packet: unknown): void; private onReadyPacket(packet: unknown): void;
private gotGuild(guildId: Snowflake): void; private gotGuild(guildId: Snowflake): void;
private checkReady(): void; private checkReady(): void;

View File

@@ -915,15 +915,6 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
} }
private debug(messages: [string, ...string[]]) { private debug(messages: [string, ...string[]]) {
const message = `${messages[0]}${ this.emit(WebSocketShardEvents.Debug, { message: messages.join('\n\t') });
messages.length > 1
? `\n${messages
.slice(1)
.map((message) => ` ${message}`)
.join('\n')}`
: ''
}`;
this.emit(WebSocketShardEvents.Debug, { message });
} }
} }