Harden extraction verification, cleanup safety, and logging in v1.3.10
This commit is contained in:
+33
-5
@@ -2,18 +2,46 @@ import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
let logFilePath = path.resolve(process.cwd(), "rd_downloader.log");
|
||||
let fallbackLogFilePath: string | null = null;
|
||||
|
||||
export function configureLogger(baseDir: string): void {
|
||||
logFilePath = path.join(baseDir, "rd_downloader.log");
|
||||
const cwdLogPath = path.resolve(process.cwd(), "rd_downloader.log");
|
||||
fallbackLogFilePath = cwdLogPath === logFilePath ? null : cwdLogPath;
|
||||
}
|
||||
|
||||
function appendLine(filePath: string, line: string): { ok: boolean; errorText: string } {
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.appendFileSync(filePath, line, "utf8");
|
||||
return { ok: true, errorText: "" };
|
||||
} catch (error) {
|
||||
return { ok: false, errorText: String(error) };
|
||||
}
|
||||
}
|
||||
|
||||
function write(level: "INFO" | "WARN" | "ERROR", message: string): void {
|
||||
const line = `${new Date().toISOString()} [${level}] ${message}\n`;
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(logFilePath), { recursive: true });
|
||||
fs.appendFileSync(logFilePath, line, "utf8");
|
||||
} catch {
|
||||
// ignore logging failures
|
||||
const primary = appendLine(logFilePath, line);
|
||||
|
||||
if (fallbackLogFilePath) {
|
||||
const fallback = appendLine(fallbackLogFilePath, line);
|
||||
if (!primary.ok && !fallback.ok) {
|
||||
try {
|
||||
process.stderr.write(`LOGGER write failed (primary+fallback): ${primary.errorText} | ${fallback.errorText}\n`);
|
||||
} catch {
|
||||
// ignore stderr failures
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!primary.ok) {
|
||||
try {
|
||||
process.stderr.write(`LOGGER write failed: ${primary.errorText}\n`);
|
||||
} catch {
|
||||
// ignore stderr failures
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user