Skip to content

Commit 38c699b

Browse files
JiraliteSyjalo
andauthored
fix: Consistent debug log spacing (#10349)
* fix: consistent debug log spacing * refactor: simplify formatting * refactor: more readable ternary Co-Authored-By: Synbulat Biishev <[email protected]> * fix: modify parameters and types --------- Co-authored-by: Synbulat Biishev <[email protected]>
1 parent c5d40d3 commit 38c699b

File tree

4 files changed

+30
-43
lines changed

4 files changed

+30
-43
lines changed

packages/discord.js/src/client/websocket/WebSocketManager.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ class WebSocketManager extends EventEmitter {
117117

118118
/**
119119
* Emits a debug message.
120-
* @param {string} message The debug message
120+
* @param {string[]} messages The debug message
121121
* @param {?number} [shardId] The id of the shard that emitted this message, if any
122122
* @private
123123
*/
124-
debug(message, shardId) {
124+
debug(messages, shardId) {
125125
this.client.emit(
126126
Events.Debug,
127-
`[WS => ${typeof shardId === 'number' ? `Shard ${shardId}` : 'Manager'}] ${message}`,
127+
`[WS => ${typeof shardId === 'number' ? `Shard ${shardId}` : 'Manager'}] ${messages.join('\n\t')}`,
128128
);
129129
}
130130

@@ -170,15 +170,8 @@ class WebSocketManager extends EventEmitter {
170170
});
171171

172172
const { total, remaining } = sessionStartLimit;
173-
174-
this.debug(`Fetched Gateway Information
175-
URL: ${gatewayURL}
176-
Recommended Shards: ${recommendedShards}`);
177-
178-
this.debug(`Session Limit Information
179-
Total: ${total}
180-
Remaining: ${remaining}`);
181-
173+
this.debug(['Fetched Gateway Information', `URL: ${gatewayURL}`, `Recommended Shards: ${recommendedShards}`]);
174+
this.debug(['Session Limit Information', `Total: ${total}`, `Remaining: ${remaining}`]);
182175
this.gateway = `${gatewayURL}/`;
183176

184177
this.client.options.shardCount = await this._ws.getShardCount();
@@ -231,7 +224,7 @@ class WebSocketManager extends EventEmitter {
231224
* @private
232225
*/
233226
attachEvents() {
234-
this._ws.on(WSWebSocketShardEvents.Debug, ({ message, shardId }) => this.debug(message, shardId));
227+
this._ws.on(WSWebSocketShardEvents.Debug, ({ message, shardId }) => this.debug([message], shardId));
235228
this._ws.on(WSWebSocketShardEvents.Dispatch, ({ data, shardId }) => {
236229
this.client.emit(Events.Raw, data, shardId);
237230
this.emit(data.t, data.d, shardId);
@@ -258,7 +251,7 @@ class WebSocketManager extends EventEmitter {
258251
* @param {number} id The shard id that disconnected
259252
*/
260253
this.client.emit(Events.ShardDisconnect, { code, reason: reasonIsDeprecated, wasClean: true }, shardId);
261-
this.debug(`Shard not resumable: ${code} (${GatewayCloseCodes[code] ?? CloseCodes[code]})`, shardId);
254+
this.debug([`Shard not resumable: ${code} (${GatewayCloseCodes[code] ?? CloseCodes[code]})`], shardId);
262255
return;
263256
}
264257

@@ -291,7 +284,7 @@ class WebSocketManager extends EventEmitter {
291284
});
292285

293286
this._ws.on(WSWebSocketShardEvents.HeartbeatComplete, ({ heartbeatAt, latency, shardId }) => {
294-
this.debug(`Heartbeat acknowledged, latency of ${latency}ms.`, shardId);
287+
this.debug([`Heartbeat acknowledged, latency of ${latency}ms.`], shardId);
295288
const shard = this.shards.get(shardId);
296289
shard.lastPingTimestamp = heartbeatAt;
297290
shard.ping = latency;
@@ -324,7 +317,7 @@ class WebSocketManager extends EventEmitter {
324317
async destroy() {
325318
if (this.destroyed) return;
326319
// TODO: Make a util for getting a stack
327-
this.debug(Object.assign(new Error(), { name: 'Manager was destroyed:' }).stack);
320+
this.debug([Object.assign(new Error(), { name: 'Manager was destroyed:' }).stack]);
328321
this.destroyed = true;
329322
await this._ws?.destroy({ code: CloseCodes.Normal, reason: 'Manager was destroyed' });
330323
}

packages/discord.js/src/client/websocket/WebSocketShard.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ class WebSocketShard extends EventEmitter {
8585

8686
/**
8787
* Emits a debug event.
88-
* @param {string} message The debug message
88+
* @param {string[]} messages The debug message
8989
* @private
9090
*/
91-
debug(message) {
92-
this.manager.debug(message, this.id);
91+
debug(messages) {
92+
this.manager.debug(messages, this.id);
9393
}
9494

9595
/**
@@ -110,10 +110,13 @@ class WebSocketShard extends EventEmitter {
110110
wasClean: false,
111111
},
112112
) {
113-
this.debug(`[CLOSE]
114-
Event Code: ${event.code}
115-
Clean : ${event.wasClean}
116-
Reason : ${event.reason ?? 'No reason received'}`);
113+
this.debug([
114+
'[CLOSE]',
115+
`Event Code: ${event.code}`,
116+
`Clean : ${event.wasClean}`,
117+
`Reason : ${event.reason ?? 'No reason received'}`,
118+
]);
119+
117120
/**
118121
* Emitted when a shard's WebSocket closes.
119122
* @private
@@ -130,7 +133,7 @@ class WebSocketShard extends EventEmitter {
130133
*/
131134
onReadyPacket(packet) {
132135
if (!packet) {
133-
this.debug(`Received broken packet: '${packet}'.`);
136+
this.debug([`Received broken packet: '${packet}'.`]);
134137
return;
135138
}
136139

@@ -167,7 +170,7 @@ class WebSocketShard extends EventEmitter {
167170
}
168171
// Step 1. If we don't have any other guilds pending, we are ready
169172
if (!this.expectedGuilds.size) {
170-
this.debug('Shard received all its guilds. Marking as fully ready.');
173+
this.debug(['Shard received all its guilds. Marking as fully ready.']);
171174
this.status = Status.Ready;
172175

173176
/**
@@ -191,12 +194,12 @@ class WebSocketShard extends EventEmitter {
191194

192195
this.readyTimeout = setTimeout(
193196
() => {
194-
this.debug(
195-
`Shard ${hasGuildsIntent ? 'did' : 'will'} not receive any more guild packets` +
196-
`${hasGuildsIntent ? ` in ${waitGuildTimeout} ms` : ''}.\nUnavailable guild count: ${
197-
this.expectedGuilds.size
198-
}`,
199-
);
197+
this.debug([
198+
hasGuildsIntent
199+
? `Shard did not receive any guild packets in ${waitGuildTimeout} ms.`
200+
: 'Shard will not receive anymore guild packets.',
201+
`Unavailable guild count: ${this.expectedGuilds.size}`,
202+
]);
200203

201204
this.readyTimeout = null;
202205
this.status = Status.Ready;

packages/discord.js/typings/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,7 +3653,7 @@ export class WebSocketManager extends EventEmitter {
36533653
public on(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this;
36543654
public once(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this;
36553655

3656-
private debug(message: string, shardId?: number): void;
3656+
private debug(messages: readonly string[], shardId?: number): void;
36573657
private connect(): Promise<void>;
36583658
private broadcast(packet: unknown): void;
36593659
private destroy(): Promise<void>;
@@ -3684,7 +3684,7 @@ export class WebSocketShard extends EventEmitter {
36843684
public status: Status;
36853685
public ping: number;
36863686

3687-
private debug(message: string): void;
3687+
private debug(messages: readonly string[]): void;
36883688
private onReadyPacket(packet: unknown): void;
36893689
private gotGuild(guildId: Snowflake): void;
36903690
private checkReady(): void;

packages/ws/src/ws/WebSocketShard.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -915,15 +915,6 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
915915
}
916916

917917
private debug(messages: [string, ...string[]]) {
918-
const message = `${messages[0]}${
919-
messages.length > 1
920-
? `\n${messages
921-
.slice(1)
922-
.map((message) => ` ${message}`)
923-
.join('\n')}`
924-
: ''
925-
}`;
926-
927-
this.emit(WebSocketShardEvents.Debug, { message });
918+
this.emit(WebSocketShardEvents.Debug, { message: messages.join('\n\t') });
928919
}
929920
}

0 commit comments

Comments
 (0)