forked from iboughtvip/Test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogWatcher.js
More file actions
128 lines (104 loc) · 3.22 KB
/
logWatcher.js
File metadata and controls
128 lines (104 loc) · 3.22 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
let watcher = null;
let currentLogPath = null;
let filePosition = 0;
let checkLogInterval = null;
function findLatestLogFile() {
const homedir = require('os').homedir();
const logDir = path.join(homedir, 'Library', 'Logs', 'Roblox');
try {
const files = fs.readdirSync(logDir)
.filter(file => file.endsWith('.log'))
.map(file => ({
path: path.join(logDir, file),
mtime: fs.statSync(path.join(logDir, file)).mtime
}))
.sort((a, b) => b.mtime - a.mtime);
return files.length > 0 ? files[0].path : null;
} catch (err) {
console.error('Error finding log files:', err);
return null;
}
}
function switchToNewLogFile(mainWindow) {
const newLogPath = findLatestLogFile();
if (!newLogPath) {
console.log('No log file found');
return;
}
if (newLogPath !== currentLogPath) {
console.log(`Switching to new log file: ${newLogPath}`);
filePosition = 0;
if (watcher) {
watcher.close();
}
currentLogPath = newLogPath;
try {
const content = fs.readFileSync(currentLogPath, 'utf8');
const lines = content.split('\n').filter(line => line.trim() !== '');
filePosition = content.length;
lines.forEach(line => {
mainWindow.webContents.send('log-update', line);
});
mainWindow.webContents.send('log-update', "Initial log content restored " + Date.now());
} catch (err) {
console.error('Error reading initial log content:', err);
}
watcher = chokidar.watch(currentLogPath, {
persistent: true,
ignoreInitial: true
});
watcher.on('change', () => {
try {
const stats = fs.statSync(currentLogPath);
if (stats.size < filePosition) {
filePosition = 0;
}
const stream = fs.createReadStream(currentLogPath, {
encoding: 'utf8',
start: filePosition
});
let remaining = '';
stream.on('data', (data) => {
const lines = (remaining + data).split('\n');
remaining = lines.pop();
lines.filter(line => line.trim() !== '').forEach(line => {
mainWindow.webContents.send('log-update', line);
if (line.includes('[FLog::Output] Connection accepted')) {
mainWindow.webContents.send('game-join-detected');
}
});
});
stream.on('end', () => {
filePosition = stats.size;
});
stream.on('error', (err) => {
console.error('Error reading log file:', err);
});
} catch (err) {
console.error('Error handling log change:', err);
}
});
watcher.on('error', (err) => {
console.error('Log watcher error:', err);
});
}
}
function start(mainWindow) {
switchToNewLogFile(mainWindow);
checkLogInterval = setInterval(() => {
switchToNewLogFile(mainWindow);
}, 5000);
return { success: true, path: currentLogPath };
}
function stop() {
if (watcher) {
watcher.close();
}
if (checkLogInterval) {
clearInterval(checkLogInterval);
}
}
module.exports = { start, stop };