forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog-reader.ts
More file actions
36 lines (30 loc) · 968 Bytes
/
log-reader.ts
File metadata and controls
36 lines (30 loc) · 968 Bytes
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
// src/lib/log-reader.ts
//
// Functions: readLogTail
import { existsSync } from "node:fs"
import { open, stat } from "node:fs/promises"
import { dirname } from "node:path"
import { DEFAULT_LOG_FILE, DEV_LOG_FILE } from "@/lib/constants"
export async function readLogTail(
maxBytes: number
): Promise<{ content: string; sizeBytes: number }> {
const logFile =
process.env.LOG_FILE ??
(existsSync(dirname(DEFAULT_LOG_FILE)) ? DEFAULT_LOG_FILE : DEV_LOG_FILE)
const info = await stat(logFile)
const readSize = Math.min(maxBytes, info.size)
const offset = info.size - readSize
const fh = await open(logFile, "r")
let tail: string
try {
const buf = Buffer.alloc(readSize)
await fh.read(buf, 0, readSize, offset)
tail = buf.toString("utf8")
} finally {
await fh.close()
}
const lines = tail.split("\n")
if (offset > 0) lines.shift()
const content = lines.join("\n").trim()
return { content, sizeBytes: info.size }
}