1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import { join } from "@std/path";
import { toFileNameString } from "./lib.ts";
export interface ExternalLogStream extends Disposable {
stream: WritableStream;
}
export class LogFileProvider {
#directory: string;
constructor(directory: string) {
this.#directory = directory;
Deno.mkdirSync(directory, { recursive: true });
}
async createExternalLogStream(
name: string,
options?: {
noTime?: boolean;
},
): Promise<ExternalLogStream> {
if (name.includes("/")) {
throw new Error(`External log stream's name (${name}) contains '/'.`);
}
const logPath = join(
this.#directory,
options?.noTime === true
? name
: `${name}-${toFileNameString(new Date())}`,
);
const file = await Deno.open(logPath, {
read: false,
write: true,
append: true,
create: true,
});
return {
stream: file.writable,
[Symbol.dispose]: file[Symbol.dispose].bind(file),
};
}
async createExternalLogStreamsForProgram(
program: string,
): Promise<{ stdout: WritableStream; stderr: WritableStream } & Disposable> {
const stdout = await this.createExternalLogStream(`${program}-stdout`);
const stderr = await this.createExternalLogStream(`${program}-stderr`);
return {
stdout: stdout.stream,
stderr: stderr.stream,
[Symbol.dispose]: () => {
stdout[Symbol.dispose]();
stderr[Symbol.dispose]();
},
};
}
}
|