import * as path from "@std/path"; import { createSingleton, generateTimeStringForFileName } from "./util.ts"; export class Logger { constructor(public readonly path: string) {} warn(message: string) { console.log(message); } generateLogFilePath( prefix: string = "", suffix: string = "", instant?: Temporal.Instant | Date, ): string { return path.join( this.path, `${prefix}-${generateTimeStringForFileName(instant)}-${suffix}`, ); } openLogFile( prefix: string = "", suffix: string = "", instant?: Temporal.Instant | Date, ): Promise { const logPath = this.generateLogFilePath(prefix, suffix, instant); return Deno.open(logPath, { read: false, write: true, append: true, create: true, }); } async logProgramOutput( process: Deno.ChildProcess, program: string, instant?: Temporal.Instant | Date, ): Promise { const stdoutFile = await this.openLogFile(program, "stdout", instant); const stderrFile = await this.openLogFile(program, "stderr", instant); process.stdout.pipeTo(stdoutFile.writable); process.stderr.pipeTo(stderrFile.writable); } } export const [getLogger, setLogger] = createSingleton("Logger");