diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2022-11-04 18:00:54 +0100 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2022-11-07 14:45:54 +0100 |
| commit | 349e339110616cd01a8bd58a77b22d232228eb66 (patch) | |
| tree | 79d844e57a134182487afd7116525a0ea4aaaf13 /gui/src/main | |
| parent | 735601ed34ab83fa4443424aa2ddf9068b27f6bd (diff) | |
| download | mullvadvpn-349e339110616cd01a8bd58a77b22d232228eb66.tar.xz mullvadvpn-349e339110616cd01a8bd58a77b22d232228eb66.zip | |
Improve renderer log forwarding
Diffstat (limited to 'gui/src/main')
| -rw-r--r-- | gui/src/main/index.ts | 12 | ||||
| -rw-r--r-- | gui/src/main/logging.ts | 25 | ||||
| -rw-r--r-- | gui/src/main/user-interface.ts | 7 |
3 files changed, 32 insertions, 12 deletions
diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 4649cac8a7..bc971b24f9 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -18,11 +18,7 @@ import { SystemNotification } from '../shared/notifications/notification'; import Account, { AccountDelegate, LocaleProvider } from './account'; import { getOpenAtLogin } from './autostart'; import { readChangelog } from './changelog'; -import { - SHOULD_DISABLE_RESET_NAVIGATION, - SHOULD_FORWARD_RENDERER_LOG, - SHOULD_SHOW_CHANGES, -} from './command-line-options'; +import { SHOULD_DISABLE_RESET_NAVIGATION, SHOULD_SHOW_CHANGES } from './command-line-options'; import { ConnectionObserver, DaemonRpc, SubscriptionListener } from './daemon-rpc'; import Expectation from './expectation'; import { IpcMainEventChannel } from './ipc-event-channel'; @@ -250,11 +246,7 @@ class ApplicationMain const mainLogPath = getMainLogPath(); const rendererLogPath = getRendererLogPath(); - if (process.env.NODE_ENV === 'development') { - if (SHOULD_FORWARD_RENDERER_LOG) { - log.addInput(new IpcInput()); - } - } else { + if (process.env.NODE_ENV === 'production') { this.rendererLog = new Logger(); this.rendererLog.addInput(new IpcInput()); diff --git a/gui/src/main/logging.ts b/gui/src/main/logging.ts index 0e488914ce..8798b52066 100644 --- a/gui/src/main/logging.ts +++ b/gui/src/main/logging.ts @@ -1,4 +1,4 @@ -import { app } from 'electron'; +import { app, WebContents } from 'electron'; import fs from 'fs'; import path from 'path'; @@ -37,6 +37,29 @@ export class IpcInput implements ILogInput { } } +export class WebContentsConsoleInput implements ILogInput { + public constructor(private webContents: WebContents) {} + + public on(handler: (level: LogLevel, message: string) => void) { + const levelMap = [LogLevel.verbose, LogLevel.info, LogLevel.warning, LogLevel.error]; + + this.webContents.on('console-message', (_event, consoleLevel, message) => { + const level = levelMap[consoleLevel]; + handler(level, WebContentsConsoleInput.formatMessage(level, message)); + }); + + this.webContents.on('preload-error', (_event, _path, error) => + handler(LogLevel.error, WebContentsConsoleInput.formatMessage(LogLevel.error, error.message)), + ); + } + + private static formatMessage(level: LogLevel, message: string) { + // Prefix all messages from renderer with [Renderer] in blue or red depending on level. + const color = level === LogLevel.error ? '\x1b[31m' : '\x1b[34m'; + return `${color}[Renderer]\x1b[0m ${message}`; + } +} + export function getMainLogPath() { return path.join(getLogDirectoryDir(), 'frontend-main.log'); } diff --git a/gui/src/main/user-interface.ts b/gui/src/main/user-interface.ts index ae9e9c8592..624347a9d3 100644 --- a/gui/src/main/user-interface.ts +++ b/gui/src/main/user-interface.ts @@ -8,9 +8,10 @@ import { IAccountData, ILocation, TunnelState } from '../shared/daemon-rpc-types import { messages, relayLocations } from '../shared/gettext'; import log from '../shared/logging'; import { Scheduler } from '../shared/scheduler'; -import { SHOULD_DISABLE_DEVTOOLS_OPEN } from './command-line-options'; +import { SHOULD_DISABLE_DEVTOOLS_OPEN, SHOULD_FORWARD_RENDERER_LOG } from './command-line-options'; import { DaemonRpc } from './daemon-rpc'; import { changeIpcWebContents, IpcMainEventChannel } from './ipc-event-channel'; +import { WebContentsConsoleInput } from './logging'; import { isMacOs11OrNewer } from './platform-version'; import TrayIconController, { TrayIconType } from './tray-icon-controller'; import WindowController, { WindowControllerDelegate } from './window-controller'; @@ -96,6 +97,10 @@ export default class UserInterface implements WindowControllerDelegate { // The devtools doesn't open on Windows if openDevTools is called without a delay here. window.once('ready-to-show', () => window.webContents.openDevTools({ mode: 'detach' })); } + + if (SHOULD_FORWARD_RENDERER_LOG) { + log.addInput(new WebContentsConsoleInput(window.webContents)); + } } switch (process.platform) { |
