38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import logManager from '@electron/service/logger';
|
|
import type { HostApiResult } from '@src/types/runtime';
|
|
import type { HostApiContext } from '../context';
|
|
import type { NormalizedHostApiRequest } from '../route-utils';
|
|
import { fail, ok } from '../route-utils';
|
|
|
|
const DEFAULT_TAIL_LINES = 100;
|
|
|
|
function parseTailLines(rawValue: string | null): number {
|
|
const parsed = Number(rawValue ?? DEFAULT_TAIL_LINES);
|
|
if (!Number.isFinite(parsed)) {
|
|
return DEFAULT_TAIL_LINES;
|
|
}
|
|
|
|
return Math.max(1, Math.floor(parsed));
|
|
}
|
|
|
|
export async function handleLogRoutes(
|
|
request: NormalizedHostApiRequest,
|
|
_ctx: HostApiContext,
|
|
): Promise<HostApiResult<unknown> | null> {
|
|
const { pathname, method, url } = request;
|
|
|
|
if (pathname === '/api/logs' && method === 'GET') {
|
|
try {
|
|
return ok({
|
|
content: await logManager.readRecentLogText(parseTailLines(url.searchParams.get('tailLines'))),
|
|
directory: logManager.getLogDirectoryPath(),
|
|
currentFile: logManager.getCurrentLogFilePath(),
|
|
});
|
|
} catch (error) {
|
|
return fail(500, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|