-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.mjs
82 lines (69 loc) · 1.75 KB
/
logger.mjs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { dtmNowStr, toFsCompatible } from "./time.mjs"
import { Rs, Us, controlToCaret_, isStr } from "./pre.mjs"
import { toStr } from "./fmt.mjs"
import { nop } from "./mat.mjs"
import { open } from "node:fs/promises"
import clone from "just-clone"
const LogDirpathAbs = process.env.BB_LogDirpathAbs
const logName = s => `${s}-${toFsCompatible(dtmNowStr(0))}`
const san_ =
raw =>
(...ss) =>
raw(...ss.map(s => controlToCaret_(toStr(s))))
const mkLogger_ = ({ id, fh, rs, us, fsync }) => {
if (rs === undefined) rs = Rs + "\n"
if (us === undefined) us = Us
const id_ = id ? `${id}${us}` : ""
const raw = async (...ss) => {
try {
await fh.write(`${id_}${dtmNowStr()}${us}${ss.map(toStr).join(us)}${rs}`)
} catch (e) {}
if (fsync && Object.hasOwn(fh, "sync")) {
try {
await fh.sync()
} catch (e) {}
}
}
const san = san_(raw)
const close = async () => {
if (![process.stdout, process.stderr].some(x => x === fh)) {
try {
await fh.close()
} catch (e) {}
}
}
return { raw, san, close }
}
const mkLogger = async ({ id, f_, rs, us, fsync }) => {
let fh
if (isStr(f_)) {
if (f_.length && f_[0] != "/") f_ = `${LogDirpathAbs}/${f_}`
fh = await open(f_, "a", "440")
} else {
fh = f_
}
return mkLogger_({ id, fh, rs, us, fsync })
}
const DummyLogger = { raw: nop, san: nop, close: nop }
const L_prefix =
(...ss0) =>
L => {
const h =
(X, k) =>
(...ss) =>
X[k](...ss0, ...ss)
const L_ = clone(L)
L_.nat.raw = h(L.nat, "raw")
L_.exc.raw = h(L.exc, "raw")
L_.nat.san = h(L.nat, "san")
L_.exc.san = h(L.exc, "san")
return L_
}
export {
LogDirpathAbs,
logName,
mkLogger_,
mkLogger,
DummyLogger,
L_prefix
}