Harden extraction verification, cleanup safety, and logging in v1.3.10

This commit is contained in:
Sucukdeluxe
2026-02-27 15:43:52 +01:00
parent ef821b69a5
commit e2a8673c94
6 changed files with 321 additions and 17 deletions
+33 -5
View File
@@ -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
}
}
}