summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--gui/src/main/index.ts12
-rw-r--r--gui/src/main/logging.ts25
-rw-r--r--gui/src/main/user-interface.ts7
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) {