frpc-desktop/electron/api/logger.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-12-04 16:19:17 +08:00
import { app, ipcMain, shell } from "electron";
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) {
callback(data);
}
});
};
ipcMain.on("logger.getLog", async (event, args) => {
readLogger(content => {
event.reply("Logger.getLog.hook", content);
});
});
2024-08-06 00:17:39 +08:00
2023-11-27 15:03:25 +08:00
ipcMain.on("logger.update", (event, args) => {
fs.watch(logPath, (eventType, filename) => {
if (eventType === "change") {
readLogger(content => {
event.reply("Logger.update.hook", content);
});
}
});
});
2024-12-04 16:19:17 +08:00
ipcMain.on("logger.openLog", (event, args) => {
console.log('正在打开日志');
shell.openPath(logPath).then((errorMessage) => {
if (errorMessage) {
console.error('Failed to open Logger:', errorMessage);
event.reply("Logger.openLog.hook", false);
} else {
console.log('Logger opened successfully');
event.reply("Logger.openLog.hook", true);
}
});
});
2023-11-27 15:03:25 +08:00
};