Include desktop app logs in diagnostics (#1914)
* fix(diagnostics): include desktop app logs Settings diagnostics now collect the Electron main-process log separately from daemon.log so desktop startup and environment-probe failures are included in app reports. * refactor(desktop): share log tail reader * fix(diagnostics): surface desktop app log read errors * refactor(diagnostics): tighten desktop report API
This commit is contained in:
@@ -19,6 +19,8 @@ const mocks = vi.hoisted(() => ({
|
||||
spawnProcess: vi.fn(),
|
||||
logInfo: vi.fn(),
|
||||
logError: vi.fn(),
|
||||
appLogPath: "/tmp/paseo-desktop-daemon-manager-test-main.log",
|
||||
getElectronLogFile: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("electron", () => ({
|
||||
@@ -32,7 +34,15 @@ vi.mock("electron", () => ({
|
||||
}));
|
||||
|
||||
vi.mock("electron-log/main", () => ({
|
||||
default: { info: mocks.logInfo, error: mocks.logError },
|
||||
default: {
|
||||
info: mocks.logInfo,
|
||||
error: mocks.logError,
|
||||
transports: {
|
||||
file: {
|
||||
getFile: mocks.getElectronLogFile,
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@getpaseo/server", () => ({
|
||||
@@ -105,11 +115,15 @@ describe("daemon-manager commands", () => {
|
||||
mocks.spawnProcess.mockReset();
|
||||
mocks.logInfo.mockReset();
|
||||
mocks.logError.mockReset();
|
||||
mocks.getElectronLogFile.mockReset();
|
||||
mocks.getElectronLogFile.mockReturnValue({ path: mocks.appLogPath });
|
||||
rmSync(mocks.paseoHome, { recursive: true, force: true });
|
||||
rmSync(mocks.appLogPath, { force: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(mocks.paseoHome, { recursive: true, force: true });
|
||||
rmSync(mocks.appLogPath, { force: true });
|
||||
});
|
||||
|
||||
it("refuses start and restart while built-in daemon management is disabled", async () => {
|
||||
@@ -435,4 +449,19 @@ describe("daemon-manager commands", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns the Electron main-process log tail from electron-log", () => {
|
||||
writeFileSync(
|
||||
mocks.appLogPath,
|
||||
Array.from({ length: 105 }, (_value, index) => `main log line ${index + 1}`).join("\n"),
|
||||
);
|
||||
const handlers = createDaemonCommandHandlers();
|
||||
|
||||
expect(handlers.desktop_app_logs()).toEqual({
|
||||
logPath: mocks.appLogPath,
|
||||
contents: Array.from({ length: 100 }, (_value, index) => `main log line ${index + 6}`).join(
|
||||
"\n",
|
||||
),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -39,6 +39,8 @@ import {
|
||||
import type { DesktopSettings } from "../settings/desktop-settings.js";
|
||||
import { getDesktopSettingsStore } from "../settings/desktop-settings-electron.js";
|
||||
import { isRunningUnderARM64Translation } from "../system/arm64-translation.js";
|
||||
import { getDesktopAppLogs } from "../diagnostics/app-logs.js";
|
||||
import { tailFile } from "../diagnostics/tail-file.js";
|
||||
|
||||
const DAEMON_LOG_FILENAME = "daemon.log";
|
||||
const STARTUP_POLL_INTERVAL_MS = 200;
|
||||
@@ -211,15 +213,6 @@ function sleep(ms: number): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
function tailFile(filePath: string, lines = 50): string {
|
||||
try {
|
||||
const content = readFileSync(filePath, "utf-8");
|
||||
return content.split("\n").filter(Boolean).slice(-lines).join("\n");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function logDesktopDaemonLifecycle(message: string, details?: Record<string, unknown>): void {
|
||||
log.info("[desktop daemon]", message, {
|
||||
pid: process.pid,
|
||||
@@ -574,6 +567,7 @@ export function createDaemonCommandHandlers(): Record<string, DesktopCommandHand
|
||||
stop_desktop_daemon: (args) => stopDesktopDaemon(parseDesktopDaemonStopReason(args)),
|
||||
restart_desktop_daemon: () => restartDaemon(),
|
||||
desktop_daemon_logs: () => getDaemonLogs(),
|
||||
desktop_app_logs: () => getDesktopAppLogs(),
|
||||
desktop_daemon_pairing: () => getDaemonPairing(),
|
||||
desktop_get_system_idle_time: () => powerMonitor.getSystemIdleTime() * 1000,
|
||||
cli_daemon_status: () => getCliDaemonStatus(),
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import log from "electron-log/main";
|
||||
import { tailFile } from "./tail-file.js";
|
||||
|
||||
const APP_LOG_TAIL_LINES = 100;
|
||||
|
||||
export interface DesktopAppLogs {
|
||||
logPath: string;
|
||||
contents: string;
|
||||
}
|
||||
|
||||
export function getDesktopAppLogs(): DesktopAppLogs {
|
||||
const logPath = log.transports.file.getFile().path;
|
||||
return {
|
||||
logPath,
|
||||
contents: tailFile(logPath, APP_LOG_TAIL_LINES, { throwOnReadError: true }),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { tailFile } from "./tail-file";
|
||||
|
||||
let testDir = "";
|
||||
|
||||
describe("tailFile", () => {
|
||||
beforeEach(() => {
|
||||
testDir = mkdtempSync(path.join(tmpdir(), "paseo-tail-file-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(testDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("returns the requested tail lines", () => {
|
||||
const filePath = path.join(testDir, "main.log");
|
||||
writeFileSync(filePath, "one\ntwo\nthree\n");
|
||||
|
||||
expect(tailFile(filePath, 2)).toBe("two\nthree");
|
||||
});
|
||||
|
||||
it("keeps missing files empty", () => {
|
||||
expect(tailFile(path.join(testDir, "missing.log"), 2)).toBe("");
|
||||
expect(tailFile(path.join(testDir, "missing.log"), 2, { throwOnReadError: true })).toBe("");
|
||||
});
|
||||
|
||||
it("can propagate read failures", () => {
|
||||
expect(() => tailFile(testDir, 2, { throwOnReadError: true })).toThrow();
|
||||
expect(tailFile(testDir, 2)).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
interface TailFileOptions {
|
||||
throwOnReadError?: boolean;
|
||||
}
|
||||
|
||||
export function tailFile(filePath: string, lines = 50, options: TailFileOptions = {}): string {
|
||||
try {
|
||||
const content = readFileSync(filePath, "utf-8");
|
||||
return content.split("\n").filter(Boolean).slice(-lines).join("\n");
|
||||
} catch (error) {
|
||||
if (options.throwOnReadError && !isMissingFileError(error)) {
|
||||
throw error;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function isMissingFileError(error: unknown): boolean {
|
||||
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
||||
}
|
||||
Reference in New Issue
Block a user