import { dirname, join } from "path"; import { mkdir, open, readFile } from "fs/promises"; export namespace OutputHandler { export const LogCachePath = join(process.cwd(), "cache", "log"); export const ErrorLogPath = join(LogCachePath, "error.log"); export function getErrorOutput(...args: any[]) { const timestamp = new Date().toISOString(); const message = args .map((arg) => { if (arg instanceof Error) { return `${arg.name}: ${arg.message}\n${arg.stack}`; } if (typeof arg === "object") { return JSON.stringify(arg); } return String(arg); }) .join(" "); return `[${timestamp}] ${message}`; } export async function errorLog(...args: any[]): Promise { const output = getErrorOutput(...args); console.log(output); await mkdir(dirname(ErrorLogPath), { recursive: true }); const fileHandle = await open(ErrorLogPath, "a"); fileHandle.write(output + "\n"); fileHandle.close(); } export async function getErrorLog(): Promise { return (await readFile(ErrorLogPath)).toString(); } }