diff --git a/apps/server-web/src/main/helpers/constant.ts b/apps/server-web/src/main/helpers/constant.ts index bcd77553f..dc547d227 100644 --- a/apps/server-web/src/main/helpers/constant.ts +++ b/apps/server-web/src/main/helpers/constant.ts @@ -50,7 +50,8 @@ export const ServerPageTypeMessage = { export const LOG_TYPES = { UPDATE_LOG: 'UPDATE-LOG', - SERVER_LOG: 'SERVER-LOG' + SERVER_LOG: 'SERVER-LOG', + SERVER_LOG_ERROR: 'SERVER-LOG-ERROR' } export const IPC_TYPES: { diff --git a/apps/server-web/src/main/helpers/services/libs/server-task.ts b/apps/server-web/src/main/helpers/services/libs/server-task.ts index 15cfedc77..e52d8d8e8 100644 --- a/apps/server-web/src/main/helpers/services/libs/server-task.ts +++ b/apps/server-web/src/main/helpers/services/libs/server-task.ts @@ -43,9 +43,8 @@ export abstract class ServerTask { this.loggerObserver = new Observer((msg: string) => { console.log('Sending log_state:', msg); - if (!this.window?.isDestroyed()) { - // this.window.webContents.send('log_state', { msg }); - } + const logType = this.isErrorMessage(msg) ? LOG_TYPES.SERVER_LOG_ERROR : LOG_TYPES.SERVER_LOG; + console.log(logType, msg); }); this.stateObserver = new Observer((state: boolean) => { @@ -64,6 +63,11 @@ export abstract class ServerTask { }); } + private isErrorMessage(msg: string): boolean { + return msg.includes('stderr:') || + this.criticalMessageError.some(error => msg.includes(error)); + } + protected async runTask(signal: AbortSignal): Promise { console.log('Run Server Task'); return new Promise((resolve, reject) => { @@ -72,7 +76,7 @@ export abstract class ServerTask { const service = ChildProcessFactory.createProcess(this.processPath, this.args, signal); - console.log(LOG_TYPES.SERVER_LOG, 'Service created', service.pid); + this.loggerObserver.notify(`Service created ${service.pid}`) service.stdout?.on('data', (data: any) => { const msg = data.toString(); @@ -80,8 +84,12 @@ export abstract class ServerTask { if (msg.includes(this.successMessage)) { const name = String(this.args.serviceName); this.stateObserver.notify(true); + console.log(this.args) this.loggerObserver.notify( - `☣︎ ${name.toUpperCase()} server listen to ${this.config.setting[`${name}Url`]}` + `☣︎ ${name.toUpperCase()} running on http://${this.args.DESKTOP_WEB_SERVER_HOSTNAME}:${this.args.PORT}` + ); + this.loggerObserver.notify( + `☣︎ ${name.toUpperCase()} connected to api ${this.args.GAUZY_API_SERVER_URL}` ); resolve(); } @@ -92,20 +100,12 @@ export abstract class ServerTask { } }); - service.stderr?.on('data', (data: any) => { - console.log(LOG_TYPES.SERVER_LOG, 'stderr:', data.toString()); - this.loggerObserver.notify(data.toString()); - }); + service.stderr?.on('data', this.handleStdErr.bind(this)); - service.on('disconnect', () => { - console.log(LOG_TYPES.SERVER_LOG, 'Webserver disconnected'); - if (this.eventEmitter) { - this.eventEmitter.emit(EventLists.webServerStopped); - } - }) + service.on('disconnect', this.handleDisconnect.bind(this)); service.on('error', (err) => { - console.log('child process error', err); + this.handleError(err, false); }) if (this.eventEmitter) { @@ -168,6 +168,19 @@ export abstract class ServerTask { } } + private handleStdErr(data: any): void { + const errorMessage: string = data.toString(); + this.loggerObserver.notify(`stderr: ${errorMessage}`); + } + + private handleDisconnect(): void { + this.loggerObserver.notify('Webserver disconnected') + if (this.eventEmitter) { + this.eventEmitter.emit(EventLists.webServerStopped); + } + this.stateObserver.notify(false); + } + protected handleError(error: any, attemptKill = true) { if (attemptKill) { this.kill(false); // Pass false to indicate that handleError should not attempt to kill again diff --git a/apps/server-web/src/main/main.ts b/apps/server-web/src/main/main.ts index f633725a7..acc556fde 100644 --- a/apps/server-web/src/main/main.ts +++ b/apps/server-web/src/main/main.ts @@ -68,6 +68,16 @@ Log.hooks.push((message: any, transport) => { } } + if (message.data[0] === LOG_TYPES.SERVER_LOG_ERROR) { + if (logWindow) { + const msg = message.data.join(' '); + logWindow.webContents.send(IPC_TYPES.SERVER_PAGE, { + type: LOG_TYPES.SERVER_LOG_ERROR, + msg + }); + } + } + if (message.data[0] === LOG_TYPES.UPDATE_LOG) { if (settingWindow) { const msg = `${message.data.join(' ')}`; diff --git a/apps/server-web/src/renderer/pages/Server.tsx b/apps/server-web/src/renderer/pages/Server.tsx index 352d2d185..498324a04 100644 --- a/apps/server-web/src/renderer/pages/Server.tsx +++ b/apps/server-web/src/renderer/pages/Server.tsx @@ -1,25 +1,54 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef, ReactNode } from 'react'; import { ServerPageTypeMessage } from '../../main/helpers/constant'; import { IPC_TYPES, LOG_TYPES } from '../../main/helpers/constant'; import { EverTeamsLogo } from '../components/svgs'; import { useTranslation } from 'react-i18next'; +const LogView = ({ children }: { children: ReactNode }) => { + return
{children}
; +}; + export function ServerPage() { + const logRef = useRef(null); const [isRun, setIsRun] = useState(false); - const [logs, setLogs] = useState([]); + const [logs, setLogs] = useState< + { + message: string; + type: 'error-log' | 'log'; + }[] + >([]); const [loading, setLoading] = useState(false); const { t } = useTranslation(); + const [logOpen, setLogOpen] = useState(false); useEffect(() => { window.electron.ipcRenderer.removeEventListener(IPC_TYPES.SERVER_PAGE); window.electron.ipcRenderer.on(IPC_TYPES.SERVER_PAGE, (arg: any) => { switch (arg.type) { case LOG_TYPES.SERVER_LOG: - setLogs((prev) => [...prev, arg.msg]); + setLogs((prev) => [ + ...prev, + { + message: arg.msg, + type: 'log', + }, + ]); + scrollToLast(); + break; + case LOG_TYPES.SERVER_LOG_ERROR: + setLogs((prev) => [ + ...prev, + { + message: arg.msg, + type: 'error-log', + }, + ]); + scrollToLast(); break; case ServerPageTypeMessage.SERVER_STATUS: if (arg.data.isRun) { setIsRun(true); + setLogOpen(true); } else { setIsRun(false); } @@ -41,6 +70,12 @@ export function ServerPage() { }); }; + const scrollToLast = () => { + logRef.current?.scrollIntoView({ + behavior: 'smooth', + }); + }; + return (
@@ -60,7 +95,14 @@ export function ServerPage() {
-
+
{ + e.preventDefault(); + setLogOpen((prev) => !prev); + }} + > Server Logs @@ -90,11 +132,16 @@ export function ServerPage() {
{logs.length > 0 && logs.map((log, i) => ( -
- {log} -
+ + {log.type === 'error-log' ? ( + {log.message} + ) : ( + {log.message} + )} + ))}
+