-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathlogFiles.ts
More file actions
289 lines (264 loc) · 7.21 KB
/
Copy pathlogFiles.ts
File metadata and controls
289 lines (264 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import * as path from "node:path";
import { type Logger } from "../logging/logger";
import {
isOutputLoggingDir,
isRemoteSshExtensionDir,
isSharedChannelRemoteSshLog,
} from "../remote/sshExtension";
import * as localJsonlFiles from "../telemetry/localJsonlFiles";
import {
addFiles,
collectDirFiles,
collectMatchingFiles,
type CollectedFile,
isLogFile,
normalizeZipPath,
prefixFiles,
readDirents,
readLogFile,
} from "./files";
import { collectSettingsFile } from "./settings";
export interface LogSources {
activeProxyLogPath?: string;
proxyLogDir?: string;
extensionLogDir?: string;
telemetryDir?: string;
}
interface WindowLogDir {
relativePath: string;
windowPath: string;
}
interface LogContext {
currentWindowPath: string;
logsRoot: string;
}
/**
* Proxy, Remote-SSH, and extension logs from recent windows plus a redacted
* settings snapshot, keyed by zip path under `vscode-logs/`.
*/
export async function collectVsCodeDiagnostics(
sources: LogSources,
logger: Logger,
): Promise<Map<string, Uint8Array>> {
const files = await collectSupportLogFiles(sources, logger);
const settings = collectSettingsFile(logger);
if (settings) {
files.set("vscode-logs/settings.json", settings);
}
return files;
}
export async function collectSupportLogFiles(
sources: LogSources,
logger: Logger,
): Promise<Map<string, Uint8Array>> {
const files = await collectProxyLogs(sources, logger);
if (sources.extensionLogDir) {
addFiles(
files,
await collectVsCodeWindowLogs(sources.extensionLogDir, logger),
);
}
if (sources.telemetryDir) {
addFiles(files, await collectTelemetryFiles(sources.telemetryDir, logger));
}
return files;
}
export function resolveLogContext(
extensionLogDir: string,
): LogContext | undefined {
const resolved = path.resolve(extensionLogDir);
const exthostDir = path.dirname(resolved);
const windowDir = path.dirname(exthostDir);
const windowName = path.basename(windowDir);
const sessionDir = path.dirname(windowDir);
// Match the layout, not the id: forks rebrand it.
if (
path.basename(exthostDir) !== "exthost" ||
!/^window\d+$/i.test(windowName)
) {
return undefined;
}
const sessionName = path.basename(sessionDir);
return {
currentWindowPath: windowDir,
// Anchored so `20240101T000000-foo` doesn't widen logsRoot.
logsRoot: /^\d{8}T\d{6}$/.test(sessionName)
? path.dirname(sessionDir)
: sessionDir,
};
}
export async function collectWindowLogDirs(
logsRoot: string,
logger: Logger,
): Promise<WindowLogDir[]> {
const windows: WindowLogDir[] = [];
await Promise.all(
(await readDirents(logsRoot, logger)).map(async (entry) => {
if (!entry.isDirectory()) return;
const entryPath = path.join(logsRoot, entry.name);
if (/^window\d+$/i.test(entry.name)) {
windows.push({ relativePath: entry.name, windowPath: entryPath });
return;
}
for (const sub of await readDirents(entryPath, logger)) {
if (sub.isDirectory() && /^window\d+$/i.test(sub.name)) {
windows.push({
relativePath: `${entry.name}/${sub.name}`,
windowPath: path.join(entryPath, sub.name),
});
}
}
}),
);
return windows.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
}
async function collectTelemetryFiles(
telemetryDir: string,
logger: Logger,
): Promise<Map<string, Uint8Array>> {
return prefixFiles(
"vscode-logs/telemetry",
await collectDirFiles(
telemetryDir,
logger,
localJsonlFiles.isFileName,
false,
),
);
}
async function collectProxyLogs(
sources: LogSources,
logger: Logger,
): Promise<Map<string, Uint8Array>> {
const files = new Map<string, Uint8Array>();
const activeBasename = sources.activeProxyLogPath
? path.basename(sources.activeProxyLogPath)
: undefined;
if (sources.activeProxyLogPath && activeBasename) {
// No age cutoff: long sessions outlive the window.
const file = await readLogFile(sources.activeProxyLogPath, logger);
if (file) {
files.set(`vscode-logs/proxy/${activeBasename}`, file.data);
}
}
if (sources.proxyLogDir) {
addFiles(
files,
prefixFiles(
"vscode-logs/proxy",
// Already added above; don't double-bundle.
await collectDirFiles(
sources.proxyLogDir,
logger,
(name) => isProxyLogFile(name) && name !== activeBasename,
),
),
);
}
return files;
}
async function collectVsCodeWindowLogs(
extensionLogDir: string,
logger: Logger,
): Promise<Map<string, Uint8Array>> {
const files = new Map<string, Uint8Array>();
const context = resolveLogContext(extensionLogDir);
if (!context) {
// Non-canonical layout: scan the ext dir and assumed window dir.
addFiles(
files,
prefixFiles(
"vscode-logs/extension",
await collectDirFiles(extensionLogDir, logger, isLogFile),
),
);
const exthostDir = path.dirname(extensionLogDir);
const windowDir =
path.basename(exthostDir) === "exthost"
? path.dirname(exthostDir)
: exthostDir;
for (const log of await collectMatchingFiles(
windowDir,
logger,
isRemoteSshLog,
)) {
files.set(
`vscode-logs/remote-ssh/${normalizeZipPath(log.relativePath)}`,
log.data,
);
}
return files;
}
const extensionId = path.basename(extensionLogDir);
const currentWindowSshLogs: CollectedFile[] = [];
for (const window of await collectWindowLogDirs(context.logsRoot, logger)) {
const extLogs = await collectDirFiles(
path.join(window.windowPath, "exthost", extensionId),
logger,
isLogFile,
false,
);
// Window never hosted Coder; its SSH logs aren't ours.
if (extLogs.size === 0) continue;
addFiles(
files,
prefixFiles(
`vscode-logs/extension/${normalizeZipPath(window.relativePath)}`,
extLogs,
),
);
const isCurrent = window.windowPath === context.currentWindowPath;
for (const sshLog of await collectMatchingFiles(
window.windowPath,
logger,
isRemoteSshLog,
)) {
const relativePath = normalizeZipPath(
path.join(window.relativePath, sshLog.relativePath),
);
files.set(`vscode-logs/remote-ssh/${relativePath}`, sshLog.data);
if (isCurrent) {
currentWindowSshLogs.push({ ...sshLog, relativePath });
}
}
}
// Current window only: others would mislabel a stale log.
const activeLog = newestLog(currentWindowSshLogs);
if (activeLog) {
files.set(
`vscode-logs/remote-ssh/${path.basename(activeLog.relativePath)}`,
activeLog.data,
);
}
return files;
}
// Coder CLI logs: `coder-ssh-*.log` or bare `<pid>.log`.
const isProxyLogFile = (name: string): boolean =>
isLogFile(name) && (name.startsWith("coder-ssh") || /^\d+\.log$/.test(name));
function isRemoteSshLog(relativePath: string, fileName: string): boolean {
if (!isLogFile(fileName)) {
return false;
}
const parts = normalizeZipPath(relativePath).split("/");
// exthost dir is per-extension; output_logging_* is shared.
if (parts.some(isRemoteSshExtensionDir)) {
return true;
}
return (
parts.some(isOutputLoggingDir) && isSharedChannelRemoteSshLog(fileName)
);
}
function newestLog(logs: CollectedFile[]): CollectedFile | undefined {
let newest: CollectedFile | undefined;
for (const log of logs) {
if (
!newest ||
log.mtimeMs > newest.mtimeMs ||
// Locale-stable tie-break (not localeCompare).
(log.mtimeMs === newest.mtimeMs && log.relativePath > newest.relativePath)
) {
newest = log;
}
}
return newest;
}