forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebugLogger.ts
More file actions
56 lines (46 loc) · 1.53 KB
/
debugLogger.ts
File metadata and controls
56 lines (46 loc) · 1.53 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as fs from 'fs';
import * as path from 'path';
const DEBUG_DIR = path.join(process.cwd(), 'debug');
const CLEANUP_INTERVAL = 60 * 60 * 1000; // 1 hour
const MAX_AGE = 60 * 60 * 1000; // 1 hour
// Initialize debug directory and cleanup on startup
function initDebugLogger() {
if (!fs.existsSync(DEBUG_DIR)) {
fs.mkdirSync(DEBUG_DIR, { recursive: true });
console.log(`[DEBUG] Created debug directory: ${DEBUG_DIR}`);
}
// Run cleanup immediately on startup
cleanupOldFiles();
// Schedule cleanup every hour
setInterval(cleanupOldFiles, CLEANUP_INTERVAL);
}
function cleanupOldFiles() {
try {
const now = Date.now();
const files = fs.readdirSync(DEBUG_DIR);
for (const file of files) {
const filePath = path.join(DEBUG_DIR, file);
const stats = fs.statSync(filePath);
const age = now - stats.mtimeMs;
if (age > MAX_AGE) {
fs.unlinkSync(filePath);
console.log(`[DEBUG] Cleaned up old file: ${file}`);
}
}
} catch (error) {
console.error('[DEBUG] Error during cleanup:', error);
}
}
function saveResponse(endpoint: string, response: any): string {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `${endpoint}_${timestamp}.json`;
const filePath = path.join(DEBUG_DIR, filename);
try {
fs.writeFileSync(filePath, JSON.stringify(response, null, 2));
return filename;
} catch (error) {
console.error('[DEBUG] Error saving response:', error);
return '';
}
}
export { initDebugLogger, saveResponse };