const fs = require('fs'); const path = require('path'); let CLAUDE_DIR = null; let isRotating = false; function setClaudeDir(dir) { CLAUDE_DIR = dir; } async function writeDebugLog(msg) { try { if (!CLAUDE_DIR) { const os = require('os'); CLAUDE_DIR = path.join(os.homedir(), '.claude'); } const logPath = path.join(CLAUDE_DIR, 'desktop-cli-debug.log'); // 파일 로테이션 (비동기로 실행하되, 백그라운드에서 진행하여 메인 루프 블로킹 방지) if (!isRotating) { isRotating = true; fs.promises.stat(logPath).then(async (stat) => { if (stat.size > 5 * 1024 * 1024) { try { const content = await fs.promises.readFile(logPath, 'utf-8'); const lines = content.split('\n'); await fs.promises.writeFile(logPath, lines.slice(-1000).join('\n'), 'utf-8'); } catch (err) { console.error('[Logger] Rotation error:', err.message); } } isRotating = false; }).catch(() => { isRotating = false; }); } const time = new Date().toISOString(); await fs.promises.appendFile(logPath, `[${time}] ${msg}\n`, 'utf-8'); } catch (e) { console.warn('[Logger] Fallback debug log:', msg); } } module.exports = { setClaudeDir, writeDebugLog };