2025-02-18 17:52:09 +08:00
|
|
|
import fs from "fs";
|
2025-02-24 16:38:39 +08:00
|
|
|
import PathUtils from "../utils/PathUtils";
|
|
|
|
import SystemService from "./SystemService";
|
2025-02-25 11:57:54 +08:00
|
|
|
import BeanFactory from "../core/BeanFactory";
|
|
|
|
import { BrowserWindow } from "electron";
|
2025-02-25 14:15:18 +08:00
|
|
|
import ResponseUtils from "../utils/ResponseUtils";
|
2025-02-18 17:52:09 +08:00
|
|
|
|
|
|
|
class LogService {
|
2025-02-24 16:38:39 +08:00
|
|
|
private readonly _systemService: SystemService;
|
|
|
|
private readonly _logPath: string = PathUtils.getFrpcLogFilePath();
|
2025-02-18 17:52:09 +08:00
|
|
|
|
2025-02-24 16:38:39 +08:00
|
|
|
constructor(systemService: SystemService) {
|
|
|
|
this._systemService = systemService;
|
2025-02-18 17:52:09 +08:00
|
|
|
}
|
|
|
|
|
2025-02-25 11:57:54 +08:00
|
|
|
async getFrpLogContent() {
|
2025-02-18 17:52:09 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2025-02-25 11:57:54 +08:00
|
|
|
if (!fs.existsSync(this._logPath)) {
|
|
|
|
resolve("");
|
|
|
|
}
|
2025-02-18 17:52:09 +08:00
|
|
|
fs.readFile(this._logPath, "utf-8", (error, data) => {
|
|
|
|
if (!error) {
|
|
|
|
resolve(data);
|
|
|
|
} else {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-21 18:30:13 +08:00
|
|
|
watchFrpcLog(listenerParam: ListenerParam) {
|
2025-02-24 16:38:39 +08:00
|
|
|
if (!fs.existsSync(this._logPath)) {
|
|
|
|
setTimeout(() => this.watchFrpcLog(listenerParam), 1000);
|
|
|
|
return;
|
|
|
|
}
|
2025-02-25 11:57:54 +08:00
|
|
|
console.log("watchFrpcLog succcess");
|
2025-02-18 17:52:09 +08:00
|
|
|
fs.watch(this._logPath, (eventType, filename) => {
|
|
|
|
if (eventType === "change") {
|
2025-02-21 18:30:13 +08:00
|
|
|
console.log("change", eventType, listenerParam.channel);
|
2025-02-25 11:57:54 +08:00
|
|
|
const win: BrowserWindow = BeanFactory.getBean("win");
|
|
|
|
win.webContents.send(
|
2025-02-21 18:30:13 +08:00
|
|
|
listenerParam.channel,
|
2025-02-25 12:37:16 +08:00
|
|
|
ResponseUtils.success(true)
|
2025-02-21 18:30:13 +08:00
|
|
|
);
|
2025-02-18 17:52:09 +08:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// return new Promise<boolean>((resolve, reject) => {
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
|
|
|
openFrpcLogFile(): Promise<boolean> {
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
2025-02-24 16:38:39 +08:00
|
|
|
this._systemService
|
2025-02-21 18:30:13 +08:00
|
|
|
.openLocalFile(this._logPath)
|
2025-02-18 17:52:09 +08:00
|
|
|
.then(result => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default LogService;
|