2024-12-04 16:19:17 +08:00
|
|
|
import { app, ipcMain, shell } from "electron";
|
2025-01-08 11:22:09 +08:00
|
|
|
import { logInfo, logError, LogModule } from "../utils/log";
|
2023-11-27 15:03:25 +08:00
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
export const initLoggerApi = () => {
|
|
|
|
const logPath = path.join(app.getPath("userData"), "frpc.log");
|
|
|
|
|
|
|
|
const readLogger = (callback: (content: string) => void) => {
|
|
|
|
fs.readFile(logPath, "utf-8", (error, data) => {
|
|
|
|
if (!error) {
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Log file read successfully.");
|
2023-11-27 15:03:25 +08:00
|
|
|
callback(data);
|
2025-01-08 11:22:09 +08:00
|
|
|
} else {
|
|
|
|
logError(LogModule.LOGGER, `Error reading log file: ${error.message}`);
|
2023-11-27 15:03:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ipcMain.on("logger.getLog", async (event, args) => {
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Received request to get log.");
|
2023-11-27 15:03:25 +08:00
|
|
|
readLogger(content => {
|
|
|
|
event.reply("Logger.getLog.hook", content);
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Log data sent to client.");
|
2023-11-27 15:03:25 +08:00
|
|
|
});
|
|
|
|
});
|
2024-08-06 00:17:39 +08:00
|
|
|
|
2023-11-27 15:03:25 +08:00
|
|
|
ipcMain.on("logger.update", (event, args) => {
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Watching log file for changes.");
|
2023-11-27 15:03:25 +08:00
|
|
|
fs.watch(logPath, (eventType, filename) => {
|
|
|
|
if (eventType === "change") {
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Log file changed, reading new content.");
|
2023-11-27 15:03:25 +08:00
|
|
|
readLogger(content => {
|
|
|
|
event.reply("Logger.update.hook", content);
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Updated log data sent to client.");
|
2023-11-27 15:03:25 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-12-04 16:19:17 +08:00
|
|
|
|
|
|
|
ipcMain.on("logger.openLog", (event, args) => {
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Attempting to open log file.");
|
2024-12-04 16:19:17 +08:00
|
|
|
shell.openPath(logPath).then((errorMessage) => {
|
|
|
|
if (errorMessage) {
|
2025-01-08 11:22:09 +08:00
|
|
|
logError(LogModule.LOGGER, `Failed to open Logger: ${errorMessage}`);
|
2024-12-04 16:19:17 +08:00
|
|
|
event.reply("Logger.openLog.hook", false);
|
|
|
|
} else {
|
2025-01-08 11:22:09 +08:00
|
|
|
logInfo(LogModule.LOGGER, "Logger opened successfully.");
|
2024-12-04 16:19:17 +08:00
|
|
|
event.reply("Logger.openLog.hook", true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-11-27 15:03:25 +08:00
|
|
|
};
|