feat(launcher): add logging and diagnostics export
This commit is contained in:
1 parent
fdac56a036
commit
d633280e23
11 files changed
+233
-4
No files matched your search
@@ -0,0 +1,86 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { lstat, mkdir, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { app, dialog, shell, type BrowserWindow } from 'electron';
|
||||
import { zipSync } from 'fflate';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import { readInstallRecord } from '../install/install-record.ts';
|
||||
import { LAUNCHER_LOG_FILES, launcherLogFolder } from './launcher-log.ts';
|
||||
|
||||
type ArchiveFiles = Record<string, Uint8Array>;
|
||||
|
||||
export async function openLogsFolder(): Promise<void> {
|
||||
const path = launcherLogFolder();
|
||||
try {
|
||||
await mkdir(path, { recursive: true });
|
||||
const failure = await shell.openPath(path);
|
||||
if (failure) throw new Error(failure);
|
||||
} catch (error) {
|
||||
console.warn('[diagnostics] opening the log folder failed:', error);
|
||||
throw new AppError('logs_folder_open_failed');
|
||||
}
|
||||
}
|
||||
|
||||
export async function exportDiagnostics(window: BrowserWindow | null): Promise<{ path: string | null }> {
|
||||
try {
|
||||
const date = new Date().toISOString().slice(0, 10);
|
||||
const options = {
|
||||
defaultPath: `WTOffline-diagnostics-${date}.zip`,
|
||||
filters: [{ name: 'ZIP', extensions: ['zip'] }],
|
||||
};
|
||||
const result = window ? await dialog.showSaveDialog(window, options) : await dialog.showSaveDialog(options);
|
||||
if (result.canceled || !result.filePath) return { path: null };
|
||||
|
||||
const files: ArchiveFiles = {};
|
||||
const userData = app.getPath('userData');
|
||||
await addFile(files, 'settings.json', join(userData, 'settings.json'));
|
||||
await addFile(files, 'install.json', join(userData, 'install.json'));
|
||||
const launcherLogs = launcherLogFolder();
|
||||
if ((await ifPresent(() => lstat(launcherLogs)))?.isDirectory()) {
|
||||
for (const name of LAUNCHER_LOG_FILES) {
|
||||
await addFile(files, `launcher-logs/${name}`, join(launcherLogs, name));
|
||||
}
|
||||
}
|
||||
const record = await readInstallRecord();
|
||||
if (record) await addServerLogs(files, record.dataPath);
|
||||
|
||||
const temporary = `${result.filePath}.${randomUUID()}.tmp`;
|
||||
try {
|
||||
await writeFile(temporary, zipSync(files), { flag: 'wx' });
|
||||
await rename(temporary, result.filePath);
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true }).catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
return { path: result.filePath };
|
||||
} catch (error) {
|
||||
console.error('[diagnostics] exporting the archive failed:', error);
|
||||
throw new AppError('diagnostics_export_failed');
|
||||
}
|
||||
}
|
||||
|
||||
/** Include the server's request journals. Traffic bodies can contain player data. */
|
||||
async function addServerLogs(files: ArchiveFiles, dataPath: string): Promise<void> {
|
||||
const root = join(dataPath, 'logs');
|
||||
if (!(await ifPresent(() => lstat(root)))?.isDirectory()) return;
|
||||
const sessions = await ifPresent(() => readdir(root, { withFileTypes: true }));
|
||||
for (const session of sessions ?? []) {
|
||||
if (!session.isDirectory() || !/^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z$/.test(session.name)) continue;
|
||||
const source = join(root, session.name, 'requests.jsonl');
|
||||
await addFile(files, `server-logs/${session.name}/requests.jsonl`, source);
|
||||
}
|
||||
}
|
||||
|
||||
async function addFile(files: ArchiveFiles, entry: string, path: string): Promise<void> {
|
||||
if (!(await ifPresent(() => lstat(path)))?.isFile()) return;
|
||||
const bytes = await ifPresent(() => readFile(path));
|
||||
if (bytes) files[entry] = bytes;
|
||||
}
|
||||
|
||||
async function ifPresent<T>(operation: () => Promise<T>): Promise<T | undefined> {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user