Skip to content

Commit e6eb70c

Browse files
committed
feat: log viewer window
1 parent 5b16d96 commit e6eb70c

File tree

13 files changed

+244
-6
lines changed

13 files changed

+244
-6
lines changed

electron/config/window.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export const WindowConfig = {
66
initHeight: 700,
77
aboutWidth: 500,
88
aboutHeight: 400,
9+
logWidth:800,
10+
logHeight: 600,
911
feedbackWidth: 700,
1012
feedbackHeight: 600,
1113
guideWidth: 800,

electron/mapi/log/index.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import FileIndex from "../file";
88

99
let fileName = null;
1010
let fileStream = null;
11+
let appFileNames = {};
12+
let appFileStreams = {};
1113

1214
const stringDatetime = () => {
1315
return date.format(new Date(), "YYYYMMDD");
@@ -17,7 +19,7 @@ const logsDir = () => {
1719
};
1820

1921
const appLogsDir = () => {
20-
return path.join(AppEnv.userData, "data/logs");
22+
return path.join(AppEnv.dataRoot, "logs");
2123
};
2224

2325
const root = () => {
@@ -28,6 +30,10 @@ const file = () => {
2830
return path.join(logsDir(), "log_" + stringDatetime() + ".log");
2931
};
3032

33+
const appFile = (name: string) => {
34+
return path.join(appLogsDir(), name + "_" + stringDatetime() + ".log");
35+
}
36+
3137
const cleanOldLogs = (keepDays: number) => {
3238
const logDirs = [
3339
// 系统日志
@@ -104,6 +110,46 @@ const error = (label: string, data: any = null) => {
104110
return log("ERROR", label, data);
105111
};
106112

113+
const appLog = (name: string, level: "INFO" | "ERROR", label: string, data: any = null) => {
114+
if (appFileNames[name] !== appFile(name)) {
115+
appFileNames[name] = appFile(name);
116+
if (appFileStreams[name]) {
117+
appFileStreams[name].end();
118+
}
119+
const logDir = appLogsDir();
120+
if (!fs.existsSync(logDir)) {
121+
fs.mkdirSync(logDir);
122+
}
123+
appFileStreams[name] = fs.createWriteStream(appFileNames[name], {flags: "a"});
124+
}
125+
let line = [];
126+
line.push(date.format(new Date(), "YYYY-MM-DD HH:mm:ss"));
127+
line.push(level);
128+
line.push(label);
129+
if (data) {
130+
if (!["number", "string"].includes(typeof data)) {
131+
data = JSON.stringify(data);
132+
}
133+
line.push(data);
134+
}
135+
console.log(`[APP:${name}] - ` + line.join(" - "));
136+
appFileStreams[name].write(line.join(" - ") + "\n");
137+
}
138+
139+
const appPath = (name: string) => {
140+
if (!appFileNames[name]) {
141+
appFileNames[name] = appFile(name);
142+
}
143+
return appFileNames[name];
144+
}
145+
146+
const appInfo = (name: string, label: string, data: any = null) => {
147+
return appLog(name, "INFO", label, data);
148+
}
149+
const appError = (name: string, label: string, data: any = null) => {
150+
return appLog(name, "ERROR", label, data);
151+
};
152+
107153
const infoRenderOrMain = (label: string, data: any = null) => {
108154
if (electron.ipcRenderer) {
109155
return electron.ipcRenderer.invoke("log:info", label, data);
@@ -119,7 +165,23 @@ const errorRenderOrMain = (label: string, data: any = null) => {
119165
}
120166
};
121167

122-
const collectRenderOrMain = async (option?: {startTime?: string; endTime?: string; limit?: number}) => {
168+
const appInfoRenderOrMain = (name: string, label: string, data: any = null) => {
169+
if (electron.ipcRenderer) {
170+
return electron.ipcRenderer.invoke("log:appInfo", name, label, data);
171+
} else {
172+
return appInfo(name, label, data);
173+
}
174+
}
175+
176+
const appErrorRenderOrMain = (name: string, label: string, data: any = null) => {
177+
if (electron.ipcRenderer) {
178+
return electron.ipcRenderer.invoke("log:appError", name, label, data);
179+
} else {
180+
return appError(name, label, data);
181+
}
182+
};
183+
184+
const collectRenderOrMain = async (option?: { startTime?: string; endTime?: string; limit?: number }) => {
123185
option = Object.assign(
124186
{
125187
startTime: dayjs().subtract(1, "day").format("YYYY-MM-DD HH:mm:ss"),
@@ -194,10 +256,18 @@ export default {
194256
error,
195257
infoRenderOrMain,
196258
errorRenderOrMain,
259+
appPath,
260+
appInfo,
261+
appError,
262+
appInfoRenderOrMain,
263+
appErrorRenderOrMain,
197264
collectRenderOrMain,
198265
};
199266

200267
export const Log = {
201268
info: infoRenderOrMain,
202269
error: errorRenderOrMain,
270+
appPath,
271+
appInfo: appInfoRenderOrMain,
272+
appError: appErrorRenderOrMain,
203273
};

electron/mapi/log/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@ ipcMain.handle("log:info", (event, label: string, data: any) => {
77
ipcMain.handle("log:error", (event, label: string, data: any) => {
88
logIndex.error(label, data);
99
});
10+
ipcMain.handle("log:appInfo", (event, name: string, label: string, data: any) => {
11+
logIndex.appInfo(name, label, data);
12+
})
13+
ipcMain.handle("log:appError", (event, name: string, label: string, data: any) => {
14+
logIndex.appError(name, label, data);
15+
});
1016

1117
export default {
1218
info: logIndex.info,
1319
error: logIndex.error,
20+
appInfo: logIndex.appInfo,
21+
appError: logIndex.appError,
1422
};
1523

1624
export const Log = {
1725
info: logIndex.info,
1826
error: logIndex.error,
27+
appPath: logIndex.appPath,
28+
appInfo: logIndex.appInfo,
29+
appError: logIndex.appError,
1930
};

electron/mapi/log/render.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ export default {
44
root: logIndex.root,
55
info: logIndex.infoRenderOrMain,
66
error: logIndex.errorRenderOrMain,
7+
appInfo: logIndex.appInfoRenderOrMain,
8+
appError: logIndex.appErrorRenderOrMain,
79
collect: logIndex.collectRenderOrMain,
810
};

electron/page/about.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {BrowserWindow} from "electron";
22
import {preloadDefault} from "../lib/env-main";
3-
import {AppRuntime} from "../mapi/env";
43
import {t} from "../config/lang";
54
import {Page} from "./index";
65
import {WindowConfig} from "../config/window";

electron/page/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {DevToolsManager} from "../lib/devtools";
99
import {PageFeedback} from "./feedback";
1010
import {PagePayment} from "./payment";
1111
import {PageMonitor} from "./monitor";
12+
import {PageLog} from "./log";
1213

1314
const Pages = {
1415
user: PageUser,
@@ -17,6 +18,7 @@ const Pages = {
1718
payment: PagePayment,
1819
feedback: PageFeedback,
1920
monitor: PageMonitor,
21+
log: PageLog,
2022
};
2123

2224
export const Page = {

electron/page/log.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {BrowserWindow} from "electron";
2+
import {preloadDefault} from "../lib/env-main";
3+
import {t} from "../config/lang";
4+
import {Page} from "./index";
5+
import {WindowConfig} from "../config/window";
6+
import {AppRuntime} from "../mapi/env";
7+
8+
export const PageLog = {
9+
NAME: "log",
10+
open: async (option: {
11+
log: string,
12+
}) => {
13+
if (AppRuntime.windows[PageLog.NAME]) {
14+
AppRuntime.windows[PageLog.NAME].close();
15+
}
16+
const win = new BrowserWindow({
17+
title: t("日志"),
18+
parent: null,
19+
minWidth: WindowConfig.logWidth,
20+
minHeight: WindowConfig.logHeight,
21+
width: WindowConfig.logWidth,
22+
height: WindowConfig.logHeight,
23+
webPreferences: {
24+
preload: preloadDefault,
25+
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
26+
nodeIntegration: true,
27+
webSecurity: false,
28+
webviewTag: true,
29+
// Consider using contextBridge.exposeInMainWorld
30+
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
31+
contextIsolation: false,
32+
},
33+
show: true,
34+
frame: false,
35+
transparent: false,
36+
});
37+
await Page.openWindow(PageLog.NAME, win, "page/log.html");
38+
const logInit = {
39+
log: option.log,
40+
}
41+
win.webContents.executeJavaScript(`
42+
const logInit = ()=>{
43+
if(!window.__logInit){
44+
setTimeout(logInit, 100);
45+
return;
46+
}
47+
window.__logInit(${JSON.stringify(logInit)});
48+
};logInit();
49+
`);
50+
},
51+
};

page/log.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<link rel="icon" type="image/svg+xml" href="/logo.svg"/>
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
7+
<title>%name%</title>
8+
<link rel="stylesheet" href="./../iconfont/iconfont.css"/>
9+
</head>
10+
<body>
11+
<div id="app"></div>
12+
<script type="module" src="/src/entry/log.ts"></script>
13+
</body>
14+
</html>

src/components/common/FileLogViewer.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ const props = withDefaults(
88
maxLines?: number;
99
height?: string;
1010
autoScroll?: boolean;
11+
isFullPath?: boolean;
1112
}>(),
1213
{
1314
file: "",
1415
maxLines: 1000,
1516
height: "100%",
1617
autoScroll: true,
18+
isFullPath: false,
1719
}
1820
);
1921
@@ -34,6 +36,7 @@ onMounted(async () => {
3436
}
3537
},
3638
{
39+
isFullPath: props.isFullPath,
3740
limit: props.maxLines,
3841
}
3942
);
@@ -46,8 +49,8 @@ onBeforeUnmount(() => {
4649
</script>
4750

4851
<template>
49-
<div>
50-
<LogViewer :height="props.height" :logs="logs" :auto-scroll="autoScroll" />
52+
<div :style="{height:props.height}">
53+
<LogViewer :height="props.height" :logs="logs" :auto-scroll="autoScroll"/>
5154
</div>
5255
</template>
5356

src/components/common/LogViewer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ watch(
5858
</script>
5959

6060
<template>
61-
<div :style="{height: props.height}" ref="logContainer" class="bg-black rounded p-3 overflow-auto">
61+
<div :style="{height: props.height}" ref="logContainer" class="bg-black p-3 overflow-auto">
6262
<div v-if="!logs.length" class="text-center text-white py-10">
6363
<div>
6464
<i class="iconfont icon-empty-box text-4xl"></i>

0 commit comments

Comments
 (0)