Skip to content

Commit 8900ed2

Browse files
feat: cLog
1 parent 4c6b024 commit 8900ed2

File tree

3 files changed

+532
-152
lines changed

3 files changed

+532
-152
lines changed

lib/compress-logger.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as fs from "fs"
2+
import * as path from "path"
3+
4+
// =============================================================================
5+
// Dedicated compress diagnostics logger for DCP
6+
// Buffered async file writes, always-on when compress is invoked
7+
// Logs to .logs/dcp-compress.log relative to cwd
8+
// =============================================================================
9+
10+
const LOG_DIR = path.join(process.cwd(), ".logs")
11+
const LOG_FILE = path.join(LOG_DIR, "dcp-compress.log")
12+
const WRITE_INTERVAL_MS = 100
13+
const MAX_DATA_CHARS = 20000
14+
15+
let buffer: string[] = []
16+
let scheduled = false
17+
let initialized = false
18+
19+
function init(): boolean {
20+
if (initialized) return true
21+
try {
22+
if (!fs.existsSync(LOG_DIR)) {
23+
fs.mkdirSync(LOG_DIR, { recursive: true })
24+
}
25+
fs.writeFileSync(LOG_FILE, "")
26+
initialized = true
27+
return true
28+
} catch {
29+
return false
30+
}
31+
}
32+
33+
async function flush(): Promise<void> {
34+
if (buffer.length === 0) {
35+
scheduled = false
36+
return
37+
}
38+
const chunk = buffer.join("")
39+
buffer = []
40+
scheduled = false
41+
try {
42+
await fs.promises.appendFile(LOG_FILE, chunk)
43+
} catch {}
44+
}
45+
46+
function schedule(): void {
47+
if (!scheduled) {
48+
scheduled = true
49+
setTimeout(flush, WRITE_INTERVAL_MS)
50+
}
51+
}
52+
53+
type Level = "DEBUG" | "INFO" | "WARN" | "ERROR"
54+
55+
function stringifyData(data: unknown): string {
56+
try {
57+
const text = JSON.stringify(data)
58+
if (text.length <= MAX_DATA_CHARS) return text
59+
return `${text.substring(0, MAX_DATA_CHARS)}... [truncated ${text.length - MAX_DATA_CHARS} chars]`
60+
} catch {
61+
return `"[unserializable data]"`
62+
}
63+
}
64+
65+
function write(level: Level, category: string, message: string, data?: unknown): void {
66+
if (!init()) return
67+
const ts = new Date().toISOString()
68+
const dataStr = data !== undefined ? ` | ${stringifyData(data)}` : ""
69+
buffer.push(`[${ts}] [${level}] [${category}] ${message}${dataStr}\n`)
70+
schedule()
71+
}
72+
73+
export const clog = {
74+
debug: (category: string, message: string, data?: unknown) =>
75+
write("DEBUG", category, message, data),
76+
info: (category: string, message: string, data?: unknown) =>
77+
write("INFO", category, message, data),
78+
warn: (category: string, message: string, data?: unknown) =>
79+
write("WARN", category, message, data),
80+
error: (category: string, message: string, data?: unknown) =>
81+
write("ERROR", category, message, data),
82+
flush,
83+
}
84+
85+
export const C = {
86+
COMPRESS: "COMPRESS",
87+
BOUNDARY: "BOUNDARY",
88+
STATE: "STATE",
89+
} as const

0 commit comments

Comments
 (0)