Skip to content

Commit 825a751

Browse files
code-asherkylecarbs
authored andcommitted
Add some debug logging
1 parent eef5b8d commit 825a751

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

packages/protocol/src/node/command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export const handleNewServer = (connection: SendableConnection, newServer: NewSe
198198
const sm = new ServerMessage();
199199
sm.setServerFailure(sf);
200200
connection.send(sm.serializeBinary());
201-
201+
202202
onExit();
203203
}
204204

packages/protocol/src/node/server.ts

+47-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from "path";
44
import { mkdir, WriteStream } from "fs";
55
import { promisify } from "util";
66
import { TextDecoder } from "text-encoding";
7-
import { logger, field } from "@coder/logger";
7+
import { Logger, logger, field } from "@coder/logger";
88
import { ClientMessage, WorkingInitMessage, ServerMessage, NewSessionMessage, WriteToSessionMessage } from "../proto";
99
import { evaluate } from "./evaluate";
1010
import { ReadWriteConnection } from "../common/connection";
@@ -95,8 +95,12 @@ export class Server {
9595
if (message.hasNewEval()) {
9696
evaluate(this.connection, message.getNewEval()!);
9797
} else if (message.hasNewSession()) {
98-
const session = handleNewSession(this.connection, message.getNewSession()!, this.options, () => {
99-
this.sessions.delete(message.getNewSession()!.getId());
98+
const sessionMessage = message.getNewSession()!;
99+
const childLogger = this.getChildLogger(sessionMessage.getCommand());
100+
childLogger.debug(sessionMessage.getIsFork() ? "Forking" : "Spawning", field("args", sessionMessage.getArgsList()));
101+
const session = handleNewSession(this.connection, sessionMessage, this.options, () => {
102+
childLogger.debug("Exited");
103+
this.sessions.delete(sessionMessage.getId());
100104
});
101105
this.sessions.set(message.getNewSession()!.getId(), session);
102106
} else if (message.hasCloseSessionInput()) {
@@ -134,10 +138,15 @@ export class Server {
134138
s.write(data);
135139
}
136140
} else if (message.hasNewConnection()) {
137-
const socket = handleNewConnection(this.connection, message.getNewConnection()!, () => {
138-
this.connections.delete(message.getNewConnection()!.getId());
141+
const connectionMessage = message.getNewConnection()!;
142+
const name = connectionMessage.getPath() || `${connectionMessage.getPort()}`;
143+
const childLogger = this.getChildLogger(name, ">");
144+
childLogger.debug("Connecting", field("path", connectionMessage.getPath()), field("port", connectionMessage.getPort()));
145+
const socket = handleNewConnection(this.connection, connectionMessage, () => {
146+
childLogger.debug("Disconnected");
147+
this.connections.delete(connectionMessage.getId());
139148
});
140-
this.connections.set(message.getNewConnection()!.getId(), socket);
149+
this.connections.set(connectionMessage.getId(), socket);
141150
} else if (message.hasConnectionOutput()) {
142151
const c = this.getConnection(message.getConnectionOutput()!.getId());
143152
if (!c) {
@@ -151,14 +160,21 @@ export class Server {
151160
}
152161
c.end();
153162
} else if (message.hasNewServer()) {
154-
const s = handleNewServer(this.connection, message.getNewServer()!, (socket) => {
163+
const serverMessage = message.getNewServer()!;
164+
const name = serverMessage.getPath() || `${serverMessage.getPort()}`;
165+
const childLogger = this.getChildLogger(name);
166+
childLogger.debug("Listening", field("path", serverMessage.getPath()), field("port", serverMessage.getPort()));
167+
const s = handleNewServer(this.connection, serverMessage, (socket) => {
155168
const id = this.connectionId--;
156169
this.connections.set(id, socket);
170+
childLogger.debug("Got connection", field("id", id));
171+
157172
return id;
158173
}, () => {
159-
this.connections.delete(message.getNewServer()!.getId());
174+
childLogger.debug("Stopped");
175+
this.connections.delete(serverMessage.getId());
160176
});
161-
this.servers.set(message.getNewServer()!.getId(), s);
177+
this.servers.set(serverMessage.getId(), s);
162178
} else if (message.hasServerClose()) {
163179
const s = this.getServer(message.getServerClose()!.getId());
164180
if (!s) {
@@ -180,4 +196,26 @@ export class Server {
180196
return this.sessions.get(id);
181197
}
182198

199+
private getChildLogger(command: string, prefix: string = ""): Logger {
200+
// TODO: Temporary, for debugging. Should probably ask for a name?
201+
let name: string;
202+
if (command.includes("vscode-ipc")) {
203+
name = "exthost";
204+
} else if (command.includes("vscode-online")) {
205+
name = "shared";
206+
} else {
207+
const basename = command.split("/").pop()!;
208+
let i = 0;
209+
for (; i < basename.length; i++) {
210+
const character = basename.charAt(i);
211+
if (character === character.toUpperCase()) {
212+
break;
213+
}
214+
}
215+
name = basename.substring(0, i);
216+
}
217+
218+
return logger.named(prefix + name);
219+
}
220+
183221
}

packages/server/src/vscode/bootstrapFork.ts

-21
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@ import * as cp from "child_process";
22
import * as fs from "fs";
33
import * as net from "net";
44
import * as path from "path";
5-
import { Logger, logger, field } from "@coder/logger/src";
6-
7-
const getChildLogger = (modulePath: string): Logger => {
8-
const basename = modulePath.split("/").pop()!;
9-
let i = 0;
10-
for (; i < basename.length; i++) {
11-
const character = basename.charAt(i);
12-
if (character === character.toUpperCase()) {
13-
break;
14-
}
15-
}
16-
17-
return logger.named(basename.substring(0, i));
18-
};
195

206
export const requireModule = (modulePath: string): void => {
217
process.env.AMD_ENTRYPOINT = modulePath;
@@ -46,9 +32,6 @@ export const requireModule = (modulePath: string): void => {
4632
* @param modulePath Path of the VS Code module to load.
4733
*/
4834
export const forkModule = (modulePath: string): cp.ChildProcess => {
49-
const childLogger = getChildLogger(modulePath);
50-
childLogger.debug("Forking...", field("module", modulePath));
51-
5235
let proc: cp.ChildProcess | undefined;
5336

5437
const args = ["--bootstrap-fork", modulePath];
@@ -61,9 +44,5 @@ export const forkModule = (modulePath: string): cp.ChildProcess => {
6144
proc = cp.spawn(process.execArgv[0], ["-r", "tsconfig-paths/register", process.argv[1], ...args], options);
6245
}
6346

64-
proc.on("exit", (exitCode) => {
65-
childLogger.debug(`Exited with ${exitCode}`);
66-
});
67-
6847
return proc;
6948
};

0 commit comments

Comments
 (0)