feat: 日志的查询与下载

This commit is contained in:
萌森 2024-07-03 18:35:02 +08:00
parent dce28ba729
commit 357fb2ecda
6 changed files with 80 additions and 6 deletions

2
iyuu

@ -1 +1 @@
Subproject commit f7eed461a0d812a3b5c1adb96047a09021d87857
Subproject commit e62cfefe353c075642a4ab5294fc86a396390b55

18
logs.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>运行日志</title>
</head>
<body>
<h1>IYUU 日志信息</h1>
<pre id="log-content"></pre>
<script>
window.electron.onDisplayLog((logContent) => {
document.getElementById('log-content').innerText = logContent;
});
</script>
</body>
</html>

13
main.js
View File

@ -1,14 +1,19 @@
const { app } = require('electron')
const path = require('node:path')
const server = require('./server/server');
const log = require('electron-log')
log.initialize();
console.log = log.log;
log.transports.file.resolvePathFn = () => path.join(app.getPath('appData'), 'iyuu-plus/logs/iyuu.log');
log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text}';
log.transports.file.encoding = 'utf8';
log.info('App is starting...');
const server = require('./server/server');
const mainWin = require('./windows/app');
app.whenReady().then(() => {
log.transports.file.encoding = 'utf8';
log.transports.file.file = path.join(app.getPath('userData'), 'logs/iyuu.log');
log.transports.file.format = '{y}-{m}-{d} {h}:{i}:{s} {text}';
log.info('App is starting...');
server.startServer()

View File

@ -1,5 +1,6 @@
const { app, Menu, BrowserWindow } = require("electron")
const path = require('node:path')
const fs = require('fs')
const server = require('../server/server');
const mainWin = require('../windows/app');
@ -72,6 +73,42 @@ let template = [
});
}
},
{
label: "查看日志",
click: () => {
let win = new BrowserWindow({
width: 800,
height: 600,
icon: iconPath,
webPreferences: {
preload: path.join(__dirname, '..', 'preload.js'),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false
}
});
win.loadFile(path.join(__dirname, '..', 'logs.html'));
win.on("closed", () => {
win = null;
});
const logsFile = path.join(app.getPath('appData'), 'iyuu-plus/logs/iyuu.log');
fs.readFile(logsFile, 'utf8', (err, data) => {
if (err) {
console.error('Error reading log file:', err);
return;
}
win.webContents.send('display-log', data);
});
}
},
{
label: "下载日志",
click: () => {
const logsFile = path.join(app.getPath('appData'), 'iyuu-plus/logs/iyuu.log');
mainWin.downloadLocalFile(logsFile);
}
},
{
label: "新疆萌森软件开发工作室提供技术支持",
click: () => {

View File

@ -4,4 +4,11 @@ window.addEventListener('DOMContentLoaded', () => {
script.src = 'https://analytics.lifebus.top/script.js';
script.setAttribute('data-website-id', '5ca63d47-0739-4059-a480-40e7edf80ff9');
document.head.appendChild(script);
});
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electron', {
onDisplayLog: (callback) => ipcRenderer.on('display-log', (event, data) => callback(data))
});

View File

@ -108,4 +108,11 @@ const visibleWindows = () => {
}
module.exports = { hideWindows, showWindows, createWindow, closeWindows, refreshWindows, rebootWindows, visibleWindows, refreshUrl };
const downloadLocalFile = (filePath) => {
log.info('[Download local file]: ', filePath)
if (mainWindow) {
mainWindow.webContents.downloadURL('file://' + filePath)
}
}
module.exports = { hideWindows, showWindows, createWindow, closeWindows, refreshWindows, rebootWindows, visibleWindows, refreshUrl, downloadLocalFile };