blob: aea653b7ea5f80bf35ee2f5b67ff00f8e705a46d (
plain)
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
|
#!/usr/bin/env python3
from pathlib import Path
from datetime import datetime
def now_str() -> str:
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def nots(line: str) -> str:
if line.startswith("[") and "]" in line:
return line.split("]", 1)[1].lstrip()
return line
def tail_lines(path: Path, n: int) -> list[str]:
# grabs the last n lines without reading the whole file into memory first,
# since memory.txt only ever grows and reading it fully every message doesn't scale
if n <= 0:
return []
avg_line_bytes = 200
with path.open("rb") as f:
f.seek(0, 2)
file_size = f.tell()
if file_size == 0:
return []
block_size = n * avg_line_bytes
data = b""
lines = []
while True:
seek_pos = max(file_size - block_size, 0)
f.seek(seek_pos)
data = f.read(file_size - seek_pos)
lines = data.splitlines()
if len(lines) > n or seek_pos == 0:
break
block_size *= 2
return [l.decode("utf-8", errors="ignore").strip() for l in lines[-n:] if l.strip()]
|